Merge pull request #7700 from dotCooCoo/fix/svg-clip-use-both-clips

SVG import: a use in a clip path clips by its own clip path and its text's
This commit is contained in:
BFlat
2026-11-15 08:24:03 +07:00
committed by GitHub
2 changed files with 77 additions and 15 deletions
+56 -15
View File
@@ -378,24 +378,29 @@ fn clip_uses(
Some(s) => own_transform(&cx.css, s),
None => t.attribute("transform").unwrap_or("").to_string(),
};
// One element can carry one clip path. The text's own clip path is in the text's user space
// and wins; a clip path of the `<use>` is in the `<use>`'s, so the text's transform moves
// to the importer instead.
let clipped = |n: XNode| cx.css.own(n, "clip-path").is_some_and(|v| v.trim() != "none");
// One element can carry one clip path. The text's own clip path is in the text's user space;
// a clip path of the `<use>` is in the `<use>`'s. With only the `<use>`'s, the text's
// transform moves to the importer. With both, the placeholder's clip path is one that
// holds both ([`both_clips`]).
let clipped = |n: XNode| cx.css.own(n, "clip-path").filter(|v| v.trim() != "none");
let id = format!("{}{i}-{k}", slots.prefix);
let transform = if style_t.is_some_and(clipped) {
if clipped(style_u) {
let label = u.attribute("id").map_or_else(|| "a use of a text".to_string(), |id| format!("'{id}'"));
warnings.push(format!("clip path of {label} in a clip path ignored: the text's own applies"));
}
format!("{placed} {text_xf}")
} else {
slots.inner.insert(id.clone(), parse_transform(Some(&text_xf)));
placed
};
let props = clip_props(&cx.css, style_t, Some(style_u));
let mut out = String::new();
clip_placeholder(&mut out, q, &id, &transform, &props, u, marker);
match (style_t.and_then(clipped), clipped(style_u)) {
(Some(text_clip), Some(use_clip)) => {
let both = both_clips(&mut out, q, &id, &text_clip, &use_clip, parse_transform(Some(&text_xf)), marker);
let props = match (both, props.split_once(';')) {
(Some(both), Some((_, rest))) => format!("clip-path:url(#{both});{rest}"),
_ => props,
};
clip_placeholder(&mut out, q, &id, &format!("{placed} {text_xf}"), &props, u, marker);
}
(Some(_), None) => clip_placeholder(&mut out, q, &id, &format!("{placed} {text_xf}"), &props, u, marker),
(None, _) => {
slots.inner.insert(id.clone(), parse_transform(Some(&text_xf)));
clip_placeholder(&mut out, q, &id, &placed, &props, u, marker);
}
}
// Another `<use>` can still show this one by its id: a copy of it stays in a `<defs>`, which
// the clip path leaves out, with the properties style rules gave it written out.
if u.attribute("id").is_some() {
@@ -654,6 +659,42 @@ fn clip_placeholder(out: &mut String, q: &str, id: &str, transform: &str, props:
let _ = write!(out, " d=\"M{} {}H{}V{}H{}Z\"/>", b.x0, b.y0, b.x1, b.y1, b.x0);
}
/// Write, in a `<defs>` (which a clip path leaves out), a clip path for the placeholder `id` of a
/// text with bounds `b` and transform `text_xf` shown by a `<use>`, when both have a clip path:
/// it clips by the text's clip path `text_clip` in the text's user space and by the `<use>`'s
/// `use_clip` in the `<use>`'s, each on a rectangle over the text's bounds there, so a clip path in
/// bounding box units measures the text. Its id, or `None` when `text_xf` can't be inverted (the
/// text then draws nothing).
fn both_clips(out: &mut String, q: &str, id: &str, text_clip: &str, use_clip: &str, text_xf: Affine, b: Rect) -> Option<String> {
let det = text_xf.determinant();
if !det.is_finite() || det == 0.0 {
return None;
}
let [a, b2, c, d, e, f] = text_xf.inverse().as_coeffs();
let ub = text_xf.transform_rect_bbox(b);
let (id_u, id_t) = (format!("{id}-u"), format!("{id}-t"));
let rect = |out: &mut String, clip: &str, r: Rect| {
let _ = write!(
out,
"<{q}path style=\"transform:{IDENTITY};clip-path:{};display:inline\" d=\"M{} {}H{}V{}H{}Z\"/>",
xml_escape(clip),
r.x0,
r.y0,
r.x1,
r.y1,
r.x0
);
};
// The `<use>`'s clip path, moved back by the inverse of the text's transform from the
// placeholder's user space to the `<use>`'s.
let _ = write!(out, "<{q}defs><{q}clipPath id=\"{}\" style=\"transform:matrix({a} {b2} {c} {d} {e} {f})\">", xml_escape(&id_u));
rect(out, use_clip, ub);
let _ = write!(out, "</{q}clipPath><{q}clipPath id=\"{}\" style=\"clip-path:url(#{})\">", xml_escape(&id_t), xml_escape(&id_u));
rect(out, text_clip, b);
let _ = write!(out, "</{q}clipPath></{q}defs>");
Some(id_t)
}
/// Write the placeholder group for text `t` (namespace prefix `q`), slot `index`, with the text's
/// id when `with_id`.
#[allow(clippy::too_many_arguments)]
+21
View File
@@ -221,6 +221,27 @@ fn a_clip_path_on_a_use_of_a_text_is_in_the_use_s_user_space() {
assert!(t.xf.translation().to_point().distance(Point::new(200.0, 160.0)) < 1e-9, "{:?}", t.xf);
}
#[test]
fn a_use_and_the_text_it_shows_in_a_clip_path_both_clip() {
// The text's own clip keeps y < 120 of its user space; the `<use>`'s keeps x < 250 of the
// `<use>`'s, which the text's transform doesn't move. The glyphs keep only what both keep.
let body = r##"<defs><clipPath id="k"><rect width="250" height="300"/></clipPath>
<clipPath id="half"><rect width="400" height="120"/></clipPath>
<text id="t" transform="translate(200 0)" x="0" y="150" font-size="100" clip-path="url(#half)">HI</text></defs>
<clipPath id="c"><use href="#t" transform="translate(0 10)" clip-path="url(#k)"/></clipPath>
<rect width="400" height="300" clip-path="url(#c)"/>"##;
let (d, warnings) = import_with_report(&svg(body)).unwrap();
let (clip, _) = clip_group(&top(&d)[0]);
let b = clip_bounds(clip);
assert!(b.x0 >= 200.0 - 1e-6 && near(b.x1, 250.0, 1e-6), "{b:?}");
assert!(near(b.y1, 130.0, 1e-6) && b.y0 > 50.0, "{b:?}");
// It clips by the glyphs, not their box: nothing between the stems of the H.
let (bp, _) = vectorcraft_effects::clip_outline(clip).unwrap();
assert!(bp.contains(Point::new(b.x0 + 2.0, 110.0)), "on a stem");
assert!(!bp.contains(Point::new(225.0, 100.0)), "between the stems");
assert!(!warnings.iter().any(|w| w.contains("ignored")), "{warnings:?}");
}
#[test]
fn a_css_transform_places_a_text_in_a_clip_path() {
let d = open(