Repository navigation
[vector_graphics_compiler] Scale the initial stroke width - #13577
Conversation
There was a problem hiding this comment.
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.
| if (a == 1 && d == 1) { | ||
| return width; | ||
| } |
There was a problem hiding this comment.
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.
| if (a == 1 && d == 1) { | |
| return width; | |
| } | |
| if (a == 1 && d == 1 && b == 0 && c == 0) { | |
| return width; | |
| } |
There was a problem hiding this comment.
+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.
There was a problem hiding this comment.
Done: the fast path now also checks b == 0 && c == 0, and I added xSkewed and ySkewed cases to the scaleStrokeWidth test.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Also, perhaps answering that question should be considered as "follow-on work" once this bug is fixed...
There was a problem hiding this comment.
Hello flar, thanks for the review; will investigate all these during this weekend and come with a follow-up on Monday.
There was a problem hiding this comment.
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
wcoversw * |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
sqrtand lets|det| == 1short-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?
There was a problem hiding this comment.
Filed flutter/flutter#197001 for the determinant-based scaling (and the xScale/yScale row-norm question).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
andywolff
left a comment
There was a problem hiding this comment.
mostly looks good to me, with a few comments
| if (a == 1 && d == 1) { | ||
| return width; | ||
| } |
There was a problem hiding this comment.
+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.
|
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.
af9345f to
b472ed9
Compare
|
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.
|
|
From triage - is this ready to land? |
|
No, @flar left some comments that the author said they would respond to |
|
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. |
…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
[vector_graphics_compiler] Scale the initial stroke width
Description
A stroke without
stroke-widthkept a null width under a transform, so it was drawn with a width of 1 while explicit widths were scaled.scaleStrokeWidthnow 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 underscale(.8)now have a width of0.8.Ghostscript Tiger - dedupes paints: strokes undermatrix(1.7656463 ...)now have a width of1.7656463.Comparison with 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
[shared_preferences]///).🤖 Generated with Claude Code
Footnotes
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