Skip to content

[vector_graphics_compiler] Scale the initial stroke width - #13577

Merged
auto-submit[bot] merged 1 commit into
flutter:mainfrom
saitatter:fix/vgc-default-stroke-width
Nov 23, 2026
Merged

auto-submit[bot] merged 1 commit into
flutter:mainfrom
saitatter:fix/vgc-default-stroke-width

Conversation

@saitatter

Copy link
Copy Markdown
Contributor

[vector_graphics_compiler] Scale the initial stroke width

Description

A stroke without stroke-width kept a null width under a transform, so it was drawn with a width of 1 while explicit widths were scaled. scaleStrokeWidth now scales a null width as the initial SVG value of 1.

The width stays null when the transform has no scale, such as a pure rotation, so that paints still dedupe and nothing changes for those SVGs.

Test changes

Two existing tests pinned the old behavior, so their expected values change:

  • Stroke width with scaling: two strokes under scale(.8) now have a width of 0.8.
  • Ghostscript Tiger - dedupes paints: strokes under matrix(1.7656463 ...) now have a width of 1.7656463.

Comparison with Chrome

Comparison of main, this PR and Chrome

Under scale(0.25), Chrome draws both lines thinner than Flutter does. With this PR, the two lines match each other, so that remaining difference also applies to explicit widths and is not part of this fix.

Fixes flutter/flutter#196088

Pre-Review Checklist

🤖 Generated with Claude Code

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2

@github-actions github-actions Bot added p: vector_graphics triage-engine Should be looked at in engine triage labels Nov 6, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the vector graphics compiler to scale the initial SVG stroke width (which defaults to 1 when null) when transforms are applied. It modifies scaleStrokeWidth in matrix.dart to handle null widths, updates parser and resolver tests, and bumps the package version to 1.3.5. Feedback on the changes suggests that the fast-path check a == 1 && d == 1 in scaleStrokeWidth is insufficient for skew transforms where b or c are non-zero, and recommends also verifying that b == 0 and c == 0.

