Skip to content

fix(apple): apply weight and slant to every family of a font list - #319

Merged
houko merged 2 commits into
mainfrom
fix/apple-font-fallback
Nov 16, 2026
Merged

houko merged 2 commits into
mainfrom
fix/apple-font-fallback

Conversation

@houko

@houko houko commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #305.

What changes

  • buildFace now styles every family of a FontFamily list, not only the first. The cascade list listedFont builds names the regular faces of the later families, so a bold or italic text set the characters the first family lacks in the regular face ("Menlo, Hiragino Sans" at 700 set Chinese in HiraginoSans-W3). Each cascade entry is now rebuilt from its family name plus the same traits dictionary the first family gets; entries for the system's UI fonts take the traits onto their own descriptor, as buildFace does for a first family.
  • A list that starts with the system font (system-ui, Hiragino Sans) restyles its cascade too. Before, the system-font branch copied the descriptor with only the traits, keeping the regular cascade.
  • Line metrics: on macOS and iOS a line keeps taking its height and baseline from the faces its runs use. docs/public/en/text.md (and zh) say so, as the issue's acceptance allows, docs/architecture/text.md records it, and the roadmap gains a "Font struts on Apple" item for the CSS strut.

Why the strut was dropped from this PR

The first version of this PR fixed every line of a font list to the first family's height (paragraph minimum and maximum line height) and moved baselines after layout. Probing it showed:

  • The framesetter drawing path moved baselines in bottom-up coordinates by the same delta that placed() applied top-down, so glyphs were drawn on the opposite side of where TextLines, carets and hit tests put them (a 6–8 pt mismatch for a fallback line with MaxLines or a LineHeight).
  • Fixing the line height to the first family's clipped taller fallbacks: two lines of Zapfino in Menlo, Zapfino measured 47 pt against 94 pt alone, and the docs' own Inter, "Noto Sans SC" example would set Chinese lines on Inter's height. CSS's strut is a floor, not a fixed height.
  • Some layouts measured taller than either family alone (two lines of Menlo, Hiragino Sans measured 57 pt against 48 and 51).
  • It only applied to lists with a comma, so the issue's own single-family example (Ω in Instrument Serif falling back to Helvetica) was untouched, and it turned off the one-line fast path for every font list.

A correct strut needs the backend to stack lines itself (size, drawing, carets, hit tests and truncation from one set of per-line metrics, max(line, first family)), which also overlaps the framesetter-height work in #340. That is recorded as a roadmap item instead.

Verification

  • TestListsFallBackInTheStyledFace lays out text with characters the first family lacks and checks the run's face is the later family's at the weight and slant asked for (Menlo, Hiragino Sans and system-ui, Hiragino Sans at 700, Didot, Times New Roman italic). Both bold cases and TestStyledFacesOfFallbackFamilies fail with the cascade left unstyled; the italic case already passed on main, where CoreText matched Times New Roman's italic face for the fallback, and stays as a guard.
  • go build ./..., go tool nsgo check -target all, go tool nsgo test ./internal/platform/apple, go tool nsgo test -run 'Layer|Skills|Doc' .
  • macOS target (nsgo test -target macos, a throwaway probe app with the lists above, before and after this change): on main the Chinese in Menlo, Hiragino Sans and system-ui, Hiragino Sans at 700 is regular beside bold Latin; with this change it is bold. The italic Cyrillic in Didot, Times New Roman is italic both before and after.
  • iOS shares the same CoreText code; it was not checked on a device, as the test iPhone is unavailable.

