Skip to content

[V3] Fix SVG glyph arcs, transforms, and gradients - #574

Merged
JimBobSquarePants merged 10 commits into
release/3.2.xfrom
js/v3-svg-glyph-fixes
Nov 24, 2026
Merged

JimBobSquarePants merged 10 commits into
release/3.2.xfrom
js/v3-svg-glyph-fixes

Conversation

@JimBobSquarePants

Copy link
Copy Markdown
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following matches the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Backports the SVG glyph fixes in #569, #571, and #573 to the 3.2 release branch.

  • Keep both arc radii valid under affine transforms.
  • Apply element transforms in SVG order and rotate about the specified centre.
  • Apply the root transform once and inherit only missing gradient attributes and stops.

All 58 focused SVG tests passed in Release on .NET 10.

jafin and others added 10 commits November 24, 2026 12:26
Matrix3x2 transforms row vectors, so the left operand of a product applies
first. SvgGlyphSource composed transforms the other way round:

- A transform list such as "translate(100 0) scale(2)" was applied left to
  right; SVG applies it right to left.
- A child's transform was applied after its parent's instead of before.
- A <use> element's parent transform was applied before its own transform
  and x/y offset instead of last.

Also honour the centre point of rotate(angle cx cy), which was ignored.

(cherry picked from commit fe6688a)
SvgGlyphSource parsed the root <svg> element's transform and applied it
more than once: it seeded Walk with it, so it was baked into every
PaintedLayer.Transform, and it was also stored in PaintedCanvasMetadata,
which PaintedGlyphMetrics.ComputeSourceToUpem applied again. When the
document had no glyph{gid} element, Walk started at the root itself and
parsed the same transform a third time as the node's own transform.

SVG 2 applies a transform on an <svg> element as if it were set on a
parent element, so it acts in the parent space outside the viewBox
mapping. Only the canvas metadata can place it there, so the root
transform now lives only on the canvas: Walk starts from identity, and
the no-glyph-id fallback walks the root's children with the root's
resolved paint and opacity instead of walking the root element itself.
ComputeSourceToUpem now applies the root transform after the viewBox
mapping and before the Y-flip, rather than before the viewBox mapping.
COLR sources always pass an identity root transform, so they are
unaffected.

Add SvgTestFonts.CreateFontFromDocument for tests that need full control
of the document, and SvgRootTransformTests covering a root transform
with a glyph{gid} group, without a glyph id, and combined with a viewBox.

(cherry picked from commit 9a25d65)
A gradient that references another through href/xlink:href inherits an
attribute, or its stops, from the referenced gradient only when it does
not specify it itself (SVG 1.1 13.2.2/13.2.3 xlink:href; SVG 2 paint
server templates). BuildLinearGradient and BuildRadialGradient got this
wrong in three ways:

- gradientTransform: every transform in the chain was multiplied
  together instead of the nearest specified one being used alone.
- gradientUnits and spreadMethod: reassigned on every step, so the
  referenced (outermost) gradient overrode the referencing one.
- Stops: the walk stopped at the first element with stops, so a
  gradient with its own stops never inherited any attribute from the
  gradient it references.

Track units, spread and transform as nullable and take the first value
found walking outward, keep collecting stops only until some are found,
and keep walking the chain for the remaining attributes. An attribute
counts as specified when present; an unrecognized value resolves to its
initial value (objectBoundingBox, pad, identity), as the existing
parsers already do, and still hides the referenced gradient's value.
Coordinate attributes already resolved first-defined-wins and are
unchanged.

(cherry picked from commit fc639a5)
Follow-up to the href inheritance fix, which keeps walking the chain past
the element that supplies the stops:

- A template must be a linearGradient or radialGradient element (SVG 1.1
  13.2.2/13.2.3 xlink:href; SVG 2 gradient href). A reference to any
  other element is now ignored and ends the chain, so a gradient no
  longer takes x1/y1/x2/y2 from a <line> or cx/cy/r from a <circle>.
