mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 03:51:16 +00:00
EMF and WMF import now plays the records with the shared craft-metafile reader (storytold/craft-libs#778, at the craft-libs rev the SVG, EPS and DXF crates already use) and maps its picture onto the document here: paths with their fills, strokes, caps, joins, miter limits and dashes, clipping groups, images and point type. The EMF and WMF record players in crates/metafile/src/import and the DIB decoder they used are removed. The writers are unchanged, and the PICT reader keeps its own player. What changes for a file being opened, placed or pasted, and for an EPS file's Windows metafile preview (read when its PostScript can't be): - An EMF that draws with EMF+ records is read from them. It opened empty, with a warning that EMF+ is not read. A file with both kinds of record is read from its EMF+ records too. - Hatched and pattern brushes fill with the average color of the brush's tile, translucent where the tile has gaps. Hatches filled with the hatch color and pattern brushes with gray. - Translucent fills, strokes and EMF+ text keep their alpha as opacity. - Text keeps a stretch across from the world transform; it kept only the turn. Text the file clips to a rectangle is clipped to it, and an underlined or struck-through font gives underlined or struck-through type. - A clip path whose bounding box holds the whole picture is kept unless it is that rectangle; a diamond or a bow tie around the picture was dropped. - Images drawn with SRCAND, SRCPAINT or SRCINVERT are drawn as they look over white, and images drawn with other raster operations are drawn as a plain copy. They were left out with a warning. - Excluded clip rectangles and the other clip region modes are applied. A clip path combined with RGN_OR, RGN_XOR or RGN_DIFF is ignored without the warning it gave. - An image past the reader's size or pixel limits, or one that can't be decoded, is left out without a warning of its own. A file past the reader's limits (more than 32 nested clip paths, 200,000 drawn items, 256 MiB held) is read up to that point without a warning. - An EMF record whose size is not a multiple of 4 ends the file, with the "ends early" warning. Tests: an EMF+ only picture imports its fill (it failed before), a dual EMF+ file draws its translucent EMF+ fill and not its fallback, hatch and pattern brush colors, clipped and underlined text, excluded clip rectangles, a shared clipping group, an odd record size, WMF at 1440 units an inch without a placeable header and at its own units with an inverted box, an EMF without a frame, diamond and bow-tie clips, text with a stretch, clips nested past the stack and 200,000 clip records; the fuzz test gains random EMF+ records.
137 lines
5.0 KiB
Rust
137 lines
5.0 KiB
Rust
//! Windows metafiles: hand-written EMF and WMF writers and readers.
|
|
//!
|
|
//! [`export`] writes the visible art over one region of a document (an artboard) as a metafile:
|
|
//!
|
|
//! - **EMF**: paths keep their Bézier curves, fills are solid brushes, strokes geometric pens with
|
|
//! their width, caps, joins, miter limit and dashes; strokes a pen can't draw (aligned inside or
|
|
//! outside, width profiles, arrowheads) are their filled outlines and brush strokes their brush
|
|
//! art. Clipping masks clip, type is written as glyph outlines, images keep their pixels (with
|
|
//! their transparency), gradients become images clipped to what they paint and pattern fills
|
|
//! their tiles clipped to the shape. The header's frame is the region in 0.01 mm.
|
|
//! - **WMF**: the same art with curves flattened into polygons and 16-bit coordinates, behind a
|
|
//! placeable header that gives the size; WMF has no clipping paths, transparency, dashes on wide
|
|
//! lines or gradients, so clipped art is written whole, images over white, dashes as separate
|
|
//! lines and gradients and patterns as one colour (each loss comes back as a warning).
|
|
//!
|
|
//! [`import`] reads either format (an EMF with its EMF+ records too, through `craft_metafile`)
|
|
//! into a document of one artboard (the picture's frame) and one layer: paths with fills and
|
|
//! strokes, clipping, images and text. Records it doesn't read are skipped, with one warning.
|
|
//! [`import_pict`] reads QuickDraw pictures (PICT, versions 1 and 2) into the same kind of
|
|
//! document.
|
|
//!
|
|
//! - `bytes`: little-endian writing.
|
|
//! - `dib`: device-independent bitmaps for the writers.
|
|
//! - `scene`: the document walk both writers share.
|
|
//! - `emf`, `wmf`: the writers.
|
|
//! - `import`: the readers.
|
|
|
|
mod bytes;
|
|
mod dib;
|
|
mod emf;
|
|
mod import;
|
|
mod scene;
|
|
mod wmf;
|
|
|
|
use vectorcraft_doc::Document;
|
|
use vectorcraft_geom::Rect;
|
|
|
|
/// A Windows metafile format.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Kind {
|
|
/// Enhanced Metafile (32-bit coordinates, Bézier curves, paths and clipping).
|
|
Emf,
|
|
/// Windows Metafile with a placeable header (16-bit coordinates, polygons).
|
|
Wmf,
|
|
}
|
|
|
|
impl Kind {
|
|
/// The format id (`emf`, `wmf`).
|
|
pub fn id(self) -> &'static str {
|
|
match self {
|
|
Self::Emf => "emf",
|
|
Self::Wmf => "wmf",
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The metafile format `bytes` start like, if any.
|
|
pub fn sniff(bytes: &[u8]) -> Option<Kind> {
|
|
if emf::is_emf(bytes) {
|
|
Some(Kind::Emf)
|
|
} else if wmf::is_wmf(bytes) {
|
|
Some(Kind::Wmf)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
/// A written metafile.
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct Output {
|
|
pub bytes: Vec<u8>,
|
|
/// What was approximated or left out.
|
|
pub warnings: Vec<String>,
|
|
}
|
|
|
|
/// Write the visible art of `doc` over `region` (document space: its top-left corner is the
|
|
/// picture's origin, its size the picture's) as `kind`. Live geometry effects are applied first;
|
|
/// template layers, hidden objects and guides are left out. Raster effects should be turned into
|
|
/// images before (the engine does); the ones left are reported and left out.
|
|
pub fn export(doc: &Document, region: Rect, kind: Kind) -> Result<Output, String> {
|
|
let r = region.abs();
|
|
if ![r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()) || r.width() <= 0.0 || r.height() <= 0.0 {
|
|
return Err("the export region is empty".into());
|
|
}
|
|
let baked = vectorcraft_effects::bake_document(doc);
|
|
let doc = baked.as_ref().unwrap_or(doc);
|
|
let (ops, mut warnings) = scene::Scene::new(doc, r, kind == Kind::Emf).run();
|
|
let bytes = match kind {
|
|
Kind::Emf => emf::write(&ops, r, doc.info_title()),
|
|
Kind::Wmf => wmf::write(&ops, r, &mut warnings),
|
|
};
|
|
Ok(Output { bytes, warnings })
|
|
}
|
|
|
|
/// A metafile read into a document.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Imported {
|
|
pub document: Document,
|
|
pub kind: Kind,
|
|
/// What was approximated or skipped.
|
|
pub warnings: Vec<String>,
|
|
}
|
|
|
|
/// Read an EMF or WMF file (with or without a placeable header) into a document whose one
|
|
/// artboard is the picture's frame.
|
|
pub fn import(bytes: &[u8]) -> Result<Imported, String> {
|
|
let mut i = import::import(bytes)?;
|
|
i.document.limit_depth_noting(&mut i.warnings);
|
|
Ok(i)
|
|
}
|
|
|
|
/// A QuickDraw picture read into a document.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Picture {
|
|
pub document: Document,
|
|
/// What was approximated or skipped.
|
|
pub warnings: Vec<String>,
|
|
}
|
|
|
|
/// Do `bytes` hold a version 2 QuickDraw picture, after a PICT file's 512 bytes of application
|
|
/// data or on their own? A version 1 picture is known by its file's name.
|
|
pub fn is_pict(bytes: &[u8]) -> bool {
|
|
import::pict::is_pict(bytes)
|
|
}
|
|
|
|
/// Read a QuickDraw picture (version 1 or 2; a PICT file, or a picture on its own) into a document
|
|
/// whose one artboard is the picture's frame.
|
|
pub fn import_pict(bytes: &[u8]) -> Result<Picture, String> {
|
|
let i = import::pict::play(bytes)?;
|
|
let mut p = Picture { document: i.document, warnings: i.warnings };
|
|
p.document.limit_depth_noting(&mut p.warnings);
|
|
Ok(p)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|