Skip to content

svg_loader: fix tspan/text positioning#4451

Open
JSUYA wants to merge 1 commit into
mainfrom
jsuya/svg-tspan-text-layout
Open

svg_loader: fix tspan/text positioning#4451
JSUYA wants to merge 1 commit into
mainfrom
jsuya/svg-tspan-text-layout

Conversation

@JSUYA

@JSUYA JSUYA commented Jun 11, 2026

Copy link
Copy Markdown
Member

Track a running text position so a run with unset x/y continues from the previous run's end and dx/dy apply as deltas on top. Apply text-anchor before measuring so the anchor takes effect.

issue : #4421

@github-actions github-actions Bot added the svg SVG features label Jun 11, 2026
@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown

Binary Size Report

Config main text main data PR text PR data Delta
arm64-clang 852,199 23,672 852,567 23,672 +368 (+0.04%)
x86-gcc 881,324 11,516 881,612 11,516 +288 (+0.03%)
x86_64-clang 857,070 22,176 857,454 22,176 +384 (+0.04%)
x86_64-gcc 862,923 21,912 863,437 21,912 +514 (+0.06%)

@JSUYA

JSUYA commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

#4421 (comment)
Screenshot from 2026-06-11 10-12-12

#4421 (comment)
(thorvg)
Screenshot from 2026-06-11 15-59-06

(chrome)
Screenshot from 2026-06-11 11-12-36

Currently, whitespace at both ends of the text is not supported.
When given text such as "Big " or " normal", the position of each text element must be set after considering the whitespace; however, it is uncertain whether the functionality to calculate the area including whitespace is supported. Additionally, since the current SVG parser includes logic that ignores whitespace, support for this spec will be addressed in a separate PR.

@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown

Pixel Test Report

Backend Compared Diff Failed
gl 292 0 0
wg 292 0 0
sw 292 0 0

@hermet hermet added the regression A side effect during main dev label Jun 11, 2026
@hermet hermet requested a review from Copilot June 11, 2026 05:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes SVG <text>/<tspan> layout so text runs without explicit x/y continue from the previous run end, with dx/dy applied as deltas and text-anchor affecting measurement/positioning (addresses #4421 pixel mismatches attributed to loader behavior).

Changes:

  • Adjust <tspan> close handling to avoid splicing in more cases, and change text-data parsing to sometimes emit an unpositioned “run” <tspan> node.
  • Track a running textPosition while building tspans, and update it after each run while accounting for text-anchor.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/loaders/svg/tvgSvgLoader.cpp Updates <tspan> splicing rules and text-data parsing to create unpositioned run tspans after inline tspans.
src/loaders/svg/tvgSvgBuilder.cpp Adds running pen-position tracking across text/tspan runs and updates positioning using alignment/measurement.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/loaders/svg/tvgSvgLoader.cpp
Comment thread src/loaders/svg/tvgSvgLoader.cpp
Comment on lines +925 to +931
static void _updateTextPosition(Text* text, const SvgTextNode& textNode, float anchor, Point& textPosition)
{
auto box = _bounds(text);
auto w = box.w > 0.0f ? box.w : 0.0f;
textPosition.x = textNode.x + textNode.dx + (1.0f - anchor) * w;
textPosition.y = textNode.y + textNode.dy;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To calculate the exact x of textPosition, we need the accurate size of the text. For now, this requires calling load() to compute the size. Since the updated flag prevents duplicate loads, the overall computation time stays the same.

@JSUYA JSUYA force-pushed the jsuya/svg-tspan-text-layout branch from a06237f to f9fe564 Compare June 11, 2026 07:09
Track a running text position so a run with unset x/y continues from
the previous run's end and dx/dy apply as deltas on top. Apply
text-anchor before measuring so the anchor takes effect.

issue : #4421
@JSUYA JSUYA force-pushed the jsuya/svg-tspan-text-layout branch from f9fe564 to 3714401 Compare June 11, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

regression A side effect during main dev svg SVG features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants