Skip to content

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 #305

Closed

Description

@houko

Context

Checked at aadbe7e; main at 6160fe6 has the same internal/platform/apple/text.go.

docs/public/en/text.md:52 says FontFamily "names a family or a CSS list (Inter, "Noto Sans SC", sans-serif), each used for the characters those before it lack", and that FontWeight and FontStyle "set the face". On macOS and iOS that holds for the first family only.

1. Weight and italic stop at the first family. listedFont makes a regular, upright CTFont for each family in the list (familyFont, internal/platform/apple/text.go:268-293, which creates it by name) and turns all but the first into a cascade list of those fonts' descriptors (:236-264, the descriptors at :253, the list at :257). buildFace then applies the weight and the italic trait: for a named family it builds a descriptor of the first family's name plus the traits, and copies the cascade list across unchanged (:296-342, the copy at :331-333). The cascade entries still name the regular faces they were made from, so CoreText uses those faces for every character the first family lacks. Two cases we hit:

  • FontFamily: "Source Serif 4, Noto Serif SC" with FontWeight: 600: the Latin text is semibold, the Chinese text is Noto Serif SC Regular.
  • FontFamily: "Instrument Serif, Times New Roman" with FontStyle: FontStyleItalic: Cyrillic, which Instrument Serif lacks, is set upright in Times New Roman.

2. A line set in a fallback family takes that family's metrics. The line's height and baseline come from CTLineGetTypographicBounds (internal/platform/apple/text.go:683-685, the origin at :806), which reports the fonts the runs actually use. When the first family has none of a line's glyphs, the line's ascent is the fallback face's alone. CSS keeps the first available font's metrics in every line (the strut), so the same text sits at the same baseline whichever face draws it. Example: Ω in "Instrument Serif" falls back to Helvetica Oblique, and the line takes Helvetica's ascent (about 0.77 em), so the glyph lands higher than in a browser.

Why it matters

A font list is how an app pairs a Latin face with a CJK or Cyrillic one, and headings are bold and quotes italic in both scripts. On Apple, the second script silently loses the weight or the slant, so a bold heading in mixed Chinese and English renders half bold. Apps work around it by splitting text into spans per script and naming the fallback family explicitly in each, which duplicates the font list and needs a script detector in the app. The metrics difference makes mixed-script or symbol-heavy lines jump vertically against the web build of the same design.

Proposal

  1. In buildFace, apply the traits to each cascade entry as well: for each descriptor in the cascade list, make a descriptor of its family name plus the same traits dictionary (as is done for the first family), and put those in the styled descriptor's cascade list. A family with no matching face keeps CoreText's nearest match, as the first family does today.
  2. For line metrics, take the first family's ascent, descent and leading as a floor for each line (for example with a paragraph style's minimum line height, or by adjusting the origins from CTLineGetTypographicBounds against the primary font's metrics), so a line drawn wholly in a fallback face keeps the primary font's baseline, as CSS's strut does. If this is deliberate, say in text.md that line metrics follow the face actually used on Apple.

Acceptance

  • FontFamily: "Source Serif 4, Noto Serif SC", FontWeight: 600 draws the Chinese characters in Noto Serif SC SemiBold (or its nearest weight).
  • An italic style with a fallback family that has an italic face draws the fallback characters in it.
  • A line whose glyphs all come from a fallback family has the same baseline offset as a line in the first family, at the same size and line height, or the docs say it does not.
  • A test in internal/platform/apple checks the cascade list of a weighted, italic face built from a list.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions