mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-14 19:22:03 +00:00
Every symbol instance was measured as its transform applied to the 20 × 20 square around the origin, which symbols made in the app keep their art in. The SVG and DXF importers keep symbol art as drawn, so the box of an instance of an imported symbol did not match its art. The SVG Star instance at x 20, y 30 draws from (20, 30) to (40, 50) and was measured from (10, 20) to (30, 40). Align moved such an instance by the wrong amount, clicks and marquees beside its art selected it, and the canvas, exports and prints left it out when its art reached the view or the page and the square did not. An instance is now measured by its symbol's art, which Document::symbol_boxes looks up in the document each time (Node::geometric_bounds_with, visual_bounds_with and reach_bounds_with). With Use Preview Bounds on, the strokes in the art count at their own weight, as the canvas draws them, and object.setBounds counts them at that weight when it sizes an instance or an object that holds one. When strokes do not scale and a width or height of 0 or more is smaller than what a path's stroke adds, object.setBounds now gives the path no width or height, as it does for an instance whose art has strokes; before, it mirrored the path. With Use Preview Bounds on, object.setBounds now measures the margins that strokes add object by object, the members of groups and the objects in symbol instances' art included, and solves for the width, height and reference point asked for when the width and height are 0 or more. Before, it measured the margins of the whole selection and could miss the size or the reference point asked for when strokes did not scale and the objects' strokes added different margins, and when strokes scaled and the width and height were out of proportion, even for one path. A clip group, a Shaper Group, a compound path, type, and a group or instance with effects or an opacity mask of its own are each measured as one object, and so is an instance in perspective or one turned or slanted relative to the bounding box, other than by quarter turns. When the selection holds more than 4,096 objects to measure this way, each selected object is measured as one. Instances inside symbol art are measured by their own symbol's art. An instance is measured as the square when its symbol is missing, when its symbol or a symbol nested in its art holds an instance of itself, and when symbols nest 32 deep in its art. Align and Distribute, the Transform panel, the bounding box and the selection outline, clicks, the marquee, document.inspect and document.node, Fit to Artwork Bounds and Fit to Selected Art, canvas culling, thumbnails, the SVG filter region of an instance with a raster effect, raster effect images, Export Selection, the SVG of an artboard, printing, and the PDF, EPS, EMF/WMF and DXF writers use the new box. The renderer also culls an instance by the effects and brushed strokes in its art, and it culls a blend, envelope or repeat by the brush art of what it holds and the effects in the art of the instances in it. It culls an instance with geometry effects of its own by the effects in its art after those geometry effects reshape the art. It measures the effects in a symbol's art once for each symbol and linear part of the transform that places it. For symbols whose art holds instances, it measures at most 4,096 placements a frame, and at most 4,096 more for each pattern tile it rasterizes and each placed document it draws. Past that, it estimates the reach from the symbol's reach with no transform, which it measures once. When the drawn document's symbols change, the renderer drops the cached bounds of the instances and of the objects that hold them. The PDF and EPS writers, the SVG of an artboard and Skip Blank Artboards grow the box of an instance whose art holds brushed strokes by 16 times the widest stroke (at least 1 pt) of the objects in that art that have a brushed stroke, plus 8 pt, as the renderer grows the cull bounds of a brushed path. They keep an instance whose brush art reaches the page. The SVG of an artboard also keeps an object that holds an instance when what the canvas paints for it, the effects in the symbol's art and the instance's own included, reaches the artboard. Not yet: other commands that measure objects, such as Select › All on Active Artboard, Transform Each and Convert to Artboards, still measure an instance by the square. Docs: docs/mcp.md, docs/target-app-parity.md.
219 lines
11 KiB
Rust
219 lines
11 KiB
Rust
//! `document.export`, `document.serialize`, `document.exportSelection`, `document.exportForOffice`.
|
|
|
|
use std::borrow::Cow;
|
|
use std::collections::HashSet;
|
|
use std::sync::Arc;
|
|
|
|
use serde_json::{Value, json};
|
|
use vectorcraft_doc::{Document, Node, NodeId, NodeKind, SymbolBoxes};
|
|
|
|
use crate::DocState;
|
|
|
|
use super::super::*;
|
|
use super::encode::single_artboard;
|
|
use super::{ARTBOARD_PARAMS, Format, default_name, encode, encode_all, encode_with_warnings, merge, writable, write_encoded, write_or_return};
|
|
|
|
pub(super) fn serialize(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let f = writable("document.serialize", Some(str_param(p, "format").unwrap_or("vectorcraft")), None)?;
|
|
let expanded = expand_presets(s, "document.serialize", p)?;
|
|
let p = &*expanded;
|
|
let doc = &*source(s, f, p, "document.serialize")?;
|
|
let enc = encode_all(doc, f.id, p)?;
|
|
let files = enc.named(doc, &default_name(doc, f.extensions[0]));
|
|
let (main, linked) = files.split_at(enc.files.len());
|
|
// Text formats come back as text; an SVG in UTF-16 or ISO 8859-1 also as its bytes.
|
|
let data = |bytes: &[u8]| match f.id {
|
|
"svg" => {
|
|
let text = vectorcraft_svg::text_of(bytes).unwrap_or_else(|_| String::from_utf8_lossy(bytes));
|
|
let mut v = json!({ "text": text });
|
|
if text.as_bytes() != bytes {
|
|
v["dataBase64"] = json!(vectorcraft_format::base64_encode(bytes));
|
|
}
|
|
v
|
|
}
|
|
_ => json!({ "dataBase64": vectorcraft_format::base64_encode(bytes) }),
|
|
};
|
|
let mut out = merge(data(main.first().map_or(&[][..], |m| &m.1)), json!({ "warnings": enc.warnings }));
|
|
if main.len() > 1 {
|
|
out["files"] = main.iter().map(|(name, bytes)| merge(json!({ "name": name }), data(bytes))).collect();
|
|
}
|
|
if !linked.is_empty() {
|
|
out["linked"] = linked.iter().map(|(name, bytes)| json!({ "name": name, "dataBase64": vectorcraft_format::base64_encode(bytes) })).collect();
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
pub(super) fn export(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let path = str_param(p, "path");
|
|
if let Some(path) = path {
|
|
super::check_not_lossy_overwrite(s.doc()?, path, p, "document.export")?;
|
|
}
|
|
let f = writable("document.export", str_param(p, "format"), path)?;
|
|
let expanded = expand_presets(s, "document.export", p)?;
|
|
let p = &*expanded;
|
|
let doc = &*source(s, f, p, "document.export")?;
|
|
let enc = encode_all(doc, f.id, p)?;
|
|
write_encoded(path, &default_name(doc, f.extensions[0]), doc, &enc, json!({ "format": f.id, "warnings": enc.warnings }))
|
|
}
|
|
|
|
/// The document an export of `st` writes: the whole document, or with `selectedOnly` just the
|
|
/// selected objects, each in its layers and groups (a clipping group keeps its clipping path).
|
|
pub fn export_source<'a>(st: &'a DocState, p: &Value) -> Result<Cow<'a, Document>> {
|
|
if !bool_or(p, "selectedOnly", false) {
|
|
return Ok(Cow::Borrowed(&*st.doc));
|
|
}
|
|
let keep: HashSet<NodeId> = edit::roots_of(&st.doc, st.selection.in_paint_order(&st.doc)).into_iter().collect();
|
|
if keep.is_empty() {
|
|
return Err(bad("document.export", "select something to export (or turn off selectedOnly)"));
|
|
}
|
|
let mut d = Document::clone(&st.doc);
|
|
d.layers = st.doc.layers.iter().filter_map(|l| selected_part(l, &keep)).map(Arc::new).collect();
|
|
Ok(Cow::Owned(d))
|
|
}
|
|
|
|
/// `n` with only the parts in `keep` (whole, with everything inside them), `None` when none is.
|
|
fn selected_part(n: &Node, keep: &HashSet<NodeId>) -> Option<Node> {
|
|
if keep.contains(&n.id) {
|
|
return Some(n.clone());
|
|
}
|
|
let children = n.children()?;
|
|
let mut kept: Vec<Arc<Node>> = children.iter().filter_map(|c| selected_part(c, keep).map(Arc::new)).collect();
|
|
if kept.is_empty() {
|
|
return None;
|
|
}
|
|
let clip = matches!(n.kind, NodeKind::Group { clip: true, .. } | NodeKind::Layer { clip: true, .. });
|
|
if let Some(first) = children.first().filter(|f| clip && kept.first().is_none_or(|k| k.id != f.id)) {
|
|
kept.insert(0, first.clone());
|
|
}
|
|
let mut out = n.clone();
|
|
*out.children_mut()? = kept;
|
|
Some(out)
|
|
}
|
|
|
|
/// `doc` with only the art over `rect` (an artboard): objects whose visual bounds miss it are left
|
|
/// out, at any depth of layers and sublayers, and objects that reach it stay whole (the file's
|
|
/// bounds clip them), as DXF's crop leaves them. Symbol instances are measured by their art, and
|
|
/// the box of one whose art holds brushed strokes grows by as far as their brush art can paint
|
|
/// ([`SymbolBoxes::with_brush_art`]). An object that holds a symbol instance also stays when what
|
|
/// the canvas paints for it reaches `rect` ([`vectorcraft_render::painted_bounds`]: the effects in
|
|
/// the symbols' art and its own effects). A clipping layer keeps every object, its clipping path
|
|
/// among them. An artboard's file then holds its own art, not a whole document of artboards.
|
|
pub(super) fn art_over(doc: &Document, rect: vectorcraft_geom::Rect) -> Document {
|
|
fn kept(n: &Arc<Node>, doc: &Document, symbols: &SymbolBoxes, rect: vectorcraft_geom::Rect) -> Option<Arc<Node>> {
|
|
if matches!(n.kind, NodeKind::Layer { clip: false, .. }) {
|
|
let mut layer = Node::clone(n);
|
|
let children = layer.children_mut()?;
|
|
*children = children.iter().filter_map(|c| kept(c, doc, symbols, rect)).collect();
|
|
return Some(Arc::new(layer));
|
|
}
|
|
let over = |b: vectorcraft_geom::Rect| b.x0 <= rect.x1 && rect.x0 <= b.x1 && b.y0 <= rect.y1 && rect.y0 <= b.y1;
|
|
let mut holds_instance = false;
|
|
n.walk(&mut |c| holds_instance |= matches!(c.kind, NodeKind::SymbolInstance { .. }));
|
|
let painted = || holds_instance && vectorcraft_render::painted_bounds(doc, n).is_some_and(over);
|
|
(n.visual_bounds_with(symbols).is_none_or(over) || painted()).then(|| n.clone())
|
|
}
|
|
let symbols = doc.symbol_boxes().with_brush_art();
|
|
let mut d = doc.clone();
|
|
d.layers = doc.layers.iter().filter_map(|l| kept(l, doc, &symbols, rect)).collect();
|
|
d
|
|
}
|
|
|
|
/// `p` without its artboard choice, also inside its SVG options (for documents made of one
|
|
/// synthetic artboard, and for callers that pick the artboard themselves).
|
|
pub(super) fn without_artboards(p: &Value) -> Value {
|
|
let strip = |o: &mut serde_json::Map<String, Value>| ARTBOARD_PARAMS.iter().for_each(|k| _ = o.remove(*k));
|
|
let mut q = p.clone();
|
|
if let Some(o) = q.as_object_mut() {
|
|
strip(o);
|
|
if let Some(svg) = o.get_mut("svg").and_then(Value::as_object_mut) {
|
|
strip(svg);
|
|
}
|
|
}
|
|
q
|
|
}
|
|
|
|
/// `p` with the saved presets it names (PDF, flattener) written out as options, for the encoders,
|
|
/// which know only the built-in ones.
|
|
fn expand_presets<'a>(s: &Session, cmd: &str, p: &'a Value) -> Result<Cow<'a, Value>> {
|
|
super::pdf::expand_preset(s, cmd, p)
|
|
}
|
|
|
|
/// The document an export of `f` writes: the active one (see [`export_source`]), or for text
|
|
/// with `selectionOnly` the selected objects alone.
|
|
fn source<'a>(s: &'a mut Session, f: &Format, p: &Value, cmd: &str) -> Result<Cow<'a, Document>> {
|
|
if f.id == "txt" && bool_or(p, "selectionOnly", false) {
|
|
return Ok(Cow::Owned(selection(s, cmd)?.0));
|
|
}
|
|
export_source(s.doc()?, p)
|
|
}
|
|
|
|
/// The selected objects (not on template layers) alone on one layer, with one artboard: their
|
|
/// visual bounds.
|
|
fn selection(s: &mut Session, cmd: &str) -> Result<(Document, vectorcraft_geom::Rect)> {
|
|
let ids = edit::selected_roots(s)?;
|
|
let st = s.doc()?;
|
|
let is_template = |id| st.doc.node(id).is_some_and(|l| matches!(l.kind, NodeKind::Layer { template: true, .. }));
|
|
// On a template layer or sublayer, at any depth.
|
|
let on_template = |id| st.doc.ancestry(id).is_some_and(|a| a.into_iter().any(is_template));
|
|
let ids: Vec<NodeId> = ids.into_iter().filter(|id| !on_template(*id)).collect();
|
|
isolated(&st.doc, &ids, "Selection").ok_or_else(|| bad(cmd, "select something to export"))
|
|
}
|
|
|
|
/// Objects `ids` of `doc` alone on one layer (in that order, back to front), with one artboard
|
|
/// named `name`: their visual bounds, symbol instances measured by their art (`None` when they
|
|
/// have none).
|
|
pub(crate) fn isolated(doc: &Document, ids: &[NodeId], name: &str) -> Option<(Document, vectorcraft_geom::Rect)> {
|
|
let nodes: Vec<Arc<Node>> = ids.iter().filter_map(|id| doc.node(*id).cloned().map(Arc::new)).collect();
|
|
let symbols = doc.symbol_boxes();
|
|
let bounds = nodes.iter().filter_map(|n| n.visual_bounds_with(&symbols)).reduce(|a, b| a.union(b))?;
|
|
let mut d = single_artboard(doc, bounds, name);
|
|
let mut layer = Node::layer(d.alloc_id(), name, vectorcraft_doc::LayerColor::Preset(0));
|
|
if let Some(ch) = layer.children_mut() {
|
|
*ch = nodes;
|
|
}
|
|
d.layers = vec![Arc::new(layer)];
|
|
Some((d, bounds))
|
|
}
|
|
|
|
/// File → Export Selection: the selected objects alone, cropped to their visual bounds. Objects on
|
|
/// template layers are guides, not artwork, and are left out.
|
|
pub(super) fn export_selection(s: &mut Session, p: &Value) -> Result<Value> {
|
|
const C: &str = "document.exportSelection";
|
|
if let Some(path) = str_param(p, "path") {
|
|
super::check_not_lossy_overwrite(s.doc()?, path, p, C)?;
|
|
}
|
|
let path = str_param(p, "path");
|
|
let f = writable(C, str_param(p, "format"), path)?;
|
|
let expanded = expand_presets(s, C, p)?;
|
|
let p = &*expanded;
|
|
let (d, bounds) = selection(s, C)?;
|
|
// The encoder's warnings (options accepted but not applied, features approximated or left
|
|
// out), as document.export reports them.
|
|
let (bytes, warnings) = encode_with_warnings(&d, f.id, &without_artboards(p))?;
|
|
write_or_return(path, &bytes, json!({ "format": f.id, "warnings": warnings, "bounds": [bounds.x0, bounds.y0, bounds.width(), bounds.height()] }))
|
|
}
|
|
|
|
/// File → Save for Office Documents: one artboard as a PNG at `ppi`, on white unless
|
|
/// `transparent`.
|
|
pub(super) fn export_for_office(s: &mut Session, p: &Value) -> Result<Value> {
|
|
const C: &str = "document.exportForOffice";
|
|
if let Some(path) = str_param(p, "path") {
|
|
super::check_not_lossy_overwrite(s.doc()?, path, p, C)?;
|
|
}
|
|
let path = str_param(p, "path");
|
|
let ppi = f64_or(p, "ppi", 150.0);
|
|
let doc = &s.doc()?.doc;
|
|
let artboard = match p.get("artboard") {
|
|
None | Some(Value::Null) => 0,
|
|
Some(v) => v.as_u64().and_then(|i| usize::try_from(i).ok()).ok_or_else(|| bad(C, "artboard is a 0-based index"))?,
|
|
};
|
|
if artboard >= doc.artboards.len() {
|
|
return Err(bad(C, format!("no artboard {artboard} (0-based; the document has {})", doc.artboards.len())));
|
|
}
|
|
let background = if bool_or(p, "transparent", false) { "transparent" } else { "white" };
|
|
let bytes = encode(doc, "png", &json!({ "ppi": ppi, "background": background, "artboard": artboard }))?;
|
|
// The pixel size, from the PNG header.
|
|
let side = |at: usize| bytes.get(at..at + 4).and_then(|b| b.try_into().ok()).map_or(0, u32::from_be_bytes);
|
|
write_or_return(path, &bytes, json!({ "width": side(16), "height": side(20) }))
|
|
}
|