Repository navigation
fix(text): Shape variable fonts at the instance's axis position - #25154
MartinZikmund wants to merge 2 commits into
Conversation
A variable font positioned on its wght/wdth/ital/slnt axes was drawn with the instance's outlines but shaped with the default instance's advances: HarfBuzz is fed the raw sfnt tables, so without variation coordinates it shapes the default instance. A SemiBold title in a font whose default instance is Black ended up with SemiBold glyphs at Black spacing. Pass the typeface's variation design position to the HarfBuzz font so shaping and rendering use the same instance, as DirectWrite does with the single font face it matches by axis values. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LP133TsLEXjMo7XFGmakoS
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LP133TsLEXjMo7XFGmakoS
|
Claude finished @MartinZikmund's task in 15s —— View job Review of #25154Verdict: ship. I found no blocking issues and posted no inline comments. Issues by severity: 0 blocking, 0 major, 0 minor. What I checked (code review only):
Notes for the author (non-blocking):
|
|
The build 238589 found UI Test snapshots differences: Details
|
|
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
Verified: the fix sits in the Skia shaping path. SkiaFont.GetHarfBuzzFont is the HarfBuzz face creation for Uno.UI.Composition.Skia, and the shaped advances it returns (SkiaFont.Shape, consumed by UnicodeText.cs:334, SkiaTextLine.cs:361, BlockLayoutHelpers.cs:854, Run.cs:328) are the only width source there. GetGlyphAdvance and the outlines already use the varied SKTypeface. Compile check: VariationDesignPosition is SKFontVariationPositionCoordinate[] with an SKFourByteTag Axis, and HarfBuzzSharp.Variation.Tag is uint. SKFourByteTag converts implicitly to uint, and SetVariations takes a ReadOnlySpan, so the code compiles against SkiaSharp 4.151.1 and HarfBuzzSharp 14.2.1.1 (checked by reflection on the local NuGet cache). The approach matches WinUI's DWrite model (LookupNominalFontFace, then GetMatchingFonts, one face used for both placement and rendering), as cited in the PR. Static fonts are a no-op because VariationDesignPosition is empty. The test asset OpenSans.ttf is variable (fvar and gvar tables present), and the wght 400 case is a passing control. Code review plus the author's before/after results on Skia Win32; I did not run the tests.
Non-blocking:
- Only Skia Desktop was run. SkiaFont is shared by Skia Android, iOS and WASM, and HarfBuzzSharp's variation support is native per target. A run of Given_VariableFont on Skia WASM would confirm that path.
|
Tick the box to add this pull request to the merge queue (same as
|
Problem
On Skia, a variable font with a non-default
FontWeight(or stretch/style) is drawn with the right instance's outlines but spaced with the default instance's advances.Found in a real app on Uno.Sdk 7.0.0-dev.54 (
net10.0-desktop, Win32): page titles useFraunces.ttf(variable;wght100–900, default 900) throughTitleTextBlockStyle(FontWeight="SemiBold"). They rendered as SemiBold glyphs laid out at Black widths, so the letters were visibly spread apart. A body font (Karla.ttf, defaultwght400) has the opposite symptom: bold glyphs squeezed into regular advances.Minimal numbers (OpenSans variable, 48px,
"Hello World"): the shaped width was 262.2px at every weight, while the instance's own glyph advances are 252.3 (wght 300), 270.9 (600) and 289.9 (800).Root cause
ffab55d5842("Position variable fonts on their axes from font properties", in #23598) clones theSKTypefaceat the requestedwght/wdth/ital/slntposition. Skia then draws and measures the instance correctly, but shaping goes through HarfBuzz.SkiaFont.GetHarfBuzzFontbuilds the HarfBuzz face from the typeface's raw sfnt tables (fvar/gvar/HVAR/... unchanged) and never sets variation coordinates, so HarfBuzz shapes the default instance. Before that commit (6.7) the axes were never applied at all: the whole face rendered at the default instance, which was self-consistent but ignoredFontWeight.Fix
SkiaFont.GetHarfBuzzFontnow passes the typeface'sVariationDesignPositiontoHbFont.SetVariations, so shaping and rendering use the same instance. Static fonts (no axes) are untouched.WinUI alignment
WinUI resolves a variable font through DirectWrite's typographic model:
DWriteFontFamily::LookupNominalFontFace(dxaml/xcp/pal/win/DWriteFontFamily.cpp) maps weight/style/stretch onto thewght/ital/slnt/wdthaxis values and callsIDWriteFontFamily2::GetMatchingFonts. The single resulting font face is used for both glyph placement and rendering, so advances always belong to the drawn instance. That meansFontWeight="SemiBold"on such a font yields thewght=600instance with its own spacing. This PR restores that consistency.Note: compared to Uno 6.7, these fonts now render lighter when the font's default instance is heavier than the requested weight (e.g. Fraunces SemiBold is now 600 rather than the default 900). That matches WinUI. 6.7 ignored
FontWeightfor variable fonts.Checked against the same app built for WinUI (Store build, About page title in Fraunces SemiBold): WinUI renders the
wght=600instance with tight spacing. With this PR, Uno renders the same weight at the same word width (~405px at the same scale). 6.7 renders it Black, and 7.0 without this PR renders it SemiBold with Black spacing.Tests
New
Given_VariableFontruntime tests (Skia), using the existing variableAssets/Fonts/OpenSans/OpenSans.ttfthroughSkiaFontProviderdirectly so its.manifestis not consulted:When_Weight_Set_Then_Shaped_Advances_Match_Instance(wght 300/400/600/800): the shaped width equals the instance's glyph advances.When_Bold_Then_Shaped_Wider_Than_NormalSkia Desktop (Win32),
net10.0-desktop:Given_TextBlock,Given_RichTextBlock,Given_GlyphRunRendererandGiven_IFont_AlternativeBackendwith the fix: 313 passed, 2 failed. The 2 failures (When_Inlines_Transitively_Change,When_IsTextSelectionEnabled_CRLF) also fail without the fix on the same machine (dark theme / shared clipboard).Before/after (real app,
Uno.UI.Composition.Skia.dllswapped into a 7.0.0-dev.1656 build)"Statistics" / "Daily completion" titles (Fraunces, SemiBold):
FontWeightignored).The managed (non-Skia) font backend does not support variation axes at all. It is unchanged here.
🤖 Generated with Claude Code
https://claude.ai/code/session_01LP133TsLEXjMo7XFGmakoS