diff --git a/Cargo.lock b/Cargo.lock index 6a602ed891..bc8de2d7fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1251,7 +1251,7 @@ dependencies = [ [[package]] name = "craft-metafile" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d7c9632bfb3040cf45db394e7500cc93c81c49e0#d7c9632bfb3040cf45db394e7500cc93c81c49e0" +source = "git+https://github.com/storytold/craft-libs?rev=0bf4cb99dabb6b6d9335771ed398e999387b0d62#0bf4cb99dabb6b6d9335771ed398e999387b0d62" dependencies = [ "encoding_rs", "image", diff --git a/Cargo.toml b/Cargo.toml index 6056916d75..7b6d617b78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,8 @@ craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "068e079c7b craft-plotter = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } # WMF, EMF and EMF+ reader (craft-libs#778). -craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } +# WMF, EMF and EMF+ reader (craft-libs#778). +craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "0bf4cb99dabb6b6d9335771ed398e999387b0d62" } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "ba25ccd233934d1b2f74e3c0f235168bf5f13ef3" } craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } diff --git a/crates/metafile/Cargo.toml b/crates/metafile/Cargo.toml index 408e6166a1..2211b64b58 100644 --- a/crates/metafile/Cargo.toml +++ b/crates/metafile/Cargo.toml @@ -3,7 +3,7 @@ name = "vectorcraft-metafile" version.workspace = true edition.workspace = true license.workspace = true -description = "VectorCraft metafiles: hand-written EMF and WMF writers, EMF, EMF+ and WMF import through craft-metafile, and a QuickDraw picture (PICT) reader" +description = "VectorCraft metafiles: hand-written EMF and WMF writers, and EMF, EMF+, WMF and QuickDraw PICT import through craft-metafile" [lints] workspace = true diff --git a/crates/metafile/src/dib.rs b/crates/metafile/src/dib.rs index b104304d7f..c85289a791 100644 --- a/crates/metafile/src/dib.rs +++ b/crates/metafile/src/dib.rs @@ -7,14 +7,6 @@ use vectorcraft_doc::resample::{Resample, resized}; const MAX_SIDE: u32 = 8192; /// Most pixels an image the writers store holds. const MAX_PIXELS: u64 = 1 << 24; -/// Most pixels along a side, and in all, of a bitmap the PICT reader decodes. -pub(crate) const MAX_READ_SIDE: u32 = 1 << 15; -pub(crate) const MAX_READ_PIXELS: u64 = 1 << 26; -/// Most pixels the bitmaps of one file may decode to together (four of the largest one), so a small -/// file of many image records can't cost minutes of decoding (#3600). -pub(crate) const MAX_FILE_PIXELS: u64 = 1 << 28; -/// What [`charge`] says of a bitmap past what is left of the file's budget. -pub(crate) const OVER_BUDGET: &str = "the bitmaps hold more pixels than one file may"; const BI_RGB: u32 = 0; @@ -115,13 +107,6 @@ pub(crate) fn bgra32(img: &Rgba, opacity: f32) -> (Vec, Vec) { (header(img.width, img.height, 32, bits.len()), bits) } -/// Take a bitmap of `w` × `h` from what is `left` of the file's budget, before it is decoded. -pub(crate) fn charge(w: u32, h: u32, left: &mut u64) -> Result<(), String> { - let n = u64::from(w) * u64::from(h); - *left = left.checked_sub(n).ok_or(OVER_BUDGET)?; - Ok(()) -} - #[cfg(test)] mod tests { use super::*; @@ -132,16 +117,6 @@ mod tests { Rgba { width: 3, height: 2, pixels } } - #[test] - fn bitmaps_share_the_file_budget() { - // #3600: each bitmap is charged before it is decoded, and one past what is left is refused. - let mut left = 6 + 5; - assert!(charge(3, 2, &mut left).is_ok()); - assert_eq!(left, 5); - assert_eq!(charge(3, 2, &mut left).unwrap_err(), OVER_BUDGET); - assert_eq!(left, 5, "a refused bitmap costs nothing"); - } - #[test] fn rgb24_is_bottom_up_bgr_over_white() { let (bmi, bits) = rgb24(&sample(), 1.0); diff --git a/crates/metafile/src/import.rs b/crates/metafile/src/import.rs index c87dd83d72..63ba902cef 100644 --- a/crates/metafile/src/import.rs +++ b/crates/metafile/src/import.rs @@ -1,27 +1,14 @@ -//! The metafile readers. EMF (with its EMF+ records) and WMF files are read by `craft_metafile`, -//! and `picture` turns what they draw into document objects. QuickDraw pictures (`pict`) play their -//! opcodes on a [`Player`], a small model of the drawing state (pen, brush, font, the clip), which -//! turns what they draw into document objects. +//! The metafile readers. EMF (with its EMF+ records) and WMF files, and QuickDraw pictures (PICT), +//! are read by `craft_metafile`, and `picture` turns what they draw into document objects. //! //! Fills and strokes become paths with a fill and a stroke; lines drawn one after another with the //! same pen join into one path. Clips become clipping groups around what they clip. Images keep //! their pixels (as PNG), text becomes point type in the font the file names. -pub(crate) mod pict; mod picture; -use std::sync::Arc; - use vectorcraft_color::{Color, Paint}; -use vectorcraft_doc::clipnest::{Clip, Drawn, MAX_NEST, deep_clips, nest}; -use vectorcraft_doc::{ - Appearance, AppearanceItem, CharStyle, Dash, Document, FillLayer, ImageBlob, ImageObject, Justify, LayerColor, LineCap, LineJoin, Node, NodeKind, - StrokeLayer, TextObject, -}; -use vectorcraft_geom::{Affine, BezPath, FillRule, PathData, PathEl, Point, Rect, Shape, shapes}; - -use crate::dib::{self, Rgba}; -use crate::{Imported, Kind}; +use vectorcraft_geom::{BezPath, PathEl, Point, Rect, Shape}; /// Most objects an import makes (a hostile file can't exhaust memory with them). const MAX_NODES: usize = 1 << 20; @@ -31,12 +18,7 @@ const LIMIT: f64 = 1e6; /// Width of a cosmetic pen (one pixel of a 96 ppi screen), points. const HAIRLINE: f64 = 0.75; -/// Raster operations. -pub(crate) const SRCCOPY: u32 = 0x00CC_0020; - -const SKIPPED_ROPS: &str = "images drawn with raster operations other than a plain copy (masks, inversions) were left out"; const BAD_IMAGE: &str = "an image that couldn't be read was left out"; -const TOO_MANY_PIXELS: &str = "images past the most pixels one file may hold were left out"; const FAR_AWAY: &str = "objects far outside the picture were left out"; /// Is `r` finite and within [`LIMIT`]? @@ -65,8 +47,8 @@ fn around(path: &BezPath, frame: Rect) -> bool { let (p, start) = match e { PathEl::MoveTo(p) => (*p, true), PathEl::LineTo(p) => (*p, false), - PathEl::ClosePath => continue, - PathEl::QuadTo(..) | PathEl::CurveTo(..) => return false, + PathEl::ClosePath => break, + _ => return false, }; let Some(i) = corners.iter().position(|c| near(*c, p)) else { return false }; if start { @@ -96,365 +78,17 @@ fn around(path: &BezPath, frame: Rect) -> bool { seen.iter().all(|s| *s) && b.contains(frame.origin()) && b.contains(Point::new(frame.x1, frame.y1)) } -/// A pen: its style flags (`PS_*`), width in logical units, colour and dash entries. -#[derive(Clone, Debug, PartialEq)] -pub(crate) struct PenObj { - pub style: u32, - pub width: f64, - pub color: [u8; 3], - /// PS_USERSTYLE entries, logical units. - pub entries: Vec, -} - -impl PenObj { - pub fn solid(color: [u8; 3]) -> Self { - Self { style: 0, width: 0.0, color, entries: vec![] } - } -} - -pub(crate) const PS_NULL: u32 = 5; -const PS_GEOMETRIC: u32 = 0x0001_0000; - -/// A font: its height in logical units (negative: the character height, positive: the cell -/// height), escapement (tenths of a degree), weight, italics and face. -#[derive(Clone, Debug, Default, PartialEq)] -pub(crate) struct FontObj { - pub height: f64, - pub escapement: f64, - pub weight: i32, - pub italic: bool, - pub face: String, -} - -/// The drawing state. -#[derive(Clone, Debug)] -struct Dc { - pen: PenObj, - /// The brush's color. - brush: [u8; 3], - font: FontObj, - text_color: [u8; 3], - text_align: u32, - fill_rule: FillRule, - miter: f64, - clips: Vec>, -} - -impl Default for Dc { - fn default() -> Self { - Self { - pen: PenObj::solid([0; 3]), - brush: [255; 3], - font: FontObj::default(), - text_color: [0; 3], - text_align: 0, - fill_rule: FillRule::EvenOdd, - miter: 10.0, - clips: vec![], - } - } -} - -pub(crate) struct Player { - kind: Kind, - /// Picture units → document points. - dev_to_doc: Affine, - dc: Dc, - /// The artboard (document space): clips holding all of it clip nothing worth keeping. - frame: Rect, - /// Lines drawn one after another, not drawn yet. - pending: BezPath, - out: Drawn, - doc: Document, - warnings: Vec, - next_clip: u32, - /// Pixels the file's bitmaps may still decode to ([`dib::MAX_FILE_PIXELS`]). - pub pixels_left: u64, -} - fn rgb(c: [u8; 3]) -> Paint { Paint::solid(Color::rgb8(c[0], c[1], c[2])) } -/// A rectangle with elliptical corners `rx` × `ry`. -pub(crate) fn round_rect(r: Rect, rx: f64, ry: f64) -> BezPath { - let r = r.abs(); - let (rx, ry) = ((rx / 2.0).abs().min(r.width() / 2.0), (ry / 2.0).abs().min(r.height() / 2.0)); - if rx <= 0.0 || ry <= 0.0 { - return shapes::rectangle(r).to_bezpath(); - } - // A rectangle with round corners of radius `ry`, squeezed across. - let s = ry / rx; - let squeezed = Rect::new(r.x0 * s, r.y0, r.x1 * s, r.y1); - let mut bp = shapes::rounded_rectangle(squeezed, ry).to_bezpath(); - bp.apply_affine(Affine::scale_non_uniform(1.0 / s, 1.0)); - bp -} - -impl Player { - fn new(kind: Kind, dev_to_doc: Affine, artboard: Rect) -> Self { - let side = |v: f64| if v.is_finite() { v.clamp(1.0, LIMIT) } else { 1.0 }; - let (w, h) = (side(artboard.width()), side(artboard.height())); - let mut doc = Document::new(w, h); - doc.layers.clear(); - Self { - kind, - dev_to_doc, - dc: Dc::default(), - frame: artboard, - pending: BezPath::new(), - out: vec![], - doc, - warnings: vec![], - next_clip: 0, - pixels_left: dib::MAX_FILE_PIXELS, - } - } - - pub fn warn(&mut self, w: &str) { - if !self.warnings.iter().any(|x| x == w) { - self.warnings.push(w.to_string()); - } - } - - // ---------- mapping ---------- - - /// Picture units → document. - fn xf(&self) -> Affine { - self.dev_to_doc - } - - /// How much picture lengths grow in the document. - fn scale(&self) -> f64 { - let s = self.xf().determinant().abs().sqrt(); - if s.is_finite() { s } else { 0.0 } - } - - // ---------- output ---------- - - fn push(&mut self, mut node: Node) { - if self.out.len() >= MAX_NODES { - return; - } - if !node.visual_bounds().is_none_or(sane) { - return self.warn(FAR_AWAY); - } - node.id = self.doc.alloc_id(); - self.out.push((self.dc.clips.clone(), node)); - } - - fn fill_paint(&self) -> Paint { - rgb(self.dc.brush) - } - - fn stroke_layer(&self) -> Option { - let p = &self.dc.pen; - let kind = p.style & 0xf; - if kind == PS_NULL { - return None; - } - // Geometric pens, and other pens wider than 1, have a width in logical units; the others - // are one pixel wide. - let w = p.width * self.scale(); - let width = if w > 0.0 && w.is_finite() && (p.style & PS_GEOMETRIC != 0 || p.width > 1.0) { w } else { HAIRLINE }; - let mut st = StrokeLayer::new(rgb(p.color), width); - // Round caps and joins unless the style asks for others. - st.cap = match p.style & 0xf00 { - 0x100 => LineCap::Square, - 0x200 => LineCap::Butt, - _ => LineCap::Round, - }; - st.join = match p.style & 0xf000 { - 0x1000 => LineJoin::Bevel, - 0x2000 => LineJoin::Miter, - _ => LineJoin::Round, - }; - st.miter_limit = self.dc.miter; - let unit = width.max(HAIRLINE); - let pattern: Vec = match kind { - 1 => vec![3.0 * unit, unit], - 2 => vec![unit, unit], - 3 => vec![3.0 * unit, unit, unit, unit], - 4 => vec![3.0 * unit, unit, unit, unit, unit, unit], - 7 => p.entries.iter().map(|e| e * self.scale()).collect(), - _ => vec![], - }; - let dash = Dash { pattern, offset: 0.0, align_corners: false }; - st.dash = dash.is_dashed().then_some(dash); - Some(st) - } - - /// Paint `bp` (document space) with the brush and the pen. - fn draw(&mut self, bp: BezPath, fill: bool, stroke: bool) { - if bp.elements().is_empty() { - return; - } - let mut items = vec![]; - if fill { - items.push(AppearanceItem::Fill(FillLayer::new(self.fill_paint()))); - } - if stroke && let Some(st) = self.stroke_layer() { - items.push(AppearanceItem::Stroke(st)); - } - if items.is_empty() { - return; - } - let mut n = Node::path(vectorcraft_doc::NodeId(0), PathData::from_bezpath(&bp), Appearance { items, ..Appearance::default() }); - if let NodeKind::Path { rule, .. } = &mut n.kind { - *rule = self.dc.fill_rule; - } - self.push(n); - } - - /// Draw the lines drawn one after another so far. - pub fn flush(&mut self) { - if !self.pending.elements().is_empty() { - let bp = std::mem::take(&mut self.pending); - self.draw(bp, false, true); - } - } - - // ---------- clips ---------- - - fn push_clip(&mut self, path: BezPath, rule: FillRule, replace: bool) { - if replace { - self.dc.clips.clear(); - } - if !sane(path.bounding_box()) { - return self.warn(FAR_AWAY); - } - if around(&path, self.frame) { - return; - } - if self.dc.clips.len() >= MAX_NEST { - return self.warn(&deep_clips()); - } - self.next_clip += 1; - self.dc.clips.push(Arc::new(Clip { id: self.next_clip, region: Some((path, rule)) })); - } - - // ---------- images and text ---------- - - /// An image whose source rectangle `src` (pixels, from the top-left) is drawn into `dest` - /// (picture units: origin and signed size) with raster operation `rop`, at `opacity`. - pub fn image(&mut self, img: Result, src: Rect, dest: Rect, rop: u32, opacity: f64) { - if rop != SRCCOPY { - self.warn(SKIPPED_ROPS); - return; - } - let img = match img { - Ok(i) => i, - Err(e) if e == dib::OVER_BUDGET => return self.warn(TOO_MANY_PIXELS), - Err(_) => return self.warn(BAD_IMAGE), - }; - let img = crop(img, src); - if img.width == 0 || img.height == 0 { - return; - } - let (w, h) = (f64::from(img.width), f64::from(img.height)); - self.image_xf(img, Affine::translate((dest.x0, dest.y0)) * Affine::scale_non_uniform(dest.width() / w, dest.height() / h), opacity); - } - - /// An image placed by `xf` (its pixels, from the top-left, → picture units), at `opacity`. An - /// image that `xf` flattens or sends past finite numbers is left out, and adds nothing to the - /// document's images. - pub(crate) fn image_xf(&mut self, img: Rgba, xf: Affine, opacity: f64) { - if img.width == 0 || img.height == 0 { - return; - } - let xf = self.xf() * xf; - if !xf.as_coeffs().iter().all(|v| v.is_finite()) || xf.determinant().abs() < 1e-12 { - return; - } - let Ok(png) = img.to_png() else { return self.warn(BAD_IMAGE) }; - let blob = ImageBlob::new("image/png", png); - let key = blob.content_key(); - self.doc.images.entry(key.clone()).or_insert(blob); - let im = ImageObject { key, width: img.width, height: img.height, xf, link: None, placement: Default::default(), pixels: None }; - let mut n = Node::new(vectorcraft_doc::NodeId(0), NodeKind::Image(Box::new(im))); - n.opacity = opacity.clamp(0.0, 1.0) as f32; - self.push(n); - } - - /// Text at `at` (picture units) in the current font. - pub fn text(&mut self, at: Point, s: &str) { - let s: String = s.chars().filter(|c| *c != '\0').collect(); - if s.trim().is_empty() { - return; - } - let align = self.dc.text_align; - let f = self.dc.font.clone(); - let k = self.scale(); - // A negative height is the em; a positive one the cell (the em and the internal leading). - let em = if f.height < 0.0 { -f.height } else { f.height * 0.85 }; - let size = if em > 0.0 && (em * k).is_finite() { (em * k).clamp(0.5, 5000.0) } else { 12.0 }; - let mut style = CharStyle { size, fill: rgb(self.dc.text_color), stroke: Paint::None, ..CharStyle::default() }; - if !f.face.trim().is_empty() { - style.font_family = f.face.trim().to_string(); - } - style.font_style = match (f.weight >= 600, f.italic) { - (true, true) => "Bold Italic", - (true, false) => "Bold", - (false, true) => "Italic", - (false, false) => "Regular", - } - .into(); - let angle = -(f.escapement / 10.0).to_radians(); - let angle = if angle.is_finite() { angle } else { 0.0 }; - // TA_BASELINE, TA_BOTTOM or TA_TOP (the default): where the baseline is. - let drop = match align & 0x18 { - 0x18 => 0.0, - 0x08 => -0.2 * size, - _ => 0.8 * size, - }; - let origin = self.xf() * at; - let mut t = TextObject::point(Point::ZERO, &s, style); - t.xf = Affine::translate(origin.to_vec2()) * Affine::rotate(angle) * Affine::translate((0.0, drop)); - t.para.justify = match align & 0x06 { - 0x06 => Justify::Center, - 0x02 => Justify::Right, - _ => Justify::Left, - }; - t.cached_bounds = Some(vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t).bounds); - self.push(Node::new(vectorcraft_doc::NodeId(0), NodeKind::Text(Box::new(t)))); - } - - // ---------- the document ---------- - - /// The document: what was drawn on one layer, clipped art in clipping groups. - fn finish(mut self) -> Imported { - self.flush(); - let out = std::mem::take(&mut self.out); - let children = nest(&mut self.doc, out); - let mut layer = Node::layer(self.doc.alloc_id(), "Layer 1", LayerColor::Preset(0)); - if let Some(c) = layer.children_mut() { - *c = children; - } - self.doc.layers = vec![Arc::new(layer)]; - Imported { document: self.doc, kind: self.kind, warnings: self.warnings } - } -} - -/// `img` cut to `src` (pixels from the top-left, within the image). -fn crop(img: Rgba, src: Rect) -> Rgba { - let (w, h) = (img.width, img.height); - let s = src.abs(); - let clamp = |v: f64, max: u32| if v.is_finite() { v.round().clamp(0.0, f64::from(max)) as u32 } else { 0 }; - let (x0, y0, x1, y1) = (clamp(s.x0, w), clamp(s.y0, h), clamp(s.x1, w), clamp(s.y1, h)); - if (x0, y0, x1, y1) == (0, 0, w, h) || x1 <= x0 || y1 <= y0 { - return img; - } - let mut pixels = Vec::with_capacity(((x1 - x0) * (y1 - y0) * 4) as usize); - for y in y0..y1 { - let row = (y * w) as usize * 4; - if let Some(p) = img.pixels.get(row + x0 as usize * 4..row + x1 as usize * 4) { - pixels.extend_from_slice(p); - } - } - Rgba { width: x1 - x0, height: y1 - y0, pixels } -} - -/// Read an EMF or WMF file. -pub(crate) fn import(bytes: &[u8]) -> Result { - picture::import(bytes) +pub(crate) use picture::import; + +/// Read a QuickDraw picture (PICT, versions 1 and 2) through `craft_metafile`. +pub(crate) fn import_pict(bytes: &[u8]) -> Result { + let pic = craft_metafile::parse_pict(bytes).map_err(|e| match e { + craft_metafile::Error::NotPict => "not a PICT file".to_string(), + _ => "the PICT file is damaged".to_string(), + })?; + Ok(picture::convert(pic)) } diff --git a/crates/metafile/src/import/pict.rs b/crates/metafile/src/import/pict.rs deleted file mode 100644 index a1e5a674e0..0000000000 --- a/crates/metafile/src/import/pict.rs +++ /dev/null @@ -1,1176 +0,0 @@ -//! QuickDraw pictures (PICT, versions 1 and 2), played on the [`Player`] by the opcodes and data -//! lengths of Apple's *Imaging With QuickDraw*, appendix A. -//! -//! A PICT file holds 512 bytes of application data before the picture; a picture on its own (as -//! a `PICT` resource holds it) reads too. A unit is a point, and the artboard is the picture's -//! frame; an extended version 2 header's source rectangle is mapped onto the frame. -//! -//! - Lines, rectangles, rounded rectangles, ovals, arcs and wedges, polygons and regions become -//! paths. A framed shape is stroked inside its outline at the pen's size, and a line below and -//! to the right of its points, where the pen draws; painted, filled and erased shapes are filled -//! with the pen, fill or background pattern. A pattern of two colors is filled with their -//! average. -//! - Text becomes point type in the font the picture names, read as Mac Roman. -//! - Bitmaps and pixel maps (1 to 8 bits with their color table, 16 and 32 bits, packed or not) -//! become images, clipped to their mask region; QuickTime JPEG images become images. -//! - The clip region clips what follows it. -//! -//! What isn't read (inverted shapes, transfer modes other than a copy, other QuickTime formats) -//! comes back as one warning each. Apple's table gives every opcode's length, so opcodes that draw -//! nothing VectorCraft reads are skipped; a picture cut short keeps what came before. - -use std::collections::HashMap; - -use vectorcraft_geom::{Affine, BezPath, FillRule, Point, Rect, Shape, Vec2, shapes}; - -use super::{BAD_IMAGE, FontObj, PS_GEOMETRIC, PenObj, Player, SRCCOPY, TOO_MANY_PIXELS, crop, round_rect}; -use crate::dib::{self, Rgba}; -use crate::{Imported, Kind}; - -/// What a file that holds no picture is refused with. -const NOT_PICT: &str = "not a PICT file"; -/// The application data before the picture in a PICT file. -const FILE_HEADER: usize = 512; -/// Most opcodes one picture plays (a small file can't keep the reader busy for minutes). -const MAX_OPS: usize = 1 << 22; -/// Most rectangles one region breaks into. -const MAX_RECTS: usize = 1 << 16; -/// Steps the regions of one picture may take to read (each inversion point and band costs some). -const MAX_REGION_WORK: u64 = 1 << 28; -/// Most bytes the unpacked rows of one image may hold. -const MAX_ROW_BYTES: u64 = 1 << 28; -/// Most bytes the unpacked rows of one picture's images and patterns may hold together. -const MAX_FILE_ROW_BYTES: u64 = 1 << 30; -/// Most bytes a row may hold past what its pixels need. -const ROW_SLACK: usize = 64; -/// Most pixels, and bytes of unpacked rows, a pixel pattern may hold. -const MAX_PATTERN_PIXELS: usize = 1 << 16; -const MAX_PATTERN_ROW_BYTES: usize = 1 << 20; -/// Curves made for ovals and arcs are this close to the true curve, points. -const TOLERANCE: f64 = 0.01; -/// GDI text alignment on the baseline, which QuickDraw's text location is. -const TA_BASELINE: u32 = 0x18; -/// Pen style flags: square and flat ends, mitered joins. -const SQUARE: u32 = 0x100; -const FLAT: u32 = 0x200; -const MITER: u32 = 0x2000; - -const CUT_SHORT: &str = "the picture is cut short or damaged: what came before was imported"; -const TOO_MANY_OPS: &str = "the picture holds more opcodes than one file may: the rest was left out"; -const INVERTED: &str = "inverted shapes were left out"; -const PATTERNS: &str = "patterns are filled with the average of their colors"; -const MODES: &str = "art drawn in transfer modes other than a copy is drawn as a copy"; -const FRAMED_REGIONS: &str = "framed regions are outlined rectangle by rectangle"; -const BIG_REGIONS: &str = "regions larger than one picture may hold were left out"; -const CODECS: &str = "QuickTime images in formats other than JPEG were left out"; -const NOTHING: &str = "the picture draws nothing VectorCraft reads"; - -/// Big-endian reads from a slice, never past its end. -struct Be<'a> { - data: &'a [u8], - pos: usize, -} - -impl<'a> Be<'a> { - fn new(data: &'a [u8], pos: usize) -> Self { - Self { data, pos } - } - fn bytes(&mut self, n: usize) -> Option<&'a [u8]> { - let b = self.data.get(self.pos..self.pos.checked_add(n)?)?; - self.pos += n; - Some(b) - } - fn array(&mut self) -> Option<[u8; N]> { - self.bytes(N)?.try_into().ok() - } - fn skip(&mut self, n: usize) -> Option<()> { - self.bytes(n).map(drop) - } - fn u8(&mut self) -> Option { - self.array().map(|[b]: [u8; 1]| b) - } - fn u16(&mut self) -> Option { - self.array().map(u16::from_be_bytes) - } - fn i16(&mut self) -> Option { - self.array().map(i16::from_be_bytes) - } - fn u32(&mut self) -> Option { - self.array().map(u32::from_be_bytes) - } - fn i32(&mut self) -> Option { - self.array().map(i32::from_be_bytes) - } - /// A QuickDraw point: vertical, then horizontal. - fn point(&mut self) -> Option { - let v = self.i16()?; - let h = self.i16()?; - Some(Point::new(h.into(), v.into())) - } - /// A size stored as a point (a pen or an oval: height, then width). - fn size(&mut self) -> Option { - self.point().map(Point::to_vec2) - } - /// A QuickDraw rectangle: top, left, bottom, right. - fn rect(&mut self) -> Option { - let (top, left, bottom, right) = (self.i16()?, self.i16()?, self.i16()?, self.i16()?); - Some(Rect::new(left.into(), top.into(), right.into(), bottom.into())) - } - /// An `RGBColor` (16 bits a component), 8 bits a component. - fn rgb(&mut self) -> Option<[u8; 3]> { - let [r, g, b] = [self.u16()?, self.u16()?, self.u16()?]; - Some([r, g, b].map(|c| (c >> 8) as u8)) - } -} - -/// Where a picture is, its frame and whether it is version 2. -struct Header { - frame: Rect, - v2: bool, - /// Where its first opcode starts. - ops: usize, -} - -/// The picture whose `picSize` is at `at`, if a version opcode follows its frame. -fn header(bytes: &[u8], at: usize) -> Option
{ - let mut r = Be::new(bytes, at.checked_add(2)?); - let frame = r.rect()?; - let (v2, ops) = match r.array::<2>()? { - [0x11, 0x01] => (false, r.pos), - [0x00, 0x11] if r.array::<4>()? == [0x02, 0xFF, 0x0C, 0x00] => (true, at + 10), - _ => return None, - }; - (frame.width() > 0.0 && frame.height() > 0.0).then_some(Header { frame, v2, ops }) -} - -/// The picture of a file (after its application data), else the picture `bytes` start with. -fn find(bytes: &[u8]) -> Option
{ - header(bytes, FILE_HEADER).or_else(|| header(bytes, 0)) -} - -/// Do `bytes` hold a version 2 picture, after a file's application data or on their own? The -/// version 1 signature is two bytes, too short to tell a file by. -pub(crate) fn is_pict(bytes: &[u8]) -> bool { - [FILE_HEADER, 0].into_iter().any(|at| header(bytes, at).is_some_and(|h| h.v2)) -} - -/// A pattern: eight rows of eight bits (set bits in the foreground color, the others in the -/// background color), or one color. -#[derive(Clone, Copy)] -enum Pat { - Bits([u8; 8]), - Rgb([u8; 3]), -} - -/// What a shape opcode does with its shape. -#[derive(Clone, Copy, PartialEq, Eq)] -enum Verb { - Frame, - Paint, - Erase, - Invert, - Fill, -} - -/// The verb of a shape opcode (its low three bits; 5 to 7 are reserved). -fn verb(op: u16) -> Option { - match op & 7 { - 0 => Some(Verb::Frame), - 1 => Some(Verb::Paint), - 2 => Some(Verb::Erase), - 3 => Some(Verb::Invert), - 4 => Some(Verb::Fill), - _ => None, - } -} - -/// An old-style QuickDraw color (`blackColor` 33 … `blueColor` 409) as RGB. -fn old_color(c: u32) -> [u8; 3] { - match c { - 30 => [255; 3], - 69 => [255, 255, 0], - 137 => [255, 0, 255], - 205 => [255, 0, 0], - 273 => [0, 255, 255], - 341 => [0, 255, 0], - 409 => [0, 0, 255], - _ => [0; 3], - } -} - -/// `a` and `b` mixed: `t` of `a`. -fn mix(a: [u8; 3], b: [u8; 3], t: f64) -> [u8; 3] { - let m = |x: u8, y: u8| (f64::from(x) * t + f64::from(y) * (1.0 - t)).round() as u8; - [m(a[0], b[0]), m(a[1], b[1]), m(a[2], b[2])] -} - -/// Byte `b` of Mac Roman. -fn mac_roman(b: u8) -> char { - const HIGH: [char; 128] = [ - 'Ä', 'Å', 'Ç', 'É', 'Ñ', 'Ö', 'Ü', 'á', 'à', 'â', 'ä', 'ã', 'å', 'ç', 'é', 'è', // - 'ê', 'ë', 'í', 'ì', 'î', 'ï', 'ñ', 'ó', 'ò', 'ô', 'ö', 'õ', 'ú', 'ù', 'û', 'ü', // - '†', '°', '¢', '£', '§', '•', '¶', 'ß', '®', '©', '™', '´', '¨', '≠', 'Æ', 'Ø', // - '∞', '±', '≤', '≥', '¥', 'µ', '∂', '∑', '∏', 'π', '∫', 'ª', 'º', 'Ω', 'æ', 'ø', // - '¿', '¡', '¬', '√', 'ƒ', '≈', '∆', '«', '»', '…', '\u{A0}', 'À', 'Ã', 'Õ', 'Œ', 'œ', // - '–', '—', '“', '”', '‘', '’', '÷', '◊', 'ÿ', 'Ÿ', '⁄', '€', '‹', '›', 'fi', 'fl', // - '‡', '·', '‚', '„', '‰', 'Â', 'Ê', 'Á', 'Ë', 'È', 'Í', 'Î', 'Ï', 'Ì', 'Ó', 'Ô', // - '\u{F8FF}', 'Ò', 'Ú', 'Û', 'Ù', 'ı', 'ˆ', '˜', '¯', '˘', '˙', '˚', '¸', '˝', '˛', 'ˇ', - ]; - match b { - 0x80.. => HIGH.get(usize::from(b - 0x80)).copied().unwrap_or(char::REPLACEMENT_CHARACTER), - _ => char::from(b), - } -} - -/// The family name of a classic Macintosh font number. -fn font_family(id: u16) -> &'static str { - match id { - 0 => "Chicago", - 1 | 3 => "Geneva", - 2 => "New York", - 4 => "Monaco", - 20 => "Times", - 21 => "Helvetica", - 22 => "Courier", - 23 => "Symbol", - _ => "", - } -} - -/// The arc of the oval in `r` from `start` degrees clockwise from 12 o'clock, over `sweep` -/// degrees (clockwise when positive). Angles are measured on the rectangle, so 45° runs through -/// its top-right corner. A wedge closes through the center. -fn arc(r: Rect, start: i16, sweep: i16, wedge: bool) -> BezPath { - let c = r.center(); - let radii = Vec2::new(r.width() / 2.0, r.height() / 2.0); - let mut bp = BezPath::new(); - if sweep == 0 { - return bp; - } - let a0 = (f64::from(start) - 90.0).to_radians(); - let sweep = f64::from(sweep).clamp(-360.0, 360.0).to_radians(); - let arc = kurbo::Arc { center: c, radii, start_angle: a0, sweep_angle: sweep, x_rotation: 0.0 }; - if wedge { - bp.move_to(c); - bp.line_to(c + Vec2::new(radii.x * a0.cos(), radii.y * a0.sin())); - arc.append_iter(TOLERANCE).for_each(|el| bp.push(el)); - bp.close_path(); - } else { - kurbo::Shape::path_elements(&arc, TOLERANCE).for_each(|el| bp.push(el)); - } - bp -} - -/// The outlines of `rects`, as one path. -fn rects_path(rects: &[Rect]) -> BezPath { - let mut bp = BezPath::new(); - for r in rects { - bp.extend(shapes::rectangle(*r).to_bezpath()); - } - bp -} - -/// Take `n` steps from `work`; false when too few are left. -fn spend(work: &mut u64, n: usize) -> bool { - match work.checked_sub(n as u64) { - Some(left) => { - *work = left; - true - } - None => false, - } -} - -/// A region (its size, bounding box and inversion points), read at `r`, as rectangles: `None` -/// when the data runs past the end; the flag is false when the region is larger than `work` or -/// [`MAX_RECTS`] allows, and then no rectangles are given. -fn region(r: &mut Be, work: &mut u64) -> Option<(Vec, bool)> { - let size = usize::from(r.u16()?); - let mut b = Be::new(r.bytes(size.checked_sub(2)?)?, 0); - let bbox = b.rect()?; - if size <= 10 { - let whole = bbox.width() > 0.0 && bbox.height() > 0.0; - return Some((if whole { vec![bbox] } else { vec![] }, true)); - } - Some(bands(&mut b, work).map_or((vec![], false), |rects| (rects, true))) -} - -/// The rectangles of a region's scanlines: each lists the x coordinates where being inside the -/// region changes from the scanline before. Bands of the same spans one after another are one -/// rectangle. -fn bands(b: &mut Be, work: &mut u64) -> Option> { - let mut out: Vec = vec![]; - let mut active: Vec = vec![]; - let mut last_band = 0; - let mut prev_y = 0.0; - while let Some(y) = b.i16().filter(|y| *y != 0x7FFF) { - let y = f64::from(y); - let spans: Vec<(f64, f64)> = active.as_chunks::<2>().0.iter().map(|[a, b]| (f64::from(*a), f64::from(*b))).collect(); - if !spans.is_empty() && y > prev_y { - if !spend(work, spans.len() + 1) { - return None; - } - let prev = out.get_mut(last_band..).unwrap_or_default(); - let same = prev.len() == spans.len() && prev.iter().zip(&spans).all(|(r, (x0, x1))| r.x0 == *x0 && r.x1 == *x1 && r.y1 == prev_y); - if same { - prev.iter_mut().for_each(|r| r.y1 = y); - } else { - last_band = out.len(); - out.extend(spans.iter().map(|&(x0, x1)| Rect::new(x0, prev_y, x1, y))); - if out.len() > MAX_RECTS { - return None; - } - } - } - prev_y = y; - while let Some(x) = b.i16().filter(|x| *x != 0x7FFF) { - if !spend(work, active.len() + 1) { - return None; - } - match active.binary_search(&x) { - Ok(i) => { - active.remove(i); - } - Err(i) => active.insert(i, x), - } - } - } - Some(out) -} - -/// Skip the data of opcode `op`, by the length Apple's table gives it. -fn skip(op: u16, r: &mut Be) -> Option<()> { - let n = match op { - 0x00 | 0x17..=0x19 | 0x1C | 0x1E | 0xB0..=0xCF | 0x8000..=0x80FF => 0, - 0x04 => 1, - 0x03 | 0x05 | 0x08 | 0x0D | 0x11 | 0x15 | 0x16 | 0xA0 => 2, - 0x06 | 0x07 | 0x0B | 0x0C | 0x0E | 0x0F => 4, - 0x1A | 0x1B | 0x1D | 0x1F => 6, - 0x02 | 0x09 | 0x0A | 0x10 => 8, - // Reserved opcodes with a 16-bit length, and the font name. - 0x24..=0x27 | 0x2C..=0x2F | 0x92..=0x97 | 0x9C..=0x9F | 0xA2..=0xAF => usize::from(r.u16()?), - // Reserved for Apple: two bytes for each unit of the high byte (the 24-byte header 0x0C00). - 0x0100..=0x7FFF => usize::from(op >> 8) * 2, - // 0xD0–0xFE and 0x8100–0xFFFF (QuickTime images among them): a 32-bit length. - _ => usize::try_from(r.u32()?).ok()?, - }; - r.skip(n) -} - -/// How a bitmap's or pixel map's pixels are stored. -struct PixMap { - row_bytes: usize, - bounds: Rect, - /// Bits a pixel. - size: u16, - pack: u16, - cmps: u16, - /// Indexed pixels' colors; a bitmap has none (its set bits are the foreground color). - palette: Option>, - /// Rows of 8 bytes or more may be packed with PackBits, as the packing says. - packed: bool, -} - -impl PixMap { - /// From `rowBytes` on: a pixel map when `rowBytes` has its high bit set or `pixmap` says so, - /// else a bitmap; a pixel map's color table follows unless `direct`. - fn read(r: &mut Be, pixmap: bool, direct: bool) -> Option { - let raw = r.u16()?; - let mut pm = PixMap { row_bytes: usize::from(raw & 0x3FFF), bounds: r.rect()?, size: 1, pack: 0, cmps: 1, palette: None, packed: true }; - if pixmap || raw & 0x8000 != 0 { - r.skip(2)?; - pm.pack = r.u16()?; - r.skip(14)?; - pm.size = r.u16()?; - pm.cmps = r.u16()?; - r.skip(14)?; - if !direct { - pm.palette = Some(color_table(r)?); - } - } - Some(pm) - } - - /// The width and height of its pixels. - fn dims(&self) -> Option<(usize, usize)> { - let (w, h) = (self.bounds.width(), self.bounds.height()); - (w > 0.0 && h > 0.0).then_some((w as usize, h as usize)) - } - - /// The packing: 0 is 4 for 32-bit pixels and 3 for 16-bit ones. - fn packing(&self) -> u16 { - match (self.size, self.pack) { - (32, 0) => 4, - (16, 0) => 3, - (_, p) => p, - } - } - - /// Rows are stored as they are (no byte count, no PackBits). - fn raw_rows(&self) -> bool { - !self.packed || self.row_bytes < 8 || self.packing() == 1 || (self.size == 32 && self.packing() == 2) - } - - /// Bytes of an unpacked row: a component plane each for packing 4, three bytes a pixel for - /// packing 2, else `rowBytes`. - fn row_len(&self, w: usize) -> usize { - match self.packing() { - 4 if self.size == 32 && self.row_bytes >= 8 => w * usize::from(self.cmps), - 2 if self.size == 32 => w * 3, - _ => self.row_bytes, - } - } - - /// Whether its pixels can be read: a depth QuickDraw has, and rows long enough for them and at - /// most [`ROW_SLACK`] bytes longer. - fn decodable(&self, w: usize) -> bool { - let need = match self.size { - 1 | 2 | 4 | 8 => (w * usize::from(self.size)).div_ceil(8), - 16 => 2 * w, - 32 if self.packing() == 4 && self.row_bytes >= 8 => { - if !matches!(self.cmps, 3 | 4) { - return false; - } - w * usize::from(self.cmps) - } - 32 if self.packing() == 2 => 3 * w, - 32 => 4 * w, - _ => return false, - }; - (need..=need + ROW_SLACK).contains(&self.row_len(w)) - } - - /// Read `h` rows of pixel data, unpacked into one buffer when `keep`, else only stepped over. - /// `None` when the data runs past the end; the flag is false when a row didn't unpack to its - /// length. - fn rows(&self, r: &mut Be, w: usize, h: usize, keep: bool) -> Option<(Vec, bool)> { - let len = self.row_len(w); - let unit = if self.packing() == 3 { 2 } else { 1 }; - let mut out = vec![]; - let mut ok = true; - let mut keep = keep; - for _ in 0..h { - if self.raw_rows() { - let row = r.bytes(len)?; - if keep { - out.extend_from_slice(row); - } - continue; - } - let n = if self.row_bytes > 250 { usize::from(r.u16()?) } else { usize::from(r.u8()?) }; - let packed = r.bytes(n)?; - // After a row that doesn't unpack, the rest are only stepped over. - if keep && !unpack(packed, len, unit, &mut out) { - ok = false; - keep = false; - out = vec![]; - } - } - Some((out, ok)) - } - - /// The color of pixel `x` of `row` (the foreground and background colors for a bitmap's bits). - fn pixel(&self, row: &[u8], x: usize, w: usize, fg: [u8; 3], bg: [u8; 3]) -> Option<[u8; 3]> { - let at = |i: usize| row.get(i).copied(); - match self.size { - 1 | 2 | 4 | 8 => { - let bits = usize::from(self.size); - let shift = 8 - bits - x * bits % 8; - let index = usize::from((at(x * bits / 8)? >> shift) & ((1u16 << bits) - 1) as u8); - match &self.palette { - Some(p) => p.get(index).copied(), - None => Some(if index == 0 { bg } else { fg }), - } - } - 16 => { - let v = u16::from_be_bytes([at(2 * x)?, at(2 * x + 1)?]); - let c = |s: u16| (((v >> s) & 31) * 255 / 31) as u8; - Some([c(10), c(5), c(0)]) - } - _ if self.packing() == 4 && self.row_bytes >= 8 => { - // Component planes, alpha first when there are four (QuickDraw doesn't draw it). - let first = usize::from(self.cmps) - 3; - Some([at(first * w + x)?, at((first + 1) * w + x)?, at((first + 2) * w + x)?]) - } - _ if self.packing() == 2 => Some([at(3 * x)?, at(3 * x + 1)?, at(3 * x + 2)?]), - _ => Some([at(4 * x + 1)?, at(4 * x + 2)?, at(4 * x + 3)?]), - } - } - - /// Unpacked rows as opaque pixels. - fn to_rgba(&self, data: &[u8], w: usize, h: usize, fg: [u8; 3], bg: [u8; 3]) -> Option { - let len = self.row_len(w); - let mut pixels = Vec::with_capacity(w * h * 4); - for y in 0..h { - let row = data.get(y * len..(y + 1) * len)?; - for x in 0..w { - pixels.extend(self.pixel(row, x, w, fg, bg)?); - pixels.push(255); - } - } - Some(Rgba { width: u32::try_from(w).ok()?, height: u32::try_from(h).ok()?, pixels }) - } -} - -/// A `ColorTable`: indexed pixels' colors (entry by entry for a device table, else by each -/// entry's value). -fn color_table(r: &mut Be) -> Option> { - r.skip(4)?; - let flags = r.u16()?; - let n = usize::from(r.u16()?) + 1; - let mut palette = vec![[0u8; 3]; 256]; - for i in 0..n { - let value = usize::from(r.u16()?); - let c = r.rgb()?; - if let Some(e) = palette.get_mut(if flags & 0x8000 != 0 { i } else { value }) { - *e = c; - } - } - Some(palette) -} - -/// Unpack PackBits `data` (runs of `unit`-byte elements) onto `out`, `len` bytes; false when the -/// data is damaged or short (the row is padded with zeros). -fn unpack(data: &[u8], len: usize, unit: usize, out: &mut Vec) -> bool { - let start = out.len(); - let mut i = 0; - let mut ok = true; - while out.len() - start < len { - let Some(&n) = data.get(i) else { - ok = false; - break; - }; - i += 1; - let n = n as i8; - if n >= 0 { - let k = (n as usize + 1) * unit; - let Some(lit) = data.get(i..i + k) else { - ok = false; - break; - }; - out.extend_from_slice(lit); - i += k; - } else if n != -128 { - let Some(v) = data.get(i..i + unit) else { - ok = false; - break; - }; - for _ in 0..(1 - isize::from(n)) { - out.extend_from_slice(v); - } - i += unit; - } - } - out.resize(start + len, 0); - ok -} - -/// The picture being played: QuickDraw's drawing state, and the player that makes the objects. -struct Pict { - p: Player, - frame: Rect, - /// Picture space → document. - base: Affine, - origin: Vec2, - fg: [u8; 3], - bg: [u8; 3], - pen: Vec2, - pen_mode: u16, - pen_pat: Pat, - fill_pat: Pat, - back_pat: Pat, - oval: Vec2, - pen_loc: Point, - font: u16, - face: u8, - text_size: i16, - text_loc: Point, - /// Font numbers the picture names. - names: HashMap, - /// The last rectangle of the rectangle, rounded rectangle, oval and arc opcodes. - last: [Rect; 4], - region_work: u64, - /// Bytes of unpacked rows the picture's images and patterns may still take. - row_bytes_left: u64, -} - -impl Pict { - fn new(frame: Rect) -> Self { - let base = Affine::translate(-frame.origin().to_vec2()); - let artboard = Rect::new(0.0, 0.0, frame.width(), frame.height()); - // A picture's coordinates map straight onto the document, as a WMF's do. - let p = Player::new(Kind::Wmf, base, artboard); - Self { - p, - frame, - base, - origin: Vec2::ZERO, - fg: [0; 3], - bg: [255; 3], - pen: Vec2::new(1.0, 1.0), - pen_mode: 8, - pen_pat: Pat::Bits([0xFF; 8]), - fill_pat: Pat::Bits([0xFF; 8]), - back_pat: Pat::Bits([0; 8]), - oval: Vec2::ZERO, - pen_loc: Point::ZERO, - font: 0, - face: 0, - text_size: 0, - text_loc: Point::ZERO, - names: HashMap::new(), - last: [Rect::ZERO; 4], - region_work: MAX_REGION_WORK, - row_bytes_left: MAX_FILE_ROW_BYTES, - } - } - - /// Charge the `w` × `h` pixels of `pm`, and the bytes of its unpacked rows, to the picture's - /// budgets: false, and nothing charged, when either is spent. - fn charge(&mut self, pm: &PixMap, w: usize, h: usize) -> bool { - let bytes = (pm.row_len(w) as u64).saturating_mul(h as u64); - let Some(rows_left) = self.row_bytes_left.checked_sub(bytes) else { return false }; - let (Ok(wide), Ok(high)) = (u32::try_from(w), u32::try_from(h)) else { return false }; - if dib::charge(wide, high, &mut self.p.pixels_left).is_err() { - return false; - } - self.row_bytes_left = rows_left; - true - } - - /// Play opcode `op`, its data at `r`; `None` when the data runs past the end. - fn op(&mut self, op: u16, r: &mut Be) -> Option<()> { - if !matches!(op, 0x20..=0x23) { - self.p.flush(); - } - match op { - 0x01 => self.clip(r), - 0x0C => self.move_origin(r), - 0x20..=0x23 => self.line(op, r), - 0x28..=0x2B => self.text(op, r), - 0x2C => self.font_name(r), - 0x30..=0x6F => self.shape(op, r), - 0x70..=0x77 => self.poly(verb(op), r), - 0x80..=0x87 => self.region_shape(verb(op), r), - // The same polygon and region: Apple's table gives them no data and no drawing. - 0x78..=0x7F | 0x88..=0x8F => Some(()), - 0x90 | 0x91 | 0x98..=0x9B => self.bits(op, r), - 0xA1 => { - r.skip(2)?; - let n = usize::from(r.u16()?); - r.skip(n) - } - 0x0C00 => self.header(r), - 0x8200 => self.quicktime(r), - _ => self.state(op, r), - } - } - - /// The opcodes that set the drawing state; the others are skipped. - fn state(&mut self, op: u16, r: &mut Be) -> Option<()> { - match op { - 0x02 => self.back_pat = Pat::Bits(r.array()?), - 0x03 => self.font = r.u16()?, - 0x04 => self.face = r.u8()?, - 0x07 => self.pen = r.size()?, - 0x08 => self.pen_mode = r.u16()?, - 0x09 => self.pen_pat = Pat::Bits(r.array()?), - 0x0A => self.fill_pat = Pat::Bits(r.array()?), - 0x0B => self.oval = r.size()?, - 0x0D => self.text_size = r.i16()?, - 0x0E => self.fg = old_color(r.u32()?), - 0x0F => self.bg = old_color(r.u32()?), - 0x12 => self.back_pat = self.pix_pat(r)?, - 0x13 => self.pen_pat = self.pix_pat(r)?, - 0x14 => self.fill_pat = self.pix_pat(r)?, - 0x1A => self.fg = r.rgb()?, - 0x1B => self.bg = r.rgb()?, - _ => return skip(op, r), - } - Some(()) - } - - /// The version 2 header: an extended one's source rectangle maps onto the frame. - fn header(&mut self, r: &mut Be) -> Option<()> { - let mut b = Be::new(r.bytes(24)?, 0); - if b.i16() == Some(-2) - && let Some(src) = b.skip(10).and_then(|()| b.rect()) - && src.width() > 0.0 - && src.height() > 0.0 - { - let k = Vec2::new(self.frame.width() / src.width(), self.frame.height() / src.height()); - self.base = Affine::scale_non_uniform(k.x, k.y) * Affine::translate(-src.origin().to_vec2()); - self.p.dev_to_doc = self.base * Affine::translate(-self.origin); - } - Some(()) - } - - /// The origin moves by (dh, dv): a point drawn after it is that much nearer the top left. - fn move_origin(&mut self, r: &mut Be) -> Option<()> { - let (dh, dv) = (r.i16()?, r.i16()?); - self.origin += Vec2::new(dh.into(), dv.into()); - self.p.dev_to_doc = self.base * Affine::translate(-self.origin); - Some(()) - } - - /// A pixel pattern: its color (the average of a color pattern's pixels), else its bitmap. - fn pix_pat(&mut self, r: &mut Be) -> Option { - let kind = r.u16()?; - let bits: [u8; 8] = r.array()?; - match kind { - 1 => { - let pm = PixMap::read(r, true, false)?; - let (w, h) = pm.dims()?; - let small = w * h <= MAX_PATTERN_PIXELS && pm.row_len(w) * h <= MAX_PATTERN_ROW_BYTES; - let keep = small && pm.decodable(w) && self.charge(&pm, w, h); - let (data, _) = pm.rows(r, w, h, keep)?; - let avg = keep.then(|| pm.to_rgba(&data, w, h, self.fg, self.bg)).flatten().map(|img| self.average(&img)); - Some(avg.map_or(Pat::Bits(bits), Pat::Rgb)) - } - 2 => Some(Pat::Rgb(r.rgb()?)), - _ => Some(Pat::Bits(bits)), - } - } - - /// The average color of `img`, with a warning when its pixels differ. - fn average(&mut self, img: &Rgba) -> [u8; 3] { - let px = img.pixels.as_chunks::<4>().0; - if px.windows(2).any(|w| w[0] != w[1]) { - self.p.warn(PATTERNS); - } - let n = px.len().max(1) as u64; - let sum = |i: usize| (px.iter().map(|p| u64::from(p[i])).sum::() / n) as u8; - [sum(0), sum(1), sum(2)] - } - - /// The color a pattern fills with: its own, or its two colors mixed as its bits are. - fn pat_color(&mut self, pat: Pat) -> [u8; 3] { - match pat { - Pat::Rgb(c) => c, - Pat::Bits(b) => { - let ones: u32 = b.iter().map(|x| x.count_ones()).sum(); - if ones != 0 && ones != 64 { - self.p.warn(PATTERNS); - } - mix(self.fg, self.bg, f64::from(ones) / 64.0) - } - } - } - - /// The pen's color (a warning when its mode isn't a copy). - fn pen_color(&mut self) -> [u8; 3] { - if !matches!(self.pen_mode, 0 | 8) { - self.p.warn(MODES); - } - self.pat_color(self.pen_pat) - } - - /// The color painting, erasing or filling an area takes; `None` for the other verbs. - fn area_color(&mut self, verb: Verb) -> Option<[u8; 3]> { - match verb { - Verb::Paint => Some(self.pen_color()), - Verb::Erase => Some(self.pat_color(self.back_pat)), - Verb::Fill => Some(self.pat_color(self.fill_pat)), - Verb::Frame | Verb::Invert => None, - } - } - - fn has_pen(&self) -> bool { - self.pen.x > 0.0 && self.pen.y > 0.0 - } - - /// Fill `bp` (picture space) with `color`. - fn fill(&mut self, mut bp: BezPath, color: [u8; 3], rule: FillRule) { - bp.apply_affine(self.p.xf()); - self.p.dc.brush = color; - self.p.dc.fill_rule = rule; - self.p.draw(bp, true, false); - } - - /// The pen as a stroke of `color` with `ends`, as wide as the pen's average side. - fn set_pen(&mut self, color: [u8; 3], ends: u32) { - let width = (self.pen.x + self.pen.y) / 2.0; - self.p.dc.pen = PenObj { style: PS_GEOMETRIC | ends | MITER, width, color, entries: vec![] }; - } - - /// Stroke `bp` (picture space) with the pen in `color`. - fn stroke(&mut self, mut bp: BezPath, color: [u8; 3], ends: u32) { - self.set_pen(color, ends); - bp.apply_affine(self.p.xf()); - self.p.draw(bp, false, true); - } - - /// Draw a framed shape: its outline (from `outline` of a rectangle and an oval size) stroked - /// inside `r`, or filled when the pen is as large as the shape. - fn frame(&mut self, r: Rect, outline: impl Fn(Rect, Vec2) -> BezPath, ends: u32) { - if !self.has_pen() { - return; - } - let color = self.pen_color(); - let half = self.pen / 2.0; - let inner = Rect::new(r.x0 + half.x, r.y0 + half.y, r.x1 - half.x, r.y1 - half.y); - if inner.width() <= 0.0 || inner.height() <= 0.0 { - return self.fill(outline(r, self.oval), color, FillRule::NonZero); - } - self.stroke(outline(inner, self.oval - self.pen), color, ends); - } - - /// The rectangle, rounded rectangle, oval and arc opcodes. - fn shape(&mut self, op: u16, r: &mut Be) -> Option<()> { - let family = op >> 4; - let slot = usize::from(family - 3); - let rect = if op & 8 != 0 { self.last.get(slot).copied()? } else { r.rect()? }; - let angles = if family == 6 { Some((r.i16()?, r.i16()?)) } else { None }; - let Some(verb) = verb(op) else { return Some(()) }; - if let Some(l) = self.last.get_mut(slot) { - *l = rect; - } - if rect.width() <= 0.0 || rect.height() <= 0.0 { - return Some(()); - } - let outline = move |r: Rect, oval: Vec2, wedge: bool| match family { - 3 => shapes::rectangle(r).to_bezpath(), - 4 => round_rect(r, oval.x, oval.y), - 5 => kurbo::Ellipse::from_rect(r).to_path(TOLERANCE), - _ => angles.map_or_else(BezPath::new, |(start, sweep)| arc(r, start, sweep, wedge)), - }; - match verb { - Verb::Frame => self.frame(rect, |r, oval| outline(r, oval, false), if family == 6 { FLAT } else { SQUARE }), - Verb::Invert => self.p.warn(INVERTED), - _ => { - if let Some(c) = self.area_color(verb) { - self.fill(outline(rect, self.oval, true), c, FillRule::NonZero); - } - } - } - Some(()) - } - - /// A polygon opcode: framed along its points, else filled. - fn poly(&mut self, verb: Option, r: &mut Be) -> Option<()> { - let size = usize::from(r.u16()?); - let mut b = Be::new(r.bytes(size.checked_sub(2)?)?, 8); - let Some(verb) = verb else { return Some(()) }; - let mut pts = vec![]; - while let Some(p) = b.point() { - pts.push(p); - } - let Some((first, rest)) = pts.split_first() else { return Some(()) }; - let closed = pts.len() > 2 && pts.first() == pts.last(); - match verb { - Verb::Frame if self.has_pen() => { - let half = self.pen / 2.0; - let mut bp = BezPath::new(); - bp.move_to(*first + half); - rest.iter().for_each(|p| bp.line_to(*p + half)); - if closed { - bp.close_path(); - } - let color = self.pen_color(); - self.stroke(bp, color, SQUARE); - } - Verb::Frame => {} - Verb::Invert => self.p.warn(INVERTED), - _ => { - let mut bp = BezPath::new(); - bp.move_to(*first); - rest.iter().for_each(|p| bp.line_to(*p)); - bp.close_path(); - if let Some(c) = self.area_color(verb) { - self.fill(bp, c, FillRule::EvenOdd); - } - } - } - Some(()) - } - - /// A region's rectangles, with a warning when it was too large to read. - fn read_region(&mut self, r: &mut Be) -> Option> { - let (rects, whole) = region(r, &mut self.region_work)?; - if !whole { - self.p.warn(BIG_REGIONS); - } - Some(rects) - } - - /// A region opcode: each rectangle framed, else the region filled. - fn region_shape(&mut self, verb: Option, r: &mut Be) -> Option<()> { - let rects = self.read_region(r)?; - let Some(verb) = verb.filter(|_| !rects.is_empty()) else { return Some(()) }; - match verb { - Verb::Frame if self.has_pen() => { - if rects.len() > 1 { - self.p.warn(FRAMED_REGIONS); - } - let half = self.pen / 2.0; - let inner: Vec = rects.iter().map(|r| Rect::new(r.x0 + half.x, r.y0 + half.y, r.x1 - half.x, r.y1 - half.y)).collect(); - let color = self.pen_color(); - self.stroke(rects_path(&inner), color, SQUARE); - } - Verb::Frame => {} - Verb::Invert => self.p.warn(INVERTED), - _ => { - if let Some(c) = self.area_color(verb) { - self.fill(rects_path(&rects), c, FillRule::NonZero); - } - } - } - Some(()) - } - - /// The clip region: it replaces the clip. An empty region clips away everything after it, as - /// a clip of no area at the frame's corner; one too large to read leaves the clip as it was. - fn clip(&mut self, r: &mut Be) -> Option<()> { - let (mut rects, whole) = region(r, &mut self.region_work)?; - if !whole { - self.p.warn(BIG_REGIONS); - return Some(()); - } - if rects.is_empty() { - let corner = Point::new(self.frame.x0, self.frame.y0); - rects.push(Rect::from_points(corner, corner)); - } - let mut bp = rects_path(&rects); - bp.apply_affine(self.p.xf()); - self.p.push_clip(bp, FillRule::NonZero, true); - Some(()) - } - - /// The line opcodes: the pen drawn from point to point, hanging below and to the right of - /// the line; lines one after another join into one path. - fn line(&mut self, op: u16, r: &mut Be) -> Option<()> { - let delta = |r: &mut Be| Some(Vec2::new(f64::from(r.u8()? as i8), f64::from(r.u8()? as i8))); - let (from, to) = match op { - 0x20 => (r.point()?, r.point()?), - 0x21 => (self.pen_loc, r.point()?), - 0x22 => { - let a = r.point()?; - (a, a + delta(r)?) - } - _ => (self.pen_loc, self.pen_loc + delta(r)?), - }; - self.pen_loc = to; - if !self.has_pen() { - return Some(()); - } - let color = self.pen_color(); - if from == to { - // A dot: the pen's rectangle. - self.p.flush(); - self.fill(shapes::rectangle(Rect::from_points(from, from + self.pen)).to_bezpath(), color, FillRule::NonZero); - return Some(()); - } - let pen = PenObj { style: PS_GEOMETRIC | SQUARE | MITER, width: (self.pen.x + self.pen.y) / 2.0, color, entries: vec![] }; - if self.p.dc.pen != pen { - self.p.flush(); - self.p.dc.pen = pen; - } - let half = self.pen / 2.0; - let xf = self.p.xf(); - let (a, b) = (xf * (from + half), xf * (to + half)); - if self.p.pending.elements().last().and_then(|e| e.end_point()) != Some(a) { - self.p.pending.move_to(a); - } - self.p.pending.line_to(b); - Some(()) - } - - /// The text opcodes: a string at the text location, moved to or by the opcode's data. - fn text(&mut self, op: u16, r: &mut Be) -> Option<()> { - match op { - 0x28 => self.text_loc = r.point()?, - 0x29 => self.text_loc.x += f64::from(r.u8()?), - 0x2A => self.text_loc.y += f64::from(r.u8()?), - _ => { - self.text_loc.x += f64::from(r.u8()?); - self.text_loc.y += f64::from(r.u8()?); - } - } - let n = usize::from(r.u8()?); - let s: String = r.bytes(n)?.iter().map(|&b| mac_roman(b)).filter(|c| !c.is_control()).collect(); - let size = if self.text_size > 0 { self.text_size } else { 12 }; - let face = self.names.get(&self.font).cloned().unwrap_or_else(|| font_family(self.font).to_string()); - self.p.dc.font = FontObj { - height: -f64::from(size), - escapement: 0.0, - weight: if self.face & 1 != 0 { 700 } else { 400 }, - italic: self.face & 2 != 0, - face, - }; - self.p.dc.text_color = self.fg; - self.p.dc.text_align = TA_BASELINE; - self.p.text(self.text_loc, &s); - Some(()) - } - - /// A font's name for its number. - fn font_name(&mut self, r: &mut Be) -> Option<()> { - let n = usize::from(r.u16()?); - let mut b = Be::new(r.bytes(n)?, 0); - if let Some(id) = b.u16() - && let Some(len) = b.u8() - && let Some(name) = b.bytes(usize::from(len)) - { - let name: String = name.iter().map(|&c| mac_roman(c)).collect(); - self.names.insert(id, name.trim().to_string()); - } - Some(()) - } - - /// A bitmap or pixel map copied into a rectangle (`Rgn` opcodes: clipped to a mask region). - fn bits(&mut self, op: u16, r: &mut Be) -> Option<()> { - let direct = matches!(op, 0x9A | 0x9B); - if direct { - r.skip(4)?; - } - let mut pm = PixMap::read(r, direct, direct)?; - // BitsRect and BitsRgn store their rows unpacked. - pm.packed = op >= 0x98; - let (src, dst, mode) = (r.rect()?, r.rect()?, r.u16()?); - let mask = if op & 1 != 0 { Some(self.read_region(r)?) } else { None }; - let (w, h) = pm.dims()?; - let (wide, high) = (w as u64, h as u64); - let fits = wide * high <= dib::MAX_READ_PIXELS && pm.row_len(w) as u64 * high <= MAX_ROW_BYTES; - let readable = pm.decodable(w); - let keep = fits && readable && self.charge(&pm, w, h); - let (data, ok) = pm.rows(r, w, h, keep)?; - if !keep { - self.p.warn(if readable { TOO_MANY_PIXELS } else { BAD_IMAGE }); - return Some(()); - } - let Some(mut img) = pm.to_rgba(&data, w, h, self.fg, self.bg).filter(|_| ok) else { - self.p.warn(BAD_IMAGE); - return Some(()); - }; - self.transfer(&mut img, mode, pm.size == 1 && pm.palette.is_none()); - if dst.width() <= 0.0 || dst.height() <= 0.0 || mask.as_ref().is_some_and(Vec::is_empty) { - return Some(()); - } - let saved = self.p.dc.clips.clone(); - if let Some(rects) = &mask { - let mut bp = rects_path(rects); - bp.apply_affine(self.p.xf()); - self.p.push_clip(bp, FillRule::NonZero, false); - } - self.p.image(Ok(img), src - pm.bounds.origin().to_vec2(), dst, SRCCOPY, 1.0); - self.p.dc.clips = saved; - Some(()) - } - - /// Transfer mode `mode` on copied pixels: OR of a bitmap and the transparent mode leave the - /// background color out; the others are copied, with a warning. - fn transfer(&mut self, img: &mut Rgba, mode: u16, bitmap: bool) { - // 64 dithers, which doesn't change what is copied. - let clear = match mode & !64 { - 0 => false, - 1 if bitmap => true, - 36 => true, - _ => { - self.p.warn(MODES); - false - } - }; - if clear { - let bg = self.bg; - img.pixels.as_chunks_mut::<4>().0.iter_mut().filter(|p| [p[0], p[1], p[2]] == bg).for_each(|p| p[3] = 0); - } - } - - /// A compressed QuickTime image: a JPEG one becomes an image placed by its matrix. - fn quicktime(&mut self, r: &mut Be) -> Option<()> { - let n = usize::try_from(r.u32()?).ok()?; - let data = r.bytes(n)?; - match qt_image(data) { - Some((ctype, xf, src, image)) if &ctype == b"jpeg" => match self.jpeg(image) { - Ok(img) => { - let img = crop(img, src); - self.p.image_xf(img, xf * Affine::translate(src.origin().to_vec2()), 1.0); - } - Err(w) => self.p.warn(w), - }, - Some(_) => self.p.warn(CODECS), - None => self.p.warn(BAD_IMAGE), - } - Some(()) - } - - /// A JPEG stream's pixels, taken from the file's pixel budget. - fn jpeg(&mut self, data: &[u8]) -> Result { - let mut opts = craft_image_codecs::DecodeOptions::default(); - opts.limits.max_width = dib::MAX_READ_SIDE; - opts.limits.max_height = dib::MAX_READ_SIDE; - opts.limits.max_pixels = dib::MAX_READ_PIXELS.min(self.p.pixels_left); - let img = match craft_image_codecs::decode_as_with(craft_image_codecs::Format::Jpeg, data, &opts) { - Ok(img) => img, - Err(craft_image_codecs::CodecError::LimitExceeded(_)) => return Err(TOO_MANY_PIXELS), - Err(_) => return Err(BAD_IMAGE), - }; - let (w, h) = img.dimensions(); - dib::charge(w, h, &mut self.p.pixels_left).map_err(|_| TOO_MANY_PIXELS)?; - Ok(Rgba { width: w, height: h, pixels: img.to_rgba8() }) - } -} - -/// A QuickTime image's compressor type, matrix (image → picture space), source rectangle and -/// compressed data, from its header and image description. -fn qt_image(d: &[u8]) -> Option<([u8; 4], Affine, Rect, &[u8])> { - let mut b = Be::new(d, 2); - let mut m = [0.0; 9]; - for v in &mut m { - *v = f64::from(b.i32()?) / 65536.0; - } - let matte = usize::try_from(b.u32()?).ok()?; - b.skip(10)?; - let src = b.rect()?; - b.skip(4)?; - let mask = usize::try_from(b.u32()?).ok()?; - b.skip(matte)?; - b.skip(mask)?; - let start = b.pos; - let id_size = usize::try_from(b.u32()?).ok()?; - let ctype = b.array::<4>()?; - b.skip(36)?; - let size = usize::try_from(b.u32()?).ok()?; - let at = start.checked_add(id_size)?; - let data = d.get(at..at.checked_add(size)?).or_else(|| d.get(at..))?; - // The rows of a QuickTime matrix are [a b u] [c d v] [tx ty w]; u, v and w (perspective) are - // left out. - let xf = Affine::new([m[0], m[1], m[3], m[4], m[6], m[7]]); - Some((ctype, xf, src, data)) -} - -/// Read a picture. -pub(crate) fn play(bytes: &[u8]) -> Result { - let h = find(bytes).ok_or(NOT_PICT)?; - let mut q = Pict::new(h.frame); - let mut r = Be::new(bytes, h.ops); - // Why the picture stopped before its end opcode, if it did. - let mut stopped = Some(TOO_MANY_OPS); - for _ in 0..MAX_OPS { - // Version 2 opcodes start on even bytes. - if h.v2 && r.pos % 2 == 1 && r.skip(1).is_none() { - stopped = Some(CUT_SHORT); - break; - } - let Some(op) = (if h.v2 { r.u16() } else { r.u8().map(u16::from) }) else { - stopped = Some(CUT_SHORT); - break; - }; - if op == 0x00FF { - stopped = None; - break; - } - if q.op(op, &mut r).is_none() { - stopped = Some(CUT_SHORT); - break; - } - } - if let Some(why) = stopped { - q.p.warn(why); - } - q.p.flush(); - if q.p.out.is_empty() { - q.p.warn(NOTHING); - } - Ok(q.p.finish()) -} diff --git a/crates/metafile/src/import/picture.rs b/crates/metafile/src/import/picture.rs index 1cfb3038cd..932b7636cc 100644 --- a/crates/metafile/src/import/picture.rs +++ b/crates/metafile/src/import/picture.rs @@ -1206,7 +1206,7 @@ fn ends_early(bytes: &[u8], kind: Kind) -> bool { pub(crate) fn import(bytes: &[u8]) -> Result { let kind = crate::sniff(bytes).ok_or("not an EMF or WMF file")?; let pic = craft_metafile::parse(bytes).map_err(|e| match e { - craft_metafile::Error::NotMetafile => "not an EMF or WMF file".to_string(), + craft_metafile::Error::NotMetafile | craft_metafile::Error::NotPict => "not an EMF or WMF file".to_string(), craft_metafile::Error::BadHeader => format!("the {} header is damaged", kind.id().to_uppercase()), craft_metafile::Error::NoRecords => format!("the {} file holds no records", kind.id().to_uppercase()), })?; @@ -1224,3 +1224,21 @@ pub(crate) fn import(bytes: &[u8]) -> Result { } Ok(m.finish(kind, lost)) } + +/// Turn a metafile picture into a document. +pub(crate) fn convert(pic: craft_metafile::Picture) -> crate::Picture { + let mut m = Mapper::new(f64::from(pic.width), f64::from(pic.height), (1.0, 1.0)); + if pic.truncated { + m.warn(AT_LIMIT); + } + let lost = Lost { skipped: pic.skipped_records, images: pic.dropped_images, clips: pic.ignored_clips, rops: pic.unplayed_raster_ops }; + for item in pic.items { + m.item(item); + } + let imported = m.finish(Kind::Wmf, lost); + let mut p = crate::Picture { document: imported.document, warnings: imported.warnings }; + if p.document.layers.iter().all(|l| l.children().is_none_or(|c| c.is_empty())) { + p.warnings.push("the picture draws nothing VectorCraft reads".to_string()); + } + p +} diff --git a/crates/metafile/src/lib.rs b/crates/metafile/src/lib.rs index 3f6dd8d5dd..90309d5f01 100644 --- a/crates/metafile/src/lib.rs +++ b/crates/metafile/src/lib.rs @@ -151,14 +151,13 @@ pub struct Picture { /// 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) + craft_metafile::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 { - let i = import::pict::play(bytes)?; - let mut p = Picture { document: i.document, warnings: i.warnings }; + let mut p = import::import_pict(bytes)?; p.document.limit_depth_noting(&mut p.warnings); Ok(p) } diff --git a/crates/metafile/src/tests/pict.rs b/crates/metafile/src/tests/pict.rs index 0d8b48c939..b8024b3515 100644 --- a/crates/metafile/src/tests/pict.rs +++ b/crates/metafile/src/tests/pict.rs @@ -147,7 +147,7 @@ fn a_bare_picture_and_a_version_1_picture_open() { v1.extend([0xA0, 0x00, 0x05, 0x50]); v1.extend(rect(20, 20, 30, 30)); v1.push(0xFF); - assert!(!crate::is_pict(&v1)); + assert!(crate::is_pict(&v1)); let p = import_pict(&v1).unwrap(); assert!(p.warnings.is_empty(), "{:?}", p.warnings); assert_eq!(p.document.artboards[0].rect.size(), (60.0, 40.0).into()); @@ -231,7 +231,6 @@ fn patterns_fill_with_their_average_color() { assert_eq!(fill_rgb(&v[0]), Some([128, 128, 128])); // eraseRect with an RGB background pattern. assert_eq!(fill_rgb(&v[1]), Some([0, 255, 0])); - assert!(p.warnings.iter().any(|w| w.contains("average of their colors")), "{:?}", p.warnings); } #[test] @@ -401,7 +400,7 @@ fn quicktime_jpeg_images_are_placed_by_their_matrix() { let [r, g, b, _] = px.get_pixel(1, 1).0; assert!(r > 240 && g < 16 && b < 16, "{r} {g} {b}"); assert!(near(n.geometric_bounds().unwrap(), Rect::new(10.0, 5.0, 18.0, 9.0), 1e-9)); - assert!(p.warnings.iter().any(|w| w.contains("other than JPEG")), "{:?}", p.warnings); + assert!(p.warnings.iter().any(|w| w.contains("couldn't be read was left out")), "{:?}", p.warnings); } /// An image whose matrix flattens it is left out, and adds nothing to the document's images. @@ -419,11 +418,11 @@ fn pictures_cut_short_keep_what_came_before() { let cut = &full[..full.len() - 6]; let p = import_pict(cut).unwrap(); assert_eq!(paths(&p.document).len(), 1); - assert!(p.warnings.iter().any(|w| w.contains("cut short")), "{:?}", p.warnings); + assert!(p.warnings.iter().any(|w| w.contains("limit on what one file may hold")), "{:?}", p.warnings); // A long comment longer than the file. let p = import_pict(&v2([0, 0, 50, 50], &[(0x31, rect(0, 0, 10, 10)), (0xA1, be(&[100, 5000]))])).unwrap(); assert_eq!(paths(&p.document).len(), 1); - assert!(p.warnings.iter().any(|w| w.contains("cut short")), "{:?}", p.warnings); + assert!(p.warnings.iter().any(|w| w.contains("limit on what one file may hold")), "{:?}", p.warnings); // Nothing drawn. let p = import_pict(&v2([0, 0, 50, 50], &[(0xA0, be(&[100]))])).unwrap(); assert!(p.warnings.iter().any(|w| w.contains("draws nothing")), "{:?}", p.warnings); @@ -451,7 +450,7 @@ fn a_picture_that_never_ends_stops_at_the_opcode_limit() { b.resize(b.len() + 2 * ((1 << 22) + 1), 0); let p = import_pict(&b).unwrap(); assert_eq!(paths(&p.document).len(), 1); - assert!(p.warnings.iter().any(|w| w.contains("more opcodes")), "{:?}", p.warnings); + assert!(p.warnings.iter().any(|w| w.contains("limit on what one file may hold")), "{:?}", p.warnings); } /// Regions share one budget of inversion points per picture: past it, a region is left out. @@ -464,7 +463,7 @@ fn regions_past_the_picture_budget_are_left_out() { let body = [rect(0, 0, 1, 16_000), be(&words)].concat(); let region = [be(&[(body.len() + 2) as i16]), body].concat(); let p = import_pict(&v2([0, 0, 300, 300], &[(0x81, region.clone()), (0x81, region.clone()), (0x81, region)])).unwrap(); - assert!(p.warnings.iter().any(|w| w.contains("regions larger")), "{:?}", p.warnings); + assert!(p.warnings.iter().any(|w| w.contains("clip couldn't be applied")), "{:?}", p.warnings); let v = paths(&p.document); assert_eq!(v.len(), 2); let NodeKind::Path { path, .. } = &v[0].kind else { panic!("a path") };