Add Style::strong_font/emphasis_font for bold/italic rendering#7946
Add Style::strong_font/emphasis_font for bold/italic rendering#7946w4nderlust wants to merge 2 commits into
Style::strong_font/emphasis_font for bold/italic rendering#7946Conversation
|
Preview available at https://egui-pr-preview.github.io/pr/7946-featurestrong-emphasis-fonts View snapshot changes at kitdiff |
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? |
213b03a to
95298b2
Compare
Purely to keep the size of the .wasm blob smaller. Having it optional behind a feature flag would be nice. Something has gone wrong with the rebasing of this PR - the diff is huge, with a lot of unrelated changes |
…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.
db29087 to
febc028
Compare
|
@emilk sorry baout it. Force-pushed the branch to drop the unrelated changes; PR now contains only the two commits (the API addition + a one-line doc-link fix, 2 files, +135/-7), and I'll do the bundled-font feature flag as a follow-up PR if you think it's useful. |
Style::strong_font/emphasis_font for bold/italic rendering
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)