Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions bench/svg-css-transform/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
# 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. 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}
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
Expand All @@ -23,5 +29,18 @@ 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"
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
19 changes: 19 additions & 0 deletions bench/svg-css-transform/viewbox.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<AbsoluteFill style={{ background: "#fff" }}>
<svg width={300} height={300} viewBox="100 50 300 300" style={{ position: "absolute", left: 0, top: 0 }}>
<rect x={100} y={50} width={40} height={40} fill="#008000" style={{ transformOrigin: "50% 50%", transform: "rotate(180deg)" }} />
<g transform="scale(2)"><rect x={100} y={50} width={60} height={60} fill="#000" /></g>
<svg x={150} y={100} width={100} height={100} viewBox="10 20 50 50">
<rect x={10} y={20} width={10} height={10} fill="#f00" transform="scale(2)" />
</svg>
</svg>
</AbsoluteFill>
);
}
registerRoot(() => <Composition id="o" component={O} width={300} height={300} fps={30} durationInFrames={1} />);
30 changes: 27 additions & 3 deletions vendor/blitz-dom/src/node/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::<svgtypes::ViewBox>().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 {
Expand Down Expand Up @@ -468,6 +471,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
Expand Down Expand Up @@ -495,7 +499,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('"');
}
Expand Down Expand Up @@ -555,6 +569,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<String> {
use style::parser::{Parse, ParserContext};
Expand Down
2 changes: 1 addition & 1 deletion vendor/vendor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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; 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]
Expand Down
Loading