Files
vectorcraft/crates/metafile/src/encode.rs
dotCooCoo 81d4dcc6ea File formats: export .cmx files
document.export and Export As write .cmx exchange files with 32-bit
coordinates through craft-cdr's write_cmx, in the layouts the .cmx
reader reads. The chosen artboard is the file's page, or with Use
Artboards each chosen artboard is a page of one file, and each layer with
art on a page is a layer of that page.

- vectorcraft-metafile gets painted_layers: the WMF writer's walk of the
  visible art, layer by layer, with each fill's and stroke's solid paint,
  its miter limit and the swatches of the document and of the documents
  it places.
- The export keeps CMYK, RGB, gray, spot colors at their tints and
  Registration; type is written as outlines, and gradients and patterns
  as one color. The warnings name images, clipping masks, transparency,
  art past a page's edge, dash offsets, dashes aligned to corners, miter
  limits, and each loss the writer reports: nonzero fills written
  even-odd, long paths, rounded dashes, shortened names, low spot tints,
  and spot colors without a name or a known color.
2026-11-27 19:12:32 -08:00

95 lines
4.5 KiB
Rust

//! Translate VectorCraft's drawing operations to the shared metafile writers.
use craft_metafile::{
Cap, Join,
write::{self, Dash, Op, Pen},
};
use vectorcraft_doc::{LineCap, LineJoin};
use vectorcraft_geom::{FillRule, Rect};
use crate::{Kind, scene};
pub(crate) fn write(ops: Vec<scene::Op>, region: Rect, kind: Kind, title: &str, warnings: &mut Vec<String>) -> Result<Vec<u8>, String> {
let ops: Vec<_> = ops.into_iter().map(|op| convert(op, kind)).collect();
let invalid = || "the export region is empty".to_string();
match kind {
Kind::Emf => write::emf(&ops, region, "VectorCraft", title).ok_or_else(invalid),
Kind::Wmf => {
let out = write::wmf(&ops, region).ok_or_else(invalid)?;
for (lost, warning) in [
(out.translucent_images, "WMF has no transparency: images are written over white"),
(out.turned_images, "WMF can't rotate or skew images: they are written upright, filling their bounds"),
(out.clips_ignored, "clipping masks are not applied: the art they clip is written whole"),
(out.oversized_images, "WMF images with a side over 32,767 pixels were left out"),
] {
if lost && !warnings.iter().any(|w| w == warning) {
warnings.push(warning.to_string());
}
}
Ok(out.bytes)
}
}
}
fn convert(op: scene::Op, kind: Kind) -> Op {
match op {
scene::Op::Fill { path, rule, rgb, .. } => Op::Fill { path, even_odd: rule == FillRule::EvenOdd, rgb },
scene::Op::Stroke { mut path, pen } => {
let dash = pen.dash.and_then(|dash| {
let native = Dash { pattern: dash.pattern.clone(), offset: dash.offset };
if kind == Kind::Emf && native.emf_pen_draws() {
Some(native)
} else {
// Keep VectorCraft's own dash placement for strokes that cannot use an
// EMF native pen, including adjusted dashes around corners in WMF.
if let Some(dashed) = vectorcraft_effects::stroke::dash(&path, &dash) {
path = dashed.path;
}
None
}
});
let cap = match pen.cap {
LineCap::Butt => Cap::Flat,
LineCap::Round => Cap::Round,
LineCap::Square => Cap::Square,
};
let join = match pen.join {
LineJoin::Miter => Join::Miter,
LineJoin::Round => Join::Round,
LineJoin::Bevel => Join::Bevel,
};
Op::Stroke { path, pen: Pen { rgb: pen.rgb, width: pen.width, cap, join, miter: pen.miter, dash } }
}
scene::Op::Image { image, xf, opacity } => Op::Image { image, xf, opacity },
scene::Op::Clip { path, rule } => Op::Clip { path, even_odd: rule == FillRule::EvenOdd },
scene::Op::Unclip => Op::Unclip,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nonnative_metafile_pens_keep_vectorcraft_corner_alignment() {
let mut path = kurbo::BezPath::new();
path.move_to((7.0, 8.0));
path.line_to((42.0, 8.0));
path.line_to((42.0, 45.0));
path.line_to((7.0, 45.0));
path.close_path();
let dash = vectorcraft_doc::Dash { pattern: vec![5.0, 3.0], offset: 1.25, align_corners: true };
let plain = scene::Pen { rgb: [30, 60, 90], paint: None, width: 1.0, cap: LineCap::Round, join: LineJoin::Round, miter: 4.0, dash: None };
let with_dash = scene::Pen { dash: Some(dash.clone()), ..plain.clone() };
let segments = vectorcraft_effects::stroke::dash(&path, &dash).unwrap().path;
let region = Rect::new(0.0, 0.0, 50.0, 50.0);
for kind in [Kind::Emf, Kind::Wmf] {
let actual = write(vec![scene::Op::Stroke { path: path.clone(), pen: with_dash.clone() }], region, kind, "", &mut Vec::new()).unwrap();
let already_dashed =
write(vec![scene::Op::Stroke { path: segments.clone(), pen: plain.clone() }], region, kind, "", &mut Vec::new()).unwrap();
let unsplit = write(vec![scene::Op::Stroke { path: path.clone(), pen: plain.clone() }], region, kind, "", &mut Vec::new()).unwrap();
assert_eq!(actual, already_dashed, "the wire writer must receive the app-aligned segments: {kind:?}");
assert_ne!(actual, unsplit, "the fixture must distinguish a lost dash pattern: {kind:?}");
}
}
}