Pathfinder: Outline edges keep their fill's opacity and blend mode

Effect › Pathfinder › Outline stroked each edge with the paint of the
source's top fill only. The fill's opacity, blend mode and overprint
were dropped, and a hidden top fill still colored the edges.

The edges now take the paint, opacity, blend mode and overprint of the
top fill that paints, or of the top stroke that paints when no fill
does. A hidden fill or a fill of no paint is skipped, so a painted fill
under it colors the edges. A hidden stroke or a 0 pt stroke is skipped
too, and an object with neither still gets edges with no paint.

Soft Mix, Hard Mix and Trap had the same gap: they read the top fill
even when it was hidden or had no paint. They now use the top fill that
paints, so a hidden fill adds no color to a mix, and a member whose only
fill is hidden takes no part in Trap.
This commit is contained in:
dotCooCoo
2026-11-15 12:03:03 -08:00
parent e34fef895f
commit 22fbf01ea5
2 changed files with 111 additions and 6 deletions
+17 -5
View File
@@ -367,9 +367,14 @@ fn compound_shape_art_in(doc: Option<&Document>, n: &Node, hook: Option<OutlineH
fn styled(src: &Node, path: PathData, outline: bool) -> Node {
let mut appearance = Appearance { items: src.appearance.items.clone(), ..Default::default() };
if outline {
let fill = src.appearance.fill_paint();
let paint = if fill.is_none() { src.appearance.stroke_paint() } else { fill };
appearance = Appearance { items: vec![AppearanceItem::Stroke(StrokeLayer::new(paint, 1.0))], ..Default::default() };
// The edges take the paint, opacity, blend mode and overprint of the top fill that paints,
// or of the top stroke that paints when no fill does.
let layer = top_painting_fill(&src.appearance).or_else(|| src.appearance.items.iter().rev().find(|i| paints(i)));
let mut stroke = StrokeLayer::new(layer.map_or(Paint::None, |i| i.paint().clone()), 1.0);
if let Some(i) = layer {
(stroke.opacity, stroke.blend, stroke.overprint) = (i.opacity(), i.blend(), i.overprint());
}
appearance = Appearance { items: vec![AppearanceItem::Stroke(stroke)], ..Default::default() };
}
let mut n = Node::path(src.id, path, appearance);
n.opacity = src.opacity;
@@ -504,7 +509,8 @@ pub fn pathfinder_children(doc: Option<&Document>, group: &Node, hook: Option<Ou
.iter()
.filter_map(|l| {
let (path, rule) = geometry(l)?;
Some((l.id, crate::trap::TrapSource { path, rule, fill: l.appearance.fill_paint() }))
let fill = top_painting_fill(&l.appearance).map_or(Paint::None, |i| i.paint().clone());
Some((l.id, crate::trap::TrapSource { path, rule, fill }))
})
.unzip();
let made = crate::trap::traps(&srcs, &crate::trap::TrapOptions::from_params(&e.params));
@@ -580,7 +586,8 @@ fn mixing_rate(params: &Value) -> f64 {
}
fn fill_cmyk(src: &Node) -> Option<[f32; 4]> {
let paint = src.appearance.fill_paint();
// The top fill that paints: a hidden fill adds no color.
let paint = top_painting_fill(&src.appearance)?.paint().clone();
let color = paint.color()?;
let tinted = match paint {
Paint::Solid { tint, .. } => color.tinted(tint),
@@ -637,6 +644,11 @@ fn is_container(n: &Node) -> bool {
matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. })
}
/// The top fill of `a` that paints: hidden fills and fills of no paint are skipped.
fn top_painting_fill(a: &Appearance) -> Option<&AppearanceItem> {
a.items.iter().rev().find(|i| i.is_fill() && paints(i))
}
/// Does `item` paint anything?
pub fn paints(item: &AppearanceItem) -> bool {
item.visible()
+94 -1
View File
@@ -1,6 +1,6 @@
use super::*;
use vectorcraft_color::{BlendMode, Color, Paint};
use vectorcraft_doc::{CharStyle, Effect, NodeId, TextObject};
use vectorcraft_doc::{CharStyle, Effect, FillLayer, NodeId, TextObject};
use vectorcraft_geom::{Point, Rect, shapes};
/// Type tests need glyph outlines, which only the optional craft-fonts build input provides.
@@ -71,6 +71,33 @@ fn trap_effect_adds_an_overprinting_strip_above_the_members() {
assert_eq!(fill.paint.color(), Some(yellow));
}
/// Trap takes each member's top fill that paints: a hidden fill on top is skipped, and a member
/// whose only fill is hidden does not take part.
#[test]
fn trap_skips_a_hidden_fill() {
let yellow = Color::cmyk(0.0, 0.0, 1.0, 0.0);
let blue = Color::cmyk(1.0, 0.6, 0.0, 0.0);
let mut hidden = FillLayer::new(Paint::solid(Color::cmyk(0.0, 0.0, 0.0, 1.0)));
hidden.visible = false;
let trap = |members: Vec<Arc<Node>>| {
let mut g = Node::group(NodeId(10), members);
let params = serde_json::json!({"thickness": 1.0, "tintReduction": 100});
g.appearance.effects.push(Effect { id: crate::trap::TRAP.into(), params, visible: true });
pathfinder_children(None, &g, None).unwrap()
};
let mut covered = (*sq(1, 0.0, yellow)).clone();
covered.appearance.items.push(AppearanceItem::Fill(hidden.clone()));
let out = trap(vec![Arc::new(covered), sq(2, 10.0, blue)]);
assert_eq!(out.len(), 3, "both squares and one trap");
assert_eq!(out[2].appearance.fill().unwrap().paint.color(), Some(yellow));
let only_hidden = Node::path(
NodeId(1),
shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)),
Appearance { items: vec![AppearanceItem::Fill(hidden)], ..Default::default() },
);
assert_eq!(trap(vec![Arc::new(only_hidden), sq(2, 10.0, blue)]).len(), 2, "no trap");
}
#[test]
fn hard_mix_takes_the_highest_of_each_cmyk_channel_and_drops_strokes() {
let mut g = two_overlapping_cmyk_squares();
@@ -87,6 +114,72 @@ fn soft_mix_blends_cmyk_bottom_to_top_and_drops_strokes() {
assert!((c - 0.3).abs() < 0.02 && (m - 0.43).abs() < 0.02 && (y - 0.35).abs() < 0.02 && (k - 0.05).abs() < 0.02, "{c} {m} {y} {k}");
}
/// Soft Mix and Hard Mix mix the top fill that paints: a hidden fill above it adds no color.
#[test]
fn mixes_skip_a_hidden_fill() {
for effect in ["pathfinder.softMix", "pathfinder.hardMix"] {
let mut plain = two_overlapping_cmyk_squares();
plain.appearance.effects.push(Effect { id: effect.into(), params: serde_json::json!({"mixingRate": 50.0}), visible: true });
let mut covered = plain.clone();
let NodeKind::Group { children, .. } = &mut covered.kind else { panic!("group") };
let mut hidden = FillLayer::new(Paint::solid(Color::cmyk(0.0, 0.0, 0.0, 1.0)));
hidden.visible = false;
for c in children.iter_mut() {
Arc::make_mut(c).appearance.items.push(AppearanceItem::Fill(hidden.clone()));
}
assert_eq!(overlap_cmyk(&covered), overlap_cmyk(&plain), "{effect}");
}
}
/// Outline strokes each edge with the top fill that paints, keeping that fill's opacity, blend
/// mode and overprint. A hidden fill or a fill of no paint gives the edges nothing; with no fill
/// that paints, the edges take the top stroke that paints, which skips hidden and 0 pt strokes.
#[test]
fn outline_edges_keep_the_fill_layers_opacity_blend_and_overprint() {
use vectorcraft_doc::StrokeLayer;
let red = Paint::solid(Color::rgb(1.0, 0.0, 0.0));
let blue = Paint::solid(Color::rgb(0.0, 0.0, 1.0));
let mut fill = FillLayer::new(red.clone());
(fill.opacity, fill.blend, fill.overprint) = (0.5, BlendMode::Multiply, true);
let mut hidden = FillLayer::new(Paint::solid(Color::rgb(0.0, 1.0, 0.0)));
hidden.visible = false;
let mut stroke = StrokeLayer::new(blue.clone(), 2.0);
(stroke.opacity, stroke.blend, stroke.overprint) = (0.5, BlendMode::Multiply, true);
let mut hidden_stroke = StrokeLayer::new(Paint::solid(Color::rgb(0.0, 1.0, 0.0)), 2.0);
hidden_stroke.visible = false;
let zero_stroke = StrokeLayer::new(Paint::solid(Color::rgb(0.0, 1.0, 0.0)), 0.0);
let look = |items: Vec<AppearanceItem>| Appearance { items, ..Default::default() };
let (f, s) = (AppearanceItem::Fill, AppearanceItem::Stroke);
let cases = [
(look(vec![f(fill.clone())]), Some(red.clone())),
(look(vec![f(fill.clone()), f(hidden.clone())]), Some(red.clone())),
(look(vec![f(fill.clone()), f(FillLayer::new(Paint::None))]), Some(red.clone())),
(look(vec![f(hidden.clone())]), None),
(look(vec![s(stroke.clone())]), Some(blue.clone())),
(look(vec![f(hidden.clone()), s(stroke.clone())]), Some(blue.clone())),
(look(vec![s(stroke.clone()), s(hidden_stroke), s(zero_stroke.clone())]), Some(blue.clone())),
(look(vec![s(zero_stroke)]), None),
];
for (appearance, want) in cases {
let member = Arc::new(Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), appearance));
let mut g = Node::group(NodeId(10), vec![member, sq(2, 5.0, Color::rgb(0.0, 0.0, 1.0))]);
g.appearance.effects.push(Effect { id: "pathfinder.outline".into(), params: serde_json::json!({}), visible: true });
let out = pathfinder_children(None, &g, None).unwrap();
let edges: Vec<_> = out.iter().filter(|n| n.id == NodeId(1)).collect();
assert!(!edges.is_empty(), "{out:#?}");
for n in edges {
let [AppearanceItem::Stroke(s)] = n.appearance.items.as_slice() else { panic!("one stroke: {n:#?}") };
match &want {
Some(paint) => {
assert_eq!(&s.paint, paint);
assert_eq!((s.opacity, s.blend, s.overprint), (0.5, BlendMode::Multiply, true), "{s:#?}");
}
None => assert!(s.paint.is_none() && s.opacity == 1.0, "{s:#?}"),
}
}
}
}
#[test]
fn shape_modes_follow_the_pathfinder_panel() {
let add = pathfinder_children(None, &group_with("pathfinder.add"), None).unwrap();