Skip to content

[vector_graphics] Forward non-similarity transforms of stroked paths to the engine instead of scaling stroke-width #197001

Open

Description

@saitatter

Update: forward the transform instead of approximating it

Per @flar's comment below and on flutter/packages#13577, the engine (ui.Path, ui.Canvas, ui.Paint) can already draw non-uniformly scaled and skewed strokes correctly. Rather than picking a better scalar approximation, the compiler should hand the transform and the untransformed stroke-width to the engine.

What stands in the way today:

  • The format has no transform for paths. The compiler bakes the accumulated SVG transform into path coordinates (visitPathNode in vector_graphics_compiler/lib/src/svg/resolver.dart). In vector_graphics_codec, writeDrawPath carries only a path id, a paint id and an optional pattern id. Only gradients, patterns, images and text positions carry a matrix.
  • The optimizers work on baked paths. clipping_optimizer, masking_optimizer and overdraw_optimizer run path ops on the transformed geometry.
  • The decoder is strict about the version. It rejects any format version other than the current _version = 1, so a new command needs a plan for compatibility with already-compiled .vec files.

A possible direction is to keep baking transforms that are similarities (translation, rotation, uniform scale, reflection), where a scalar width is exact. For other transforms on stroked paths, emit the path in local coordinates together with its matrix, for example with a new draw-path-with-transform command or save/transform/restore commands, and let the engine stroke it.

I'd like input from the vector_graphics owners on whether a format change like this is acceptable before working on it.

Original proposal (determinant-based scale)

Use case

When vector_graphics_compiler bakes a transform into a path, it also has to scale stroke-width. It does this in AffineMatrix.scaleStrokeWidth (packages/vector_graphics_compiler/lib/src/geometry/matrix.dart) by multiplying the width by the arithmetic mean of xScale and yScale:

double get xScale => math.sqrt(a * a + c * c);
double get yScale => math.sqrt(b * b + d * d);
// ...
final double scale = (xScale + yScale) / 2;

This was raised by @flar in flutter/packages#13577 (comment) and was agreed to be follow-on work for that PR.

The SVG 2 spec doesn't define a scalar factor for this. The stroke outline is computed in the user coordinate system and then transformed with the geometry (Painting 13.6, 13.5.7). Under non-uniform scale or skew the true stroke has a width that varies with direction, so any single factor is an approximation. The current one has some problems:

  1. It doesn't go to zero. If one axis is scaled to 0, the primitive collapses, but the mean of the two scales is still non-zero.
  2. The axis scales are row norms. transformPoint maps x' = a*x + c*y + e, y' = b*x + d*y + f, so the image of the x axis is (a, b) with length sqrt(a² + b²). xScale/yScale use (a, c)/(b, d) instead, which only matches when the matrix has no skew.
  3. It's expensive. It needs two sqrt calls and two multiply-adds, plus a separate identity fast path.

Proposal

Scale by the geometric mean of the axis scales, sqrt(|ad - bc|):

double? scaleStrokeWidth(double? width) {
  final double det = (a * d - b * c).abs();
  if ((det - 1).abs() < precisionErrorTolerance) {
    return width;
  }
  return math.sqrt(det) * (width ?? 1);
}
  • On average it preserves the area covered by the stroke.
  • It is 0 when either axis collapses.
  • It needs one sqrt, and |det| == 1 short-circuits, which also covers rotations, reflections and pure skews.

This changes the output for strokes under non-uniform scale or skew. For example, scale(5, 0.2) currently gives 2.6 and would give 1, and skewX(45) currently gives about 1.207 and would give 1. The existing scaleStrokeWidth tests in test/matrix_test.dart (including the xSkewed/ySkewed cases added in flutter/packages#13577) and any affected goldens would need updating.

Whether xScale/yScale should also be corrected to column norms is a separate question. They are also used to scale blur sigmas in lib/src/svg/resolver.dart (filterBlurX/filterBlurY), so changing them would affect filters too.

Activity

  1. added
    in triage
    team-engine
    engine
    package
    p: vector_graphics
    c: proposal
    and removed
    in triage
    on Nov 20, 2026
  2. flar commented on Nov 20, 2026

    @flar
    Contributor

    The engine (i.e. the ui.Path ui.Canvas and ui.Paint objects) are capable of doing a non-uniformly scaled stroke. The compiler should just rely on the engine to do the right thing here rather than try to bake everything into a device-space result here.

  3. changed the title [-][vector_graphics_compiler] Scale stroke width by sqrt(|det|) instead of the mean of xScale/yScale[/-] [+][vector_graphics] Forward non-similarity transforms of stroked paths to the engine instead of scaling stroke-width[/+] on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: proposalenginep: vector_graphicspackageteam-engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions