From eb3c8ea6bf6a1db34b88d7a0c7fa0b111426072e Mon Sep 17 00:00:00 2001 From: James Dimachkie Date: Tue, 10 Nov 2026 12:31:59 -0800 Subject: [PATCH 1/2] An SVG shape whose style attribute holds transforms alone is turned by them Since de1a6830 an SVG element's computed transform goes to usvg in its style attribute: the element's own transform declarations are taken out and `;transform:matrix(...);transform-origin:0 0` is written after what is left. When the style held nothing else, nothing was left, and the attribute began with `;`. usvg reads a style attribute with simplecss, whose declaration parser stops at a `;` before the first declaration, so every declaration in it was dropped: `` drew unturned, and so did one with only transform-box, transform-origin and transform. A style that also set a fill or a stroke was read. The element's own declarations are now written from the first of them on (without the separators, spaces and comments CSS allows before it, which stop simplecss the same way), and the separator only after them when there are any. bench/svg-css-transform/check.sh compared mixed.tsx's two squares with each other. Both are of this kind, both drew unturned, and the comparison passed at 0 px. The check now also probes each square on its turned top edge and where its unturned edge would be. The raea before this fails those four probes; with it they pass, and the two comparisons read 75 and 0 px as before. Co-Authored-By: Claude --- bench/svg-css-transform/check.sh | 14 +++++++++++++- vendor/blitz-dom/src/node/serialize.rs | 23 ++++++++++++++++++++++- vendor/vendor.toml | 2 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/bench/svg-css-transform/check.sh b/bench/svg-css-transform/check.sh index 26c1ee556..a268e0cc5 100755 --- a/bench/svg-css-transform/check.sh +++ b/bench/svg-css-transform/check.sh @@ -4,7 +4,10 @@ # transform-origin over the transform raea had computed from the cascade (`rotate(30deg)`, which SVG's syntax can't # read: the shape drew unturned). sheet.tsx's class-turned square (red) must be the inline-turned one (green) moved # 600 px (each recoloured black, so only its shape counts), and mixed.tsx's class box and origin with an inline turn -# must be the all-inline square. Before: 8827 and 5598 px differed. Exits 1 on a failure. +# must be the all-inline square. Before: 8827 and 5598 px differed. Two squares drawn the same can both be wrong, and +# mixed.tsx's were: a style attribute holding transforms alone was written back for usvg as `;transform:matrix(...)`, +# and usvg's declaration parser stops at a leading `;`, so both drew unturned and the comparison passed. Each is now +# probed on its turned top edge as well, and where its unturned edge would be. Exits 1 on a failure. # bench/svg-css-transform/check.sh (RAEA_BIN: the raea to run, default target/release/raea) cd "$(dirname "$0")/../.." || exit 2 BIN=${RAEA_BIN:-$PWD/target/release/raea} @@ -23,5 +26,14 @@ same() { # PNG LEFT-COLOUR RIGHT-COLOUR MAX WHAT } same "$OUT/sheet/frame-00000.png" "#e0245e" "#17bf63" 300 "a class's transform, box and origin turn it as inline ones do" same "$OUT/mixed/frame-00000.png" "#000000" "#000000" 50 "a class's box and origin with an inline turn" +at() { # PAGE X,Y COLOUR WHAT: the page's pixel there + local c; c=$(magick "$OUT/$1/frame-00000.png" -alpha off -format "%[hex:p{$2}]" info:) + [ "$c" = "$3" ] && echo "ok $4" || { echo "FAIL $4: #$c at $2, wanted #$3"; status=1; } +} +# a 200 px square turned 30deg about its middle: its top edge passes 15 px above the middle of its unturned one +at mixed 150,34 000000 "the square turned by a style attribute of a transform alone: its turned top edge" +at mixed 150,50 FFFFFF "and nothing where its unturned edge was" +at mixed 750,34 000000 "the all-inline square: its turned top edge" +at mixed 750,50 FFFFFF "and nothing where its unturned edge was" [ $status = 0 ] && echo "PASS SVG transforms from a stylesheet" exit $status diff --git a/vendor/blitz-dom/src/node/serialize.rs b/vendor/blitz-dom/src/node/serialize.rs index b9f8ebf2c..df22c9520 100644 --- a/vendor/blitz-dom/src/node/serialize.rs +++ b/vendor/blitz-dom/src/node/serialize.rs @@ -468,6 +468,7 @@ impl Node { writer.push(' '); write_attr_name(&attr.name, writer); writer.push_str("=\""); + let value_at = writer.len(); // raea: an svg's attribute that names var() or a colour usvg can't read, as Chrome resolves it let resolved = opts .resolve_current_color @@ -495,7 +496,17 @@ impl Node { encode_quoted_attribute_to_string(&attr.value, writer); } if !css_paint.is_empty() && attr.name.ns == ns!() && &*attr.name.local == "style" { - encode_quoted_attribute_to_string(&format!(";{css_paint}"), writer); + // raea: after the element's own declarations when any are left, which start at the first + // of them. A style attribute of transforms alone is empty by now, and a `;` before the + // first declaration stops usvg's parser (simplecss) there: the element lost its transform + // and its CSS paint, so `style="transform: rotate(30deg)"` drew unturned + let own = from_first_declaration(&writer[value_at..]).to_string(); + writer.truncate(value_at); + if !own.is_empty() { + writer.push_str(&own); + writer.push(';'); + } + encode_quoted_attribute_to_string(&css_paint, writer); } writer.push('"'); } @@ -555,6 +566,16 @@ fn without_transform_decls(style: &str) -> String { .join(";") } +/// raea: a style attribute from its first declaration on: without the separators, spaces and comments before it, +/// which CSS allows there and simplecss doesn't (a comment left open runs to the end, as in CSS: nothing is left) +fn from_first_declaration(mut style: &str) -> &str { + loop { + style = style.trim_start_matches(|c: char| c == ';' || c.is_whitespace()); + let Some(comment) = style.strip_prefix("/*") else { return style }; + style = comment.split_once("*/").map_or("", |(_, rest)| rest); + } +} + /// raea: a CSS colour (any syntax stylo parses) as sRGB `rgba()`, the form usvg reads; None when `value` isn't one. fn css_color_rgba(value: &str) -> Option { use style::parser::{Parse, ParserContext}; diff --git a/vendor/vendor.toml b/vendor/vendor.toml index ac84fa754..c0939fc09 100644 --- a/vendor/vendor.toml +++ b/vendor/vendor.toml @@ -70,7 +70,7 @@ exit = "a Blitz release that draws these" path = "vendor/blitz-dom" upstream = "crates.io:0.3.0-beta.4" kind = "patch" -reason = "inline flex/grid baselines, auto table columns, table row rects, inline image load events, SVG text fonts, SVG non-scaling strokes, url() filters; Blitz main's #1244 and #1246; a node removed in a mutation batch dropped from the style, form and title nodes the batch flushes (raea #24); a node in a 3D rendering context (or with a projection) placed again on every resolve, so it follows a preserve-3d parent whose transform a script set (raea #19); a ParseErrorReporter (not_applied.rs) on every author parse, keeping what stylo drops for the render to say once (raea #34); a bevy_text box whose lines are no finite size (an Apple bitmap-only face, which parley scales by a units-per-em of 0) measures 0 by 0, as bevy_text does; a family list ending in "raea-bevy fallback" (bevy_text's FontSource::Family) falls to the collection's fallback families, not to serif nor to the CoreText pick (parley: NO_HOOK_FAMILY); an out-of-flow box painted by the stacking context between it and its containing block, inside its opacity group (raea #39); an SVG's paint properties from CSS written into its serialized source for usvg (raea #45); transform-box: stroke-box as fill-box under non-scaling-stroke, a stylesheet's stroke in the stroke box, and the non-scaling mark on an element with its own id (raea #50)" +reason = "inline flex/grid baselines, auto table columns, table row rects, inline image load events, SVG text fonts, SVG non-scaling strokes, url() filters; Blitz main's #1244 and #1246; a node removed in a mutation batch dropped from the style, form and title nodes the batch flushes (raea #24); a node in a 3D rendering context (or with a projection) placed again on every resolve, so it follows a preserve-3d parent whose transform a script set (raea #19); a ParseErrorReporter (not_applied.rs) on every author parse, keeping what stylo drops for the render to say once (raea #34); a bevy_text box whose lines are no finite size (an Apple bitmap-only face, which parley scales by a units-per-em of 0) measures 0 by 0, as bevy_text does; a family list ending in "raea-bevy fallback" (bevy_text's FontSource::Family) falls to the collection's fallback families, not to serif nor to the CoreText pick (parley: NO_HOOK_FAMILY); an out-of-flow box painted by the stacking context between it and its containing block, inside its opacity group (raea #39); an SVG's paint properties from CSS written into its serialized source for usvg (raea #45); transform-box: stroke-box as fill-box under non-scaling-stroke, a stylesheet's stroke in the stroke box, and the non-scaling mark on an element with its own id (raea #50); an SVG element's rewritten style attribute without a leading `;` when its own declarations were transforms alone" exit = "a Blitz release with #1244/#1246 and these" [parley_engine] From c6f4c9132d06cbbbf18fce97a4255f1604e72d34 Mon Sep 17 00:00:00 2001 From: James Dimachkie Date: Tue, 10 Nov 2026 12:32:00 -0800 Subject: [PATCH 2/2] An SVG element's transform under a viewBox that doesn't start at 0 0 lands where Chrome puts it transform-box: view-box, an SVG element's default, takes the nearest viewport as its reference box, and css-transforms-1 puts that box at the origin of the user space the viewBox sets up, with the viewBox's width and height. blitz-dom's svg_view_box returned the viewBox's own rectangle, x and y included, and svg_css_transform moves the origin by the box's corner. So under `viewBox="100 50 400 300"` a `transform-origin` of 0 0 became 100 50: `` drew its content 100 px left of and 50 px above where a browser draws it, and a rotation turned about the wrong point. A viewBox starting at 0 0, a translate, and a fill-box or stroke-box origin weren't affected. svg_view_box now returns the viewBox's size at 0 0. A viewport without a viewBox was at 0 0 already. bench/svg-css-transform/check.sh gains viewbox.tsx: a square scaled by 2, one turned 180deg about 50% 50%, and one scaled inside a nested svg with a viewBox of its own, each probed where Chrome 157 draws it (their client rects read from the same markup in Chrome). The raea before this fails all four probes; with it they pass, and the bench's two earlier cases are unchanged. Co-Authored-By: Claude --- bench/svg-css-transform/check.sh | 11 +++++++++-- bench/svg-css-transform/viewbox.tsx | 19 +++++++++++++++++++ vendor/blitz-dom/src/node/serialize.rs | 7 +++++-- vendor/vendor.toml | 2 +- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 bench/svg-css-transform/viewbox.tsx diff --git a/bench/svg-css-transform/check.sh b/bench/svg-css-transform/check.sh index a268e0cc5..796bdb509 100755 --- a/bench/svg-css-transform/check.sh +++ b/bench/svg-css-transform/check.sh @@ -7,7 +7,10 @@ # must be the all-inline square. Before: 8827 and 5598 px differed. Two squares drawn the same can both be wrong, and # mixed.tsx's were: a style attribute holding transforms alone was written back for usvg as `;transform:matrix(...)`, # and usvg's declaration parser stops at a leading `;`, so both drew unturned and the comparison passed. Each is now -# probed on its turned top edge as well, and where its unturned edge would be. Exits 1 on a failure. +# probed on its turned top edge as well, and where its unturned edge would be. And viewbox.tsx's three shapes, +# turned or scaled under a viewBox that doesn't start at 0 0, must be where Chrome 157 draws them: transform-box: +# view-box's reference box is at the origin of user space, and raea put it at the viewBox's x and y, which moved +# every origin by them. Exits 1 on a failure. # bench/svg-css-transform/check.sh (RAEA_BIN: the raea to run, default target/release/raea) cd "$(dirname "$0")/../.." || exit 2 BIN=${RAEA_BIN:-$PWD/target/release/raea} @@ -15,7 +18,7 @@ export RAEA_SIGN=0 RAEA_MARK=0 RAEA_EXPORT_CACHE=0 OUT=$(mktemp -d "${TMPDIR:-/tmp}/raea-svg-css-transform.XXXXXX") trap 'rm -rf "$OUT"' EXIT status=0 -for c in sheet mixed; do +for c in sheet mixed viewbox; do "$BIN" render bench/svg-css-transform/$c.tsx --at 0 -o "$OUT/$c" > "$OUT/$c.log" 2>&1 && [ -f "$OUT/$c/frame-00000.png" ] || { echo "FAIL $c: the render failed"; sed 's/^/ /' "$OUT/$c.log" | tail -4; exit 1; } done same() { # PNG LEFT-COLOUR RIGHT-COLOUR MAX WHAT @@ -35,5 +38,9 @@ at mixed 150,34 000000 "the square turned by a style attribute of a transform al at mixed 150,50 FFFFFF "and nothing where its unturned edge was" at mixed 750,34 000000 "the all-inline square: its turned top edge" at mixed 750,50 FFFFFF "and nothing where its unturned edge was" +at viewbox 160,110 000000 "scale(2) under an offset viewBox scales about user 0 0" +at viewbox 30,30 FFFFFF "and not about the viewBox's corner" +at viewbox 80,180 008000 "a 50% 50% origin is the middle of a box at user 0 0" +at viewbox 85,110 FF0000 "a nested svg's viewBox, the same" [ $status = 0 ] && echo "PASS SVG transforms from a stylesheet" exit $status diff --git a/bench/svg-css-transform/viewbox.tsx b/bench/svg-css-transform/viewbox.tsx new file mode 100644 index 000000000..2a7fbc962 --- /dev/null +++ b/bench/svg-css-transform/viewbox.tsx @@ -0,0 +1,19 @@ +// transform-box: view-box (an SVG element's default) under a viewBox that doesn't start at 0 0: the reference box is the +// viewBox's size at the origin of user space, so a 0 0 origin is user 0 0 and 50% 50% is half the viewBox's size. +// Chrome 157 draws the black square at 100,50 to 220,170, the green at 60,160 to 100,200 and the red at 70,90 to +// 110,130. bench/svg-css-transform/check.sh +import { AbsoluteFill, Composition, registerRoot } from "raea"; +function O() { + return ( + + + + + + + + + + ); +} +registerRoot(() => ); diff --git a/vendor/blitz-dom/src/node/serialize.rs b/vendor/blitz-dom/src/node/serialize.rs index df22c9520..ec94e2fc4 100644 --- a/vendor/blitz-dom/src/node/serialize.rs +++ b/vendor/blitz-dom/src/node/serialize.rs @@ -247,7 +247,10 @@ impl Node { Some(format!("matrix({a} {b} {c} {d} {e} {f})")) } - /// The nearest svg viewport's box in this element's user space: its `viewBox`, or its size. + /// The nearest svg viewport's reference box in this element's user space: the `viewBox`'s size (or the viewport's) + /// at the origin of user space, where css-transforms-1 #transform-box puts it. A viewBox's x and y move the + /// content, not the box: placed at them, the box moved a 0 0 origin there, and `scale(2)` under + /// `viewBox="100 50 400 300"` drew its content 100 px left of and 50 px above where Chrome draws it. #[cfg(feature = "svg")] fn svg_view_box(&self) -> kurbo::Rect { let mut at = self.parent; @@ -256,7 +259,7 @@ impl Node { let Some(el) = node.element_data() else { break }; if el.name.ns == ns!(svg) && &*el.name.local == "svg" { if let Some(vb) = el.attr(local_name!("viewBox")).and_then(|v| v.parse::().ok()) { - return kurbo::Rect::new(vb.x, vb.y, vb.x + vb.w, vb.y + vb.h); + return kurbo::Rect::new(0.0, 0.0, vb.w, vb.h); } let outer = node.parent.and_then(|p| self.tree()[p].element_data()).is_none_or(|p| p.name.ns != ns!(svg)); if outer { diff --git a/vendor/vendor.toml b/vendor/vendor.toml index c0939fc09..e58b55e7b 100644 --- a/vendor/vendor.toml +++ b/vendor/vendor.toml @@ -70,7 +70,7 @@ exit = "a Blitz release that draws these" path = "vendor/blitz-dom" upstream = "crates.io:0.3.0-beta.4" kind = "patch" -reason = "inline flex/grid baselines, auto table columns, table row rects, inline image load events, SVG text fonts, SVG non-scaling strokes, url() filters; Blitz main's #1244 and #1246; a node removed in a mutation batch dropped from the style, form and title nodes the batch flushes (raea #24); a node in a 3D rendering context (or with a projection) placed again on every resolve, so it follows a preserve-3d parent whose transform a script set (raea #19); a ParseErrorReporter (not_applied.rs) on every author parse, keeping what stylo drops for the render to say once (raea #34); a bevy_text box whose lines are no finite size (an Apple bitmap-only face, which parley scales by a units-per-em of 0) measures 0 by 0, as bevy_text does; a family list ending in "raea-bevy fallback" (bevy_text's FontSource::Family) falls to the collection's fallback families, not to serif nor to the CoreText pick (parley: NO_HOOK_FAMILY); an out-of-flow box painted by the stacking context between it and its containing block, inside its opacity group (raea #39); an SVG's paint properties from CSS written into its serialized source for usvg (raea #45); transform-box: stroke-box as fill-box under non-scaling-stroke, a stylesheet's stroke in the stroke box, and the non-scaling mark on an element with its own id (raea #50); an SVG element's rewritten style attribute without a leading `;` when its own declarations were transforms alone" +reason = "inline flex/grid baselines, auto table columns, table row rects, inline image load events, SVG text fonts, SVG non-scaling strokes, url() filters; Blitz main's #1244 and #1246; a node removed in a mutation batch dropped from the style, form and title nodes the batch flushes (raea #24); a node in a 3D rendering context (or with a projection) placed again on every resolve, so it follows a preserve-3d parent whose transform a script set (raea #19); a ParseErrorReporter (not_applied.rs) on every author parse, keeping what stylo drops for the render to say once (raea #34); a bevy_text box whose lines are no finite size (an Apple bitmap-only face, which parley scales by a units-per-em of 0) measures 0 by 0, as bevy_text does; a family list ending in "raea-bevy fallback" (bevy_text's FontSource::Family) falls to the collection's fallback families, not to serif nor to the CoreText pick (parley: NO_HOOK_FAMILY); an out-of-flow box painted by the stacking context between it and its containing block, inside its opacity group (raea #39); an SVG's paint properties from CSS written into its serialized source for usvg (raea #45); transform-box: stroke-box as fill-box under non-scaling-stroke, a stylesheet's stroke in the stroke box, and the non-scaling mark on an element with its own id (raea #50); an SVG element's rewritten style attribute without a leading `;` when its own declarations were transforms alone; transform-box: view-box's reference box at the origin of user space, not at the viewBox's x and y" exit = "a Blitz release with #1244/#1246 and these" [parley_engine]