Effects: a painted clipping path's fill and stroke effects apply before a group's

A clipping path that paints a fill or stroke kept the geometry effects of
those fills and strokes live, so a geometry effect on the clip group or on
a group around it reshaped the path first and the fill's or stroke's
effect applied after it, on the canvas and in export alike. Unpainted
clipping paths already applied them first.

The clipping path now becomes one path per fill and stroke with those
effects applied, as an unpainted one does, in a group marked as such
pieces (`NodeExtras::clip_pieces`, saved with the document). `clip_paint`
paints that group's fill pieces behind the clipped art and its stroke
pieces over it, as the path paints its fills and strokes, also after an
effect around it turns a piece into a compound path or puts it in a group.
The clip group's visual bounds take in the stroke pieces' width, and HP-GL
export draws the clipping path once, in its stroke pieces.

An export of a painted clipping path with such effects in a clip group
without effects of its own also keeps its fill and stroke now, where the
baked pieces painted nothing.
This commit is contained in:
dotCooCoo
2026-11-14 21:37:54 -08:00
parent 0c1ac3bdc5
commit db1feeea10
10 changed files with 201 additions and 25 deletions
+42 -5
View File
@@ -74,19 +74,32 @@ impl Node {
/// the SVG and PDF writers: its fills, painted behind the clipped art, and its strokes, painted
/// over it (unclipped). Each part is this object keeping only those appearance items (type:
/// also only its characters' fills or strokes); `None` where it paints nothing, as right after
/// Make Clipping Mask. Only paths, compound paths and type paint as clipping paths.
/// Make Clipping Mask. Only paths, compound paths and type paint as clipping paths, and so does
/// a group of the pieces a clipping path's fills and strokes draw ([`crate::NodeExtras::clip_pieces`]),
/// even after effects around it split or copy those pieces into groups: each part is then that
/// group keeping only the pieces of those fills or strokes.
pub fn clip_paint(&self) -> ClipPaint {
use crate::appearance::AppearanceItem;
let item = |fill: bool, i: &AppearanceItem| match i {
AppearanceItem::Fill(f) => fill && f.visible && !f.paint.is_none(),
AppearanceItem::Stroke(s) => !fill && s.visible && !s.paint.is_none() && s.width > 0.0,
};
if matches!(self.kind, NodeKind::Group { clip: false, .. }) && self.extras().clip_pieces {
let part = |fill: bool| {
let mut paints = false;
let mut n = pieces_of(self, fill, &|i| item(fill, i), &mut paints)?;
n.extras_mut().clip_pieces = false;
paints.then_some(n)
};
return ClipPaint { fill: part(true), stroke: part(false) };
}
let runs = match &self.kind {
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } => None,
NodeKind::Text(t) => Some(&t.runs),
_ => return ClipPaint::default(),
};
let paints = |fill: bool| {
let item = |i: &AppearanceItem| match i {
AppearanceItem::Fill(f) => fill && f.visible && !f.paint.is_none(),
AppearanceItem::Stroke(s) => !fill && s.visible && !s.paint.is_none() && s.width > 0.0,
};
let item = |i: &AppearanceItem| item(fill, i);
let run = |r: &crate::text::TextRun| if fill { !r.style.fill.is_none() } else { !r.style.stroke.is_none() && r.style.stroke_width > 0.0 };
self.appearance.items.iter().any(item) || runs.is_some_and(|rs| rs.iter().any(run))
};
@@ -110,6 +123,30 @@ impl Node {
}
}
/// `n`, a group of a clipping path's pieces or one of them, keeping only the pieces of its fills
/// (`fill`) or of its strokes, at every depth of the groups an effect put them in; `None` when it
/// holds none. `paints` is set when one of the kept fills or strokes paints.
fn pieces_of(n: &Node, fill: bool, paints_item: &dyn Fn(&crate::appearance::AppearanceItem) -> bool, paints: &mut bool) -> Option<Node> {
use crate::appearance::AppearanceItem;
if let NodeKind::Group { children, clip: false } = &n.kind {
let kept: Vec<_> = children.iter().filter_map(|c| pieces_of(c, fill, paints_item, paints).map(std::sync::Arc::new)).collect();
if kept.is_empty() {
return None;
}
let mut m = n.clone();
if let Some(ch) = m.children_mut() {
*ch = kept;
}
return Some(m);
}
let items = &n.appearance.items;
if !items.iter().any(|i| matches!(i, AppearanceItem::Fill(_)) == fill) {
return None;
}
*paints |= items.iter().any(paints_item);
Some(n.clone())
}
/// What a clipping path paints ([`Node::clip_paint`]).
#[derive(Clone, Debug, Default)]
pub struct ClipPaint {
+5
View File
@@ -79,6 +79,10 @@ pub struct NodeExtras {
/// knockout group its knockout shape is the whole area of its triangle, not only where the
/// image is opaque ([`Node::knockout_outline`]).
pub warp_piece: bool,
/// A group of the pieces a clipping path's fills and strokes draw once their geometry effects
/// are applied, one path or compound path per fill and stroke: as the clipping path of a clip group it paints
/// those pieces as the path paints its fills and strokes ([`Node::clip_paint`]).
pub clip_pieces: bool,
}
/// No extras: what [`Node::extras`] gives for a node without any.
@@ -108,6 +112,7 @@ pub(super) static NO_EXTRAS: NodeExtras = NodeExtras {
pixel_aligned: false,
color_mask: None,
warp_piece: false,
clip_pieces: false,
};
/// A node's [`NodeExtras`], if it has any ([`Node::extras`]). Extras that are all unset equal
+1 -1
View File
@@ -369,7 +369,7 @@ impl Node {
NodeKind::Group { children, clip: true } | NodeKind::Layer { children, clip: true, .. } => {
// A clipping path that paints (see `clip_paint`) strokes over the clip edge.
let clip = children.first()?;
if matches!(clip.kind, NodeKind::Path { .. } | NodeKind::Compound { .. } | NodeKind::Text(_)) {
if matches!(clip.kind, NodeKind::Path { .. } | NodeKind::Compound { .. } | NodeKind::Text(_)) || clip.extras().clip_pieces {
clip.visual(symbols)
} else {
clip.geometric(symbols)
+3
View File
@@ -24,6 +24,7 @@ fn extras_are_written_flat_in_order() {
e.envelope_edit_margin = Some([0.5, 0.5, 0.25, 0.25]);
e.pixel_aligned = true;
e.warp_piece = true;
e.clip_pieces = true;
}
/// The top-level keys of a JSON object, in the order written.
struct Keys(Vec<String>);
@@ -71,11 +72,13 @@ fn extras_are_written_flat_in_order() {
"envelope_edit_margin",
"pixel_aligned",
"warp_piece",
"clip_pieces",
];
assert_eq!(keys, order);
assert_eq!(v["shape_mode"], "exclude");
assert_eq!(v["pixel_aligned"], true);
assert_eq!(v["warp_piece"], true);
assert_eq!(v["clip_pieces"], true);
assert_eq!(serde_json::from_value::<Node>(v).unwrap(), n);
let plain = Node::path(NodeId(9), shapes::rectangle(Rect::new(0.0, 0.0, 4.0, 2.0)), Appearance::default_art());
let back: Node = serde_json::from_value(serde_json::to_value(&plain).unwrap()).unwrap();
+5
View File
@@ -57,6 +57,7 @@ impl Serialize for Node {
is_default(&e.pixel_aligned),
e.color_mask.is_none(),
is_default(&e.warp_piece),
is_default(&e.clip_pieces),
];
let mut st = s.serialize_struct("Node", skips.iter().filter(|s| !**s).count())?;
let mut skip = skips.into_iter();
@@ -102,6 +103,7 @@ impl Serialize for Node {
field!("pixel_aligned", &e.pixel_aligned);
field!("color_mask", &e.color_mask);
field!("warp_piece", &e.warp_piece);
field!("clip_pieces", &e.clip_pieces);
st.end()
}
}
@@ -249,6 +251,8 @@ pub(super) struct NodeWire {
color_mask: Option<Box<crate::color_mask::ColorMask>>,
#[serde(default)]
warp_piece: bool,
#[serde(default)]
clip_pieces: bool,
}
impl From<NodeWire> for Node {
@@ -279,6 +283,7 @@ impl From<NodeWire> for Node {
pixel_aligned: w.pixel_aligned,
color_mask: w.color_mask,
warp_piece: w.warp_piece,
clip_pieces: w.clip_pieces,
};
Node {
id: w.id,