Files
vectorcraft/crates/metafile/src/encode.rs
T
dotCooCoo ca2c567f74 PICT: Export As writes QuickDraw pictures
File › Export As and document.export {format: "pict"} write a version 2
QuickDraw picture (.pct) of an artboard, or one per artboard with Use
Artboards, with the extended header at up to 16 device units a point.

- Fills are PaintPoly polygons. A path of several figures is one
  polygon that runs back along the line between its figures, so the
  even-odd fill QuickDraw gives polygons keeps its holes. Non-zero fills
  are first resolved into figures that don't overlap, and art is cut to
  the picture's frame so every coordinate fits 16 bits. A fill too
  complex to resolve is written as it is, with a warning.
- Strokes are written as their filled outlines (line_outline, with
  dashes, caps, joins and miter limit).
- Images are DirectBitsRect pixel maps in tiles of at most 1,024
  pixels, rows packed with PackBits, composited over white. Mirrored and
  quarter-turned images keep their placement, and an image reaching far
  past the frame keeps its columns in place.
- Clipping masks, gradients, patterns and transparency are written as
  WMF writes them, with the same warnings. Artboards over 32,767 points
  on a side are refused.

The writer follows the opcode tables of Inside Macintosh: Imaging With
QuickDraw, and the tests read every picture back through the PICT
reader.
2026-12-05 05:52:10 -08:00

113 lines
5.6 KiB
Rust

//! Translate VectorCraft's drawing operations to the shared metafile writers (EMF, WMF) and to
//! the PICT writer.
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 invalid = || "the export region is empty".to_string();
let mut note = |lost: bool, warning: &str| {
if lost && !warnings.iter().any(|w| w == warning) {
warnings.push(warning.to_string());
}
};
match kind {
Kind::Pict => {
crate::pict::check_region(region)?;
let out = crate::pict::write(&ops, region);
note(out.translucent_images, "PICT has no transparency: images are written over white");
note(out.turned_images, "PICT turns images only by quarter turns: other turns and skews are written upright, filling their bounds");
note(out.split_fills, "fills with more than 16,381 points are written as several polygons, which may fill their holes");
note(out.thinned_fills, "outlines with more than 16,379 points are written with fewer points");
note(out.unresolved_fills, "non-zero fills too complex to resolve are written even-odd, which may leave overlaps unfilled");
note(out.uncut_fills, "fills too complex to cut to the artboard that reach far outside it may be written distorted");
Ok(out.bytes)
}
Kind::Emf => {
let ops: Vec<_> = ops.into_iter().map(|op| convert(op, kind)).collect();
write::emf(&ops, region, "VectorCraft", title).ok_or_else(invalid)
}
Kind::Wmf => {
let ops: Vec<_> = ops.into_iter().map(|op| convert(op, kind)).collect();
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"),
] {
note(lost, warning);
}
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:?}");
}
}
}