mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 16:37:32 +00:00
Effects: a group's own fill and effects measure clipping type by its clip, not its strokes
When a group with a geometry effect holds a clip group whose clipping type paints strokes, the clip group becomes a group of a clip group and the type's stroke art. The clipping path in it is now the region the live clip uses, so the geometry effects of the type's fills and strokes reshape what they paint and leave the clip as it is. The group's own fills and strokes, their unplaced gradients and the box its geometry effects scale around count that group as its clip group, so a stroke moved outside the clip no longer widens them.
This commit is contained in:
@@ -28,7 +28,8 @@ use vectorcraft_geom::{Affine, FillRule, PathData, Rect, Vec2};
|
||||
use crate::distort::{transform_move, transform_offsets};
|
||||
use crate::group::{has_own_paint, paints};
|
||||
use crate::reshape::{
|
||||
as_art, clipping_text_outline, dressed, geometry_effects, glyph_outlines, knockout_inside, outline_nested_text, painted_type_clip, reshape_leaves,
|
||||
as_art, clip_region_path, clipping_text_outline, dressed, geometry_effects, glyph_outlines, knockout_inside, outline_nested_text,
|
||||
painted_type_clip, reshape_leaves,
|
||||
};
|
||||
use crate::{
|
||||
GeomContext, adjust_in_document, apply_geometry_carrying, crop_marks_art, evaluate_container, has_adjustment, has_crop_marks, has_geometry,
|
||||
@@ -472,13 +473,7 @@ fn clip_group_with_type_clip(d: &mut Document, n: &Node) -> Option<Node> {
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let (bp, rule) = crate::clip_outline(first)?;
|
||||
let mut clip = Node::path(first.id, PathData::from_bezpath(&bp), Appearance::default());
|
||||
if let NodeKind::Path { rule: r, clipping, .. } = &mut clip.kind {
|
||||
(*r, *clipping) = (rule, true);
|
||||
}
|
||||
clip.name = first.name.clone();
|
||||
clip.visible = first.visible;
|
||||
let clip = clip_region_path(first)?;
|
||||
let paint = first.clip_paint();
|
||||
// The type's fills and strokes as art: baked with the type's id, as the renderer draws them,
|
||||
// then given ids of their own.
|
||||
|
||||
@@ -691,6 +691,11 @@ pub fn member_shapes(doc: Option<&Document>, n: &Node) -> Vec<(PathData, FillRul
|
||||
}
|
||||
}
|
||||
fn members(doc: Option<&Document>, n: &Node, out: &mut Vec<(PathData, FillRule)>) {
|
||||
// A clip group whose clipping type's strokes paint around it is outlined as that clip
|
||||
// group: the strokes outside the clip add nothing.
|
||||
if let Some(inner) = crate::reshape::painted_clip_inner(n) {
|
||||
return members(doc, inner, out);
|
||||
}
|
||||
// A clip group shows its members only inside its clipping path: its outline is that
|
||||
// region, not the clipped members' own shapes, which may reach far outside it (#7289).
|
||||
if n.clips() {
|
||||
@@ -881,17 +886,14 @@ pub fn evaluate_container(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
if paint {
|
||||
// A clip group whose clipping type paints strokes is now a group of a clip group and those
|
||||
// strokes ([`crate::reshape::painted_type_clip`]): its own fills and strokes paint inside
|
||||
// that clip group.
|
||||
let wrapped = n.clips() && !m.clips();
|
||||
let (shapes, bounds) = match m.children().and_then(|ch| ch.first()).filter(|_| wrapped) {
|
||||
Some(inner) => (member_shapes(doc, inner), inner.geometric_bounds()),
|
||||
None => (member_shapes(doc, &m), m.geometric_bounds()),
|
||||
};
|
||||
// that clip group. Such groups among the members are measured as their clip groups.
|
||||
let wrapped = n.clips() && crate::reshape::painted_clip_inner(&m).is_some();
|
||||
let (shapes, bounds) = (member_shapes(doc, &m), crate::reshape::clip_bounds(&m));
|
||||
let art =
|
||||
|items: &[AppearanceItem]| -> Vec<Arc<Node>> { items.iter().filter_map(|i| item_art(n.id, i, &shapes, bounds)).map(Arc::new).collect() };
|
||||
let (below, above) = m.appearance.split_contents();
|
||||
let (below, above) = (art(below), art(above));
|
||||
let at = usize::from(n.clips());
|
||||
let at = usize::from(wrapped || m.clips());
|
||||
let target = if wrapped { m.children_mut().and_then(|ch| ch.first_mut()).map(Arc::make_mut) } else { Some(&mut m) };
|
||||
if let Some(ch) = target.and_then(Node::children_mut) {
|
||||
let at = at.min(ch.len());
|
||||
|
||||
@@ -744,15 +744,30 @@ pub(crate) fn clipping_text_outline(t: &Node) -> Option<Node> {
|
||||
Some(Node { visible: t.visible, ..o })
|
||||
}
|
||||
|
||||
/// Clipping path `clip` of a clip group as a clipping path of the region it clips to
|
||||
/// ([`crate::clip_outline`]), with its id, name and visibility. For type that paints, the geometry
|
||||
/// effects of its fills and strokes reshape what they paint and leave that region as it is.
|
||||
/// `None` when it has nothing to clip by.
|
||||
pub(crate) fn clip_region_path(clip: &Node) -> Option<Node> {
|
||||
let (bp, rule) = crate::clip_outline(clip)?;
|
||||
let mut p = Node::path(clip.id, PathData::from_bezpath(&bp), Appearance::default());
|
||||
if let NodeKind::Path { rule: r, clipping, .. } = &mut p.kind {
|
||||
(*r, *clipping) = (rule, true);
|
||||
}
|
||||
p.name = clip.name.clone();
|
||||
p.visible = clip.visible;
|
||||
Some(p)
|
||||
}
|
||||
|
||||
/// Clip group or clipping layer `n` whose clipping path is type that paints ([`Node::clip_paint`]),
|
||||
/// with that type as plain art drawn as the renderer draws it: its outlines clip
|
||||
/// ([`clipping_text_outline`]), its fills become art behind the clipped members inside the clip,
|
||||
/// with that type as plain art drawn as the renderer draws it: a path of the region it clips to
|
||||
/// clips ([`clip_region_path`]), its fills become art behind the clipped members inside the clip,
|
||||
/// and its strokes become art over the clip group, outside the clip. Each part is the type's art
|
||||
/// with only those fills or strokes ([`as_art`]), its own geometry effects applied. With strokes,
|
||||
/// `n` becomes a group with its id, name, visibility and transparency that holds a clip group of
|
||||
/// the clipping path, the fills and the members, then the strokes; a clipping layer keeps its
|
||||
/// ruler guides last. `None` when `n` does not clip, its clipping path is not type, or the type
|
||||
/// paints nothing.
|
||||
/// the clipping path, the fills and the members (with `n`'s id), then the strokes; a clipping
|
||||
/// layer keeps its ruler guides last ([`painted_clip_inner`] finds that clip group). `None` when
|
||||
/// `n` does not clip, its clipping path is not type, or the type paints nothing.
|
||||
pub(crate) fn painted_type_clip(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
let first = n.children()?.first().filter(|_| n.clips())?;
|
||||
if !matches!(first.kind, NodeKind::Text(_)) {
|
||||
@@ -762,7 +777,7 @@ pub(crate) fn painted_type_clip(doc: Option<&Document>, n: &Node) -> Option<Node
|
||||
if paint.fill.is_none() && paint.stroke.is_none() {
|
||||
return None;
|
||||
}
|
||||
let clip = clipping_text_outline(first)?;
|
||||
let clip = clip_region_path(first)?;
|
||||
let fill = paint.fill.and_then(|p| as_art(doc, &p, None));
|
||||
let stroke = paint.stroke.and_then(|p| as_art(doc, &p, None));
|
||||
let mut m = n.clone();
|
||||
@@ -796,6 +811,48 @@ pub(crate) fn knockout_inside(m: &mut Node, inner: &mut Node) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The clip group in `n` when `n` is a group or layer that [`painted_type_clip`] made of a clip
|
||||
/// group and its clipping type's strokes: a group that does not clip whose first member is a clip
|
||||
/// group with `n`'s id and whose second member has the id of that clip group's clipping path. The
|
||||
/// clip group's region is what `n` stands for, as the clip group it was made from: the strokes
|
||||
/// paint outside it and add nothing to its outline ([`crate::member_shapes`]) or its bounds
|
||||
/// ([`clip_bounds`]).
|
||||
pub(crate) fn painted_clip_inner(n: &Node) -> Option<&Node> {
|
||||
if n.clips() || !matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }) {
|
||||
return None;
|
||||
}
|
||||
let [inner, stroke, ..] = n.children()?.as_slice() else { return None };
|
||||
let clip = inner.children()?.first()?;
|
||||
(inner.clips() && inner.id == n.id && stroke.id == clip.id && !stroke.clips()).then_some(&**inner)
|
||||
}
|
||||
|
||||
/// The geometric bounds of `n` ([`Node::geometric_bounds`]) with each group or layer in it that
|
||||
/// [`painted_clip_inner`] finds measured as its clip group, so the strokes of a clipping type
|
||||
/// outside the clip do not widen them.
|
||||
pub(crate) fn clip_bounds(n: &Node) -> Option<Rect> {
|
||||
fn holds(n: &Node) -> bool {
|
||||
painted_clip_inner(n).is_some() || n.children().into_iter().flatten().any(|c| holds(c))
|
||||
}
|
||||
fn unwrap(n: &mut Node) {
|
||||
if let Some(inner) = painted_clip_inner(n) {
|
||||
let mut inner = inner.clone();
|
||||
inner.visible = n.visible;
|
||||
*n = inner;
|
||||
}
|
||||
for c in n.children_mut().into_iter().flatten() {
|
||||
if holds(c) {
|
||||
unwrap(Arc::make_mut(c));
|
||||
}
|
||||
}
|
||||
}
|
||||
if !holds(n) {
|
||||
return n.geometric_bounds();
|
||||
}
|
||||
let mut probe = n.clone();
|
||||
unwrap(&mut probe);
|
||||
probe.geometric_bounds()
|
||||
}
|
||||
|
||||
/// Replace type anywhere inside `n` by its outlines, as art that keeps the type's look
|
||||
/// ([`as_art`]). Type in a clip group's clipping path (all of `n` when `clipping` is set) becomes
|
||||
/// its outlines with its visibility and effects ([`clipping_text_outline`]); when it paints, the
|
||||
@@ -1585,7 +1642,7 @@ impl Step<'_> {
|
||||
if !groups.is_empty() {
|
||||
carry_groups(art, groups, after, &|from, to| crate::Carry::of(&e.id, params, bounds, from, to, seed));
|
||||
}
|
||||
let drawn = warped.as_deref().map_or_else(|| art.geometric_bounds(), |w| w.bounds(art));
|
||||
let drawn = warped.as_deref().map_or_else(|| clip_bounds(art), |w| w.bounds(art));
|
||||
let whole = (follows && !held).then(|| crate::Carry::of(&e.id, params, bounds, Some(bounds), drawn, seed));
|
||||
Ran { kept: !kept_ids.is_empty(), took, masks, short, whole }
|
||||
}
|
||||
@@ -3113,16 +3170,17 @@ impl WarpedImages {
|
||||
|
||||
/// The geometric bounds of `art` as it draws after the effects so far: each image this table
|
||||
/// lists measured by the pieces the warps and moves it took would cut it into, when the art
|
||||
/// has room for them ([`cut_warped_images`]).
|
||||
/// has room for them ([`cut_warped_images`]). The strokes of clipping type outside its clip
|
||||
/// are left out ([`clip_bounds`]).
|
||||
pub(crate) fn bounds(&self, art: &Node) -> Option<Rect> {
|
||||
if self.images.is_empty() || !self.fits(art, crate::distort::MAX_COPY_OBJECTS) {
|
||||
return art.geometric_bounds();
|
||||
return clip_bounds(art);
|
||||
}
|
||||
// The pieces the images would be cut into now: each group of pieces is shown or hidden as
|
||||
// its image is, and hidden objects are left out of a layer's bounds.
|
||||
let mut probe = art.clone();
|
||||
restore_warped_images(&mut probe, self, true);
|
||||
probe.geometric_bounds()
|
||||
clip_bounds(&probe)
|
||||
}
|
||||
|
||||
/// The geometric bounds of image `n`: when this table lists it, measured by the pieces the
|
||||
|
||||
@@ -646,3 +646,82 @@ fn a_clip_groups_own_gradient_spans_the_clip_when_its_clipping_type_strokes() {
|
||||
let (left, right) = (geom.start.x.min(geom.end.x), geom.start.x.max(geom.end.x));
|
||||
assert!((left - region.x0).abs() < 0.5 && (right - region.x1).abs() < 0.5, "the gradient {geom:?} spans the clip {region:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_groups_own_fill_paints_only_the_clip_of_clipping_type_whose_stroke_moves() {
|
||||
// A group with a fill of its own (solid, or an unplaced linear gradient) and a Transform effect
|
||||
// (moving everything 10 pt down, or scaling it to half its width around its middle) holds a
|
||||
// clip group clipped by type whose stroke has a Transform effect moving it 60 pt right,
|
||||
// outside the clip. On the canvas and in the export, the clip region is the type's glyphs
|
||||
// reshaped by the group's effect, and the group's fill paints that region alone, its gradient
|
||||
// spanning it: the moved stroke adds nothing to the region, the fill or the box the group's
|
||||
// effect scales around.
|
||||
use vectorcraft_color::{Gradient, GradientKind, GradientPaint, GradientStop};
|
||||
let yellow = Paint::solid(Color::rgb(1.0, 1.0, 0.0));
|
||||
let stops = vec![GradientStop::new(0.0, Color::BLACK), GradientStop::new(1.0, Color::WHITE)];
|
||||
let unplaced = Paint::Gradient(Box::new(GradientPaint::new(Gradient::new(GradientKind::Linear, stops))));
|
||||
let mut ty = painted_type(3, false, false);
|
||||
ty.appearance = Appearance::basic(Paint::None, green(), 2.0).into();
|
||||
for item in &mut ty.appearance.items {
|
||||
if let vectorcraft_doc::AppearanceItem::Stroke(s) = item {
|
||||
s.effects.push(fx("distort.transform", json!({"moveH": 60})));
|
||||
}
|
||||
}
|
||||
let live = clip_outline(&ty).expect("the type clips").0.bounding_box();
|
||||
let moved = Rect::new(live.x0, live.y0 + 10.0, live.x1, live.y1 + 10.0);
|
||||
// Scaled to half its width around the middle of the clip, which the stroke does not move.
|
||||
let (mid, quarter) = ((live.x0 + live.x1) / 2.0, live.width() / 4.0);
|
||||
let narrowed = Rect::new(mid - quarter, live.y0, mid + quarter, live.y1);
|
||||
// The group's own fill art: every object outside the clip groups whose own fill is `paint`.
|
||||
fn own_fills<'a>(n: &'a Node, paint: &Paint, out: &mut Vec<&'a Node>) {
|
||||
let mine = n.appearance.items.iter().any(|i| match (i, paint) {
|
||||
(vectorcraft_doc::AppearanceItem::Fill(f), Paint::Gradient(_)) => matches!(f.paint, Paint::Gradient(_)),
|
||||
(vectorcraft_doc::AppearanceItem::Fill(f), p) => f.paint == *p,
|
||||
_ => false,
|
||||
});
|
||||
if mine {
|
||||
out.push(n);
|
||||
}
|
||||
for c in n.children().into_iter().flatten().filter(|c| !c.clips()) {
|
||||
own_fills(c, paint, out);
|
||||
}
|
||||
}
|
||||
let cases = [(json!({"moveV": 10}), moved), (json!({"scaleH": 50, "scaleV": 100}), narrowed)];
|
||||
for ((params, region), paint) in cases.iter().flat_map(|c| [yellow.clone(), unplaced.clone()].map(|p| (c.clone(), p))) {
|
||||
let mut outer = Node::group(NodeId(1), vec![Arc::new(clip_group(2, ty.clone()))]);
|
||||
outer.appearance =
|
||||
Appearance { effects: vec![fx("distort.transform", params.clone())], ..Appearance::basic(paint.clone(), Paint::None, 0.0) }.into();
|
||||
let canvas = crate::evaluate_container(None, &outer).unwrap();
|
||||
let mut d = Document::new(400.0, 400.0);
|
||||
let layer = d.layers[0].id;
|
||||
d.insert(Some(layer), 0, outer).unwrap();
|
||||
let baked = bake_document(&d).unwrap();
|
||||
let exported = baked.layers[0].children().and_then(|ch| ch.first()).cloned().expect("the export holds the group");
|
||||
for (what, art) in [("canvas", &canvas), ("export", &*exported)] {
|
||||
let case = format!("{what}, {params}, {}", if matches!(paint, Paint::Gradient(_)) { "gradient" } else { "solid" });
|
||||
let (clip, _, strokes, _) = painted_clip(art).unwrap_or_else(|| panic!("{case}: no clip group in {art:?}"));
|
||||
assert!(near_rect(clip, region), "{case}: the clip {clip:?} is the type's glyphs reshaped {region:?}");
|
||||
let strokes = strokes.unwrap_or_else(|| panic!("{case}: no stroke art"));
|
||||
assert!(strokes.x1 > region.x1 + 25.0, "{case}: the stroke {strokes:?} moves outside the clip {region:?}");
|
||||
let mut fills = vec![];
|
||||
own_fills(art, &paint, &mut fills);
|
||||
assert!(!fills.is_empty(), "{case}: no fill art of the group in {art:?}");
|
||||
let painted = fills.iter().filter_map(|f| f.geometric_bounds()).reduce(|a, b| a.union(b)).unwrap();
|
||||
assert!(near_rect(painted, region), "{case}: the group's fill {painted:?} paints the clip {region:?}");
|
||||
if let Paint::Gradient(_) = paint {
|
||||
for fill in fills {
|
||||
let geom = match fill.appearance.fill_paint() {
|
||||
Paint::Gradient(g) => g.geom,
|
||||
_ => None,
|
||||
};
|
||||
let geom = geom.unwrap_or_else(|| panic!("{case}: the gradient is not placed: {fill:?}"));
|
||||
let (left, right) = (geom.start.x.min(geom.end.x), geom.start.x.max(geom.end.x));
|
||||
assert!(
|
||||
(left - region.x0).abs() < 0.5 && (right - region.x1).abs() < 0.5,
|
||||
"{case}: the gradient {geom:?} spans the clip {region:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user