Skip to content

Place fallback-font glyphs on the row baseline - #8804

Open
lixk wants to merge 1 commit into
emilk:mainfrom
lixk:fix/fallback-font-baseline
Open

lixk wants to merge 1 commit into
emilk:mainfrom
lixk:fix/fallback-font-baseline

Conversation

@lixk

@lixk lixk commented Oct 30, 2026 •

Copy link
Copy Markdown

glyph.pos.y was computed from font_face_ascent plus a term centering the face box in the row. Glyphs from a fallback face therefore sat on their own baseline, and a face much taller than the primary one (CJK: 1.5em, versus 1.15em for the default latin font) was pushed above the row, clipping the top of its glyphs.

Glyph::pos is documented as "Baseline position, relative to the row. pos.y is the same for all chars of the same TextFormat", so this makes the code match that contract:

-            glyph.pos.y = glyph.font_face_ascent
+            glyph.pos.y = glyph.font_ascent
                 + format.valign.to_factor() * (max_row_height - glyph.line_height)
                 + 0.5 * (glyph.line_height - glyph.font_height)
-                + 0.5 * (glyph.font_height - glyph.font_face_height);

Effect

"H国👋" at 40px, pixels_per_point = 1.0, CJK fallback = Hiragino Sans GB:

glyph baseline before baseline after
H (primary face) 37.00 37.00
国 (CJK fallback) 28.00 37.00
👋 (emoji fallback) 36.00 37.00
  • Latin-only text is unchanged (font_face_ascent == font_ascent for the primary face).
  • Emoji move by at most 1px — their face height is already close to the layout font height, so the centering term was near zero for them.
  • CJK moves down by 0.22em. At 40px the ink goes from [-5, 32] (top outside the 46px row, so clipped) to [4, 41] (inside).

Snapshot changes

45 snapshots are touched, which looks like a lot for a one-line change. It is one pixel of movement, on emoji only:

  • The demos are full of emoji icons, and emoji come from a fallback face, so they follow this fix by 1px.
  • Snapshots are Git LFS pointers, so each one shows up as a two-line oid/size change rather than as an image.

Diffing the actual images (base vs. head, per pixel) confirms latin text does not move at all — the only difference is the icon sitting 1px lower:

snapshot changed pixels share of the image
widget_gallery_light_x1.png 118 0.055%
demos/Painting.png 95 0.031%
demos/Scrolling.png 72 0.023%
demos/Table.png 150 0.066%
demos/Text Layout.png 201 0.11%

Visual diff of every snapshot: kitdiff.

Test

glyphs_from_different_faces_share_one_baseline now checks every glyph of a row against the baseline of the primary face, rather than merely checking that the glyphs agree with each other. It first asserts that a fallback face with different metrics was actually used, so it can't pass trivially. Without the fix it fails with [37.0, 37.0, 36.0].

  • cargo test -p epaint --features monochrome_emoji_fonts: 96 passed
  • cargo test -p egui --all-features: 286 passed
  • cargo fmt --check, cargo clippy -p epaint --all-targets --features monochrome_emoji_fonts -- -D warnings, scripts/lint.py: clean

