mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-31 13:17:19 -06:00
Export: the art's bounds take in the effects of groups, type and images
The bounds of the visible art (`vectorcraft_render::encode::art_bounds`) are the crop of Export with Use Artboards off, EPS without artboards, Export for Screens' Full Document and Save for Web (Legacy) with Clip to Artboard off, and the size Fit to Artwork Bounds gives an artboard. They measured a group or layer by its members and left out its own effects, and measured type and images without their effects. A group's drop shadow, or the shadow of a line of type, was cut off at the image's edge and left outside the fitted artboard. They also counted a Shaper group's transparent source art. Each visible object is now measured by `vectorcraft_render::effect_bounds`, the measure of Export Selection's crop. A group or layer without effects of its own is still measured by its visible members, skipping a Shaper group's source art. A group or layer with a drop shadow, a glow or another raster effect is measured without its hidden members, guides and template layers, unless it is a Shaper group or also has a geometry, Pathfinder, Crop Marks or 3D effect or a visible effect on its own fill or stroke. The SVG of a chosen artboard also keeps an object whose effects alone reach the artboard. Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
@@ -15,6 +15,8 @@ pub mod tga;
|
||||
pub mod tiff;
|
||||
pub mod web;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use vectorcraft_doc::{Document, Node, NodeKind, SymbolBoxes};
|
||||
use vectorcraft_geom::Rect;
|
||||
|
||||
@@ -191,8 +193,9 @@ pub(crate) fn webp(img: &Rendered) -> Result<Vec<u8>, String> {
|
||||
}
|
||||
|
||||
/// Bounds of the art an export draws: visible objects off template layers (guides left out),
|
||||
/// with their strokes and live effects, symbol instances measured by their art. `None` when
|
||||
/// nothing would be drawn.
|
||||
/// each measured by [`crate::effect_bounds`]: with its strokes and its effects (the effects of
|
||||
/// groups, layers, type and images included), symbol instances by their art, and a Shaper group
|
||||
/// without effects of its own by the art it shows. `None` when nothing would be drawn.
|
||||
pub fn art_bounds(doc: &Document) -> Option<Rect> {
|
||||
let symbols = doc.symbol_boxes();
|
||||
doc.layers.iter().fold(None, |acc, l| vectorcraft_geom::union_opt(acc, drawn_bounds(&symbols, l)))
|
||||
@@ -222,16 +225,54 @@ fn drawn_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
}
|
||||
match &n.kind {
|
||||
NodeKind::Layer { template: true, .. } | NodeKind::Path { guide: true, .. } => None,
|
||||
NodeKind::Layer { children, clip: false, .. } | NodeKind::Group { children, clip: false } => {
|
||||
children.iter().fold(None, |acc, c| vectorcraft_geom::union_opt(acc, drawn_bounds(symbols, c)))
|
||||
}
|
||||
_ if fx::has_fx(n) => fx::visual_bounds(symbols, n),
|
||||
// An instance's box takes in its art, the effects in the art and its own effects.
|
||||
NodeKind::SymbolInstance { .. } => fx::cull_bounds(symbols, n),
|
||||
_ => n.visual_bounds_with(symbols),
|
||||
NodeKind::Layer { children, clip: false, .. } | NodeKind::Group { children, clip: false } if !fx::has_object_fx(n) => children
|
||||
.iter()
|
||||
.skip(usize::from(n.extras().shaper.is_some()))
|
||||
.fold(None, |acc, c| vectorcraft_geom::union_opt(acc, drawn_bounds(symbols, c))),
|
||||
_ => crate::effect_bounds(symbols, without_undrawn(n).as_ref().unwrap_or(n)),
|
||||
}
|
||||
}
|
||||
|
||||
/// `n`, a group or layer that doesn't clip, without the hidden objects, guides and template layers
|
||||
/// in it and in the groups and layers in it that don't clip, or `None` when it holds none of them
|
||||
/// or is anything else. A Shaper group keeps all its members, and so does a group or layer with a
|
||||
/// visible geometry, Pathfinder, Crop Marks or 3D effect, or with a visible effect on one of its
|
||||
/// own visible fills or strokes.
|
||||
fn without_undrawn(n: &Node) -> Option<Node> {
|
||||
use crate::effects;
|
||||
let keeps_all = n.extras().shaper.is_some()
|
||||
|| effects::has_geometry(&n.appearance.effects)
|
||||
|| effects::has_pathfinder(n)
|
||||
|| effects::has_crop_marks(n)
|
||||
|| effects::has_revolve(n)
|
||||
|| effects::has_solid(n)
|
||||
|| n.appearance.items.iter().any(|i| i.visible() && i.effects().iter().any(|e| e.visible));
|
||||
if keeps_all || !matches!(n.kind, NodeKind::Layer { clip: false, .. } | NodeKind::Group { clip: false, .. }) {
|
||||
return None;
|
||||
}
|
||||
let undrawn = |c: &Node| !c.visible || matches!(c.kind, NodeKind::Layer { template: true, .. } | NodeKind::Path { guide: true, .. });
|
||||
let mut changed = false;
|
||||
let kept: Vec<Arc<Node>> = n
|
||||
.children()?
|
||||
.iter()
|
||||
.filter_map(|c| {
|
||||
if undrawn(c) {
|
||||
changed = true;
|
||||
return None;
|
||||
}
|
||||
let part = without_undrawn(c);
|
||||
changed |= part.is_some();
|
||||
Some(part.map_or_else(|| c.clone(), Arc::new))
|
||||
})
|
||||
.collect();
|
||||
if !changed {
|
||||
return None;
|
||||
}
|
||||
let mut out = n.clone();
|
||||
*out.children_mut()? = kept;
|
||||
Some(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user