From 1c5f97acfcbf961da9a65589afe9aac425f891bf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 17:43:31 -0800 Subject: [PATCH] File formats: .cmx gray and CMY colors, unnamed layers, stricter chunk search - Colors in the older color fields read gray (model 9) and CMY (model 4) as the .cdr reader numbers them, instead of opening as black. - A dash pattern whose lengths are all 0 draws a solid line, as for a .cdr outline. - A curve whose point field is too short for its count and is not whole points with their type bytes is left out as damaged, instead of reading coordinates as point types. - A layer without a name comes in unnamed, and the importer names it by its place, as for a .cdr drawing. - After stray bytes, a chunk id starts with a letter and may hold digits and spaces, and the id found must name a chunk the reader uses or have a length that fits its list. Padding is no longer taken for a chunk. - A canvas or a container in an object's attributes gives the unsupported-objects warning. - A page with no size and no bounds is 612 x 792 pt, VectorCraft's own size, as for a .cdr page; PROVENANCE R61 says so. PROVENANCE also names the other program's .cmx reader, read for field order. --- crates/cdr/PROVENANCE.md | 18 +-- crates/cdr/README.md | 4 +- crates/cdr/src/cmx.rs | 44 +++++-- crates/cdr/src/synth_cmx.rs | 13 ++ crates/cdr/tests/it/cmx.rs | 137 +++++++++++++++++++++- crates/engine/src/cmd/fileio/tests_cmx.rs | 21 ++++ docs/file-format-parity.md | 4 +- docs/mcp.md | 11 +- 8 files changed, 223 insertions(+), 29 deletions(-) diff --git a/crates/cdr/PROVENANCE.md b/crates/cdr/PROVENANCE.md index ba2e8d642b..b710c7d4b2 100644 --- a/crates/cdr/PROVENANCE.md +++ b/crates/cdr/PROVENANCE.md @@ -44,7 +44,11 @@ Sources: of their drawing. - **CN**: the `.cmx` notes of an MIT OR Apache-2.0 drawing program, which mark each layout "confirmed" when the program's authors found it in sample files, - https://github.com/tracedraw/tracedraw/blob/2ac6d02e97773e040a51be6cfb27cc424e3deeaa/docs/cmx-format.md + https://github.com/tracedraw/tracedraw/blob/2ac6d02e97773e040a51be6cfb27cc424e3deeaa/docs/cmx-format.md. + The same program's `.cmx` reader at that commit, + https://github.com/tracedraw/tracedraw/blob/2ac6d02e97773e040a51be6cfb27cc424e3deeaa/crates/cdr/src/cmx.rs, + was read for the order of fields that the notes do not spell out (the table entries, the rendering + attributes and the curve body); no code was copied from it Status: `public-spec` (K, KF, KR, Z, F or PR), `sample` (S), `public-notes` (a layout CN marks confirmed that no CX file shows), `unconfirmed` (a point no source states and no sample file shows; @@ -110,14 +114,14 @@ file is committed. | R50 | Bit 5 (0x20) of the outline's line type scales the outline's width and dashes with the shape's matrix. The engine multiplies them by the square root of the matrix's area factor (VectorCraft's own value for a matrix that scales x and y differently) | S: UC `outl_scale_with_cdr7.cdr` to `outl_scale_with_cdr12.cdr`, of whose outlines only one, 3.969 mm wide, has line type 0x22 and the others 0x02 or 0x01; TDF 76771 (version 15), whose outline of line type 0x22 is 10 mm wide on a shape that the matrix stretches and skews, and whose picture draws that outline several times wider and skewed | sample (the bit; the single width for a matrix that scales x and y differently: VectorCraft's own) | | R51 | A `trfd` offsets table holds one offset more than the entries; each entry runs from its offset to the next, and the last offset is the record's end | KF L1079, L1113-L1121; S: every `trfd` of versions 4 to 25 in the sample files (25,425 records) has the extra offset, equal to the record's length | public-spec | | R52 | Byte 0 of argument 14001 is the fill's winding flag. VectorCraft reads a byte other than 0 as the nonzero fill rule, and 0 or an object without the argument as even-odd | KF L575-L583, L1047; S: the argument is 4 bytes in the sample files that carry it, with byte 0 = 1 on LT `complexshape_blackfill_nooutline` of `testset/12` and `testset/15` to `testset/25` and on objects of TDF 127576 (13) and 60026 (14), and 0 on 27 objects of TP `example.cdr` (17) | public-spec (the flag: KF; which rule each value means, and the rule without the argument: VectorCraft's own reading, and a wrong reading fills or leaves holes where a curve's subpaths overlap) | -| R53 | A `.cmx` file is a RIFF form of type `CMX1`, little-endian (a `RIFX` form is big-endian and is refused); its chunks are laid out as R4. A chunk can run short of its contents: when the bytes after it don't read as a chunk id, the next id is four letters at an even distance of 2 to 64 bytes | CN "Container"; S: all 14 CX files are `RIFF` forms of type `CMX1`; in `balloon.cmx`, `bars.cmx`, `bluecros.cmx`, `bluedrop.cmx`, `gold_bar.cmx`, `goldblue.cmx`, `squares.cmx` and `thatch.cmx` 16 bytes follow `ccmm` and 4 follow `rlst` before the next id | sample | +| R53 | A `.cmx` file is a RIFF form of type `CMX1`, little-endian (a `RIFX` form is big-endian and is refused); its chunks are laid out as R4. A chunk can run short of its contents: when the bytes after it don't read as a chunk id (an ASCII letter, then three ASCII letters, digits or spaces), the next id is the first at an even distance of 2 to 64 bytes that names a chunk the reader uses or whose length keeps it inside its list (VectorCraft's own rule) | CN "Container"; S: all 14 CX files are `RIFF` forms of type `CMX1`; in `balloon.cmx`, `bars.cmx`, `bluecros.cmx`, `bluedrop.cmx`, `gold_bar.cmx`, `goldblue.cmx`, `squares.cmx` and `thatch.cmx` 16 bytes follow `ccmm` and 4 follow `rlst` before the next id | sample | | R54 | `cont`: byte 52 is the coordinate size as an ASCII digit, `4` for 32-bit coordinates and `2` for 16-bit ones (refused); bytes 64 to 72 hold an f64, the size of a coordinate unit in meters. A value that isn't above 0 and at most 1 reads as 0.1 µm (VectorCraft's own fallback) | CN "Container"; S: the nine CX files with `4` all hold 1e-7; `abydos.cmx`, `CM33.CMX`, `LIZARDS.CMX`, `output.000.cmx` and `test.cmx` hold `2` | sample | | R55 | A table (`rclr`, `rpen`, `rott`, `rdot`, `rotl`) is a u16 entry count, then one field list per entry; an instruction's body is field lists too. A field is a u8 tag, a u16 length counting its 3 header bytes, and the data; 0xff ends the list. Objects name table entries from 1 | CN "Tables"; S: every one of these tables in the nine CX files with 32-bit coordinates reads its count of entries with no bytes left | sample | -| R56 | A color entry: a property list in field 10 (a u32 length, then parts laid out as R11's, the color in part 1 as R14), or in older files the model and palette bytes in field 1 and the values in field 2. Models of the older fields: 2 cyan, magenta, yellow and black in percent; 3 the same in 0 to 255; 5 red, green and blue in 0 to 255. Other models read as black with a warning | CN "Tables" (`rclr`); S: every color of the nine CX files is model 2 in the older fields | sample (model 2), public-notes (field 10), unconfirmed (models 3 and 5 of the older fields, read as the program of CN reads them; a wrong reading gives a wrong color) | -| R57 | `rpen` field 1: the width as a u32 in coordinate units (then an aspect, an angle and a matrix, not read). `rott` field 1: a flags byte (1 no outline, 2 solid, 4 dashed, 8 behind the fill, 16 scaled with the object), then the caps in the low 4 bits and the join in the high 4 bits, numbered as R12. `rdot` field 1: a u16 count, then the dash and gap lengths in outline widths (u16 each) | CN "Tables"; S: line types 0x02 and 0x22 with caps and joins 0x00, 0x01 and 0x11 in the CX files | public-notes (the flags 4, 8 and 16 and the cap and join numbering; bit 0x20, set in `balloon.cmx`, is not read) | +| R56 | A color entry: a property list in field 10 (a u32 length, then parts laid out as R11's, the color in part 1 as R14), or in older files the model and palette bytes in field 1 and the values in field 2. Models of the older fields: 2 cyan, magenta, yellow and black in percent; 3 the same in 0 to 255; 4 cyan, magenta and yellow in 0 to 255; 5 red, green and blue in 0 to 255; 9 gray in 0 to 255, 255 white. Other models read as black with a warning | CN "Tables" (`rclr`); S: every color of the nine CX files is model 2 in the older fields | sample (model 2), public-notes (field 10), unconfirmed (models 3 and 5 of the older fields, read as the program of CN reads them, and models 4 and 9, read as R14's; a wrong reading gives a wrong color) | +| R57 | `rpen` field 1: the width as a u32 in coordinate units (then an aspect, an angle and a matrix, not read). `rott` field 1: a flags byte (1 no outline, 2 solid, 4 dashed, 8 behind the fill, 16 scaled with the object), then the caps in the low 4 bits and the join in the high 4 bits, numbered as R12. `rdot` field 1: a u16 count, then the dash and gap lengths in outline widths (u16 each). A pattern whose lengths are all 0 draws a solid line, as R48's | CN "Tables"; S: line types 0x02 and 0x22 with caps and joins 0x00, 0x01 and 0x11 in the CX files | public-notes (the flags 4, 8 and 16 and the cap and join numbering; bit 0x20, set in `balloon.cmx`, is not read) | | R58 | `rotl` field 1: six u16, the 1-based entries of the outline's line type, screen, color, arrowhead pair, pen and dash pattern | CN "Tables"; S: every `rotl` entry of the CX files names entries that exist | sample | | R59 | `rota`: a u16 count, then pairs of u16 (start and end arrowheads, 0 for none), not field lists | CN "Tables"; S: `rota` is `01 00 00 00 00 00` in every CX file | sample (pairs of 0), public-notes (other values) | | R60 | A `page` chunk is a stream of instructions: a u16 size counting the whole instruction and an i16 code, or 0xFFFF, a u32 size and the code (the long form); then field lists. Codes: 9 and 10 begin and end a page, 11 and 12 a layer, 13 and 14 a group, 67 a curve; 20 and 21 a text object, 65 its drawn characters, 66 an ellipse, 68 a rectangle, 69 an image, 88 and 89 add and remove a clipping path, 111 a jump whose field 1 is the u32 file offset to go on from (the bytes jumped over hold images). Objects are drawn in file order, the first at the back | CN "page: instructions"; S: codes 9 to 14 and 67 in the CX files; in `squares.cmx` the large square comes first and the two smaller ones inside it after | sample (9 to 14, 67, the order), public-notes (the long form and the other codes) | -| R61 | Field 4 of instruction 9: the page's width and height, u32 each in coordinate units. Without it, field 1 gives the drawing's bounds: a u16 page number, a u32, then the left, top, right and bottom edges as i32 in coordinate units; the page takes the bounds' size and is centered on them (VectorCraft's own choice) | CN "page: instructions"; S: none of the CX files has field 4; in all nine with 32-bit coordinates, the four edges in field 1 match the extent of the file's curves | public-notes (field 4), sample (field 1) | -| R62 | A layer's name: field 4 as a u32 count and UTF-16 units, or field 1 from byte 12 as a u16 count and 8-bit characters (read as Windows-1252, VectorCraft's own choice). A curve: field 1 its rendering attributes, field 2 a u16 point count, the points as i32 x and y, then a type byte for each as R16; coordinates are from the page's center, y up | CN "page: instructions" and "Coordinates"; S: the 8-bit names `Layer 1`, `Desktop`, `Grid` and `Guides` of the CX files (the last three hold nothing in every file); every curve of the CX files has field 2 of exactly 2 + 9 bytes per point | sample (8-bit names, curves), public-notes (field 4) | -| R63 | Rendering attributes: a mask (1 fill, 2 outline, 4 lens), then a field list for the fill when the mask names it and one for the outline. The fill's field 1 is a u16 type and a field list; type 0 is no fill, type 1 a uniform fill whose field 1 is the color entry (u16) and a screen (u16); other types (2 fountain and more) are not read. The outline's field 1 is its `rotl` entry (u16) | CN "Rendering attributes"; S: masks 1, 2 and 3, fill types 1, 2 and 6, and uniform fills with a 4-byte field 1 in the CX files | sample | +| R61 | Field 4 of instruction 9: the page's width and height, u32 each in coordinate units. Without it, field 1 gives the drawing's bounds: a u16 page number, a u32, then the left, top, right and bottom edges as i32 in coordinate units; the page takes the bounds' size and is centered on them (VectorCraft's own choice). Without either, the page has no size, and the importer gives it 612 × 792 pt as it does a `.cdr` page without one (VectorCraft's own size) | CN "page: instructions"; S: none of the CX files has field 4; in all nine with 32-bit coordinates, the four edges in field 1 match the extent of the file's curves | public-notes (field 4), sample (field 1) | +| R62 | A layer's name: field 4 as a u32 count and UTF-16 units, or field 1 from byte 12 as a u16 count and 8-bit characters (read as Windows-1252, VectorCraft's own choice); a layer without a name is left unnamed, as a `.cdr` layer is. A curve: field 1 its rendering attributes, field 2 a u16 point count, the points as i32 x and y, then a type byte for each as R16; coordinates are from the page's center, y up. A field 2 too short for its count is read as whole points with their type bytes when its length is a multiple of 9, and the curve is left out as damaged otherwise (VectorCraft's own rule) | CN "page: instructions" and "Coordinates"; S: the 8-bit names `Layer 1`, `Desktop`, `Grid` and `Guides` of the CX files (the last three hold nothing in every file); every curve of the CX files has field 2 of exactly 2 + 9 bytes per point | sample (8-bit names, curves), public-notes (field 4) | +| R63 | Rendering attributes: a mask (1 fill, 2 outline, 4 lens, 8 canvas, 16 container; lenses, canvases and containers are not read, with a warning), then a field list for the fill when the mask names it and one for the outline. The fill's field 1 is a u16 type and a field list; type 0 is no fill, type 1 a uniform fill whose field 1 is the color entry (u16) and a screen (u16); other types (2 fountain and more) are not read. The outline's field 1 is its `rotl` entry (u16) | CN "Rendering attributes"; S: masks 1, 2 and 3, fill types 1, 2 and 6, and uniform fills with a 4-byte field 1 in the CX files | sample | diff --git a/crates/cdr/README.md b/crates/cdr/README.md index e0ad0b1eb8..007258e931 100644 --- a/crates/cdr/README.md +++ b/crates/cdr/README.md @@ -66,10 +66,10 @@ arrowhead tables, then each page's instructions in drawing order: | In the `.cmx` file | Read as | |---|---| | Pages, with the size the page gives, or else the size of the drawing's bounds that the page stores | pages; a page sized to the bounds is centered on them | -| Layers, by their UTF-16 or 8-bit name | drawing layers; a layer that holds nothing is left out | +| Layers, by their UTF-16 or 8-bit name | drawing layers, unnamed when the file gives no name; a layer that holds nothing is left out | | Groups | groups | | Curves (moves, lines and cubic curves, as `.cdr` point lists) | paths | -| Uniform fills: CMYK in percent or 0 to 255, RGB, and library colors with their names | colors, spot colors | +| Uniform fills: CMYK in percent or 0 to 255, CMY, RGB, gray, and library colors with their names | colors, spot colors | | Outlines: width, color, caps, joins, dashes, the no-outline, behind-the-fill and scaled-with flags | outlines | Text, images, rectangles, ellipses, clipping paths, fills other than uniform ones, outline diff --git a/crates/cdr/src/cmx.rs b/crates/cdr/src/cmx.rs index 5bd7439291..fc343a1870 100644 --- a/crates/cdr/src/cmx.rs +++ b/crates/cdr/src/cmx.rs @@ -108,7 +108,7 @@ fn chunks<'a>(b: &'a [u8], budget: &mut Budget, warn: &mut Vec) -> Resu while let Some((mut pos, end, depth)) = stack.pop() { while pos.saturating_add(8) <= end { let Some(id) = Rd::at(b, pos).fourcc() else { break }; - if !id.iter().all(|c| c.is_ascii_alphanumeric() || *c == b' ') { + if !is_id(&id) { match resync(b, pos, end) { Some(next) => { pos = next; @@ -157,12 +157,18 @@ fn chunks<'a>(b: &'a [u8], budget: &mut Budget, warn: &mut Vec) -> Resu Ok(out) } -/// Where the next chunk id after `pos` lies: four letters at an even distance of 2 to [`RESYNC`] -/// bytes (R53). +/// Is `t` a chunk id: an ASCII letter, then three ASCII letters, digits or spaces (R53)? +fn is_id(t: &[u8]) -> bool { + matches!(t, [first, rest @ ..] if t.len() == 4 && first.is_ascii_alphabetic() && rest.iter().all(|c| c.is_ascii_alphanumeric() || *c == b' ')) +} + +/// Where the next chunk after `pos` lies: the first chunk id at an even distance of 2 to [`RESYNC`] +/// bytes that names a chunk the reader uses, or another chunk whose length keeps it inside the list +/// that ends at `end` (R53). fn resync(b: &[u8], pos: usize, end: usize) -> Option { - (pos + 2..=pos.saturating_add(RESYNC).min(end.saturating_sub(8))) - .step_by(2) - .find(|&p| b.get(p..p + 4).is_some_and(|t| t.iter().all(u8::is_ascii_alphabetic))) + let fits = |p: usize| u32_at(b, p + 4).and_then(|s| (p + 8).checked_add(s as usize)).is_some_and(|stop| stop <= end); + let chunk_at = |p: usize| Rd::at(b, p).fourcc().is_some_and(|id| is_id(&id) && (READ_IDS.contains(&id) || fits(p))); + (pos + 2..=pos.saturating_add(RESYNC).min(end.saturating_sub(8))).step_by(2).find(|&p| chunk_at(p)) } /// The end of the field list at `pos` of `b`: after its 0xff, or the end of `b` when a field runs @@ -278,6 +284,8 @@ impl Tables { if flags & 4 != 0 { let stored = usize::from(dash).checked_sub(1).and_then(|i| self.dashes.get(i)); match stored { + // A pattern of zero lengths draws as a solid line, as in a `.cdr` outline (R48). + Some(d) if d.iter().all(|x| *x == 0) => {} Some(d) if budget.take_dashes(d.len()) => dashes = d.iter().map(|x| f64::from(*x)).collect(), Some(_) => push(warn, Warning::TooDeep), None => push(warn, Warning::Damaged), @@ -323,7 +331,9 @@ fn color(e: &[u8], record: Option<&Profile>, warn: &mut Vec) -> Option< Some(match model { 2 => Color::Cmyk([pct(0)?, pct(1)?, pct(2)?, pct(3)?]), 3 => Color::Cmyk([full(0)?, full(1)?, full(2)?, full(3)?]), + 4 => Color::Cmyk([full(0)?, full(1)?, full(2)?, 0.0]), 5 => Color::Rgb([full(0)?, full(1)?, full(2)?]), + 9 => Color::Gray(1.0 - full(0)?), _ => { push(warn, Warning::UnknownColors); Color::Rgb([0.0; 3]) @@ -453,7 +463,8 @@ impl<'t, 'b> PageRead<'t, 'b> { } BEGIN_LAYER => { self.close_layer(); - let name = self.layer_name(body).unwrap_or_else(|| format!("Layer {}", self.layers.len() + 1)); + // A layer without a name is left unnamed, as in a `.cdr` drawing. + let name = self.layer_name(body).unwrap_or_default(); self.open = Some(LayerRead { name, lists: vec![Vec::new()] }); } END_LAYER => self.close_layer(), @@ -515,10 +526,9 @@ impl<'t, 'b> PageRead<'t, 'b> { } } - /// The open layer, or a new one when objects come before any layer begins. + /// The open layer, or a new unnamed one when objects come before any layer begins. fn layer(&mut self) -> &mut LayerRead { - let n = self.layers.len() + 1; - self.open.get_or_insert_with(|| LayerRead { name: format!("Layer {n}"), lists: vec![Vec::new()] }) + self.open.get_or_insert_with(|| LayerRead { name: String::new(), lists: vec![Vec::new()] }) } fn close_layer(&mut self) { @@ -560,11 +570,17 @@ impl<'t, 'b> PageRead<'t, 'b> { }; // `stored` points are read, so the type bytes are found after all of them, and the first // `n`, those that have a type byte, are kept. When the field holds every point, a short - // field has lost type bytes; otherwise it holds as many whole points and types as fit. + // field has lost type bytes. When it holds whole points and their types for fewer points, + // the count is too large. Otherwise the field lost points, and its type bytes can't be + // found: the curve is lost. let left = r.left(); let (stored, n) = match count.checked_mul(8).filter(|c| *c <= left) { Some(c) => (count, count.min(left - c)), - None => (count.min(left / 9), count.min(left / 9)), + None if left.is_multiple_of(9) => (left / 9, left / 9), + None => { + push(self.warn, Warning::Damaged); + return None; + } }; if n < count { push(self.warn, Warning::Damaged); @@ -626,6 +642,10 @@ impl<'t, 'b> PageRead<'t, 'b> { if mask & 4 != 0 { push(self.warn, Warning::Lenses); } + // A canvas (8) or a container (16) is not read. + if mask & (8 | 16) != 0 { + push(self.warn, Warning::UnsupportedObjects); + } (fill, outline) } } diff --git a/crates/cdr/src/synth_cmx.rs b/crates/cdr/src/synth_cmx.rs index 8cb07aadb6..c70fd8545c 100644 --- a/crates/cdr/src/synth_cmx.rs +++ b/crates/cdr/src/synth_cmx.rs @@ -69,12 +69,25 @@ pub fn color(model: u8, values: &[u8]) -> Vec { /// A color entry with a property list in field 10 whose color record (part 1) holds `model` and /// the four value bytes `v` (R56). pub fn color_record(model: u16, v: [u8; 4]) -> Vec { + color_record_named(model, v, None) +} + +/// A color entry as [`color_record`], with a name part (part 3: a u32 count and UTF-16 units) +/// after the color when `name` is given (R56). +pub fn color_record_named(model: u16, v: [u8; 4], name: Option<&str>) -> Vec { let mut part = vec![1u8]; part.extend(12u32.to_le_bytes()); part.extend(model.to_le_bytes()); part.extend(5u16.to_le_bytes()); part.extend([0; 4]); part.extend(v); + if let Some(name) = name { + let units: Vec = name.encode_utf16().collect(); + part.push(3); + part.extend((4 + 2 * units.len() as u32).to_le_bytes()); + part.extend((units.len() as u32).to_le_bytes()); + part.extend(units.iter().flat_map(|u| u.to_le_bytes())); + } let mut rec = (part.len() as u32).to_le_bytes().to_vec(); rec.extend(part); fields(&[field(10, &rec)]) diff --git a/crates/cdr/tests/it/cmx.rs b/crates/cdr/tests/it/cmx.rs index 643df2779b..381eaddfcb 100644 --- a/crates/cdr/tests/it/cmx.rs +++ b/crates/cdr/tests/it/cmx.rs @@ -120,7 +120,8 @@ fn objects_before_any_layer_get_one() { let p = page(&[begin_page(None), square(&attrs(Some(1), None, false), 0, 0, 1000)]); let d = read_cmx(&file(&[p]), Limits::default()).unwrap(); assert_eq!(d.pages[0].layers.len(), 1); - assert_eq!(d.pages[0].layers[0].name, "Layer 1"); + // The layer is left unnamed, as a `.cdr` drawing's is; the importer names it by its place. + assert_eq!(d.pages[0].layers[0].name, ""); } #[test] @@ -219,6 +220,31 @@ fn colors_of_other_models_and_missing_entries() { assert_eq!(shape(&objects[1]).fill, Some(Color::Rgb([0.0; 3]))); } +#[test] +fn gray_cmy_and_rgb_colors_of_the_older_fields() { + let mut children = vec![cont(b'4', 1e-7)]; + children.push(page(&[ + begin_page(None), + begin_layer("L", None), + square(&attrs(Some(1), None, false), 0, 0, 1000), + square(&attrs(Some(2), None, false), 0, 0, 1000), + square(&attrs(Some(3), None, false), 0, 0, 1000), + ])); + children.push(table(b"rclr", &[color(9, &[51]), color(4, &[255, 0, 51]), color(5, &[255, 0, 0])])); + let d = read_cmx(&riff(&children), Limits::default()).unwrap(); + assert!(d.warnings.is_empty(), "{:?}", d.warnings); + let objects = &d.pages[0].layers[0].objects; + // The model holds objects front to back: the RGB square, the CMY one, then the gray one. + assert_eq!(shape(&objects[0]).fill, Some(Color::Rgb([1.0, 0.0, 0.0]))); + match (shape(&objects[1]).fill.clone(), shape(&objects[2]).fill.clone()) { + (Some(Color::Cmyk([c, m, y, k])), Some(Color::Gray(g))) => { + assert!((c - 1.0).abs() < 1e-6 && m == 0.0 && (y - 0.2).abs() < 1e-6 && k == 0.0, "{c} {m} {y} {k}"); + assert!((g - 0.8).abs() < 1e-6, "{g}"); + } + other => panic!("{other:?}"), + } +} + #[test] fn outline_arrowheads_are_reported() { let mut children = vec![cont(b'4', 1e-7)]; @@ -411,6 +437,115 @@ fn stray_letters_with_a_length_past_the_file_are_passed_over() { assert_eq!(d.pages[0].layers[0].objects.len(), 1); } +#[test] +fn a_dash_pattern_of_zero_lengths_draws_a_solid_line() { + let mut children = vec![cont(b'4', 1e-7)]; + children.push(page(&[begin_page(None), begin_layer("L", None), square(&attrs(None, Some(1), false), 0, 0, 1000)])); + children.extend(tables().into_iter().filter(|t| !t.starts_with(b"rdot") && !t.starts_with(b"rotl"))); + children.push(table(b"rdot", &[dashes(&[0, 0])])); + children.push(table(b"rotl", &[outline([3, 1, 2, 1, 1, 1])])); + let d = read_cmx(&riff(&children), Limits::default()).unwrap(); + assert!(d.warnings.is_empty(), "{:?}", d.warnings); + let o = shape(&d.pages[0].layers[0].objects[0]).outline.clone().unwrap(); + assert!(o.dashes.is_empty(), "{:?}", o.dashes); +} + +#[test] +fn a_curve_field_that_lost_points_is_left_out() { + // Three points claimed, two whole points stored and the third cut inside its y: the type + // bytes can't be found, so no node types are read from coordinates. + let mut g = 3u16.to_le_bytes().to_vec(); + for (x, y) in [(0i32, 0i32), (1_000_000, 0)] { + g.extend(x.to_le_bytes()); + g.extend(y.to_le_bytes()); + } + g.extend(1_000_000i32.to_le_bytes()); + g.extend([0x40, 0x42]); + let c = instr(67, &fields(&[field(1, &attrs(Some(1), None, false)), field(2, &g)])); + let d = read_cmx(&file(&[page(&[begin_page(None), begin_layer("L", None), c])]), Limits::default()).unwrap(); + assert!(d.warnings.contains(&Warning::Damaged), "{:?}", d.warnings); + assert!(d.pages[0].layers.is_empty(), "{:?}", d.pages[0].layers); +} + +#[test] +fn a_chunk_whose_id_has_a_digit_is_found_after_stray_bytes() { + // Stray bytes, a chunk the reader passes over whose id ends in a digit, then the page. + let p = page(&[begin_page(None), begin_layer("L", None), square(&attrs(Some(1), None, false), 0, 0, 1000), end_layer()]); + let mut b = vec![1u8; 6]; + b.extend(chunk(b"inf1", &[0; 70])); + b.extend(p); + let mut children = vec![cont(b'4', 1e-7), b]; + children.extend(tables()); + let d = read_cmx(&riff(&children), Limits::default()).unwrap(); + assert!(d.warnings.is_empty(), "{:?}", d.warnings); + assert_eq!(d.pages.len(), 1); + assert_eq!(d.pages[0].layers[0].objects.len(), 1); +} + +#[test] +fn padding_and_ids_with_impossible_lengths_are_not_taken_for_chunks() { + // After stray bytes, four spaces with a small length, or letters with a length past the list, + // come before the page: neither is taken for a chunk, so the page is found. + for filler in [b" \x08\x00\x00\x00".to_vec(), b"junk\x40\x42\x0f\x00".to_vec()] { + let p = page(&[begin_page(None), begin_layer("L", None), square(&attrs(Some(1), None, false), 0, 0, 1000), end_layer()]); + let mut b = vec![1u8; 2]; + b.extend(filler); + b.extend(p); + let mut children = vec![cont(b'4', 1e-7), b]; + children.extend(tables()); + let d = read_cmx(&riff(&children), Limits::default()).unwrap(); + assert!(d.warnings.is_empty(), "{:?}", d.warnings); + assert_eq!(d.pages.len(), 1); + assert_eq!(d.pages[0].layers[0].objects.len(), 1); + } +} + +#[test] +fn after_stray_bytes_a_chunk_the_reader_passes_over_keeps_what_it_holds() { + // Stray bytes, then a chunk the reader passes over whose body starts with the letters of a + // page chunk, then the page: only the page is read. + let p = page(&[begin_page(None), begin_layer("L", None), square(&attrs(Some(1), None, false), 0, 0, 1000), end_layer()]); + let mut inner = b"page".to_vec(); + inner.extend(4u32.to_le_bytes()); + inner.extend([0; 10]); + let mut b = vec![1u8; 2]; + b.extend(chunk(b"inf1", &inner)); + b.extend(p); + let mut children = vec![cont(b'4', 1e-7), b]; + children.extend(tables()); + let d = read_cmx(&riff(&children), Limits::default()).unwrap(); + assert!(d.warnings.is_empty(), "{:?}", d.warnings); + assert_eq!(d.pages.len(), 1); + assert_eq!(d.pages[0].layers[0].objects.len(), 1); +} + +#[test] +fn a_canvas_or_a_container_is_reported() { + for bit in [8u8, 16] { + let mut a = attrs(Some(1), None, false); + a[0] |= bit; + let p = page(&[begin_page(None), begin_layer("L", None), square(&a, 0, 0, 1000), end_layer()]); + let d = read_cmx(&file(&[p]), Limits::default()).unwrap(); + assert_eq!(d.warnings, vec![Warning::UnsupportedObjects], "{bit}"); + assert!(shape(&d.pages[0].layers[0].objects[0]).fill.is_some()); + } +} + +#[test] +fn a_library_color_with_its_name_reads_as_a_spot_color() { + let mut children = vec![cont(b'4', 1e-7)]; + children.push(page(&[begin_page(None), begin_layer("L", None), square(&attrs(Some(1), None, false), 0, 0, 1000)])); + children.push(table(b"rclr", &[color_record_named(5, [0, 0, 255, 0], Some("Brick 12"))])); + let d = read_cmx(&riff(&children), Limits::default()).unwrap(); + match shape(&d.pages[0].layers[0].objects[0]).fill.clone() { + Some(Color::Spot { name, tint, look }) => { + assert_eq!((name.as_str(), tint), ("Brick 12", 1.0)); + assert!(look.is_some()); + } + other => panic!("{other:?}"), + } +} + proptest! { #![proptest_config(ProptestConfig { cases: 256, ..ProptestConfig::default() })] diff --git a/crates/engine/src/cmd/fileio/tests_cmx.rs b/crates/engine/src/cmd/fileio/tests_cmx.rs index d374737143..527c0be1be 100644 --- a/crates/engine/src/cmd/fileio/tests_cmx.rs +++ b/crates/engine/src/cmd/fileio/tests_cmx.rs @@ -122,3 +122,24 @@ fn files_that_are_not_read_say_why() { let e = open_bytes(&mut s, "d.cmx", b"not a cmx file", None).unwrap_err().to_string(); assert!(e.contains("this CMX file can't be read (not a CMX file)"), "{e}"); } + +#[test] +fn a_layer_without_a_name_is_named_by_its_place() { + // An empty layer, which is left out, then a layer named only by spaces: it comes in unnamed, + // and the importer numbers it by its place among the layers it keeps, as for a `.cdr` drawing. + let p = page(&[ + begin_page(None), + begin_layer("Grid", None), + end_layer(), + begin_layer(" ", None), + square(&attrs(Some(2), None, false), 0, 0, 1000), + end_layer(), + ]); + let mut children = vec![cont(b'4', 1e-7), p]; + children.extend(tables()); + let mut s = Session::new(); + let r = open_bytes(&mut s, "drawing.cmx", &riff(&children), None).unwrap(); + assert!(warnings(&r).is_empty(), "{r}"); + let names: Vec = s.doc().unwrap().doc.layers.iter().map(|l| l.name.clone().unwrap_or_default()).collect(); + assert_eq!(names, ["Layer 1"]); +} diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 9f7fe56646..61a01148a8 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -21,7 +21,7 @@ Ordinary File > Save offers Save As when its own location refuses writes with a | | Illustrator | VectorCraft | Notes | |---|---:|---:|---| -| Formats opened or placed | ~30 | 34 | missing: FreeHand, DWG; extra: `.cmx` | +| Formats opened or placed | ~30 | 34 | missing: FreeHand, DWG | | Formats saved (Save As) | 6 (AI, AIT, PDF, EPS, SVG, SVGZ) | 6 + native | `.ai` written PDF-compatible (with an editing copy of the art: layers, groups, paths, colors, gradients, patterns, images, type, symbols, artboards, transparency, opacity masks, clipping, text wrap options) or as legacy version 7 (layers and groups, one artboard per file) | | Formats exported | ~16 | 18 | missing: DWG; extra: GIF/PNG-8 palette export, DXF up to 2018, layered PSD | | **Overall (weighted by use)** | | **~75%** | `.ai` and PDF dominate the weighting | @@ -336,7 +336,7 @@ Gradient meshes in the editing data keep each point's own color where a patch's | Pixar PXR | open, place (legacy) | yes (any set of the R, G, B and A channels in 8 or 12 bits, dumped or run-length encoded, in tiles; R alone is gray; alpha matted to black or unassociated) | no | recognized by its magic number; 12-bit samples are clamped to 0.0 to 1.0; null tiles are transparent with A and black without; the offsets aren't read, and the image takes the default resolution; a file cut short, with header values or packets the format doesn't define, or past the pixel allowance is refused with a message; tested on synthetic files only; tests in `crates/engine/src/cmd/fileio/tests_pxrread.rs` and `crates/engine/tests/import_fuzz.rs` | | FreeHand `.fh*` | open (legacy) | no | no | | | `.cdr` drawings | open | yes (versions 1 to 27, the `WL` files of versions 1 and 2 and the ZIP packages of versions 14 on included: pages, layers, guidelines, groups, clipping containers, curves, paths, rectangles, ellipses, arcs, polygons, uniform fills and outlines, from the object's style where it has none of its own; hidden and locked layers, groups and objects; the winding flag as the nonzero fill rule; uniform transparency of version 13 on as opacity and blend mode; text, bitmaps, blends, contours, brush strokes, fountain, pattern, texture and PostScript fills and arrowheads are left out with a warning, and lenses, other transparency and merge modes without a blend mode are not applied, with a warning; versions 1 and 2 open as one page and one layer without groups, arcs or pies; version 3 envelopes are not applied, so the objects and groups under one keep their outlines and matrices, with a warning; encrypted, ZIP64 and split packages are refused) | no | `.cdt` templates open as new untitled documents; File › Place reads both; after an open with a warning, an export or Save As to the drawing's own path needs `acknowledgeLoss: true`; record layouts and their sources in `crates/cdr/PROVENANCE.md`; synthetic test files only; tests in `crates/cdr/tests/it/read.rs`, `crates/engine/src/cmd/fileio/tests_cdr.rs` and `crates/engine/tests/import_fuzz.rs` | -| `.cmx` exchange files | no (not in the [supported formats](https://helpx.adobe.com/illustrator/desktop/get-started/learn-the-basics/supported-file-formats-in-illustrator.html) list) | yes (files with 32-bit coordinates: pages, layers, groups, curves, uniform fills and outlines with their width, color, caps, joins and dashes; text, images, rectangles, ellipses, clipping paths, other fills and arrowheads are left out with a warning, and lenses are not applied, with a warning; big-endian files and files with 16-bit coordinates are refused) | no | File › Place reads them too; after an open with a warning, an export or Save As to the file's own path needs `acknowledgeLoss: true`; layouts and their sources in `crates/cdr/PROVENANCE.md` (R53 to R63); synthetic test files only; tests in `crates/cdr/tests/it/cmx.rs`, `crates/engine/src/cmd/fileio/tests_cmx.rs` and `crates/engine/tests/import_fuzz.rs` | +| `.cmx` exchange files | | yes (files with 32-bit coordinates: pages, layers, groups, curves, uniform fills and outlines with their width, color, caps, joins and dashes; text, images, rectangles, ellipses, clipping paths, other fills and arrowheads are left out with a warning, and lenses are not applied, with a warning; big-endian files and files with 16-bit coordinates are refused) | no | File › Place reads them too; after an open with a warning, an export or Save As to the file's own path needs `acknowledgeLoss: true`; layouts and their sources in `crates/cdr/PROVENANCE.md` (R53 to R63); synthetic test files only; tests in `crates/cdr/tests/it/cmx.rs`, `crates/engine/src/cmd/fileio/tests_cmx.rs` and `crates/engine/tests/import_fuzz.rs` | | Text `.txt` | place, export | place (Text Import Options) | yes | place reads Unicode and ANSI, and Shift JIS, GB2312, Chinese Big 5, Cyrillic, GB18030, Greek, Turkish, Baltic and Central European through the WHATWG Encoding Standard's decoders (VectorCraft's own mapping); `tests/textimport.rs`, `place/text.rs`; fuzzed (`text_import_fuzz.rs`) | | RTF `.rtf` | place | place (area type with fonts, size, bold, italic, underline, strikethrough, caps, superscript and subscript, color, alignment, indents and paragraph spacing) | no | `tests/textimport.rs`, `place/rtf.rs`; fuzzed (`text_import_fuzz.rs`); `\'hh` bytes read as Windows-1252 or Mac Roman only | | Word `.docx` | place | place (area type with the document's styles and its fonts, size, bold, italic, underline, strikethrough, caps, superscript and subscript, color, alignment, indents and paragraph spacing) | no | `tests/textimport.rs`, `place/docx.rs`; fuzzed (`text_import_fuzz.rs`); list numbers, pictures and text boxes are left out | diff --git a/docs/mcp.md b/docs/mcp.md index f6532754bf..041747af0c 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -10197,12 +10197,13 @@ its header fit the file. A RIFF file that names a version before 3 is refused wi `.cmx` exchange files with 32-bit coordinates open and place as editable art, found by their content whatever their extension. Each page becomes an artboard, side by side, at the size the page gives. A page that gives no size takes -the size of the drawing's bounds stored with it, centered on the art, or 612 × 792 pt when it stores none either. -Layers keep their names, and a layer that holds nothing is left out. Groups stay groups, and curves become paths with -their uniform fills and outlines: colors in CMYK or RGB, and library colors stored with their names as spot swatches; -outline widths, caps, joins, dashes, an outline behind the fill and an outline scaled with the object. Text, images, +the size of the drawing's bounds stored with it, centered on the art, or 612 × 792 pt (VectorCraft's own size, as +for a `.cdr` page without one) when it stores none either. Layers keep their names, a layer without one is named by +its place as in a `.cdr` drawing, and a layer that holds nothing is left out. Groups stay groups, and curves become +paths with their uniform fills and outlines: colors in CMYK, CMY, RGB or gray, and library colors stored with their +names as spot swatches; outline widths, caps, joins, dashes, an outline behind the fill and an outline scaled with the object. Text, images, rectangles, ellipses, clipping paths and other objects the reader doesn't read yet, fills other than uniform ones and -outline arrowheads are left out, and lenses are not applied. The objects a clipping path clips come in without it. The +outline arrowheads are left out, and lenses, canvases and containers are not applied. The objects a clipping path clips come in without it. The `warnings` of `document.open` name each of these kinds the file had, and say when damaged parts or parts past the import limits were left out. Files in big-endian byte order and files with 16-bit coordinates are refused with a message. VectorCraft doesn't write `.cmx` files. After a file opens with warnings, an export or Save As to its own path