diff --git a/crates/engine/src/dev/codegen/css.rs b/crates/engine/src/dev/codegen/css.rs index a5e6726a3..b2b398bd5 100644 --- a/crates/engine/src/dev/codegen/css.rs +++ b/crates/engine/src/dev/codegen/css.rs @@ -162,6 +162,7 @@ pub(crate) fn css_decls(doc: &Document, n: &Node, t: &mut Tokens, hug: (bool, bo } if matches!(n.kind, NodeKind::Text(_)) { // Text: a solid is the ink (`color`), one per fill, so the last (top) one wins. + let top = n.fills.iter().rposition(|p| p.visible); for (i, p) in n.fills.iter().enumerate().filter(|(_, p)| p.visible) { match &p.kind { PaintKind::Solid { color } => { @@ -169,7 +170,16 @@ pub(crate) fn css_decls(doc: &Document, n: &Node, t: &mut Tokens, hug: (bool, bo // A variable binds the first paint's colour. put("color", if i == 0 { tok(doc, n, &["fill"], c, t) } else { c }); } - PaintKind::Gradient(g) => put("background", css_gradient(g, n.width, n.height, p.opacity)), + PaintKind::Gradient(g) => { + put("background", css_gradient(g, n.width, n.height, p.opacity)); + // A gradient on top fills the glyphs: clip the background to the text and clear the ink. + // Under a later paint it must not hide that paint's colour, so it stays a plain background. + if Some(i) == top { + put("-webkit-background-clip", "text".into()); + put("background-clip", "text".into()); + put("color", "transparent".into()); + } + } PaintKind::Image(_) => put("background", "url(image.png) center / cover".into()), PaintKind::Pattern(_) => put("background", "url(pattern.png) repeat".into()), } diff --git a/crates/engine/src/dev/tailwind.rs b/crates/engine/src/dev/tailwind.rs index fcc98a9a8..b8629e6b3 100644 --- a/crates/engine/src/dev/tailwind.rs +++ b/crates/engine/src/dev/tailwind.rs @@ -293,6 +293,9 @@ fn decl(key: &str, value: &str) -> Vec<(String, String)> { // An image shorthand (`url(…) center / cover`) is a whole `background`, not a bg colour. ("background", v) if v.starts_with("url(") => one(format!("[background:{}]", arb(v).trim_matches(['[', ']']))), ("background", v) => one(util("bg", "color", v, "color:")), + ("-webkit-background-clip", "text") => Vec::new(), + ("background-clip", "text") => kw("bg-clip-text"), + ("color", "transparent") => kw("text-transparent"), ("color", v) => one(util("text", "color", v, "color:")), ("border", v) => border("", v), ("border-top", v) => border("t", v), diff --git a/crates/engine/src/inspect_behaviour_tests.rs b/crates/engine/src/inspect_behaviour_tests.rs index ec6a89bcf..5da4ec62b 100644 --- a/crates/engine/src/inspect_behaviour_tests.rs +++ b/crates/engine/src/inspect_behaviour_tests.rs @@ -355,3 +355,35 @@ fn rem_conversion_keeps_the_original_pixels() { assert_eq!(f("padding: 3px;", 16.0), "padding: 0.1875rem;"); assert_eq!(f("gap: 0.5px;", 16.0), "gap: 0.03125rem;"); } + +#[test] +fn text_gradient_fill_clips_to_the_glyphs() { + let mut s = Session::new(); + let fills = json!([{"type": "linearGradient", "stops": ["#D98200", "#FF6B4A"]}]); + let t = id(&ex(&mut s, "node.create", json!({"type": "text", "text": "Gradient", "props": {"fontSize": 48, "fills": fills}}))); + let c = css(&mut s, t); + assert!(c.contains("background: linear-gradient("), "{c}"); + assert!(c.contains("-webkit-background-clip: text;"), "{c}"); + assert!(c.contains("\nbackground-clip: text;"), "{c}"); + assert!(c.contains("color: transparent;"), "{c}"); + let tw = code(&mut s, t, json!({"language": "tailwind"})); + assert!(tw.contains("bg-clip-text") && tw.contains("text-transparent"), "{tw}"); + // A box keeps its gradient as a plain background. + let r = rect(&mut s, 0.0, 0.0, 50.0, 50.0, None); + let set = s.execute("node.set", &json!({"id": r, "props": {"fills": fills}})); + assert!(set.is_ok(), "{set:?}"); + let box_css = css(&mut s, r); + assert!(box_css.contains("background: linear-gradient("), "{box_css}"); + assert!(!box_css.contains("background-clip"), "{box_css}"); +} + +#[test] +fn text_gradient_under_a_solid_fill_keeps_the_solid_ink() { + let mut s = Session::new(); + let fills = json!([{"type": "linearGradient", "stops": ["#D98200", "#FF6B4A"]}, {"type": "solid", "color": "#112233"}]); + let t = id(&ex(&mut s, "node.create", json!({"type": "text", "text": "Gradient", "props": {"fontSize": 48, "fills": fills}}))); + let c = css(&mut s, t); + assert!(!c.contains("color: transparent;"), "{c}"); + assert!(!c.contains("background-clip"), "{c}"); + assert!(c.contains("color: #112233;"), "{c}"); +}