Add Style::strong_font and Style::emphasis_font for real bold/italic rendering#7946
Open
w4nderlust wants to merge 2 commits intoemilk:mainfrom
Open
Add Style::strong_font and Style::emphasis_font for real bold/italic rendering#7946w4nderlust wants to merge 2 commits intoemilk:mainfrom
w4nderlust wants to merge 2 commits intoemilk:mainfrom
Conversation
|
Preview is being built... Preview will be available at https://egui-pr-preview.github.io/pr/7946-feature/strong-emphasis-fonts View snapshot changes at kitdiff |
1d47a57 to
a25d046
Compare
…rendering Currently, `RichText::strong()` fakes bold by changing the text color, and `RichText::italics()` fakes italic by applying a vertex skew. This works as a zero-configuration default, but produces noticeably different results from real bold/italic typefaces. This commit adds two optional fields to `Style`: - `strong_font: Option<FontFamily>` — when set, strong text uses this font family instead of applying a color change. - `emphasis_font: Option<FontFamily>` — when set, italic text uses this font family instead of applying a vertex skew. Both default to `None`, preserving full backwards compatibility. Usage: 1. Register bold/italic font data via `Context::set_fonts()` 2. Set `style.strong_font` / `style.emphasis_font` to the family names When a bold font is configured, `get_text_color()` skips the strong color so the typeface weight alone provides emphasis. When an italic font is configured, the vertex skew is disabled in `TextFormat`. When text is both strong and italic and both overrides are set, `emphasis_font` takes precedence since a single `FontId` can only reference one font family. Users needing combined bold-italic can register a dedicated BoldItalic family via `RichText::family()`.
Use fully qualified `crate::Visuals::override_text_color` since Visuals is not imported in widget_text.rs. Fixes cargo doc with -D warnings.
a25d046 to
213b03a
Compare
|
This looks like a great small quality of life feature! @emilk is there a reason the italic and bold versions of the UbuntuLight default font aren't included in the default fonts (or behind a feature if binary size/performance is a concern for some targets)? Would a PR along these lines be of interest? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds two optional fields to
Stylethat let users swap in real bold and italic font families forRichText::strong()andRichText::italics(), instead of relying on the default approximations (color change for bold, vertex skew for italic).Currently,
RichText::strong()only changes the text color (viaVisuals::strong_text_color()), which does not produce visually bold text. AndRichText::italics()applies a 25-degree horizontal vertex skew, which is a rough approximation that looks noticeably different from a real italic typeface. These defaults are fine when no custom fonts are available, but when users have access to proper Bold/Italic font files, there is no way to tell egui to use them for strong/italic markup.Changes
Two new
Option<FontFamily>fields onStyle:strong_font: When set,RichText::strong()renders with this font family instead of applying the strong text color. The color override is skipped because the heavier strokes of a real bold typeface provide visual distinction on their own.emphasis_font: When set,RichText::italics()renders with this font family instead of applying the vertex skew. The skew is skipped because the real italic letterforms are used instead.Both default to
None, so existing behavior is completely unchanged.Implementation details
The logic lives in
RichText::into_text_and_format()andRichText::get_text_color()inwidget_text.rs:into_text_and_format()resolves the bold/italic font family overrides before building theFontId. If bothstrong_fontandemphasis_fontare set and the text is both strong and italic,emphasis_fontwins (since aFontIdcan only reference one family). Users who need combined bold-italic can register a dedicatedBoldItalicfamily and apply it viaRichText::family().get_text_color()skips the strong color override whenstrong_fontis set, falling through to the default text color instead.Usage
After this, any
RichText::new("hello").strong()will render using the Bold font family.Motivation
This came up while building a markdown renderer on top of egui. Markdown has
**bold**and*italic*syntax, and users expect these to render with proper bold/italic typefaces when available. Libraries likeegui_commonmarkuseRichText::strong()andRichText::italics()internally, so configuring the style once is enough to get real bold/italic rendering throughout the whole UI without patching downstream libraries.Checklist
cargo fmtpassescargo clippypassesNone)