Skip to content

fix(text): Shape variable fonts at the instance's axis position - #25154

Open
MartinZikmund wants to merge 2 commits into
masterfrom
dev/mazi/variable-font-weight
Open

MartinZikmund wants to merge 2 commits into
masterfrom
dev/mazi/variable-font-weight

Conversation

@MartinZikmund

@MartinZikmund MartinZikmund commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

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 use Fraunces.ttf (variable; wght 100–900, default 900) through TitleTextBlockStyle (FontWeight="SemiBold"). They rendered as SemiBold glyphs laid out at Black widths, so the letters were visibly spread apart. A body font (Karla.ttf, default wght 400) 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 the SKTypeface at the requested wght/wdth/ital/slnt position. Skia then draws and measures the instance correctly, but shaping goes through HarfBuzz. SkiaFont.GetHarfBuzzFont builds 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 ignored FontWeight.

Fix

SkiaFont.GetHarfBuzzFont now passes the typeface's VariationDesignPosition to HbFont.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 the wght/ital/slnt/wdth axis values and calls IDWriteFontFamily2::GetMatchingFonts. The single resulting font face is used for both glyph placement and rendering, so advances always belong to the drawn instance. That means FontWeight="SemiBold" on such a font yields the wght=600 instance 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 FontWeight for variable fonts.

Checked against the same app built for WinUI (Store build, About page title in Fraunces SemiBold): WinUI renders the wght=600 instance 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_VariableFont runtime tests (Skia), using the existing variable Assets/Fonts/OpenSans/OpenSans.ttf through SkiaFontProvider directly so its .manifest is 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_Normal

Skia Desktop (Win32), net10.0-desktop:

  • Without the fix: 4 failed, 1 passed (400 = the default instance passes).
  • With the fix: 5/5 passed.
  • Given_TextBlock, Given_RichTextBlock, Given_GlyphRunRenderer and Given_IFont_AlternativeBackend with 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.dll swapped into a 7.0.0-dev.1656 build)

"Statistics" / "Daily completion" titles (Fraunces, SemiBold):

  • 6.7: Black instance (FontWeight ignored).
  • 7.0 before: SemiBold outlines at Black spacing, so letters are spread apart.
  • 7.0 after: SemiBold outlines with SemiBold spacing.

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

MartinZikmund and others added 2 commits October 9, 2026 18:11
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
Copilot AI balanced review requested due to automatic review settings October 9, 2026 16:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 15s —— View job


Review of #25154

Verdict: 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):

  • Root cause: the fix targets the actual broken invariant. HarfBuzz was shaping the default instance while Skia drew the requested one. SetVariations now puts both at the same axis position. This is a root-cause fix, not a guard.
  • Static fonts: a null or empty VariationDesignPosition returns early, so static fonts take the same path as before.
  • Style: the code follows the repo conventions (tabs, braces, nullable, short why-comment).
  • Tests: Given_VariableFont has a fail-before/pass-after result in the PR description (4 failed → 5 passed). It also covers the default-instance case (wght 400) as a control.

Notes for the author (non-blocking):

  • I did not build or run the tests in this job. The runtime results are the ones reported in the PR description.
  • The behavior change versus 6.7 is intentional. It is documented in the PR and matches WinUI. It is worth a line in release notes, since variable fonts whose default instance is heavier than the requested weight will now render lighter.

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 238589 found UI Test snapshots differences: skia-linux-screenshots: 118, skia-windows-screenshots: 1145

Details
  • skia-linux-screenshots: 118 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Buttons.png-dark
    • Buttons.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ClipboardTests.png-dark
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • DisplayInformation.png-dark
    • DropDownButtonPage.png-dark
    • DoubleImageBrushInList.png-dark
    • DoubleImageBrushInList.png
    • DynamicFont.png-dark
    • DynamicFont.png
    • ImageBrushInList.png-dark
    • ImageBrushInList.png
    • ImageBrushAlignmentXY.png-dark
  • skia-windows-screenshots: 1145 changed over 2474

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedIconPage.png-dark
    • Attributed_text_Simple.png-dark
    • Attributed_text_Simple.png
    • AutomationProperties_AutomationId.png-dark
    • AutoSizedPathCentered.png
    • AutoSuggestBox_Icons.png-dark
    • AutoSuggestBox_Icons.png
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • BitmapIcon_Monochromatic.png-dark
    • BitmapIcon_Monochromatic.png
    • BorderBottomwithmargins.png-dark
    • BorderBottomwithmargins.png
    • BorderLeftwithmargins.png-dark
    • BorderLeftwithmargins.png
    • CalendarDatePicker_Description.png-dark
    • CalendarDatePicker_Description.png
    • ClippingWithOpacity.png-dark
    • ClippingWithOpacity.png
    • ComboBox_DropDownPlacement.png-dark

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 238589 has failed on Uno.UI - CI.

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mergify

mergify Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

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.

5 participants