Clipping: painted type keeps its fill and stroke in a group with geometry effects

A clip group whose clipping path is type with a fill or stroke lost that
paint when a group around it had a geometry effect. Outlining the type
for the effect replaced it with a group of glyph outlines, and a group
paints nothing as a clipping path. Distort effects lost it on the canvas
and in SVG and PDF; Transform effects that only move lost it in SVG and
PDF when other type in the group stayed type.

The type's outlines now clip, its fills become art behind the clipped
art inside the clip, and its strokes become art over it outside the
clip, as the canvas draws an unaffected clip group.
This commit is contained in:
dotCooCoo
2026-11-14 21:34:17 -08:00
parent 0c1ac3bdc5
commit 814cae044f
3 changed files with 99 additions and 3 deletions
+57
View File
@@ -84,6 +84,63 @@ fn release_and_ungroup_keep_the_clipping_paths_paint() {
}
}
/// Pixels of `doc` rendered on white that are mostly blue and mostly green.
fn blue_and_green(doc: &Document) -> (usize, usize) {
let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
let r = Renderer::new().render(doc, 200, 200, Affine::IDENTITY, &opts);
let (mut blue, mut green) = (0, 0);
for y in 0..200 {
for x in 0..200 {
let p = r.pixel(x, y);
blue += usize::from(p[2] > 200 && p[0] < 60 && p[1] < 60);
green += usize::from(p[1] > 200 && p[0] < 60 && p[2] < 60);
}
}
(blue, green)
}
#[test]
fn painted_type_clipping_inside_a_group_with_geometry_effects_keeps_its_paint() {
// The canvas and the SVG and PDF output paint the type's fill and stroke as they did before
// the effect. Type that stays type beside the clip group takes the second Transform case
// through baking that keeps type.
let cases =
[("distort.transform", json!({"moveH": 5}), false), ("distort.transform", json!({"moveH": 5}), true), ("distort.roughen", json!({}), false)];
for (effect, params, other_type) in cases {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
rect(&mut s, 0.0, 30.0, "#ff0000");
let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 120, "text": "Clip", "size": 72})).unwrap()["id"].as_u64().unwrap());
s.execute("select.all", &json!({})).unwrap();
let clip = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap());
s.execute("paint.setFill", &json!({"color": "#0000ff", "ids": [t.0]})).unwrap();
s.execute("paint.setStroke", &json!({"color": "#00ff00", "ids": [t.0]})).unwrap();
s.execute("stroke.set", &json!({"weight": 2, "ids": [t.0]})).unwrap();
let before = blue_and_green(&s.doc().unwrap().doc);
let mut ids = vec![clip.0];
if other_type {
ids.push(s.execute("text.create", &json!({"x": 10, "y": 190, "text": "x", "size": 10})).unwrap()["id"].as_u64().unwrap());
}
let group = NodeId(s.execute("object.group", &json!({"ids": ids})).unwrap()["id"].as_u64().unwrap());
s.execute("effect.apply", &json!({"effect": effect, "params": params, "ids": [group.0], "item": null})).unwrap();
let doc = s.doc().unwrap().doc.clone();
let (blue, green) = blue_and_green(&doc);
assert!(
blue * 2 > before.0 && green * 2 > before.1,
"{effect} {other_type}: the canvas paints the type's fill and stroke: {blue} {green}, {before:?} before"
);
let svg = vectorcraft_svg::export(&doc, &Default::default());
let pdf = vectorcraft_pdf::export(&doc, &Default::default()).unwrap();
for (what, out) in [("SVG", vectorcraft_svg::import(&svg).unwrap()), ("PDF", vectorcraft_pdf::import(&pdf).unwrap())] {
let (b, g) = blue_and_green(&out);
assert!(
b.abs_diff(blue) * 10 < blue && g.abs_diff(green) * 5 < green,
"{effect} {other_type}, {what}: {b} {g} against the canvas's {blue} {green}"
);
}
}
}
#[test]
fn type_as_a_clipping_path_paints_its_characters() {
let mut s = Session::new();