@github-actions github-actions Bot added bug Something isn't working macOS iOS size/M labels Nov 15, 2026
@houko
houko force-pushed the fix/apple-font-fallback branch from 9eb6eac to 4268f8e Compare November 16, 2026 01:16
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Nov 16, 2026
@houko houko changed the title fix(apple): preserve font fallback traits and metrics fix(apple): apply weight and slant to every family of a font list Nov 16, 2026
@houko
houko force-pushed the fix/apple-font-fallback branch from 4268f8e to aa31df6 Compare November 16, 2026 01:27
buildFace styled only the first family of a FontFamily list: the cascade list kept the regular faces listedFont made, so a bold or italic text set the characters the first family lacks in the later family's regular face. Each cascade entry now takes the same traits, by family name for a named family and on its own descriptor for the system's UI fonts, and a list that starts with the system font restyles its cascade too.
On macOS and iOS a line takes its height and baseline from the faces its runs use, so a line set wholly in a later family of a list takes that family's metrics, where CSS keeps the first family's as a strut. Say so in the text docs and record the strut as a roadmap item.
@houko
houko force-pushed the fix/apple-font-fallback branch from aa31df6 to 1df3e57 Compare November 16, 2026 01:31
@houko

houko commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator Author

Reviewed.

  • Head reviewed: 4268f8e on a45d732. I pushed one small fix on top, folded into the fix commit: the new Go comments in styledCascade and TestListsFallBackInTheStyledFace were hard-wrapped and are now single lines. No code change. Then I rebased it onto current main (475a87f, which only changes layout) and re-ran go build ./... for darwin, windows, linux and android, plus the apple package tests (51 passed, 1 skipped). Final head 1df3e57.
  • Code: styledCascade frees everything it copies (each entry's family name, the attribute dictionaries, the descriptors after the array holds them), and buildFace frees the new cascade in both branches. It is only called when the descriptor has a cascade list, which listedFont only sets with two or more fonts, so the list is never empty. Regular upright text still returns the base font unchanged.
  • The tests fail without the fix: with main's text.go and this PR's tests, TestStyledFacesOfFallbackFamilies fails (TimesNewRomanPSMT) and TestListsFallBackInTheStyledFace fails for both bold cases (HiraginoSans-W3 where W6 was asked for).
  • Extra probe of the run faces, before and after (CoreText on macOS): Menlo, Hiragino Sans at 700, 900, 300 and bold italic gives W6, W8, W2 and W6, where before it was always W3. Menlo, PingFang SC at 600 gives PingFangSC-Semibold (was Regular). Menlo, Songti SC bold italic gives STSongti-SC-Bold (was Regular). A family named by its PostScript name (Menlo, HiraginoSans-W3 at 700) gives W6, the same as a first family. Things that must not change stay the same: a family with one face (Hiragino Maru Gothic ProN, the registered Pacifico) still keeps that face at 700 and in italic and does not drop to the system fallback; italic CJK stays in Hiragino Sans W3 and is not swapped for PingFang; Menlo, Pacifico, Hiragino Sans still reaches the third family.
  • Mac Studio, go 1.27.3: go build ./..., GOOS=ios go vet ./internal/platform/apple, go tool nsgo check -target all (every target vetted, exit 0), go tool nsgo test ./internal/platform/apple (51 passed, 1 skipped) and go tool nsgo test -run 'Layer|Skills|Doc' . (31 passed).
  • macOS, real window (nsgo test -target macos, a throwaway probe app with six font lists, captured with main's text.go and then with this PR's): Chinese in Menlo, Hiragino Sans and system-ui, Hiragino Sans at 700 and Menlo, PingFang SC at 600 was regular next to bold Latin on main and is bold with this change. Songti in bold italic is heavier. Italic Hiragino is unchanged. Line positions are the same before and after.
  • Docs: en and zh text.md say the same thing. The architecture note and the new roadmap row (in "Finish the existing platforms") match what the code does now. CHANGELOG.md is not changed, the commit messages follow the conventions and there are no unrelated changes.
  • Not checked: iOS on a device, as the test iPhone cannot be used right now. iOS runs the same CoreText code, and that code vets for ios and ios_simulator.

@houko
houko marked this pull request as ready for review November 16, 2026 01:32
@houko
houko merged commit e4f30bb into main Nov 16, 2026
11 checks passed
@houko
houko deleted the fix/apple-font-fallback branch November 18, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation iOS macOS size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apple: in a FontFamily list only the first family takes the weight and italic, and a line set in a fallback family takes that family's metrics

1 participant