Note that this only fixes the baseline. A row containing a tall fallback face still keeps the primary row height; growing the row to fit it is a separate change (as noted in #5126).

Fixes #8802

@github-actions

Copy link
Copy Markdown

Preview is being built...

Preview will be available at https://egui-pr-preview.github.io/pr/8804-fix/fallback-font-baseline

View snapshot changes at kitdiff

@lixk
lixk marked this pull request as ready for review October 30, 2026 09:03
@lixk
lixk force-pushed the fix/fallback-font-baseline branch from b51e3da to 3284487 Compare October 31, 2026 03:38
@lixk

lixk commented Oct 31, 2026

Copy link
Copy Markdown
Author

The workflow runs on this PR are sitting in action_required — Rust, Preview Build and the kittest snapshots haven't run at all yet. Could a maintainer approve them when convenient?

What I can check locally on 1.95.0 is green:

  • cargo fmt --all -- --check
  • scripts/lint.py
  • cargo test -p epaint --features monochrome_emoji_fonts — 96 passed
  • cargo test -p egui --all-features — 286 passed
  • cargo clippy -p epaint --all-targets --features monochrome_emoji_fonts -- -D warnings

On the 45 snapshot changes: they are one pixel of emoji movement each. The PR description has a per-image breakdown — latin text does not move at all.

@kevynf

kevynf commented Nov 12, 2026 •

Copy link
Copy Markdown

I tested this PR with a minimal repro: egui 0.37.0 defaults, no FontTweak, 13 pt, pixels_per_point = 1. Each line is a LayoutJob with one highlighted section (TextFormat::background). The primary font is Consolas or Cascadia Mono, falling back to Noto Sans SC.

The baseline fix works: CJK now sits on the Latin baseline with both fonts. With Consolas, the old centering term actually pushed CJK 4 px down, because font_height puts the whole line gap below the descent.

It also removes the steps in TextFormat::background. Before, 汉Latin字Mix中 was drawn as 5 rects at two heights, because add_row_backgrounds breaks the run whenever logical_rect().top() changes. Now it is one rect.

But with Consolas, the CJK ink reaches the top edge of the highlight (screenshot pixels):

background (incl. 1 px expand_bg) Latin ink CJK ink
Consolas, before Latin 37–54, CJK 41–55 39–47 42–52
Consolas, this PR 37–55 39–47 38–48
Cascadia Mono, this PR 86–102 89–98 89–99

The root cause is that the ideographs rise above the row. Consolas' font_ascent is 9.66 px, but the CJK ink reaches about 10 px above the baseline, and nothing grows the row for the taller fallback face. So the ink fills the logical rect to its very top, and only the 1 px expand_bg is left above it. The line gap makes this look worse: row_height = ascent − descent + line_gap puts the whole gap below the text, so 7 px of highlight stay empty under the CJK ink.

This predates the PR, but it is the remaining half of the problem. Growing the row to fit the fallback face (as #8824 does) would fix it, and splitting the line gap evenly (CSS half-leading) would balance the box. Cascadia Mono has a larger ascent (12.06 px) and no line gap, so the box looks fine there.

5× zoom. Left: before, right: this PR. Top: Consolas, bottom: Cascadia Mono.

compare

@lixk
lixk force-pushed the fix/fallback-font-baseline branch from 3284487 to ccdc8f8 Compare November 13, 2026 03:55
@lixk

lixk commented Nov 13, 2026

Copy link
Copy Markdown
Author

Thanks for the careful measurement — that matches what I see, and the pixel table is a much better argument than anything in the PR description.

Two updates since your comment:

The PR is rebased. It had started conflicting with main — #8845, #8868, #8874 and #8872 all touched text_layout.rs. It now sits on 0a5d30c2. The code hunk applied unchanged; I only had to regenerate the affected snapshots. Seven needed it, Code Editor among them, which changed height by 8 px because of the inline-code padding from #8872.

Your "one rect instead of five" observation still holds on the new base, and it matters slightly more now. #8872 rewrote add_row_backgrounds for rounded corners, but it still ends a run when logical_rect().top() changes. Since backgrounds are rounded now, a split run would show a visible seam at the join rather than just a step in a flat bar — so uniform glyph tops are doing a bit more work than when you measured.

On the remaining half, I agree with your diagnosis, and I don't think it should be folded into this PR:

  • Growing the row to the tallest fallback face changes row_height for every layout that mixes scripts. That's a visible layout change for a lot of existing UIs, and the real question is whether it should be unconditional or opt-in. That is exactly the decision #8824 (draft, FontTweak::fallback_align) is set up to make, and I'd rather not pre-empt it from here.
  • Splitting the line gap evenly (half-leading) is independent again — it moves text in layouts that have no fallback face at all.

This PR is deliberately the narrow half: after it, every glyph in a row shares the primary face's baseline, which is the invariant the other two changes need before either can be discussed on its own.

If a follow-up issue for the row-growth case would help, I'm happy to open one with your Consolas repro attached so the two halves can be reviewed separately — or go ahead and file it yourself if you'd rather own the repro.

One caveat on the regenerated snapshots: they were produced on macOS, and CI on this PR has still never run — the workflow runs are waiting for a maintainer to approve them — so none of this has been exercised on Linux yet.

`glyph.pos.y` was the ascent of whichever face the glyph happened to be found
in (`font_face_ascent`), plus a term centering that face's box inside the row.
So a row that mixed scripts put its glyphs on several different baselines, and
a face much taller than the primary one — a CJK fallback, say — had its glyphs
pushed above the row, clipping their tops.

Use the primary face's `font_ascent` for every glyph instead: that is the
baseline of the section's row, and every glyph belongs on it. The existing
CSS-style centering of the glyph inside an explicit `line_height` is unchanged.

Adds a test asserting that a latin glyph and a fallback-font glyph in the same
row share one y position. The 44 updated kittest snapshots only move emoji
icons down by one pixel, which is the expected consequence: they used to be
centered against the primary face instead of sitting on its baseline.

Closes emilk#8802.
@lixk
lixk force-pushed the fix/fallback-font-baseline branch from ccdc8f8 to 3327c42 Compare November 16, 2026 03:05
@lixk

lixk commented Nov 16, 2026

Copy link
Copy Markdown
Author

Pushed a cleanup pass on the branch. The behaviour is unchanged; only the size of the change got smaller:

  • Rebased onto main (c61b45f6) and squashed the three commits into one.
  • Trimmed the code comment and the test: the source diff is now +39/-8 instead of +54/-16. The actual fix is still the two-line change quoted above.
  • While re-running the snapshot tests I found that three of my snapshots (demos/Code Editor, demos/Code Example, demos/Popups) still carried their pre-Make inline code prettier: rounded corners, padding, and text color #8872 content — they had been resolved to the wrong side of a conflict in an earlier rebase of mine. They now match main again. That was my mistake and not part of the fix, but it is worth knowing that those three files show up in the diff because of it.

Re-verified on the new head: cargo test -p epaint --all-features (98 passed), cargo test -p egui_tests --all-features and cargo test -p egui_demo_lib --all-features (all snapshots pass), plus cargo fmt --check and cargo clippy -p epaint --all-targets --all-features -- -D warnings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CJK fallback font glyphs are shifted above the Latin baseline (font-box centering in text_layout.rs)

2 participants