Skip to content

Fit text to textLength with lengthAdjust - #221

Merged
swhitty merged 1 commit into
claude/text-spacingfrom
claude/text-length
Nov 17, 2026
Merged

swhitty merged 1 commit into
claude/text-spacingfrom
claude/text-length

Conversation

@swhitty

@swhitty swhitty commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Step 5 of the text series: textLength and lengthAdjust.

Based on #220 (claude/text-spacing), which hasn't merged yet, so this PR targets that branch. Once #220 merges, retarget it to main.

Changes

  • Parsing. textLength and lengthAdjust are parsed on <text> and <tspan>.
    • The length takes units and % (of the viewport width).
    • A negative or invalid length, including inherit, is ignored. 0 is valid and squashes the characters together.
  • Fitting. Builder.makeFittedSpacing is plain arithmetic over the shaped advances from Shape text once and lay out letter-spacing and word-spacing in Swift #220, so it is tested on every platform. It fits each element's characters, innermost first.
    • spacing (the default) spreads the difference between the element's natural length and its textLength over the gaps between its characters. The natural length is the advances plus the letter-spacing, word-spacing and dx between the characters.
    • spacingAndGlyphs scales the advances, the gaps and the glyphs. Scaled glyphs are a GlyphRun with scaleX, stretched about each glyph's origin when its outlines are drawn.
    • The text after a fitted tspan moves on. As resvg does it, the gap is added after the tspan's last character too (textLength/on-a-single-tspan). The span's own extent still measures exactly its textLength.
    • An element whose characters a descendant already fitted keeps its natural length. Per SVG 2 those characters are resolved; resvg and textLength/on-text-and-tspan agree. So does an element that starts a new text chunk within itself.
    • Anchoring uses the fitted extent, so a centred badge spans exactly its textLength.
  • Badges. A shields.io-style badge (transform="scale(.1)", text-anchor="middle", textLength) is in the new sample.
  • Unchanged output. Text without textLength is unchanged, and all existing samples are identical.

resvg text/ suite (387 files), against #220

pass pass, WebKit differs pass, resvg differs SwiftDraw bug
base (#220, e6a7f10) 104 102 19 162
head 104 110 17 156

Now pass, WebKit differs: textLength/{150, 150-on-tspan, 40mm, 75percent, on-a-single-tspan, on-text-and-tspan, zero}, lengthAdjust/spacingAndGlyphs. WebKit's <img> path doesn't apply textLength on these.

Unchanged passes: textLength/{150-on-parent, inherit, negative}

Out of scope:

  • lengthAdjust/with-underline goes from 46% to 11%; only the underline (text-decoration) is missing.
  • lengthAdjust/vertical is vertical text.
  • lengthAdjust/text-on-path needs textLength on <textPath>, which was optional and isn't done.
  • textLength/arabic (36.9% → 40.4%) and arabic-with-lengthAdjust (43.2% → 45.0%) are flagged as regressions. They need the Amiri font, which isn't installed, and bidi: a fitted Arabic run is placed glyph by glyph in logical order.

Checks

  • swiftdraw-diff compare --base origin/main --resvg Samples.bundle shows no regressions. The new text-length.svg matches resvg (0.00%). It covers spacing, stretched and squeezed text, a tspan with the following text moving on, and the badge.
  • swift test passes on macOS. ./container-run-tests.sh passes with Swift 6.4 and with SWIFT_VERSION=6.2.
  • New Swift Testing suite LayerTreeTextLengthTests.
    • On every platform: parsing; innermost-first order; spacing and spacingAndGlyphs arithmetic, including existing spacing; nested elements; single characters; elements spanning chunks; and the text after a fitted span.
    • With CoreText: a stretched glyph run.

🤖 Generated with Claude Code

textLength and lengthAdjust are parsed on <text> and <tspan>; a negative
or invalid length (inherit) is ignored. Each element's characters are
fitted innermost first, as plain Swift over the shaped advances:
lengthAdjust="spacing" spreads the difference over the space between
them, and "spacingAndGlyphs" scales their advances, the space between
them and their glyphs. The text after a fitted tspan moves on, by the
gap after its last character too, as resvg does. An element whose
characters a descendant already fitted, or that starts a new chunk
within itself, keeps its natural length.

Scaled glyphs are a GlyphRun with scaleX, stretched about each glyph's
origin when its outlines are drawn.

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 97.71429% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.24%. Comparing base (e6a7f10) to head (7633c35).

Files with missing lines Patch % Lines
...aw/Tests/LayerTree/LayerTree.TextLengthTests.swift 97.50% 2 Missing ⚠️
SwiftDraw/Sources/LayerTree/LayerTree.Layer.swift 0.00% 1 Missing ⚠️
...tDraw/Sources/Renderer/Renderer.CoreGraphics.swift 66.66% 1 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                  @@
##           claude/text-spacing     #221   +/-   ##
====================================================
  Coverage                95.24%   95.24%           
====================================================
  Files                      292      293    +1     
  Lines                    30825    30893   +68     
====================================================
+ Hits                     29360    29425   +65     
- Misses                    1465     1468    +3     

☔ 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 5c4926d into claude/text-spacing Nov 17, 2026
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