- Geometry attributes are read only from elements of the gradient's own
  type. A linear template supplies a radial gradient only the common
  attributes (gradientUnits, spreadMethod, gradientTransform) and the
  stops, and vice versa.
- Stops are inherited only when the gradient has no stop children, not
  when none of its stop children parse.
- The visited set is seeded with the starting gradient's id, so a chain
  that leads back to it ends there instead of processing it twice.

(cherry picked from commit 8db09a0)
Moving the root <svg> transform onto the canvas left PaintedGlyph.Bounds
without it, and GetDesignBounds applied it by transforming that cached
axis-aligned box. A rotation or skew in the root transform therefore
produced the box around the rotated box, inflating ink bounds used for
measurement, line metrics and the box passed to renderers.

GetDesignBounds keeps the cheap cached-box path while the source-to-UPEM
mapping keeps the axes aligned (scale, translation, Y-flip), where it is
exact, and otherwise measures the geometry under the full mapping through
the new PaintedGlyph.GetBounds. This keeps the root transform in a single
place, after the viewBox mapping, without the layers needing to know
unitsPerEm and independently of how Walk composes nested transforms.

Replace the no-glyph-id special case in TryGetPaintedGlyph, which
duplicated Walk's container handling, with one rule in Walk: when the
node is the document root, its children start from the parent transform
rather than from one that includes the root's own transform, since that
transform is applied once on the canvas.

Add a test that a rotate(45) root transform gives the same measured and
renderer bounds as the equivalent pre-rotated geometry.

(cherry picked from commit f1dde0d)
The geometry pass for a rotating or skewing root transform ran on every
GetDesignBounds call; the glyph and canvas are fixed per instance, so
cache the result. Also treat transforms that swap the axes, and zero
terms carrying trigonometric rounding noise, as axis-aligned so
rotate(90/180/270) keep transforming the cached box.

(cherry picked from commit 9d76e7f)
Drop two branches that could never run: a gradient is only built through
the id it was referenced by, so pass that id in rather than re-reading an
attribute that is always present, and detect the document root as the
element without a parent rather than through a nullable Document.

Add tests for the paths the patch left untested: quarter-turn and skewed
root transforms, cubic curves and arcs under a rotated root transform,
root fill="none" with a filled child, a radial gradient's own focal
radius, and radial defaults following inherited userSpaceOnUse units.

(cherry picked from commit 5856ab6)
PaintedGlyphMetrics scaled an arc's y-radius by M12, the rotation/shear
term of the transform, so every arc in an unrotated SVG glyph reached the
renderer with ry = 0 and was drawn as a straight line. The x-radius used
M11 alone, which is zero under a 90 degree rotation and negative under a
180 degree rotation or a mirror.

Map the arc's ellipse through the full linear part of the transform
instead: the transformed radii and axis come from the eigen-decomposition
of the ellipse's shape matrix, which also handles non-uniform scale and
the shear added by synthetic oblique. A negative determinant still
reverses the sweep.

(cherry picked from commit f83e135)
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.14530% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82%. Comparing base (ccbfefa) to head (a15f6c3).

Files with missing lines Patch % Lines
...c/SixLabors.Fonts/Rendering/PaintedGlyphMetrics.cs 96% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           release/3.2.x    #574    +/-   ##
==============================================
  Coverage             81%     82%            
==============================================
  Files                408     408            
  Lines              35802   35826    +24     
  Branches            5586    5596    +10     
==============================================
+ Hits               29186   29441   +255     
+ Misses              5440    5242   -198     
+ Partials            1176    1143    -33     
Flag Coverage Δ
unittests 82% <99%> (+<1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

@JimBobSquarePants
JimBobSquarePants merged commit 50dae68 into release/3.2.x Nov 24, 2026
10 checks passed
@JimBobSquarePants
JimBobSquarePants deleted the js/v3-svg-glyph-fixes branch November 24, 2026 03:14
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.

2 participants