Synthetic bold for fonts that have no bold face and no weight axis

Open
#9,012 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
57/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Start in epaint’s font data and outline glyph rasterization, then check the font tweak UI because the issue identifies both as possible homes for the setting. Trace how outline bounds and strokes are handled, and compare the behavior for color glyphs and unchanged advance widths. Done means a per-font strength can thicken outline glyphs while leaving layout metrics unchanged.

Written by the indexing model from the issue text.

Description

Is your feature request related to a problem? Please describe.

Some fonts have no bold face and no wght axis, for example MS Gothic and MS Mincho on Windows. egui has no way to draw such a font in bold: an app can register a separate font for bold text, or use a variable font's weight axis, but these fonts offer neither. Bold text therefore looks the same as regular text.

Other apps on Windows show these fonts in bold because GDI and DirectWrite thicken the glyphs themselves when no bold face exists. FreeType offers the same with its embolden functions.

Describe the solution you'd like

A per-font setting that thickens outline glyphs when they are rasterized: the stroke width added around each outline, as a fraction of the font size. An app could then register the regular font file a second time with this setting and use it for bold text.

I use this in a local patch of epaint 0.38.0. It adds embolden: f32 (default 0.0) to FontData. When rasterizing an outline glyph, the outline is stroked after it is filled, and the glyph bitmap is widened by half the stroke width on each side:

let stroke_width = f64::from(embolden.max(0.0) * metrics.scale);
let bounds = path
    .control_box()
    .inflate(stroke_width / 2.0, stroke_width / 2.0)
    .expand();
// ...
ctx.fill_path(&path);
if stroke_width > 0.0 {
    ctx.set_stroke(kurbo::Stroke::new(stroke_width));
    ctx.stroke_path(&path);
}

Advance widths and metrics are unchanged, so the layout does not move. Color glyphs are not affected. With a strength of 1/24 of the font size (what FreeType uses), bold MS Gothic and MS Mincho look right on Windows 11.

FontTweak might be a more natural home for the setting. I put it in FontData only because egui's font tweak UI destructures FontTweak field by field, so a new field there also needs a UI change.

Describe alternatives you've considered

  • A separate bold font file or a variable font's wght axis: these fonts have neither.
  • Painting the text twice with a small horizontal offset: this only works for text the app paints itself, not for labels and other widgets, and it only thickens strokes horizontally.

Additional context

Related: #3218 (selecting a font weight). This request covers fonts that have no weight to select.

Dominant language
Rust
Stars
32.6k
Forks
2.5k
Avg merge
2d 1h
Merged PRs (30d)
71

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from emilk/egui

All issues in emilk/egui

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.