Comment on lines 52 to 54
if (a == 1 && d == 1) {
return width;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The fast-path check a == 1 && d == 1 is insufficient to determine if the matrix has no scaling or skewing. In SVG, a skew transform (e.g., skewX or skewY) can have a == 1 and d == 1 while b or c are non-zero, which actually scales the stroke width. To prevent incorrectly skipping the scale calculation for skewed matrices, we should also verify that b == 0 and c == 0.

Suggested change
if (a == 1 && d == 1) {
return width;
}
if (a == 1 && d == 1 && b == 0 && c == 0) {
return width;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

+1 to @gemini-code-assist's comment here. Please update this condition and add skew cases for xSkewed and ySkewed to the scaleStrokeWidth test in test/matrix_test.dart.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done: the fast path now also checks b == 0 && c == 0, and I added xSkewed and ySkewed cases to the scaleStrokeWidth test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The determinant (ad - bc) describes the overall scale of the 4 element matrix. Using abs(ad-bc) would tell you if there is no scaling. It also ignores skews in doing so.

The determinant is the signed area of the transformed unit square. So, if it is 1 or -1 then there is no scaling. If it is any other value then it is the product of the X and Y scaling factors.

Unfortunately this means that if the X scale is 5 and the Y scale is .2 then it would return 1 since the area of that transformed unit square is still 1.0 whereas the current implementation would want to return 2.6 as the arithmetic average.

We could also ask the question as to why this method chose the arithmetic average rather than the geometric average. Is that part of the SVG spec? If not, then perhaps we should be using the geometric mean - sqrt(abs(ad - bc))? The first part of that would be to calculate the (abs) determinant abs(ad - bc) and then use 1.0 as a short-cut for avoiding the sqrt?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, perhaps answering that question should be considered as "follow-on work" once this bug is fixed...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hello flar, thanks for the review; will investigate all these during this weekend and come with a follow-up on Monday.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @flar, these are good points. I looked into the spec question.

The SVG 2 spec doesn't prescribe a scalar here. stroke-width is a length in user units (13.5.3), and the stroke outline is computed in the user coordinate system and then transformed along with the geometry (13.6):

Normally stroking involves calculating stroke outline of the shape's path in current user coordinate system and filling that outline with the stroke paint

So under a non-uniform scale or a skew, the correct result is a stroke whose width varies with direction. No single factor can reproduce that. The only case where the spec says the width doesn't depend on the transform is vector-effect: non-scaling-stroke, which strokes in the host coordinate space instead. Computing the shape of the stroke (13.5.7) never mentions a scale factor either. Since the compiler bakes the transform into the path, it has to approximate, and the arithmetic mean is an implementation choice, not a spec requirement.

Given that, I agree sqrt(|ad - bc|) is the better approximation:

  • it preserves the stroke's area (a stroke of width w covers w * |det| per unit length on average),
  • it goes to 0 when either axis is scaled to 0, so a collapsed primitive gets no stroke,
  • it needs one sqrt and lets |det| == 1 short-circuit.

While checking this, I also noticed that xScale/yScale use sqrt(a² + c²) and sqrt(b² + d²), which are row norms. With transformPoint mapping x' = a*x + c*y + e, the image of the x axis is (a, b), so those aren't the per-axis scales for skewed matrices. The determinant version avoids that question entirely.

As you suggested, I'd like to do this as follow-on work rather than in this PR. Switching to the geometric mean changes the output for every skewed or non-uniformly scaled stroke, including explicit widths that this PR doesn't touch. It would also flip the xSkewed/ySkewed expectations that were just added here, since a pure skew has |det| == 1. This PR keeps the existing formula and only fixes the default width not being scaled. I'll file an issue for the determinant change and send a separate PR with tests. Does that work for you?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Filed flutter/flutter#197001 for the determinant-based scaling (and the xScale/yScale row-norm question).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The engine is capable of performing non-uniformly scaled strokes, but it looks like the compiler is trying to deliver a device-space stroke instead. Am I reading that correctly? I'm not sure what the purpose of that is, but if it just forwards the non-uniform transform and stroke width to the engine the right thing should happen.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's right. The compiler bakes the accumulated SVG transform into the path coordinates (visitPathNode in lib/src/svg/resolver.dart), so the engine only ever sees pre-transformed geometry plus a scalar width. The transform the widget is painted with is still applied by the engine, so it's the SVG root coordinate space rather than device space.

As far as I can tell, this comes from the .vec format. writeDrawPath in vector_graphics_codec carries only a path, a paint and an optional pattern, with no matrix. Only gradients, patterns, images and text positions carry a transform. The clipping, masking and overdraw optimizers in the compiler also run path ops on the baked paths, and the decoder rejects any format version other than the current one.

So I agree the right fix is to forward the transform and the untransformed stroke width for strokes the engine can do properly, at least for non-similarity transforms, where no scalar is correct. That needs a new codec command, so I've reframed flutter/flutter#197001 around that and will ask the vector_graphics owners there about the format change.

This PR stays a fix for the default width within the current model: today a transformed path with no stroke-width doesn't get scaled at all. Is that OK with you?

@gaaclarke
gaaclarke requested a review from andywolff November 9, 2026 19:02

@andywolff andywolff 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.

mostly looks good to me, with a few comments

Comment on lines 52 to 54
if (a == 1 && d == 1) {
return width;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

+1 to @gemini-code-assist's comment here. Please update this condition and add skew cases for xSkewed and ySkewed to the scaleStrokeWidth test in test/matrix_test.dart.

@andywolff andywolff added the CICD Run CI/CD label Nov 9, 2026
@flutter-dashboard

Copy link
Copy Markdown

This pull request is not mergeable in its current state, likely because of a merge conflict. Pre-submit CI jobs were not triggered. Pushing a new commit to this branch that resolves the issue will result in pre-submit jobs being scheduled.

A stroke without stroke-width kept a null width under a transform, so it was drawn with a width of 1 while explicit widths were scaled. Scale it as the initial SVG value of 1, and keep it null when the transform has no scale so that paints still dedupe.
@saitatter
saitatter force-pushed the fix/vgc-default-stroke-width branch from af9345f to b472ed9 Compare November 12, 2026 20:59
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 12, 2026

@andywolff andywolff 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.

LGTM, thanks

@andywolff andywolff added autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD labels Nov 12, 2026
@auto-submit

auto-submit Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/packages/13577, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 1 more review(s) in order to merge this PR.

  • Merge guidelines: A PR needs at least one approved review if the author is already part of flutter-hackers or two member reviews if the author is not a member of flutter-hackers before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 12, 2026
@andywolff
andywolff requested a review from gaaclarke November 12, 2026 23:20

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@Piinks

Piinks commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

From triage - is this ready to land?

@andywolff

Copy link
Copy Markdown

No, @flar left some comments that the author said they would respond to

@saitatter

saitatter commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Yes, not ready yet; I've been a little busy these few days. Will come with a response in a few days. Testing some of flars ideas.

@gaaclarke gaaclarke added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 23, 2026
@auto-submit
auto-submit Bot merged commit 80330d7 into flutter:main Nov 23, 2026
14 checks passed
harryterkelsen pushed a commit to harryterkelsen/flutter that referenced this pull request Nov 24, 2026
…r#197316)

flutter/packages@5b93485...f455dfa

2026-11-24 [email protected] [material_ui]
Add Material 3 Expressive `FilledButton` and Filled Tonal button
(`FilledButton.tonal`) (flutter/packages#13702)
2026-11-24 [email protected] Roll Flutter from
685f4cf to 98744d3 (8 revisions) (flutter/packages#13768)
2026-11-23 [email protected] [material_ui] Migrate Chip
and ChipTheme API doc snippets to {@example} (batch 11)
(flutter/packages#13720)
2026-11-23 [email protected] [material_ui] Migrate
Dialog, AlertDialog, and SimpleDialog API doc snippets to {@example}
(batch 12) (flutter/packages#13721)
2026-11-23 [email protected] [vector_graphics_compiler] Scale
the initial stroke width (flutter/packages#13577)
2026-11-23 [email protected] [in_app_purchase_storekit] Suppress
StoreKit 1 deprecations in imported headers (flutter/packages#13745)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD p: vector_graphics triage-engine Should be looked at in engine triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[vector_graphics_compiler] The initial stroke-width is not scaled by transforms

5 participants