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 inline SVG's fill, stroke and the other paint properties set from a stylesheet are drawn, as Chrome draws them (closes #45). raea draws an inline SVG by serializing it and handing the source to usvg, which reads attributes and the style attribute only. Stylo computed `#a path { fill: none; stroke: #13502B }`, but nothing carried it into the serialized source, so the path was drawn with usvg's defaults: a black square.

shinyobjectz0ef223922615
diff --git a/bench/svg-css-paint/check.sh b/bench/svg-css-paint/check.sh
new file mode 100755
index 00000000..c555f2bf
--- /dev/null
+++ b/bench/svg-css-paint/check.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+# #45: an inline SVG's fill and stroke from a stylesheet reach usvg, as Chrome draws them. index.html draws five
+# squares: a and b styled by stylesheet rules, c by attributes, each a green outline with a white inside; d a filled
+# orange square (its attribute beats the fill its svg's rule gives it); e a filled green one (its rule beats its
+# attribute). Chrome 148 gives these same pixels. Exits 1 on a failure.
+# bench/svg-css-paint/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-paint.XXXXXX")
+trap 'rm -rf "$OUT"' EXIT
+"$BIN" render bench/svg-css-paint/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
+}
+for x in 0 120 240; do
+ near $((x + 20)),70 19 80 43 "square at $x: its green stroke"
+ near $((x + 50)),70 255 255 255 "square at $x: white inside (fill: none)"
+done
+near 410,70 240 128 0 "d: its attribute's orange over the fill its svg's rule gives it"
+near 530,70 19 80 43 "e: its rule's green over its attribute"
+grep -q "vector-effect" "$OUT/log" && { echo "FAIL the render says vector-effect wasn't applied"; status=1; }
+[ $status = 0 ] && echo "PASS SVG paint from stylesheets"
+exit $status
diff --git a/bench/svg-css-paint/index.html b/bench/svg-css-paint/index.html
new file mode 100644
index 00000000..a8eefcb1
--- /dev/null
+++ b/bench/svg-css-paint/index.html
@@ -0,0 +1,22 @@
+<!doctype html>
+<!-- #45: an inline SVG's fill and stroke from a stylesheet, as Chrome draws them. a and b set them on the path from a
+ stylesheet rule (a with vector-effect too), c as attributes; all three are a green outline on white, empty inside
+ (raea drew a and b as black squares: the rule wasn't in the serialized SVG usvg reads). d sets a stylesheet fill
+ on the svg and an attribute fill on the path, which wins over what it inherits (orange); e's rule on the path
+ beats its attribute (green). bench/svg-css-paint/check.sh -->
+<html><head><style>
+ #a path { fill: none; stroke: #13502B; stroke-width: 6; vector-effect: non-scaling-stroke }
+ #b path { fill: none; stroke: #13502B; stroke-width: 6 }
+ #d { fill: #13502B }
+ #e path { fill: #13502B }
+ svg { width: 100px; height: 100px; position: absolute; top: 20px }
+</style></head>
+<body style="margin:0;background:#fff">
+<div data-composition-id="main" data-start="0" data-duration="1" data-width="600" data-height="140" style="position:relative;width:600px;height:140px">
+ <svg id="a" style="left:0" viewBox="0 0 100 100"><path d="M20 20 H80 V80 H20 Z"/></svg>
+ <svg id="b" style="left:120px" viewBox="0 0 100 100"><path d="M20 20 H80 V80 H20 Z"/></svg>
+ <svg id="c" style="left:240px" viewBox="0 0 100 100"><path d="M20 20 H80 V80 H20 Z" fill="none" stroke="#13502B" stroke-width="6" vector-effect="non-scaling-stroke"/></svg>
+ <svg id="d" style="left:360px" viewBox="0 0 100 100"><path d="M20 20 H80 V80 H20 Z" fill="#f08000"/></svg>
+ <svg id="e" style="left:480px" viewBox="0 0 100 100"><path d="M20 20 H80 V80 H20 Z" fill="#f08000"/></svg>
+</div>
+</body></html>
diff --git a/vendor/blitz-dom/src/node/serialize.rs b/vendor/blitz-dom/src/node/serialize.rs
index 3ff639be..2de59187 100644
--- a/vendor/blitz-dom/src/node/serialize.rs
+++ b/vendor/blitz-dom/src/node/serialize.rs
@@ -290,6 +290,41 @@ impl Node {
}
}
+ /// raea: the SVG paint properties CSS sets on this svg element, as declarations for its style attribute: a
+ /// stylesheet's or a class's were left out of the serialized SVG, which usvg reads (attributes and the style
+ /// attribute only), so `#b path { fill: none; stroke: … }` drew a black silhouette (#45). An inherited one is
+ /// written where its computed value differs from the parent's: presentation attributes aren't in this cascade,
+ /// so one still beats what the element inherits and loses to a rule on the element itself, as in Chrome.
+ #[cfg(feature = "svg")]
+ fn svg_css_paint(&self, current_color: Option<&str>) -> String {
+ const INHERITED: [&str; 11] = [
+ "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 (Some(st), Some(parent)) = (self.primary_styles(), self.parent.and_then(|p| self.tree()[p].primary_styles())) else {
+ return String::new();
+ };
+ let mut out = Vec::new();
+ for name in INHERITED {
+ if let Some(v) = value(&st, name).filter(|v| value(&parent, name).as_ref() != Some(v)) {
+ out.push(format!("{name}:{v}"));
+ }
+ }
+ // not inherited: where it isn't the initial value
+ if let Some(v) = value(&st, "vector-effect").filter(|v| v != "none") {
+ out.push(format!("vector-effect:{v}"));
+ }
+ let out = out.join(";");
+ match current_color {
+ Some(c) => out.replace("currentcolor", c),
+ None => out,
+ }
+ }
+
/// `stroke-width` in user units, when the element is stroked (as an attribute or in its style).
#[cfg(feature = "svg")]
fn svg_stroke_width(&self) -> f64 {
@@ -406,6 +441,13 @@ impl Node {
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))
+ .then(|| self.svg_css_paint(current_color.as_deref()))
+ .unwrap_or_default();
+ #[cfg(not(feature = "svg"))]
+ let css_paint = String::new();
for attr in data.attrs() {
if css_transform.is_some()
&& attr.name.ns == ns!()
@@ -442,6 +484,14 @@ impl Node {
} else {
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);
+ }
+ writer.push('"');
+ }
+ if !css_paint.is_empty() && !data.attrs().iter().any(|a| a.name.ns == ns!() && &*a.name.local == "style") {
+ writer.push_str(" style=\"");
+ encode_quoted_attribute_to_string(&css_paint, writer);
writer.push('"');
}
if !has_children {
diff --git a/vendor/stylo/properties/longhands.toml b/vendor/stylo/properties/longhands.toml
index 62433ad2..07c1eabb 100644
--- a/vendor/stylo/properties/longhands.toml
+++ b/vendor/stylo/properties/longhands.toml
@@ -2348,7 +2348,7 @@ affects = ""
type = "VectorEffect"
initial = "computed::VectorEffect::none()"
struct = "svg"
-engine = "gecko"
+# raea: on servo too, so a stylesheet's reaches the serialized SVG (blitz-dom svg_css_paint, #45)
animation_type = "discrete"
spec = "https://svgwg.org/svg2-draft/coords.html#VectorEffects"
affects = "layout"
diff --git a/vendor/vendor.toml b/vendor/vendor.toml
index 2bf35f3f..08c93790 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)"
+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)"
exit = "a Blitz release with #1244/#1246 and these"
[parley_engine]
@@ -84,7 +84,7 @@ exit = "parley carrying the fix"
path = "vendor/stylo"
upstream = "crates.io:0.22.0"
kind = "patch"
-reason = "line-clamp, text-wrap-style (with Chrome's pretty) and pointer-events' SVG keywords (all, visible, painted...) for Servo too, Gecko-only upstream; url() filters; ::-webkit-slider-thumb and ::-moz-range-thumb as the slider thumb; unknown vendor-prefixed properties reported like any other, so a page can be told what it lost (raea #34); background-clip's text value for Servo too (#37); text-autospace for Servo too, initial no-autospace as Chrome's (#44)"
+reason = "line-clamp, text-wrap-style (with Chrome's pretty) and pointer-events' SVG keywords (all, visible, painted...) for Servo too, Gecko-only upstream; url() filters; ::-webkit-slider-thumb and ::-moz-range-thumb as the slider thumb; unknown vendor-prefixed properties reported like any other, so a page can be told what it lost (raea #34); background-clip's text value for Servo too (#37); text-autospace for Servo too, initial no-autospace as Chrome's (#44); vector-effect for Servo too (#45)"
exit = "Servo's stylo enabling them"
[anyrender]