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.

background-clip: text clips a background to the element's text, as Chrome does (closes #37). Gradient text, a common headline style, came out as a gradient box with the text invisible. Two gaps: the vendored stylo knew the `text` value only for Gecko, so in Servo mode the declaration didn't parse (and #34's note didn't list it: stylo doesn't report a drop of a property the block already set, here through its `background` shorthand), and blitz-paint had no text clip. Now stylo takes `text` for Servo too, and blitz-paint draws a background with a text clip in a group that the text then masks (Compose::DestIn). The mask is the element's own inline layout plus the text of every element inside it, at its place (draw_text_mask: glyphs opaque, without their shadows). A scroller's offset isn't applied inside the mask.

shinyobjectz3892ac4367d8
diff --git a/bench/background-clip-text/check.sh b/bench/background-clip-text/check.sh
new file mode 100755
index 00000000..12b9b219
--- /dev/null
+++ b/bench/background-clip-text/check.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+# #37: background-clip: text clips a background to the element's glyphs, as Chrome does. index.html draws "Gradient"
+# three times in a pink-to-cyan gradient on #111, clipped by background-clip (first line), -webkit-background-clip
+# alone (second), and background-clip on a block holding the word in a block of its own (third). In each line the G's
+# stem must be the gradient's pink, and the box beside and above the word the page's #111 (it was the gradient: the
+# box was filled and the text was invisible). Chrome 148 gives these same pixels.
+# Exits 1 on a failure.
+# bench/background-clip-text/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-bg-clip-text.XXXXXX")
+trap 'rm -rf "$OUT"' EXIT
+"$BIN" render bench/background-clip-text/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)] %[fx:int(255*p{$1}.g)] %[fx:int(255*p{$1}.b)]" info:; }
+near() { # X,Y R G B: each channel within 6
+ local got; read -ra got <<< "$(px "$1")"
+ (( ${got[0]} - $2 <= 6 && $2 - ${got[0]} <= 6 && ${got[1]} - $3 <= 6 && $3 - ${got[1]} <= 6 && ${got[2]} - $4 <= 6 && $4 - ${got[2]} <= 6 )) && return
+ echo "FAIL at $1: ${got[*]}, wanted $2 $3 $4 ($5)"; status=1
+}
+status=0
+for y in 60 180 300; do
+ near 8,$y 251 2 204 "the G's stem, in the gradient"
+ near 600,$y 17 17 17 "beside the word: the page"
+done
+near 200,10 17 17 17 "above the first line's glyphs"
+near 200,130 17 17 17 "above the second line's glyphs"
+near 200,250 17 17 17 "above the third line's glyphs"
+[ $status = 0 ] && echo "PASS background-clip: text"
+exit $status
diff --git a/bench/background-clip-text/index.html b/bench/background-clip-text/index.html
new file mode 100644
index 00000000..67ce546c
--- /dev/null
+++ b/bench/background-clip-text/index.html
@@ -0,0 +1,10 @@
+<!doctype html><html><body style="margin:0;background:#111">
+<!-- #37: gradient text. The first line clips its background to its text with background-clip, the second with
+ -webkit-background-clip alone (as older Tailwind and most snippets write it), the third through a block inside
+ the clipped one, whose text is in the clip too. bench/background-clip-text/check.sh -->
+<div data-composition-id="main" data-start="0" data-duration="1" data-width="640" data-height="360" style="font:bold 96px sans-serif;line-height:120px;color:transparent">
+ <div style="background:linear-gradient(90deg,#f0c,#0cf);background-clip:text">Gradient</div>
+ <div style="background:linear-gradient(90deg,#f0c,#0cf);-webkit-background-clip:text">Gradient</div>
+ <div style="background:linear-gradient(90deg,#f0c,#0cf);background-clip:text"><div>Gradient</div></div>
+</div>
+</body></html>
diff --git a/vendor/blitz-paint/src/render.rs b/vendor/blitz-paint/src/render.rs
index 3e3691d0..ebc22935 100644
--- a/vendor/blitz-paint/src/render.rs
+++ b/vendor/blitz-paint/src/render.rs
@@ -1621,6 +1621,59 @@ impl ElementCx<'_, '_> {
}
}
+ /// Where an inline root's text is drawn from `pos` (its content box's place): the place, its block offset added,
+ /// and the transform its glyphs are drawn with
+ fn inline_text_at(&self, pos: Point, block_offset: f32) -> (Point, Affine) {
+ // raea: the text at its box's place as laid out, not as rounded to whole pixels for its background (each
+ // box's place here is its rounded one): Chrome snaps a box's background and border but draws its text from
+ // the fractional place (a button at x 78.4 has its label at 86.4, not 86)
+ let pos = if crate::text::chrome_glyphs().text_unrounded {
+ let (mut dx, mut dy) = (0.0f32, 0.0f32);
+ let mut current = Some(self.node.id);
+ while let Some(id) = current {
+ let Some(n) = self.context.dom.get_node(id) else { break };
+ dx += n.unrounded_layout().location.x - n.final_layout().location.x;
+ dy += n.unrounded_layout().location.y - n.final_layout().location.y;
+ current = n.containing_block();
+ }
+ Point::new(pos.x + dx as f64, pos.y + dy as f64)
+ } else {
+ pos
+ };
+ let pos = Point {
+ x: pos.x,
+ y: pos.y + block_offset as f64,
+ };
+ let transform =
+ self.transform * Affine::translate((pos.x * self.scale, pos.y * self.scale));
+ (pos, transform)
+ }
+
+ /// raea: the element's text as a mask (background-clip: text, #37): its glyphs where they're drawn, opaque and
+ /// without their shadows, for the background group under them to keep only what they cover (DestIn). Its own
+ /// inline layout and, as Chrome's, the text of every element inside it at its place (a scroller's offset aside).
+ pub(super) fn draw_text_mask(&self, scene: &mut impl PaintScene) {
+ if let Some(text_layout) = self.element.inline_layout_data.as_ref().filter(|_| self.node.flags.is_inline_root()) {
+ let l = self.node.final_layout();
+ let at = Point::new((l.padding.left + l.border.left) as f64, (l.padding.top + l.border.top) as f64);
+ let (_, transform) = self.inline_text_at(at, text_layout.block_offset);
+ let mut cx = self.context.draw_text_context.borrow_mut();
+ (cx.color, cx.mask) = (Some(peniko::color::palette::css::BLACK), true);
+ crate::text::stroke_text(scene, text_layout.layout.lines(), self.context.dom, transform, self.scale, self.node.id, &mut cx);
+ (cx.color, cx.mask) = (None, false);
+ }
+ let Some(children) = self.node.paint_children.borrow().clone() else { return };
+ for id in children {
+ let child = &self.context.dom.as_ref().tree()[id];
+ if child.element_data().is_none() || child.primary_styles().is_none() || matches!(child.taffy_display(), taffy::Display::None) {
+ continue;
+ }
+ let at = child.final_layout().location;
+ let transform = self.transform * Affine::translate(Vec2::new(at.x as f64, at.y as f64) * self.scale) * child.transform().as_deref().copied().unwrap_or_default();
+ self.context.element_cx(child, *child.final_layout(), transform, None).draw_text_mask(scene);
+ }
+ }
+
fn draw_inline_layout(&self, scene: &mut impl PaintScene, pos: Point) {
if self.node.flags.is_inline_root() {
let text_layout = self.element
@@ -1630,28 +1683,7 @@ impl ElementCx<'_, '_> {
panic!("Tried to render node marked as inline root that does not have an inline layout: {:?}", self.node);
});
- // raea: the text at its box's place as laid out, not as rounded to whole pixels for its background (each
- // box's place here is its rounded one): Chrome snaps a box's background and border but draws its text from
- // the fractional place (a button at x 78.4 has its label at 86.4, not 86)
- let pos = if crate::text::chrome_glyphs().text_unrounded {
- let (mut dx, mut dy) = (0.0f32, 0.0f32);
- let mut current = Some(self.node.id);
- while let Some(id) = current {
- let Some(n) = self.context.dom.get_node(id) else { break };
- dx += n.unrounded_layout().location.x - n.final_layout().location.x;
- dy += n.unrounded_layout().location.y - n.final_layout().location.y;
- current = n.containing_block();
- }
- Point::new(pos.x + dx as f64, pos.y + dy as f64)
- } else {
- pos
- };
- let pos = Point {
- x: pos.x,
- y: pos.y + text_layout.block_offset as f64,
- };
- let transform =
- self.transform * Affine::translate((pos.x * self.scale, pos.y * self.scale));
+ let (pos, transform) = self.inline_text_at(pos, text_layout.block_offset);
// Render inline element backgrounds (e.g. `<span style="background: ...">`)
// behind the text and selection highlight.
diff --git a/vendor/blitz-paint/src/render/background.rs b/vendor/blitz-paint/src/render/background.rs
index 315ef6ce..d97730c2 100644
--- a/vendor/blitz-paint/src/render/background.rs
+++ b/vendor/blitz-paint/src/render/background.rs
@@ -51,6 +51,8 @@ impl From<StyloBackgroundClip> for BoxModelBox {
// TODO: support BorderArea
StyloBackgroundClip::BorderArea => Self::BorderBox,
+ // raea: text (#37) paints the border box, kept under the glyphs by draw_background's mask
+ StyloBackgroundClip::Text => Self::BorderBox,
}
}
}
@@ -146,7 +148,10 @@ impl ElementCx<'_, '_> {
// raea: background-blend-mode blends a layer with the layers and the colour under it, never with what's behind
// the element: the background is a group of its own when any layer blends
let blends: Vec<_> = (0..layer_count).map(|i| crate::blend::background_blend(&self.style, i)).collect();
- let isolated = blends.iter().any(Option::is_some);
+ // raea: background-clip: text (#37), on any layer: the background is a group the element's glyphs then mask
+ // (a later group composed DestIn), so it shows only through the text, colour and every layer alike
+ let text = (0..layer_count).any(|i| matches!(get_cyclic(&bg_styles.background_clip.0, i), StyloBackgroundClip::Text));
+ let isolated = blends.iter().any(Option::is_some) || text;
let bounds = self.frame.border_box_path().bounding_box();
self.context.layer_manager.maybe_with_blend_layer(scene, isolated, 1.0, Some(peniko::Mix::Normal.into()), self.transform, &bounds, None, None, |scene| {
// Draw background color (if any)
@@ -173,6 +178,10 @@ impl ElementCx<'_, '_> {
},
);
}
+ if text {
+ let dest_in = peniko::BlendMode::new(peniko::Mix::Normal, peniko::Compose::DestIn);
+ self.context.layer_manager.maybe_with_blend_layer(scene, true, 1.0, Some(dest_in), self.transform, &bounds, None, None, |scene| self.draw_text_mask(scene));
+ }
});
}
diff --git a/vendor/blitz-paint/src/text.rs b/vendor/blitz-paint/src/text.rs
index 8156bb6b..98efdba3 100644
--- a/vendor/blitz-paint/src/text.rs
+++ b/vendor/blitz-paint/src/text.rs
@@ -434,6 +434,8 @@ pub(crate) struct DrawTextContext {
/// raea: -webkit-line-clamp, the index of the last line drawn and where the box's content ends (layout units): that
/// line ends in an ellipsis, after its text or cutting what doesn't fit before it, as Chrome's clamp does
pub(crate) clamp: Option<(usize, f32)>,
+ /// raea: the glyphs as a mask (background-clip: text, #37): in `color`, without their text-shadow
+ pub(crate) mask: bool,
}
/// raea: synthetic bold as Chrome draws it on macOS, in glyph space (it grows with the text): a weight of 600 or more
@@ -817,7 +819,9 @@ pub(crate) fn stroke_text<'a>(
color: color_override,
ellipsis,
clamp,
+ mask,
} = context;
+ let mask = *mask;
let color_override = *color_override;
let ellipsis = *ellipsis;
let clamp = *clamp;
@@ -962,7 +966,7 @@ pub(crate) fn stroke_text<'a>(
let normalized_coords: &[i16] = bytemuck::cast_slice(run.normalized_coords());
// raea: text-shadow, under the run's glyphs, the last shadow first so the first is on top; a blurred
// one drawn in a layer the blur filter runs on (blitz-paint drew no text shadows)
- let shadows = stack.last().map(|e| e.shadows.as_slice()).unwrap_or(&[]);
+ let shadows = stack.last().filter(|_| !mask).map(|e| e.shadows.as_slice()).unwrap_or(&[]);
for &(dx, dy, blur, color) in shadows.iter().rev() {
if color.components[3] == 0.0 {
continue;
diff --git a/vendor/stylo/values/specified/background.rs b/vendor/stylo/values/specified/background.rs
index 405d50e1..43902613 100644
--- a/vendor/stylo/values/specified/background.rs
+++ b/vendor/stylo/values/specified/background.rs
@@ -210,7 +210,7 @@ pub enum BackgroundClip {
#[value_info(skip)]
NoClip,
// TODO: text and border-area are supposed to combine in backgrounds-4...
- #[cfg(feature = "gecko")]
+ // raea: text for Servo too (#37): blitz-paint keeps the background under the element's glyphs
Text,
#[parse(condition = "background_clip_border_area_enabled")]
#[value_info(skip)]
@@ -241,7 +241,6 @@ impl BackgroundClip {
Self::ViewBox => ClipValidity::MASK,
#[cfg(feature = "gecko")]
Self::NoClip => ClipValidity::MASK,
- #[cfg(feature = "gecko")]
Self::Text => ClipValidity::BACKGROUND,
Self::BorderArea => ClipValidity::BACKGROUND,
}
diff --git a/vendor/vendor.toml b/vendor/vendor.toml
index d4342440..a16e729c 100644
--- a/vendor/vendor.toml
+++ b/vendor/vendor.toml
@@ -56,7 +56,7 @@ exit = "a bevy_ui_widgets release on parley 0.12"
path = "vendor/blitz-paint"
upstream = "crates.io:0.3.0-beta.4"
kind = "patch"
-reason = "angled tile gradients, many-tile gradient layers as one brush, text-shadow, ellipsis, inline-box opacity, box-shadow blur as Chrome's; filters by GPU passes (B4a/B4b); z-index boxes clipped by the overflow between them and their stacking context; a range input's thumb; what an element paints at an offset of its own (background position and tiles, box and text shadows, scroll, inline SVG) placed in its own axes under a rotated ancestor (#18)"
+reason = "angled tile gradients, many-tile gradient layers as one brush, text-shadow, ellipsis, inline-box opacity, box-shadow blur as Chrome's; filters by GPU passes (B4a/B4b); z-index boxes clipped by the overflow between them and their stacking context; a range input's thumb; what an element paints at an offset of its own (background position and tiles, box and text shadows, scroll, inline SVG) placed in its own axes under a rotated ancestor (#18); background-clip: text, its background masked by the text of the element and everything in it (#37)"
exit = "a Blitz release that draws these"
[blitz-dom]
@@ -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)"
+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)"
exit = "Servo's stylo enabling them"
[anyrender]