An SVG shape's transform from a <style> inside its <svg> turns it, as Chrome does (closes #60)

In a composition, a <style> inside an <svg> goes to usvg in the SVG's markup (raea's HTML host turns it into a hidden
div first, so HTML pages weren't hit). Blitz had already computed the shape's transform from the cascade, with its
transform-box and transform-origin, and written it as a transform attribute; usvg then applied the sheet's own
transform and transform-origin over it. `rotate(30deg)` isn't SVG transform syntax, so the shape drew unturned, and
a class giving only transform-box and transform-origin turned an inline transform about the SVG's 0,0.

The computed transform now goes into the element's style attribute with transform-origin: 0 0 (its origin is in
the matrix), where usvg lets it win over a sheet's rule as CSS does; the identity is written too, so a sheet can't
turn a shape the cascade leaves unturned. The separate transform attribute is no longer written.

bench/svg-css-transform/check.sh: the class-turned square against the inline-turned one, 78 px differ (antialiased
edges; before 8827), and a class's box and origin with an inline turn against all-inline, 0 px (before 5598). The
svg-css-paint, svg-stroke-box, rotated-paint and background-clip-text benches pass; the P26 rows that draw SVG
score the same as before (studio 3/3, css/svg 1/1; bevy/datamaps is 0.971-0.976 on main with or without this).
This commit is contained in:
shinyobjectz committed 2026-11-08 13:34:33 -08:00
1 parent 3f95080394
commit de1a6830c2
4 files changed
+75 -7

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
#!/bin/bash
# #60: an SVG shape's transform, transform-box and transform-origin from a <style> inside its <svg> turn it, as Chrome
# does. A composition's <style> there goes to usvg in the SVG's markup, and usvg applied that sheet's transform and
# 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.
# 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}
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
"$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
magick "$1" -crop 300x300+0+0 +repage -fuzz 35% -fill black -opaque "$2" "$OUT/a.png"
magick "$1" -crop 300x300+600+0 +repage -fuzz 35% -fill black -opaque "$3" "$OUT/b.png"
local d; d=$(magick compare -metric AE -fuzz 10% "$OUT/a.png" "$OUT/b.png" null: 2>&1 | cut -d' ' -f1)
(( ${d%.*} <= $4 )) && echo "ok $5 ($d px differ)" || { echo "FAIL $5: $d px differ, at most $4"; status=1; }
}
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"
[ $status = 0 ] && echo "PASS SVG transforms from a stylesheet"
exit $status
+16
View File
@@ -0,0 +1,16 @@
// #60: a class giving transform-box and transform-origin (from a <style> inside the <svg>) with the transform inline
// (left), against all three inline (right). Both turn 30deg about their centres, so the two are the same picture.
// bench/svg-css-transform/check.sh
import { AbsoluteFill, Composition, registerRoot } from "raea";
function O() {
return (
<AbsoluteFill style={{ background: "#fff" }}>
<svg width={900} height={300}>
<style>{`.box { transform-box: fill-box; transform-origin: center; } .k { stroke: #000; stroke-width: 6; fill: none; }`}</style>
<rect className="box k" x={50} y={50} width={200} height={200} style={{ transform: "rotate(30deg)" }} />
<rect className="k" x={650} y={50} width={200} height={200} style={{ transformBox: "fill-box", transformOrigin: "center", transform: "rotate(30deg)" }} />
</svg>
</AbsoluteFill>
);
}
registerRoot(() => <Composition id="o" component={O} width={900} height={300} fps={30} durationInFrames={1} />);
+18
View File
@@ -0,0 +1,18 @@
// #60: an SVG shape turned by a <style> inside its <svg> (red: transform-box, transform-origin and transform from a
// class), by the transform attribute (blue) and by the three set inline (green). Chrome turns all three 30deg about
// their centres. bench/svg-css-transform/check.sh
import { AbsoluteFill, Composition, registerRoot } from "raea";
function O() {
return (
<AbsoluteFill style={{ background: "#fff" }}>
<svg width={900} height={300}>
<style>{`.css { fill: none; stroke: #e0245e; stroke-width: 6; transform-box: fill-box; transform-origin: center; transform: rotate(30deg); }
.attr { fill: none; stroke: #1d9bf0; stroke-width: 6; }`}</style>
<rect className="css" x={50} y={50} width={200} height={200} />
<rect className="attr" x={350} y={50} width={200} height={200} transform="rotate(30 450 150)" />
<rect x={650} y={50} width={200} height={200} style={{ fill: "none", stroke: "#17bf63", strokeWidth: 6, transformBox: "fill-box", transformOrigin: "center", transform: "rotate(30deg)" }} />
</svg>
</AbsoluteFill>
);
}
registerRoot(() => <Composition id="o" component={O} width={900} height={300} fps={30} durationInFrames={1} />);
+14 -7
View File
@@ -438,19 +438,26 @@ impl Node {
.flatten();
#[cfg(not(feature = "svg"))]
let css_transform: Option<String> = None;
if let Some(t) = css_transform.as_deref().filter(|t| !t.is_empty()) {
writer.push_str(" transform=\"");
writer.push_str(t);
writer.push('"');
}
// raea: the SVG paint properties CSS sets on it (a stylesheet's, a class's), into its style attribute (#45)
#[cfg(feature = "svg")]
let css_paint = (opts.resolve_current_color && data.name.ns == ns!(svg))
let mut css_paint = (opts.resolve_current_color && data.name.ns == ns!(svg))
.then(|| self.svg_css_paint(current_color.as_deref()))
.unwrap_or_default();
#[cfg(not(feature = "svg"))]
let css_paint = String::new();
let mut css_paint = String::new();
// raea: and the computed transform there too, with its origin already in the matrix: a <style> inside
// the svg goes to usvg with it, and usvg applies that sheet's transform and transform-origin over a
// transform attribute (`rotate(30deg)`, which SVG's syntax can't read, so the shape drew unturned, #60).
// A style attribute's declarations win over a sheet's in usvg, as in CSS. The identity is written too,
// so a sheet's transform can't turn a shape the cascade leaves unturned.
if let Some(t) = css_transform.as_deref() {
let t = if t.is_empty() { "matrix(1 0 0 1 0 0)" } else { t };
if !css_paint.is_empty() {
css_paint.push(';');
}
css_paint.push_str(&format!("transform:{t};transform-origin:0 0"));
}
for attr in data.attrs() {
if css_transform.is_some()
&& attr.name.ns == ns!()