Repository navigation
Fit text to textLength with lengthAdjust - #221
Merged
Merged
Conversation
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 Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Step 5 of the text series:
textLengthandlengthAdjust.Based on #220 (
claude/text-spacing), which hasn't merged yet, so this PR targets that branch. Once #220 merges, retarget it tomain.Changes
textLengthandlengthAdjustare parsed on<text>and<tspan>.%(of the viewport width).inherit, is ignored.0is valid and squashes the characters together.Builder.makeFittedSpacingis 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 itstextLengthover the gaps between its characters. The natural length is the advances plus the letter-spacing, word-spacing anddxbetween the characters.spacingAndGlyphsscales the advances, the gaps and the glyphs. Scaled glyphs are aGlyphRunwithscaleX, stretched about each glyph's origin when its outlines are drawn.textLength/on-a-single-tspan). The span's own extent still measures exactly itstextLength.textLength/on-text-and-tspanagree. So does an element that starts a new text chunk within itself.textLength.transform="scale(.1)",text-anchor="middle",textLength) is in the new sample.textLengthis unchanged, and all existing samples are identical.resvg
text/suite (387 files), against #220Now 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 applytextLengthon these.Unchanged passes: textLength/{150-on-parent, inherit, negative}
Out of scope:
lengthAdjust/with-underlinegoes from 46% to 11%; only the underline (text-decoration) is missing.lengthAdjust/verticalis vertical text.lengthAdjust/text-on-pathneedstextLengthon<textPath>, which was optional and isn't done.textLength/arabic(36.9% → 40.4%) andarabic-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.bundleshows no regressions. The newtext-length.svgmatches resvg (0.00%). It covers spacing, stretched and squeezed text, a tspan with the following text moving on, and the badge.swift testpasses on macOS../container-run-tests.shpasses with Swift 6.4 and withSWIFT_VERSION=6.2.LayerTreeTextLengthTests.spacingAndGlyphsarithmetic, including existing spacing; nested elements; single characters; elements spanning chunks; and the text after a fitted span.🤖 Generated with Claude Code