Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

An SVG shape's transform-box: stroke-box resolves as Chrome's: fill-box under vector-effect: non-scaling-stroke, and wide by a stylesheet's stroke (closes #50). WPT's svgbox-stroke-box-003/004 (a rect with id="target", a 20-wide non-scaling stroke, stroke-box, scale(0.5) translateY(-100%)) drew the rect smaller and off by a few pixels. It used to pass only because, before #45, the test and its reference were both drawn as plain black rects. Three things in blitz-dom were wrong:

shinyobjectzfb7a4e4aeb20
diff --git a/bench/svg-stroke-box/check.sh b/bench/svg-stroke-box/check.sh
new file mode 100755
index 00000000..edf26ad2
--- /dev/null
+++ b/bench/svg-stroke-box/check.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+# #50: an SVG shape's transform-box: stroke-box as Chrome resolves it. index.html moves two stroked rects by
+# translateX(50%) of their stroke box: a's stroke from a stylesheet makes that box 120 wide (it moves 60); b's
+# vector-effect: non-scaling-stroke makes it the fill box (it moves 50), and b keeps its 20-wide stroke under
+# scaleY(0.5) though it has an id of its own. Chrome 148 gives these same pixels. Exits 1 on a failure.
+# bench/svg-stroke-box/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-stroke-box.XXXXXX")
+trap 'rm -rf "$OUT"' EXIT
+"$BIN" render bench/svg-stroke-box/index.html -o "$OUT/a.png" > "$OUT/log" 2>&1 || { echo "FAIL render (log below)"; sed 's/^/ /' "$OUT/log"; exit 1; }
+px() { magick "$OUT/a.png" -format "%[fx:int(255*p{$1}.r+.5)] %[fx:int(255*p{$1}.g+.5)] %[fx:int(255*p{$1}.b+.5)]" info:; }
+status=0
+near() { # X,Y R G B WHAT: each channel within 8
+ local got; read -ra got <<< "$(px "$1")"
+ (( ${got[0]} - $2 <= 8 && $2 - ${got[0]} <= 8 && ${got[1]} - $3 <= 8 && $3 - ${got[1]} <= 8 && ${got[2]} - $4 <= 8 && $4 - ${got[2]} <= 8 )) && return
+ echo "FAIL at $1: ${got[*]}, wanted $2 $3 $4 ($5)"; status=1
+}
+near 65,90 255 255 255 "a: white left of its stroke (moved 60, not 50)"
+near 75,90 0 0 0 "a: its left stroke"
+near 185,90 0 0 0 "a: its right stroke"
+near 195,90 255 255 255 "a: white right of its stroke"
+near 305,90 0 0 0 "b: its left stroke (moved 50: the fill box)"
+near 425,90 255 255 255 "b: white right of its stroke"
+near 360,128 0 0 0 "b: its bottom stroke, 20 wide under scaleY(0.5)"
+near 360,105 19 80 43 "b: its green fill"
+[ $status = 0 ] && echo "PASS transform-box: stroke-box on SVG shapes"
+exit $status
diff --git a/bench/svg-stroke-box/index.html b/bench/svg-stroke-box/index.html
new file mode 100644
index 00000000..da692e27
--- /dev/null
+++ b/bench/svg-stroke-box/index.html
@@ -0,0 +1,18 @@
+<!doctype html>
+<!-- #50: an SVG shape's transform-box: stroke-box, as Chrome resolves it. a's stroke (20 wide) comes from a stylesheet,
+ so its stroke box is 120 wide and translateX(50%) moves it 60 (raea read the stroke from attributes only, used the
+ fill box and moved it 50). b has vector-effect: non-scaling-stroke and an id of its own: its stroke box is its fill
+ box (css-transforms-1, csswg-drafts#9640), so it moves 50, and its stroke stays 20 wide under scaleY(0.5) (raea
+ kept the id unmarked, so the stroke was scaled). bench/svg-stroke-box/check.sh -->
+<html><head><style>
+ svg { position: absolute; top: 0; width: 220px; height: 200px }
+ rect { fill: #13502B; stroke: #000; stroke-width: 20; transform-box: stroke-box }
+ #a rect { transform: translateX(50%) }
+ #b-rect { vector-effect: non-scaling-stroke; transform: translateX(50%) scaleY(0.5) }
+</style></head>
+<body style="margin:0;background:#fff">
+<div data-composition-id="main" data-start="0" data-duration="1" data-width="460" data-height="200" style="position:relative;width:460px;height:200px">
+ <svg id="a" style="left:0"><rect x="20" y="60" width="100" height="60"/></svg>
+ <svg id="b" style="left:240px"><rect id="b-rect" x="20" y="60" width="100" height="120"/></svg>
+</div>
+</body></html>
diff --git a/vendor/blitz-dom/src/node/serialize.rs b/vendor/blitz-dom/src/node/serialize.rs
index 2de59187..790d2141 100644
--- a/vendor/blitz-dom/src/node/serialize.rs
+++ b/vendor/blitz-dom/src/node/serialize.rs
@@ -220,10 +220,14 @@ impl Node {
use style::values::computed::{CSSPixelLength, TransformBox};
let styles = self.primary_styles()?;
let box_styles = styles.get_box();
- // an element without a CSS layout box uses fill-box for content-box and stroke-box for border-box
+ // an element without a CSS layout box uses fill-box for content-box and stroke-box for border-box; with
+ // vector-effect: non-scaling-stroke, stroke-box is fill-box (css-transforms-1, csswg-drafts#9640; raea #50)
+ let non_scaling = svg_computed(&styles, "vector-effect").is_some_and(|v| v == "non-scaling-stroke")
+ || self.attr(markup5ever::LocalName::from("vector-effect")).is_some_and(|v| v.trim() == "non-scaling-stroke");
let rect = match box_styles.transform_box {
TransformBox::ViewBox => self.svg_view_box(),
TransformBox::FillBox | TransformBox::ContentBox => self.svg_fill_box().unwrap_or_default(),
+ TransformBox::StrokeBox | TransformBox::BorderBox if non_scaling => self.svg_fill_box().unwrap_or_default(),
TransformBox::StrokeBox | TransformBox::BorderBox => {
let r = self.svg_fill_box().unwrap_or_default();
let half = self.svg_stroke_width() / 2.0;
@@ -301,10 +305,7 @@ impl Node {
"fill", "fill-opacity", "fill-rule", "stroke", "stroke-width", "stroke-opacity", "stroke-linecap",
"stroke-linejoin", "stroke-miterlimit", "stroke-dasharray", "stroke-dashoffset",
];
- fn value(st: &style::properties::ComputedValues, name: &str) -> Option<String> {
- let id = style::properties::PropertyId::parse_enabled_for_all_content(name).ok()?;
- Some(st.computed_value_to_string(id.as_shorthand().err()?))
- }
+ let value = svg_computed;
let (Some(st), Some(parent)) = (self.primary_styles(), self.parent.and_then(|p| self.tree()[p].primary_styles())) else {
return String::new();
};
@@ -325,22 +326,24 @@ impl Node {
}
}
- /// `stroke-width` in user units, when the element is stroked (as an attribute or in its style).
+ /// `stroke-width` in user units, when the element is stroked: each as usvg reads the serialized element (its
+ /// style attribute, then a rule's value svg_css_paint writes there, then its attribute, then what it inherits),
+ /// so a stylesheet's stroke widens the stroke box too (WPT svgbox-stroke-box-001, raea #50).
#[cfg(feature = "svg")]
fn svg_stroke_width(&self) -> f64 {
let Some(el) = self.element_data() else { return 0.0 };
let style = el.attr(local_name!("style")).unwrap_or("");
- let decl = |name: &str| {
- style.split(';').filter_map(|d| d.split_once(':')).find(|(p, _)| p.trim() == name).map(|(_, v)| v.trim().to_string())
+ let (st, parent) = (self.primary_styles(), self.parent.and_then(|p| self.tree()[p].primary_styles()));
+ let get = |name: &str| {
+ let decl = style.split(';').filter_map(|d| d.split_once(':')).find(|(p, _)| p.trim() == name).map(|(_, v)| v.trim().to_string());
+ let computed = st.as_ref().and_then(|st| svg_computed(st, name));
+ let ruled = computed.clone().filter(|v| parent.as_ref().and_then(|p| svg_computed(p, name)).as_ref() != Some(v));
+ decl.or(ruled).or_else(|| el.attr(markup5ever::LocalName::from(name)).map(str::to_string)).or(computed)
};
- let stroke = decl("stroke").or_else(|| el.attr(local_name!("stroke")).map(str::to_string));
- if stroke.is_none_or(|s| s == "none") {
+ if get("stroke").is_none_or(|s| s == "none") {
return 0.0;
}
- decl("stroke-width")
- .or_else(|| el.attr(local_name!("stroke-width")).map(str::to_string))
- .and_then(|v| v.parse::<svgtypes::Length>().ok())
- .map_or(1.0, |l| l.number)
+ get("stroke-width").and_then(|v| v.parse::<svgtypes::Length>().ok()).map_or(1.0, |l| l.number)
}
/// A length attribute in user units (unitless or px); None when it's missing or relative.
@@ -524,6 +527,13 @@ impl Node {
}
}
+/// raea: an SVG property's computed value as CSS text (`none`, `rgb(0, 0, 0)`, `20px`), for the serialized source.
+#[cfg(feature = "svg")]
+fn svg_computed(st: &style::properties::ComputedValues, name: &str) -> Option<String> {
+ let id = style::properties::PropertyId::parse_enabled_for_all_content(name).ok()?;
+ Some(st.computed_value_to_string(id.as_shorthand().err()?))
+}
+
/// raea: a style attribute without its transform declarations (transform, transform-origin, transform-box, translate,
/// rotate, scale), which an svg element's written `transform` matrix already resolves
fn without_transform_decls(style: &str) -> String {
diff --git a/vendor/blitz-dom/src/node/svg.rs b/vendor/blitz-dom/src/node/svg.rs
index afdc6e42..5a7c603e 100644
--- a/vendor/blitz-dom/src/node/svg.rs
+++ b/vendor/blitz-dom/src/node/svg.rs
@@ -365,7 +365,8 @@ fn view_box_transform(
pub const NON_SCALING_STROKE_ID: &str = "raea-nss-";
/// raea: `source` with each element that says vector-effect="non-scaling-stroke" (as an attribute or in its style)
-/// and has no id of its own given one starting NON_SCALING_STROKE_ID. None when there's none (or it doesn't parse).
+/// given an id starting NON_SCALING_STROKE_ID: a new one, or its own renamed when nothing refers to it. None when
+/// there's none (or it doesn't parse).
fn mark_non_scaling_strokes(source: &str) -> Option<String> {
if !source.contains("non-scaling-stroke") {
return None;
@@ -378,23 +379,34 @@ fn mark_non_scaling_strokes(source: &str) -> Option<String> {
st.split(';').any(|d| d.split_once(':').is_some_and(|(k, v)| k.trim() == "vector-effect" && v.trim() == "non-scaling-stroke"))
})
};
- // after each marked start tag's name, from the last so the earlier offsets hold
- let mut at: Vec<usize> = doc
+ // an id of its own is renamed to carry the mark when nothing in the source refers to it (`#id`): WPT's
+ // svgbox-stroke-box-003 strokes `<rect id="target">` (raea #50); one referred to keeps its id, and its stroke scales
+ let mut edits: Vec<(std::ops::Range<usize>, Option<String>)> = doc
.descendants()
- .filter(|n| n.is_element() && n.attribute("id").is_none() && says(n))
- .filter_map(|n| {
- let start = n.range().start + 1;
- let len = source[start..].find(|c: char| c.is_whitespace() || c == '/' || c == '>')?;
- Some(start + len)
+ .filter(|n| n.is_element() && says(n))
+ .filter_map(|n| match n.attributes().find(|a| a.name() == "id") {
+ Some(a) if !source.contains(&format!("#{}", a.value())) => Some((a.range(), Some(a.value().to_string()))),
+ Some(_) => None,
+ // after the start tag's name
+ None => {
+ let start = n.range().start + 1;
+ let end = start + source[start..].find(|c: char| c.is_whitespace() || c == '/' || c == '>')?;
+ Some((end..end, None))
+ }
})
.collect();
- if at.is_empty() {
+ if edits.is_empty() {
return None;
}
- at.sort_unstable();
+ // numbered in source order, applied from the last so the earlier offsets hold
+ edits.sort_unstable_by_key(|(r, _)| r.start);
let mut out = source.to_string();
- for (k, i) in at.iter().enumerate().rev() {
- out.insert_str(*i, &format!(" id=\"{NON_SCALING_STROKE_ID}{k}\""));
+ for (k, (r, own)) in edits.into_iter().enumerate().rev() {
+ let id = match own {
+ Some(own) => format!("id=\"{NON_SCALING_STROKE_ID}{k}-{own}\""),
+ None => format!(" id=\"{NON_SCALING_STROKE_ID}{k}\""),
+ };
+ out.replace_range(r, &id);
}
Some(out)
}
@@ -444,12 +456,13 @@ mod non_scaling_stroke_tests {
/// raea: a path with vector-effect="non-scaling-stroke" (attribute or style) keeps blitz-dom's mark through usvg
#[test]
fn marked_paths_keep_their_mark() {
- let src = r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 24"><polyline points="0,0 48,24" stroke="red" vector-effect="non-scaling-stroke"/><rect width="4" height="4" stroke="red" style="vector-effect: non-scaling-stroke"/><path id="own" d="M0 0L1 1" stroke="red" vector-effect="non-scaling-stroke"/><path d="M0 0L2 2" stroke="red"/></svg>"#;
+ let src = r##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 24"><polyline points="0,0 48,24" stroke="red" vector-effect="non-scaling-stroke"/><rect width="4" height="4" stroke="red" style="vector-effect: non-scaling-stroke"/><path id="own" d="M0 0L1 1" stroke="red" vector-effect="non-scaling-stroke"/><path d="M0 0L2 2" stroke="red"/><path id="kept" d="M0 0L3 3" stroke="red" vector-effect="non-scaling-stroke"/><use href="#kept"/></svg>"##;
let svg = super::SvgImageData::from_data(src.as_bytes(), &usvg::Options::default()).unwrap();
let ids: Vec<String> = svg.tree.root().children().iter().filter_map(|n| match n {
usvg::Node::Path(p) => Some(p.id().to_string()),
_ => None,
}).collect();
- assert_eq!(ids, ["raea-nss-0", "raea-nss-1", "own", ""]);
+ // an id of its own carries the mark when nothing refers to it (#50); `kept` is used, so it stays
+ assert_eq!(ids, ["raea-nss-0", "raea-nss-1", "raea-nss-2-own", "", "kept"]);
}
}
diff --git a/vendor/vendor.toml b/vendor/vendor.toml
index 08c93790..30a2a4b3 100644
--- a/vendor/vendor.toml
+++ b/vendor/vendor.toml
@@ -63,7 +63,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)"
+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)"
exit = "a Blitz release with #1244/#1246 and these"
[parley_engine]