mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-18 08:07:26 +00:00
Merge pull request #2431 from Silvertree2010/fix/2429-inspect-code-gradient-fill-on-a
Clip text gradient fills to the glyphs in inspect.code
This commit is contained in:
3 files changed
+46
-1
No files matched your search
@@ -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()),
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
Reference in new issue
Block a user