Skip to content

Place text on its dominant and alignment baselines, with baseline-shift - #222

Merged
swhitty merged 1 commit into
claude/text-lengthfrom
claude/text-baselines
Nov 17, 2026
Merged

swhitty merged 1 commit into
claude/text-lengthfrom
claude/text-baselines

Conversation

@swhitty

@swhitty swhitty commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Step 6 of the text series, and the last planned: the common baselines and baseline-shift.

Based on #221 (claude/text-length, itself based on #220). Neither has merged yet, so this PR targets that branch. Retarget it once they merge.

Changes

  • Baselines. dominant-baseline (inherited) and alignment-baseline take auto, alphabetic, middle, central, hanging, mathematical, ideographic, text-before-edge and text-after-edge.
    • alignment-baseline is not inherited, and overrides the dominant baseline for its own element. That's how resvg treats alignment-baseline on a tspan.
    • Aliases from CSS Inline 3 and SVG 1.1 are accepted: text-top, text-bottom, before-edge, after-edge, baseline and use-script. no-change and reset-size keep the parent's baseline.
    • text-top used to be rejected; Parser.TolerantTests now uses a value that is really invalid.
  • Offsets. The offsets come from the font's metrics in makeYOffset, which stays the one place that asks CoreText for them.
    • The formulas match resvg's. I measured its renders of Times to confirm them:
      • ascent for text-before-edge
      • −descent for text-after-edge and ideographic
      • (ascent − descent) / 2 for central
      • x-height / 2 for middle
      • 0.8 × ascent for hanging
      • 0.5 × ascent for mathematical
    • hanging fixed. Its sign was wrong: the text was lifted above y instead of hanging from it.
    • central used half the cap height; it's now the middle of the em box.
  • baseline-shift. It applies to tspans (and textPath), not to <text> itself or <g>, as resvg does.
    • sub and super come from the font's OS/2 script offsets, through CTFontCopyTable.
    • Lengths and em are supported. Percentages are of the font size the shift is declared at.
    • Nested shifts add up. baseline adds nothing, and inherit takes the parent's value.
  • Layout. The shift goes into each run's baseline offset in makeBaselineOffset, so it reaches text, glyph runs, outlines, <textPath> and the text's bounding box.

resvg tests

dominant-baseline, alignment-baseline and baseline-shift (63 files, against #221):

pass pass, WebKit differs pass, resvg differs SwiftDraw bug
base 4 15 1 43
head 5 52 1 5

WebKit's <img> path ignores most of these properties, so "pass, WebKit differs" is the expected verdict.

The 5 that remain are out of scope:

  • alignment-baseline/hanging-on-vertical needs vertical text.
  • alignment-baseline/hanging-with-underline (23% → 9%) needs text-decoration.
  • alignment-baseline/two-textPath-with-middle-on-first (41% → 20%) is a textPath case.
  • dominant-baseline/hanging (50% → 33%) needs a Devanagari font that isn't installed.
  • alignment-baseline/inherit needs inherit on a non-inherited property.

text/ suite (387 files):

pass pass, WebKit differs pass, resvg differs SwiftDraw bug
base (#221, 7633c35) 104 110 17 156
head 107 147 17 116

No regressions. Over steps 4–6 together (#220, #221 and this PR), SwiftDraw bugs in text/ go from 174 on main to 116.

Among the files that now pass: all the baseline-shift length, percentage, sub, super and nested cases; baseline-shift/with-rotate; and dominant-baseline/inherited-through-nested-tspan. Those last two were among the files #214 and #216 made worse.

Checks

  • swiftdraw-diff compare --base origin/main --resvg Samples.bundle shows no regressions. Existing samples are unchanged.
    • The new text-baselines.svg is at 0.47% against resvg, a pass. It has each baseline against a guide line, alignment-baseline on a tspan, E = mc², H₂O, a nested superscript, and percentage and length shifts.
  • swift test passes on macOS. ./container-run-tests.sh passes with Swift 6.4 and with SWIFT_VERSION=6.2.
  • New Swift Testing suite LayerTreeTextBaselineTests.
    • On every platform: parsing and aliases, inheritance of dominant-baseline but not alignment-baseline, nested shifts adding up, shifts ignored on <text> and <g>, inherit, and length shifts.
    • With CoreText: the metric formulas, OS/2 sub and super offsets, and run positions.

🤖 Generated with Claude Code

dominant-baseline (inherited) and alignment-baseline (not inherited,
overriding it for its own element) take every common value: auto,
alphabetic, middle, central, hanging, mathematical, ideographic,
text-before-edge and text-after-edge, with the CSS Inline 3 and SVG 1.1
aliases (text-top, text-bottom, before-edge, after-edge, baseline,
use-script; no-change and reset-size keep the parent's). Each comes from
the font's ascent, descent and x-height in makeYOffset, as resvg derives
them. The sign of hanging, which lifted the text instead of hanging it,
is fixed.

baseline-shift applies to tspans: sub and super from the font's OS/2
script offsets, lengths, and percentages of the font-size they are
declared at. Shifts of nested tspans add up; baseline adds nothing,
and inherit takes the parent's value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.80952% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.29%. Comparing base (7633c35) to head (a5364d5).

Files with missing lines Patch % Lines
...raw/Sources/LayerTree/LayerTree.Builder.Text.swift 95.65% 2 Missing ⚠️
Additional details and impacted files
@@                  Coverage Diff                   @@
##           claude/text-length     #222      +/-   ##
======================================================
+ Coverage               95.24%   95.29%   +0.04%     
======================================================
  Files                     293      294       +1     
  Lines                   30893    31042     +149     
======================================================
+ Hits                    29425    29581     +156     
+ Misses                   1468     1461       -7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@swhitty
swhitty merged commit 5c50f02 into claude/text-length Nov 17, 2026
19 of 20 checks passed
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.

1 participant