From 4c8cfda86affac2755731f5233e9311227858581 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 12 Nov 2026 11:38:11 -0800 Subject: [PATCH] .cdr import: read layouts as public sample files store them Version 2 files (markers m and l) start their settings after a 1-bit picture, so the page box is read two bytes after the settings offset in the header, and their drawn objects keep the fill at byte 105 and the shape at 136. Files with the marker l flag drawn objects 0xc0 and are now read; the marker d, which no sample shows, is refused. A record that does not start with 24, or a drawn object of an unknown type, ends the walk instead of reading the bytes after it as records. A rectangle's radius follows its height directly. A layer's kind comes from the object type in its own loda (11 grid, 12 guides, 17 desktop), so drawing layers flagged 0x10 or 0x08 are no longer dropped or taken for the desktop. Object type 0x25 is read as a path from version 10, and arrowhead ids are looked for from version 6. PROVENANCE.md now cites the Kaitai specification, the ZIP specification, the file utility's magic, PRONOM and the public sample files whose bytes each row was checked against. --- crates/cdr/PROVENANCE.md | 120 ++++++++-------- crates/cdr/README.md | 14 +- crates/cdr/src/build.rs | 22 ++- crates/cdr/src/layout.rs | 16 ++- crates/cdr/src/records.rs | 34 +++++ crates/cdr/src/synth.rs | 64 +++++++-- crates/cdr/src/wl.rs | 165 +++++++++++++++++----- crates/cdr/tests/read.rs | 58 +++++--- crates/engine/src/cmd/fileio/tests_cdr.rs | 2 +- crates/engine/tests/import_fuzz.rs | 4 +- docs/mcp.md | 9 +- docs/target-app-parity.md | 2 +- 12 files changed, 353 insertions(+), 157 deletions(-) diff --git a/crates/cdr/PROVENANCE.md b/crates/cdr/PROVENANCE.md index 874f5bb4fc..c9a96f1206 100644 --- a/crates/cdr/PROVENANCE.md +++ b/crates/cdr/PROVENANCE.md @@ -7,13 +7,6 @@ Sources: - **K**: the Kaitai Struct specification `coreldraw_cdr.ksy` (MIT, notice in `LICENSE-kaitai.txt`), https://github.com/kaitai-io/coreldraw_cdr.ksy/blob/9d8e2704b946961f5114115f1a488750a3c86ed0/coreldraw_cdr.ksy -- **T**: an open-source converter's CDR format notes (MIT OR Apache-2.0), rows its authors mark - confirmed, - https://github.com/tracedraw/tracedraw/blob/2ac6d02e97773e040a51be6cfb27cc424e3deeaa/docs/cdr-format.md -- **C**: the published object model reference for the format's drawing program, - https://community.coreldraw.com/sdk/api/draw/17 -- **P**: a web editor's published CDR notes, - https://github.com/photopea/CDR-specification/blob/f91b3c7e8c812b28473f6399e95a624a72a3d6b8/README.md - **KR**: the README and the `bin/cdr-unpk` script of the Kaitai Struct specification (MIT), at the same commit as K, https://github.com/kaitai-io/coreldraw_cdr.ksy/blob/9d8e2704b946961f5114115f1a488750a3c86ed0/README.md @@ -21,10 +14,6 @@ Sources: - **Z**: the PKWARE ZIP specification, APPNOTE.TXT version 6.3.10, https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT, and RFC 1951 (DEFLATE), https://www.rfc-editor.org/rfc/rfc1951 -- **TS**: the source of the reader that T's notes describe (MIT OR Apache-2.0), at the same commit - as T, read for where a field lies when the notes leave it open; no code is copied, - https://github.com/tracedraw/tracedraw/blob/2ac6d02e97773e040a51be6cfb27cc424e3deeaa/crates/cdr/src/wl.rs - and https://github.com/tracedraw/tracedraw/blob/2ac6d02e97773e040a51be6cfb27cc424e3deeaa/crates/cdr/src/parse.rs - **F**: the magic database of the `file` utility (BSD-2-Clause), https://github.com/file/file/blob/b778217dbd35/magic/Magdir/images and https://github.com/file/file/blob/b778217dbd35/magic/Magdir/riff @@ -32,67 +21,72 @@ Sources: https://pronom.nationalarchives.gov.uk/fmt/467.xml, https://pronom.nationalarchives.gov.uk/fmt/466.xml and https://pronom.nationalarchives.gov.uk/fmt/465.xml +- **S**: own inspection of the bytes of public sample files, checked against the picture each file + carries of its drawing where it has one (versions 2 and 4 to 25; files of versions 1 and 3 carry + none). The files come from: + - **TP**: https://telparia.com/fileFormatSamples/image/cdr/, by file name + - **LT**: https://git.libreoffice.org/libcdr-test at commit + `dafe4fe53d685d89df20a5c0bb18f318ccefb4a8`, `testset//` + - **UC**: https://svn.code.sf.net/p/uniconvertor/code/formats/CDR/testing%20base/ at revision + 434, by file name + - **TDF**: attachments to The Document Foundation's bug tracker, + https://bugs.documentfoundation.org/attachment.cgi?id= + - **NR**: Natural Resources Canada's tactile maps, + https://ftp.maps.canada.ca/pub/nrcan_rncan/raster/tactile/approach-approch/cdr/banff_ap_en.cdr and + https://ftp.maps.canada.ca/pub/nrcan_rncan/raster/tactile/atlas-atlas/cdr/ab_en_br.cdr + - **DM**: the clip art of https://discmaster.textfiles.com/browse/31541, by file name -Status: `public-spec` (K, KR, C, Z, F or PR), `public-notes` (a confirmed row of T, a row of T's -table of the layouts its writer takes from sample files, TS, or P), `unconfirmed` (a row T marks as -assumed, or a point no source states; listed so it can be checked against a file). +Status: `public-spec` (K, KR, Z, F or PR), `sample` (S), `unconfirmed` (a point no source states +and no sample file shows; listed so it can be checked against a file, with what the reader does +when it is wrong). -No public specification covers versions 1 to 4, and the files of those versions that this reader -has read are the synthetic ones `src/synth.rs` builds from the rows below. T's authors took their -rows for versions 3 and 4 from four version 3 drawings, six version 4 drawings and a set of -version 3 clip art, and their rows for versions 1 and 2 from 18 files; none of those files is -public. - -T and P are themselves notes taken from files, not specifications. Drawings of versions 7 to 18, -20, 22, 24, 26 and 27 written by the converter that T's authors publish (release 0.44.0) read back -with the pages, layers, groups, shapes, colors, outline widths, caps, joins and dashes it wrote. -That converter also reads the synthetic packages of versions 14 to 18, 20, 22, 24, 26 and 27 that -`src/synth.rs` writes, with the page, its layer and the shapes on it. Those files are not -committed. +No public specification covers versions 1 to 4. Their rows rest on the sample files they name. +The test inputs are the synthetic files that `src/synth.rs` builds from the rows below; no sample +file is committed. | Id | What the code relies on | Source | Status | |---|---|---|---| -| R1 | `RIFF`, the body length, then the form type `CDR` or `cdr` and a version character: `1` to `9`, `A` (10) to `H` (17), `J` (18) on, `I` unused; `cdr8` is 8.01, `CDR ` is 3, `CDRX` is another format. Files of versions 23 to 27 carry `O` to `U`, which count differently from the version 24 update on (`CDRQ` with a `vrsn` of 2430, `CDRU` with 2700), so their `vrsn` chunk decides (R2) | K L68-L133; T L22 | public-spec (letters from version 23: public-notes) | +| R1 | `RIFF`, the body length, then the form type `CDR` or `cdr` and a version character: `1` to `9`, `A` (10) to `H` (17), `J` (18) on, `I` unused; `cdr8` is 8.01, `CDR ` is 3, `CDRX` is another format. Files of versions 23 to 25 carry `O` (23), `P` (24), `Q` (24.3, with a `vrsn` of 2430) and `R` (25), which count differently from the version 24 update on, so their `vrsn` chunk decides (R2) | K L68-L133; S: LT `testset/23` to `testset/25`, such as `circle_redfill_nooutline_cdr25.cdr` | public-spec (letters from version 23: sample; letters of versions 26 and 27: unconfirmed, and the `vrsn` chunk decides when the file has one) | | R2 | `vrsn`: the version as a u16, in hundreds | K L276-L278 | public-spec | -| R3 | `LIST cmpr`: two (compressed, uncompressed) u32 pairs, then two blocks of `CPng`, `01 00 04 00` and a zlib stream; the first inflates to the chunk stream, the second to a pool of u32 sizes that each chunk's size field indexes | K L193-L220, L2626-L2682; P "LIST cmpr"; T L30 | public-spec | +| R3 | `LIST cmpr`: two (compressed, uncompressed) u32 pairs, then two blocks of `CPng`, `01 00 04 00` and a zlib stream; the first inflates to the chunk stream, the second to a pool of u32 sizes that each chunk's size field indexes | K L193-L220, L2626-L2682 | public-spec | | R4 | A chunk is an id, a u32 size and the body, padded to an even length; a `LIST` body starts with its form type; from version 7 a `LIST stlt` holds the style tables, not chunks | K L68-L78, L135-L155, L307-L334 | public-spec | -| R5 | Coordinates: i16 in 1/1000 inch before version 6, else i32 in 1/254000 inch; header fields, counts and offsets are u16 before version 6, else u32 | K L58-L66, L2688-L2700; T L145 | public-spec | -| R6 | `loda` and `lobj`: length, argument count, offsets table, types table, object type; count + 1 offsets bound the arguments; the types table runs in reverse; object types (1 rectangle, 2 ellipse, 3 curve, 0x14 polygon) and argument types (10 outline, 20 fill, 30 coordinates, 1000 name, 11000 polygon, 19130 page size) | K L335-L450, L846-L893; T L145-L157 | public-spec | +| R5 | Coordinates: i16 in 1/1000 inch before version 6, else i32 in 1/254000 inch; header fields, counts and offsets are u16 before version 6, else u32 | K L58-L66, L2688-L2700 | public-spec | +| R6 | `loda` and `lobj`: length, argument count, offsets table, types table, object type; count + 1 offsets bound the arguments; the types table runs in reverse; object types (1 rectangle, 2 ellipse, 3 curve, 0x14 polygon) and argument types (10 outline, 20 fill, 30 coordinates, 1000 name, 11000 polygon, 19130 page size) | K L335-L450, L846-L893 | public-spec | | R7 | Angles: i16 tenths of a degree before version 6, else i32 millionths of a degree | K L2835-L2855 | public-spec | -| R8 | `trfd`: length, count, offsets table; each entry has 8 bytes first from version 13, a u16 type, and type 8 is a matrix after 6 bytes (from version 6) of six f64 `a c tx b d ty`, `tx` and `ty` in coordinate units; the matrices of one record apply in file order | K L895-L953, L2808-L2834; T L201-L204 | public-spec | +| R8 | `trfd`: length, count, offsets table; each entry has 8 bytes first from version 13, a u16 type, and type 8 is a matrix after 6 bytes (from version 6) of six f64 `a c tx b d ty`, `tx` and `ty` in coordinate units; the matrices of one record apply in file order | K L895-L953, L2808-L2834 | public-spec | | R9 | Name argument (1000): Windows-1252 up to a NUL before version 12, UTF-16LE from it | K L601-L630 | public-spec | -| R10 | `mcfg`: 12 bytes (13 on), 4 (9 to 12), 28 (6) or none, then the page width and height as coordinates | K L1670-L1765; P "mcfg" | public-spec | -| R11 | `fild` (`fill` before 7): id; from 13 a record tagged 1300 with its length, which records tagged 2400 or more may precede; the fill type u16 (0 none, 1 uniform, others not read); uniform: 2 bytes then a color before 13, from 13 a record tagged 1300 holding a property list (u8 type, u32 length, body; 0 ends it; 1 a color, 8 a palette id and a UTF-16 name, 3 a UTF-16 name) | K L1025-L1302; T L217-L220 | public-spec (2400 records: public-notes) | -| R12 | `outl`: id; from 13 records (u32 id, u32 length, body) up to id 1, whose body follows; line type, caps and join u16; 2 bytes (6 to 12); width; stretch u16; 2 bytes (6 on); nib angle; 52 bytes (6 to 12) or 46 (13 on); the color; 16 bytes (10 before 6); the dash count u16 and the dashes, in multiples of the width; the arrowhead ids 22 bytes after the first dash in 7 and 11 to 12, 20 bytes from 13. Caps 0 butt, 1 round, 2 square; joins 0 miter, 1 round, 2 bevel | K L954-L1024; T L242-L243; C `cdrOutlineLineCaps`, `cdrOutlineLineJoin` | public-spec (arrowhead places: public-notes) | -| R13 | Bit 0 of the outline's line type: no outline | T L246 | unconfirmed | -| R14 | A color record from version 5: model u16, palette u16, 4 bytes, four value bytes. Models: 2 CMYK in percent, 3 and 17 CMYK 0 to 255, 4 CMY 0 to 255, 5 blue, green, red, 9 gray 0 to 255, 20 registration, 21 blue, green, red and a tint in percent already applied; 1, 14 and 25 a library color's number (u16) and tint in percent (u16) | K L2856-L2965; T L256-L257 | public-spec (library values: public-notes) | -| R15 | Rectangle coordinates: width, height and one radius, three more radii from version 9, running counterclockwise from the corner at (width, 0) | K L674-L700; T L172-L173 | public-spec | -| R16 | Point list: u32 count (before version 6 as R39 gives it), the points, then one type byte each, the count held to what fits; type bits 7 and 6 move, line, curve end, control point; bit 3 closes the subpath | K L782-L787, L840-L845, L2701-L2807; T L170-L171, L180 | public-spec | -| R17 | Ellipse coordinates: width, height, start and end angles, pie flag (u16 before 6, else u32); the radii are half the width and height; the angles turn from the ellipse's own axes, which a negative width or height points the other way | K L754-L781; T L179 | public-spec (axis direction of a negative size: public-notes) | -| R18 | Polygon argument (11000): 4 bytes before 13, the side count u32, the next point u32 (the u32 after it when it is 0 or 1, else 4 bytes), 4 more from 13, rx and ry f64, the centre; the base curve is one side on a circle of 1000 mm from the top vertex to the vertex it runs to clockwise, copied once for each side | K L631-L655; T L182-L183 | public-spec (base curve: public-notes) | -| R19 | `flgs`: byte 3 the record type, byte 2 marks the master page; a layer's byte 0 is 0, 8 desktop, 0x0a guides, 0x1a grid; in versions 7 to 13 the master page flags its guides [2, 1, 1], desktop [0, 1, 1] and grid [0x12, 0, 1]; from 14 it flags them [0x0a, 0, 1], [8, 0, 1] and [0x1a or 0x5a, 1, 1], by byte 0 as other layers | K L1602-L1639; T L132-L133 | public-spec (master page flags: public-notes for versions 7 to 21, unconfirmed for 22 to 27) | +| R10 | `mcfg`: 12 bytes (13 on), 4 (9 to 12), 28 (6) or none, then the page width and height as coordinates | K L1670-L1765 | public-spec | +| R11 | `fild` (`fill` before 7): id; from 13 a record tagged 1300 with its length, which records tagged 2400 or more may precede; the fill type u16 (0 none, 1 uniform, others not read); uniform: 2 bytes then a color before 13, from 13 a record tagged 1300 holding a property list (u8 type, u32 length, body; 0 ends it; 1 a color, 8 a palette id and a UTF-16 name, 3 a UTF-16 name) | K L1025-L1302; S: LT `testset/15` to `testset/25` `circle_redfill_nooutline_cdr.cdr`, whose fill records hold a record tagged 2400 before the one tagged 1300 | public-spec (2400 records: sample) | +| R12 | `outl`: id; from 13 records (u32 id, u32 length, body) up to id 1, whose body follows; line type, caps and join u16; 2 bytes (6 to 12); width; stretch u16; 2 bytes (6 on); nib angle; 52 bytes (6 to 12) or 46 (13 on); the color; 16 bytes (10 before 6); the dash count u16 and the dashes, in multiples of the width; the arrowhead ids 22 bytes after the first dash in versions 6 to 12, 20 bytes from 13. Caps 0 butt, 1 round, 2 square; joins 0 miter, 1 round, 2 bevel | K L954-L1024; S: caps and joins in UC `outl_line_caps_cdr7.cdr` and `outl_corner_cdr7.cdr`; arrowhead ids, matched against the drawing's `arrw` records, in TP `web_cd.cdr` (6), UC `rogan_logo_cdr7.cdr`, `rogan_logo_cdr8.cdr` and `rogan_logo_cdr11.cdr`, NR `ab_en_br.cdr` (9) and `banff_ap_en.cdr` (10), TDF 76820 (12) and 140638 (13) | public-spec (caps, joins and arrowhead places: sample; arrowheads before version 6: no sample names one, and the reader does not look for them) | +| R13 | Bit 0 of the outline's line type: no outline | S: LT `square_blackfill_nooutline_cdr.cdr` and the other `nooutline` files of `testset/15` to `testset/25`, whose outline records all have line type 1 and whose pictures show no outline | sample | +| R14 | A color record from version 5: model u16, palette u16, 4 bytes, four value bytes. Models: 2 CMYK in percent, 3 and 17 CMYK 0 to 255, 4 CMY 0 to 255, 5 blue, green, red, 9 gray 0 to 255, 20 registration, 21 blue, green, red and a tint in percent already applied; 1, 14 and 25 a library color's number (u16) and tint in percent (u16) | K L2856-L2965; S: model 25 in LT `testset/12/square_pantoneblackfill_nooutline.cdr` (number 14, tint 100), model 1 in UC `rogan_logo_cdr11.cdr` (number 4, tint 50) | public-spec (library values of models 1 and 25: sample; model 14: unconfirmed, and its color opens as a spot color without a look) | +| R15 | Rectangle coordinates: width, height and one radius, three more radii from version 9, running counterclockwise from the corner at (width, 0) | K L674-L700 | public-spec | +| R16 | Point list: u32 count (before version 6 as R39 gives it), the points, then one type byte each, the count held to what fits; type bits 7 and 6 move, line, curve end, control point; bit 3 closes the subpath | K L782-L787, L840-L845, L2701-L2807 | public-spec | +| R17 | Ellipse coordinates: width, height, start and end angles, pie flag (u16 before 6, else u32); the radii are half the width and height; the angles turn from the ellipse's own axes, which a negative width or height points the other way | K L754-L781; S: the pies of negative height in the ellipse-sector drawings of versions 12 and 13 in UC | public-spec (axis direction of a negative size: sample) | +| R18 | Polygon argument (11000): 4 bytes before 13, the side count u32, the next point u32 (the u32 after it when it is 0 or 1, else 4 bytes), 4 more from 13, rx and ry f64, the centre; the base curve is one side on a circle of 1000 mm from the top vertex to the vertex it runs to clockwise, copied once for each side | K L631-L655; S: the polygons and stars of UC `shapes_cdr10.cdr` | public-spec (base curve: sample) | +| R19 | `flgs`: byte 3 the record type, byte 2 marks the master page. A layer's own `loda` names what it is by its object type: 11 a grid, 12 guides, 17 the desktop, any other a drawing layer (version 3 one less, R32). Only a layer without a `loda` is taken by its byte 0: 0 drawing, 8 desktop, 0x0a guides, a set bit 0x10 grid, and in versions 7 to 13 [2, 1, 1] guides and [0, 1, 1] desktop | K L1602-L1639 (record types); S: in every sample file of versions 3 to 25 the layer's object type agrees with its name; byte 0 does not: TDF 114901 (version 8) flags its master page's guides [2, 1, 1], desktop [0, 1, 1], grid [0x12, 0, 1] and drawing layer [8, 1, 0], and NR `ab_en_br.cdr` (version 9) flags drawing layers 0x10 and its guides 0x1a | public-spec (layer types: sample; byte 0 of a layer without a `loda`: unconfirmed, as no sample has such a layer, and a wrong guess drops or shows that layer's objects) | | R20 | Fill and outline arguments (20, 10): a u32 record id | K L452-L460, L548-L556 | public-spec | | R21 | Page size argument (19130): width and height | K L666-L672 | public-spec | -| R22 | `LIST page` holds `LIST layr` lists, in versions 7 to 13 inside a `LIST gobj`; a layer holds `LIST obj ` and `LIST grp `; a page, layer or object keeps its own `loda` (and an object its `trfd`, in a `trfl` list) among its chunks or in a `LIST lgob` | K L1602-L1639 (record types); T L120, L129, L359, L362 | public-notes | -| R23 | Guideline (object type 0x16), argument 16000: 12 bytes, the angle in millionths of a degree, then points from the page centre; version 7 keeps them in a `guid` chunk: a u32 count, the count again, 40-byte records laid out the same way | T L159-L160 | public-notes | +| R22 | `LIST page` holds `LIST layr` lists, directly or in a list it holds (a `LIST gobj` in the samples); a layer holds `LIST obj ` and `LIST grp `; a page, layer or object keeps its own `loda` (and an object its `trfd`, in a `trfl` list) among its chunks or in a `LIST lgob` | K L1602-L1639 (record types); S: the chunk trees of every sample file of versions 3 to 25 | sample | +| R23 | Guideline (object type 0x16), argument 16000: 12 bytes, the angle in millionths of a degree, then points from the page centre; version 7 keeps them in a `guid` chunk: a u32 count, the count again, 40-byte records laid out the same way | S: argument 16000 in NR `ab_en_br.cdr` (9) and TDF 194220 (13); the `guid` chunk of TDF 123971 (7). Pictures do not show guidelines | sample | | R24 | ZIP: the end of central directory record (signature `PK\5\6`, 22 bytes and a comment of up to 65,535 bytes) gives the member count and where the central directory lies; each central directory entry (46 bytes, then name, extra field and comment) gives the flags, method, CRC-32, sizes and its local header; a local header is 30 bytes, then its name and extra field, then the member's bytes. Flag bit 0 marks an encrypted member; method 0 is stored and 8 deflated; 0xFFFF and 0xFFFFFFFF mark ZIP64 fields, and a ZIP64 locator (`PK\6\7`, 20 bytes) precedes the end record of a ZIP64 archive; disk numbers other than 0 mark an archive split across files | Z 4.3.7, 4.3.12, 4.3.15, 4.3.16, 4.4.1.4, 4.4.4, 4.4.5, 4.4.7, 4.4.8, 4.4.13 | public-spec | -| R25 | Versions 14 and 15 save a ZIP package whose member `content/riffData.cdr` is the RIFF file | KR README L33, `cdr-unpk` L30; T L29, L355 | public-spec | -| R26 | From version 16 the package's member `content/root.dat` is the RIFF file; `content/dataFileList.dat` names the data streams, one per line, members of `content/data/`; a redirect's stream number counts lines from 0 | KR README L34, L40, `cdr-unpk` L34-L66; K L173-L180; T L37, L355 | public-spec | -| R27 | In a package of version 16 on, a chunk outside a compressed list whose size is 16 (in `root.dat` or in a redirected list's body) is a redirect record: stream u32, length u32, offset u32 and a reserved u32; its body is `length` bytes at `offset` of the stream; stream `0xFFFFFFFF` holds the body (at most 8 bytes) in the record after the length; a redirected list's body starts with its form type | K L144-L193; T L38-L40 | public-spec | -| R28 | Templates have the form type `CDT` and a letter of their own, `CDTQ` in version 21 with a `vrsn` of 2100; the `vrsn` chunk names the version. The reader takes any letter `A` to `Z` there | T L26 | public-notes (letters other than `Q`: unconfirmed) | -| R29 | Rectangle coordinates from version 15: width and height as f64 coordinate units, scale x and y (f64, repeating the transform's scale), the scale-with flag (u8) and 7 bytes, then the four radii as f64, 16 bytes apart (a corner type byte and 15 bytes after the first), in the order of R15. With the flag clear each radius is a share of half the shorter side; with it set the radii are multiplied by 254000 coordinate units, which makes them inches | K L674-L752; T L173-L177 | public-spec (radius share with the flag clear, scale not applied: public-notes) | -| R30 | Object type 0x25 (version 16 on) is a path: 4 bytes, two u16 counts that add up to the point count, 16 bytes, then the points and their type bytes as in R16 | K L788-L799, L892; T L157, L181 | public-spec (from version 16: public-notes) | -| R31 | Version 3 is a RIFF file with the form type `CDR ` (a space) and a `vrsn` of 300; version 4 has the form type `CDR4` and a `vrsn` chunk. Both use the 16-bit fields of R5 and R7. Their pages, layers, groups and objects are read by the rules of R19 and R22 | K L117-L134; T L44; PR fmt/465; F `riff` L786 | public-spec (pages and layers as R19 and R22: unconfirmed) | -| R32 | Version 3 counts its object types from 0: rectangle 0, ellipse 1, curve 2, text 3, bitmap 4; later versions use these plus one | T L53; TS `parse.rs` | public-notes | -| R33 | Version 3 keeps an object's transforms in argument 100 of its `lobj`: a u16, the entry count (u16), then 10-byte entries of a type (u16) and, at byte 6, where the entry's data starts, counted from the start of the argument. Type 0x8c is a matrix as R37, and the matrices apply in file order. Type 0x14 is an envelope, which this reader does not apply: the shape keeps its outline, placed by the matrices alone, and the drawing's warnings name the envelopes. A group's own `lobj` holds an argument 100 that places all of the group's objects after their own transforms | T L54-L56, L60; K L851; TS `parse.rs` | public-notes | -| R34 | Version 3 fill, argument 20: a type byte (0 none, 1 uniform, 2 and 4 fountain, 6 texture, 7 and 10 pattern); a uniform fill's color follows as R36 | K L452-L547; T L62 | public-spec | -| R35 | Version 3 outline, argument 10: a flags byte with bit 0 set when the object is outlined, the width (coordinate), the stretch (u16), the nib angle, the color as R36, 7 bytes, the dash count (u8) and 10 bytes that hold the dashes as one byte each, the join (u16 at byte 30) and the caps (u16 at byte 32) numbered as R12, then the two arrowhead ids (u32). Dashes are read in multiples of the width as R12 | K L548-L592; T L64 | public-spec (flag bit: public-notes; dash unit, and the join, which T puts in the byte at 33: unconfirmed) | -| R36 | A color before version 5: in versions 1 to 3 a model byte and four value bytes, in version 4 a model u16, four u16 values and 2 bytes. Model 1 is cyan, magenta, yellow and black in percent. Model 0 is an ink number (u16) and a tint in percent (u16): ink 0 is black at the tint, ink 1 is white, and other numbers are inks of a numbered library that the file does not store. Other models are read as R14 | K L2882-L2908; T L65-L66, L89; TS `parse.rs`, `wl.rs` | public-notes (models other than 0 and 1: unconfirmed, K and T differ on model 1) | -| R37 | A matrix of versions 3 and 4: six i32 `a c tx b d ty`, the four factors in 16.16 fixed point and `tx` and `ty` in coordinate units. A version 4 `trfd` entry of type 8 holds one where R8 has six f64 | T L55, L61; TS `parse.rs` | public-notes | -| R38 | `mcfg` of version 3: 2 bytes, then the page's two corners as four coordinates; the page size is the distance between them | K L1676-L1678, L1745-L1756; T L71 | public-spec | -| R39 | A point list before version 6 starts with a u16 count and another u16 (1 in some curves of versions 3 and 4), where K reads the two as one u32 | T L67 | public-notes | -| R40 | Versions 1 and 2 are not RIFF files. A file starts with `WL`, a marker byte, a zero byte and five u32: the start of the settings (28), the start and the end of the object records, the start of the trailer and that value minus two. F and PR give the marker `e` for version 1.0 and `m` for 2.0; T's authors also saw `d`. The reader takes these three markers and reads the three alike | T L82; F `images` L4159-L4172; PR fmt/467, fmt/466 | public-notes (which version `d` marks: unconfirmed) | -| R41 | The page's box in a file of version 1 or 2: four i16 at byte 30 (left, top, right, bottom) in 1/1000 inch around the page's centre, y pointing up | T L75-L78, L83; TS `wl.rs` | public-notes | -| R42 | The records of a file of version 1 or 2 run from the front of the stacking order to the back. A group or layer record is 28 bytes, and the low byte of its flags at byte 26 is not 0x40. A drawn object has 0x40 there and a u16 body size at byte 28; it is the body size plus 32 bytes long, and at least 118 | T L85, L86, L93; TS `wl.rs` | public-notes | -| R43 | A drawn object of version 1 or 2: a point (two i16) at byte 32; six i32 in 16.16 fixed point at 36, `a b tx c d ty`, with x' = a x + b y + tx + the point's x and y' = c x + d y + ty + the point's y; the object type at 60 (0 rectangle, 1 ellipse, 2 curve, 3 text); a byte at 67 that is not 0 when the object is outlined, the outline's width (u16) at 68 and its color at 74; the fill type at 94 (0 none, 1 uniform) and its color at 95; the shape from byte 114. Colors are as R36 | T L87-L89; TS `wl.rs` | public-notes | -| R44 | Shapes of version 1 or 2, from byte 114 of the object: a rectangle's width and height (i16), then after 2 bytes a corner radius; an ellipse's width and height, then two angles; a curve's point list as R39 and R16 | T L90, L91; TS `wl.rs` | public-notes (radius: unconfirmed; how the angles make an arc: unconfirmed, so an ellipse whose angles differ is left out) | +| R25 | Versions 14 and 15 save a ZIP package whose member `content/riffData.cdr` is the RIFF file | KR README L33, `cdr-unpk` L30 | public-spec | +| R26 | From version 16 the package's member `content/root.dat` is the RIFF file; `content/dataFileList.dat` names the data streams, one per line, members of `content/data/`; a redirect's stream number counts lines from 0 | KR README L34, L40, `cdr-unpk` L34-L66; K L173-L180 | public-spec | +| R27 | In a package of version 16 on, a chunk outside a compressed list whose size is 16 (in `root.dat` or in a redirected list's body) is a redirect record: stream u32, length u32, offset u32 and a reserved u32; its body is `length` bytes at `offset` of the stream; stream `0xFFFFFFFF` holds the body (at most 8 bytes) in the record after the length; a redirected list's body starts with its form type | K L144-L193 | public-spec | +| R28 | Templates have the form type `CDT` and a letter of their own; the `vrsn` chunk names the version. The reader takes any letter `A` to `Z` there | none: no sample template | unconfirmed (a template is read as a drawing of the version its `vrsn` chunk names, and one without that chunk is refused) | +| R29 | Rectangle coordinates from version 15: width and height as f64 coordinate units, scale x and y (f64, repeating the transform's scale), the scale-with flag (u8) and 7 bytes, then the four radii as f64, 16 bytes apart (a corner type byte and 15 bytes after the first), in the order of R15. With the flag clear each radius is a share of half the shorter side; with it set the radii are multiplied by 254000 coordinate units, which makes them inches | K L674-L752; S: LT `square_blackfill_nooutline_roundedcorners_cdr.cdr` of `testset/15` to `testset/25` (flag clear, a share of 0.25) | public-spec (radius share with the flag clear: sample; that the scale is not applied: unconfirmed, as every sample has a scale of 1, and a wrong guess changes only the corners' size) | +| R30 | Object type 0x25 (version 10 on) is a path: 4 bytes, two u16 counts that add up to the point count, 16 bytes, then the points and their type bytes as in R16 | K L788-L799, L892; S: TDF 78250 (version 15) and UC `shapes_cdr10.cdr`, `shapes_cdr11.cdr`, `shapes_cdr12.cdr` and `shapes_x3.cdr` (versions 10 to 13), whose type bytes start where the counts put them; no sample before version 10 holds the type | public-spec (from version 10: sample) | +| R31 | Version 3 is a RIFF file with the form type `CDR ` (a space) and a `vrsn` of 300; version 4 has the form type `CDR4` and a `vrsn` chunk. Both use the 16-bit fields of R5 and R7. Their pages, layers, groups and objects are read by the rules of R19 and R22 | K L117-L134; PR fmt/465; F `riff` L786; S: TP `FOX.CDR` and `DALI.CDR` (3), DM `lion1.cdr` and `tiger.cdr` (4) | public-spec (pages and layers as R19 and R22: sample) | +| R32 | Version 3 counts its object types from 0: rectangle 0, ellipse 1, curve 2, text 3, bitmap 4; later versions use these plus one | S: the rectangles, curves and text of TP `ARMYSTF1.CDR`, `DALI.CDR` and `FOX.CDR`, which come out whole | sample (ellipse 1 and bitmap 4: unconfirmed, as no sample holds them; a bitmap is left out with a warning) | +| R33 | Version 3 keeps an object's transforms in argument 100 of its `lobj`: a u16, the entry count (u16), then 10-byte entries of a type (u16) and, at byte 6, where the entry's data starts, counted from the start of the argument. Type 0x8c is a matrix as R37, and the matrices apply in file order. Type 0x14 is an envelope, which this reader does not apply: the shape keeps its outline, placed by the matrices alone, and the drawing's warnings name the envelopes. A group's own `lobj` holds an argument 100 that places all of the group's objects after their own transforms | K L851; S: the matrices of every version 3 file in TP, whose drawings come out whole | sample (envelope type 0x14: unconfirmed, as no sample holds one; it is not applied, with a warning) | +| R34 | Version 3 fill, argument 20: a type byte (0 none, 1 uniform, 2 and 4 fountain, 6 texture, 7 and 10 pattern); a uniform fill's color follows as R36 | K L452-L547 | public-spec | +| R35 | Version 3 outline, argument 10: a flags byte with bit 0 set when the object is outlined, the width (coordinate), the stretch (u16), the nib angle, the color as R36, 7 bytes, the dash count (u8) and 10 bytes that hold the dashes as one byte each, the join (u16 at byte 30) and the caps (u16 at byte 32) numbered as R12, then the two arrowhead ids (u32). Dashes are read in multiples of the width as R12 | K L548-L592; S: the outlines of the version 3 files in TP | public-spec (flag bit: sample; dash unit, and the join, where bytes 32 and 33 of some samples hold 2 and 2: unconfirmed, and a wrong guess changes only how corners, ends and dashes are drawn) | +| R36 | A color before version 5: in versions 1 to 3 a model byte and four value bytes, in version 4 a model u16, four u16 values and 2 bytes. Model 1 is cyan, magenta, yellow and black in percent. Model 0 is an ink number (u16) and a tint in percent (u16): ink 0 is black at the tint, ink 1 is white, and other numbers are inks of a numbered library that the file does not store. Other models are read as R14 | K L2882-L2908; S: model 1 in TP `ARMYSTF1.CDR` (3) and DM `lion1.cdr` (4), model 0 in TP `ARROW5.CDR` (3) and `WI.CDR` (1) | sample (ink 1, and models other than 0 and 1: unconfirmed; an ink of another number opens as a spot color without a look) | +| R37 | A matrix of versions 3 and 4: six i32 `a c tx b d ty`, the four factors in 16.16 fixed point and `tx` and `ty` in coordinate units. A version 4 `trfd` entry of type 8 holds one where R8 has six f64 | S: TP `FOX.CDR` and `DALI.CDR` (3), DM `lion1.cdr` and `tiger.cdr` (4) | sample | +| R38 | `mcfg` of version 3: 2 bytes, then the page's two corners as four coordinates; the page size is the distance between them | K L1676-L1678, L1745-L1756 | public-spec | +| R39 | A point list before version 6 starts with a u16 count and another u16 (1 in some curves of versions 1, 2 and 4, 0 in the others), where K reads the two as one u32 | S: TP `DALI.CDR` (3), DM `headup.cdr` and `lion1.cdr` (4), TP `WI.CDR` (1), `FLAG.CDR` and `MENU.CDR` (2) | sample | +| R40 | Versions 1 and 2 are not RIFF files. A file starts with `WL`, a marker byte, a zero byte and five u32: the start of the settings, the start and the end of the object records, the start of the trailer and that value minus two. Version 1 has the marker `e` and its settings start at byte 28. Version 2 has the marker `m` or `l`, and its settings start after a 1-bit picture of the drawing, whose header starts where the u32 at byte 28 says. The reader takes these three markers | F `images` L4159-L4172; PR fmt/467, fmt/466; S: TP `WI.CDR` (`e`), `FLAG.CDR` (`m`), `MENU.CDR` and `DRINKS.CDR` (`l`) | public-spec (markers `e` and `m`: F and PR; the offsets and the marker `l`: sample) | +| R41 | The page's box in a file of version 1 or 2: four i16 two bytes after the start of the settings (left, top, right, bottom) in 1/1000 inch around the page's centre, y pointing up | S: TP `WI.CDR`, `FLAG.CDR` and `MENU.CDR` | sample | +| R42 | The records of a file of version 1 or 2 run from the front of the stacking order to the back. Every record starts with the u32 24. A group or layer record is 28 bytes, and bit 0x40 of its flags byte at 26 is clear. A drawn object has that bit set (0x40 in files with the markers `e` and `m`, 0xc0 with `l`) and a u16 body size at byte 28; it is the body size plus 32 bytes long. A drawn object of another type than those of R43 gives its length otherwise, and the records after it are not read | S: TP `WI.CDR`, `FLAG.CDR`, `MENU.CDR` and `DRINKS.CDR`; the order checked against the picture of `MENU.CDR` | sample | +| R43 | A drawn object of version 1 or 2: a point (two i16) at byte 32; six i32 in 16.16 fixed point at 36, `a b tx c d ty`, with x' = a x + b y + tx + the point's x and y' = c x + d y + ty + the point's y; the object type at 60 (0 rectangle, 1 ellipse, 2 curve, 3 text); a byte at 67 that is not 0 when the object is outlined, the outline's width (u16) at 68 and its color at 74; the fill type (0 none, 1 uniform) and its color after it, at 94 and 95 with the marker `e` and at 105 and 106 with `l` and `m`; the shape from byte 114 with `e` and 136 with `l` and `m`. Colors are as R36 | S: TP `WI.CDR` (`e`), `FLAG.CDR` (`m`) and `MENU.CDR` (`l`), checked against the pictures of `FLAG.CDR` and `MENU.CDR` | sample | +| R44 | Shapes of version 1 or 2: a rectangle's width and height (i16), then a corner radius; an ellipse's width and height, then two angles; a curve's point list as R39 and R16 | S: rectangles in TP `FLAG.CDR`, ellipses and curves in TP `WI.CDR`, curves in TP `MENU.CDR` | sample (the radius of files with the marker `e`, which no sample shows, and how the angles make an arc: unconfirmed; a radius is kept only when it is at most half the shorter side, and an ellipse whose angles differ is left out with a warning) | diff --git a/crates/cdr/README.md b/crates/cdr/README.md index 56ded19cfb..d5cf86ef54 100644 --- a/crates/cdr/README.md +++ b/crates/cdr/README.md @@ -15,7 +15,7 @@ with each object's transform. It does not write `.cdr` files. | Layers (drawing, desktop, guides and grid layers), the master page's among them | layers by name and kind | | Guidelines (object type 0x16, and the version 7 `guid` chunk) | guidelines at their point and angle | | Groups (`LIST grp `) | groups | -| Curves (point lists of moves, lines and cubic curves) and paths (object type 0x25, version 16 on) | paths | +| Curves (point lists of moves, lines and cubic curves) and paths (object type 0x25, version 10 on) | paths | | Rectangles (one corner radius, four from version 9) | rectangles with each corner's radius | | Ellipses, arcs and pies | ellipses, and paths for arcs and pies | | Polygons and stars (a base side copied for each side) | paths | @@ -29,19 +29,19 @@ that are encrypted, ZIP64 archives or split across files. ## Versions 1 to 4 -No public specification covers these versions. The reader follows the published notes that -`PROVENANCE.md` cites (rows R31 to R44), and the files of these versions it has read are the -synthetic ones that `src/synth.rs` builds from those rows. +No public specification covers these versions. The reader follows the layouts of public sample +files that `PROVENANCE.md` lists (rows R31 to R44), and its tests read the synthetic files that +`src/synth.rs` builds from those rows. | Version | Read | Left out or refused | |---|---|---| -| 1 and 2 (`WL` files) | the page size; rectangles with one corner radius, whole ellipses and curves, each with its matrix; uniform fills and outlines with their width and color, in CMYK percent and the inks black and white. The objects come in on one page and one layer, front to back | text and objects of other types, and ellipses whose two angles differ (arcs and pies), with the unsupported-objects warning; fills of other types, with the unsupported-fills warning; inks of a numbered library, which read as spot colors without a look; groups and layers, whose records are passed over; dashes, caps, joins and arrowheads, which the notes do not place | +| 1 and 2 (`WL` files) | the page size; rectangles with one corner radius, whole ellipses and curves, each with its matrix; uniform fills and outlines with their width and color, in CMYK percent and the inks black and white. The objects come in on one page and one layer, front to back | text and objects of other types, and ellipses whose two angles differ (arcs and pies), with the unsupported-objects warning; fills of other types, with the unsupported-fills warning; inks of a numbered library, which read as spot colors without a look; groups and layers, whose records are passed over; dashes, caps, joins and arrowheads, which the reader does not look for; the records after a drawn object of another type, with the unsupported-objects warning | | 3 (RIFF, form type `CDR `) | the page size from the page's corners; pages, layers and groups; rectangles, ellipses, arcs and curves with the matrices of the object and of the groups around it; uniform fills; outlines with their width, color, caps, joins and dashes | text and bitmaps; envelopes, with the envelopes warning (the objects and groups under one keep their outlines and matrices); fountain, pattern and texture fills; arrowheads | | 4 (RIFF, form type `CDR4`) | as version 5, with the colors and the fixed-point transforms of version 4 | as version 5 | A file is taken for version 1 or 2 by its content only when, after `WL`, its marker byte is one of -`d`, `e` and `m`, the next byte is zero, and the record offsets of its header fit the file. The -reader reports version 2 for the marker `m` and version 1 for the other two. A RIFF file that names +`e`, `l` and `m`, the next byte is zero, and the offsets of its header fit the file. The reader +reports version 1 for the marker `e` and version 2 for the other two. A RIFF file that names a version before 3 is refused, and so is the version 4 template form type `CDST`, which reads as not a `.cdr` drawing. diff --git a/crates/cdr/src/build.rs b/crates/cdr/src/build.rs index b4909aca29..b78474136b 100644 --- a/crates/cdr/src/build.rs +++ b/crates/cdr/src/build.rs @@ -193,8 +193,20 @@ fn flags(ix: &Index, list: &Chunk) -> Option<[u8; 4]> { ix.data(c).get(..4)?.try_into().ok() } -/// What a layer's flags make it (R19). Byte 0 holds the kind; versions 7 to 13 flag the master -/// page's desktop layer 0 with bytes 1 and 2 set, and its guides layer 2. +/// What a layer's object type makes it (R19): 11 a grid, 12 guides, 17 the desktop, any other a +/// drawing layer. Version 3 counts its types from 0, one less (R32). +fn layer_kind_of_type(kind: u32, types_from_zero: bool) -> LayerKind { + match kind.saturating_add(u32::from(types_from_zero)) { + 11 => LayerKind::Grid, + 12 => LayerKind::Guides, + 17 => LayerKind::Desktop, + _ => LayerKind::Drawing, + } +} + +/// What a layer's flags make it, for a layer without an object type of its own (R19). Byte 0 holds +/// the kind; versions 7 to 13 may flag the master page's desktop layer 0 with bytes 1 and 2 set, +/// and its guides layer 2. fn layer_kind(f: [u8; 4], version_below_14: bool) -> LayerKind { let [b0, b1, b2, _] = f; if b0 & 0x10 != 0 { @@ -283,8 +295,12 @@ impl<'a, 'b> Ctx<'a, 'b> { fn layer(&mut self, list: &Chunk) -> Layer { let ix = self.ix; - let kind = flags(ix, list).map_or(LayerKind::Drawing, |f| layer_kind(f, self.p.old_master_flags)); let loda = own_loda(ix, list).and_then(|c| records::loda(ix.data(c), &self.p, &self.budget.limits, self.warn)); + // The layer's own object type says what it is; its flags, only when it has none (R19). + let kind = match &loda { + Some(l) => layer_kind_of_type(l.kind, self.p.types_from_zero), + None => flags(ix, list).map_or(LayerKind::Drawing, |f| layer_kind(f, self.p.old_master_flags)), + }; let name = loda.and_then(|l| l.arg(ARG_NAME).map(|b| records::name(b, &self.p, self.warn))).unwrap_or_default(); let mut guides = Vec::new(); let objects = self.objects(ix.children(list), &mut guides); diff --git a/crates/cdr/src/layout.rs b/crates/cdr/src/layout.rs index e4d4e3456d..46c28496cf 100644 --- a/crates/cdr/src/layout.rs +++ b/crates/cdr/src/layout.rs @@ -95,7 +95,7 @@ const fn profile(from: u16) -> Profile { outl_after_color: if short { 10 } else { 16 }, marker_after_dash: if from >= 1300 { Some(20) - } else if from == 700 || from >= 1100 { + } else if from >= 600 { Some(22) } else { None @@ -103,7 +103,7 @@ const fn profile(from: u16) -> Profile { four_radii: from >= 900, f64_rect: from >= 1500, old_master_flags: from < 1400, - paths: from >= 1600, + paths: from >= 1000, utf16_names: from >= 1200, } } @@ -120,6 +120,7 @@ const PROFILES: &[(u16, Profile)] = &[ (700, profile(700)), (800, profile(800)), (900, profile(900)), + (1000, profile(1000)), (1100, profile(1100)), (1200, profile(1200)), (1300, profile(1300)), @@ -166,12 +167,13 @@ mod tests { assert!(for_version(500).is_some_and(|p| !p.fixed_matrix && !p.types_from_zero && p.color == ColorLayout::Record12)); assert!(for_version(1300).is_some_and(|p| p.old_master_flags)); assert!(for_version(1400).is_some_and(|p| p.tagged && !p.f64_rect && !p.old_master_flags)); - assert!(for_version(1500).is_some_and(|p| p.tagged && p.f64_rect && p.mcfg_skip == 12 && !p.paths)); + assert!(for_version(1500).is_some_and(|p| p.tagged && p.f64_rect && p.mcfg_skip == 12 && p.paths)); assert!(for_version(2430).is_some_and(|p| p.f64_rect && p.paths && p.utf16_names)); - assert!(for_version(500).is_some_and(|p| p.short && p.mcfg_skip == 0)); - assert!(for_version(600).is_some_and(|p| !p.short && p.mcfg_skip == 28)); - assert!(for_version(801).is_some_and(|p| p.mcfg_skip == 0 && p.marker_after_dash.is_none())); - assert!(for_version(1000).is_some_and(|p| p.mcfg_skip == 4 && p.four_radii && !p.utf16_names)); + assert!(for_version(500).is_some_and(|p| p.short && p.mcfg_skip == 0 && p.marker_after_dash.is_none())); + assert!(for_version(600).is_some_and(|p| !p.short && p.mcfg_skip == 28 && p.marker_after_dash == Some(22))); + assert!(for_version(801).is_some_and(|p| p.mcfg_skip == 0 && p.marker_after_dash == Some(22))); + assert!(for_version(999).is_some_and(|p| !p.paths && p.marker_after_dash == Some(22))); + assert!(for_version(1000).is_some_and(|p| p.mcfg_skip == 4 && p.four_radii && !p.utf16_names && p.paths)); assert!(for_version(1300).is_some_and(|p| p.tagged && p.trfd_head == 8 && p.outl_after_angle == 46)); // The table is in order, so the search finds each range's own row. assert!(PROFILES.windows(2).all(|w| w[0].0 < w[1].0)); diff --git a/crates/cdr/src/records.rs b/crates/cdr/src/records.rs index 379b4f8340..dd3ac5e18a 100644 --- a/crates/cdr/src/records.rs +++ b/crates/cdr/src/records.rs @@ -414,6 +414,40 @@ fn windows_1252(c: u8) -> char { mod tests { use super::*; + /// An outline that names an arrowhead says so with a warning, in each version whose arrowhead + /// ids have a known place: 22 bytes after the first dash in versions 6 to 12, 20 from 13. + #[test] + fn arrowheads_are_named_from_version_6() { + let pen = crate::synth::Pen { line_type: 0, width: 1.0, cap: 0, join: 0 }; + let color = [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100]; + for (version, place) in [ + (500, None), + (600, Some(22)), + (700, Some(22)), + (800, Some(22)), + (900, Some(22)), + (1000, Some(22)), + (1100, Some(22)), + (1200, Some(22)), + (1300, Some(20)), + (1700, Some(20)), + ] { + let p = crate::layout::for_version(version).unwrap(); + let plain = crate::synth::V(version).outline(7, pen, &color, &[]); + // The record ends with 32 bytes after its dashes, and this one has none. + let first_dash = plain.len() - 32; + let mut w = vec![]; + assert!(outline(&plain, &p, &mut w).is_some_and(|(id, o)| id == 7 && o.is_some()), "{version}"); + assert!(w.is_empty(), "{version}: {w:?}"); + let Some(place) = place else { continue }; + let mut arrow = plain.clone(); + arrow[first_dash + place..first_dash + place + 4].copy_from_slice(&9u32.to_le_bytes()); + let mut w = vec![]; + assert!(outline(&arrow, &p, &mut w).is_some(), "{version}"); + assert_eq!(w, vec![Warning::Arrowheads], "{version}"); + } + } + #[test] fn single_byte_names_read_as_windows_1252() { let p = crate::layout::for_version(1100).unwrap(); diff --git a/crates/cdr/src/synth.rs b/crates/cdr/src/synth.rs index bc55b4245d..e78028b675 100644 --- a/crates/cdr/src/synth.rs +++ b/crates/cdr/src/synth.rs @@ -426,11 +426,25 @@ pub fn object(v: V, kind: u32, args: &[(u32, Vec)], matrix: Option<[f64; 6]> list(b"obj ", vec![chunk(b"flgs", vec![0, 0, 0, 0x08]), list(b"lgob", own)]) } -/// A layer list of `kind` flags named `name` holding `objects`, its `loda` in a `lgob` list (R22). +/// A layer list of `kind` flags named `name` holding `objects`, its `loda` in a `lgob` list (R22), +/// with the object type that layers of such flags have in sample files (R19). pub fn layer(v: V, flags: [u8; 4], name: &str, objects: Vec) -> Node { + let kind = match flags { + [b0, ..] if b0 & 0x10 != 0 => 11, + [b0, ..] if b0 & 0x0f == 0x0a || b0 & 0x0f == 0x02 => 12, + [0x08, ..] | [0, 1, 1, _] => 17, + _ => 0, + }; + // Version 3 counts its types from 0, one less (R32). + let kind = if v.0 < 400 && kind > 0 { kind - 1 } else { kind }; + layer_of_type(v, flags, kind, name, objects) +} + +/// A layer list as [`layer`] writes it, with the object type `kind` in its `loda`. +pub fn layer_of_type(v: V, flags: [u8; 4], kind: u32, name: &str, objects: Vec) -> Node { let name: Vec = if v.0 >= 1200 { name.encode_utf16().chain([0]).flat_map(|u| u.to_le_bytes()).collect() } else { name.bytes().chain([0]).collect() }; - let mut kids = vec![chunk(b"flgs", flags.to_vec()), list(b"lgob", vec![chunk(b"loda", v.loda(0, &[(1000, name)]))])]; + let mut kids = vec![chunk(b"flgs", flags.to_vec()), list(b"lgob", vec![chunk(b"loda", v.loda(kind, &[(1000, name)]))])]; kids.extend(objects); list(b"layr", kids) } @@ -730,19 +744,20 @@ pub enum WlShape { Text, } -/// A drawn object's record of a version 1 or 2 file (R42, R43, R44): `shape` placed by `matrix` +/// A drawn object's record of a version 1 file (R42, R43, R44): `shape` placed by `matrix` /// `[a, b, c, d, e, f]` with offsets in points, a uniform fill of `fill` and an outline of a width /// in points and a color. The record's point holds the matrix's x offset and its `ty` the y -/// offset. +/// offset. [`wl_file`] moves the fill and the shape to their places in a version 2 file. pub fn wl_object(shape: WlShape, matrix: [f64; 6], fill: Option<[u8; 5]>, outline: Option<(f64, [u8; 5])>) -> Vec { let v = V(300); let (kind, body): (u8, Vec) = match shape { - WlShape::Rect { w, h, radius } => (0, [v.coord(w), v.coord(h), vec![0; 2], v.coord(radius)].concat()), + WlShape::Rect { w, h, radius } => (0, [v.coord(w), v.coord(h), v.coord(radius), vec![0; 2]].concat()), WlShape::Ellipse { w, h, angles } => (1, [v.coord(w), v.coord(h), angles.0.to_le_bytes().to_vec(), angles.1.to_le_bytes().to_vec()].concat()), WlShape::Curve(points) => (2, v.points(&points)), WlShape::Text => (3, vec![0; 30]), }; let mut r = vec![0u8; (114 + body.len()).max(118)]; + r[..4].copy_from_slice(&WL_RECORD_START); r[26] = 0x40; r[27] = 0x20; let size = (r.len() - 32) as u16; @@ -768,27 +783,54 @@ pub fn wl_object(shape: WlShape, matrix: [f64; 6], fill: Option<[u8; 5]>, outlin r } +/// The u32 that starts every record of a version 1 or 2 file (R42). +const WL_RECORD_START: [u8; 4] = 24u32.to_le_bytes(); + /// A group or layer record of a version 1 or 2 file (R42): 28 bytes. pub fn wl_group() -> Vec { let mut r = vec![0u8; 28]; - r[27] = 1; + r[..4].copy_from_slice(&WL_RECORD_START); + r[26] = 1; + r[27] = 0x60; r } -/// A file of version 1 or 2 (R40, R41): the header with `marker` (`e` or `d`, or `m` for version -/// 2), a page of `page` points, the `records` and an empty font table. +/// A drawn object's record of a version 1 file laid out as in a version 2 file (R43): 11 more bytes +/// before the fill and 11 more before the shape. Group records stay as they are. +fn wl_v2_record(rec: &[u8]) -> Vec { + if rec.get(26).is_none_or(|flags| flags & 0x40 == 0) || rec.len() < 114 { + return rec.to_vec(); + } + let mut out = [&rec[..94], &[0; 11], &rec[94..114], &[0; 11], &rec[114..]].concat(); + let size = (out.len() - 32) as u16; + out[28..30].copy_from_slice(&size.to_le_bytes()); + out +} + +/// A file of version 1 or 2 (R40, R41): the header with `marker` (`e` for version 1, `l` or `m` +/// for version 2), a page of `page` points, the `records` and an empty font table. A version 1 +/// file's settings start after the header; a version 2 file's start further on, where a picture +/// lies in the files of that version, and its drawn objects are laid out as [`wl_v2_record`] says. pub fn wl_file(marker: u8, page: (f64, f64), records: &[Vec]) -> Vec { let v = V(300); + let settings = if marker == b'e' { 28u32 } else { 40 }; let start = 64u32; let mut out = vec![0u8; start as usize]; out[..4].copy_from_slice(&[b'W', b'L', marker, 0]); let (w, h) = page; - out[30..38].copy_from_slice(&[v.coord(-w / 2.0), v.coord(h / 2.0), v.coord(w / 2.0), v.coord(-h / 2.0)].concat()); - out.extend(records.iter().flatten()); + let page_box = settings as usize + 2; + out[page_box..page_box + 8].copy_from_slice(&[v.coord(-w / 2.0), v.coord(h / 2.0), v.coord(w / 2.0), v.coord(-h / 2.0)].concat()); + for r in records { + if marker == b'e' { + out.extend(r); + } else { + out.extend(wl_v2_record(r)); + } + } let end = out.len() as u32; out.extend([0; 2]); let trailer = out.len() as u32; - for (k, offset) in [28, start, end, trailer, trailer - 2].into_iter().enumerate() { + for (k, offset) in [settings, start, end, trailer, trailer - 2].into_iter().enumerate() { out[4 + 4 * k..8 + 4 * k].copy_from_slice(&offset.to_le_bytes()); } out diff --git a/crates/cdr/src/wl.rs b/crates/cdr/src/wl.rs index c407421c98..0a20c00893 100644 --- a/crates/cdr/src/wl.rs +++ b/crates/cdr/src/wl.rs @@ -1,8 +1,8 @@ //! Drawings of versions 1 and 2: the file before RIFF, which starts with `WL` (R40 to R44). A -//! header of offsets, the page's box, then one flat list of records: 28 bytes for a group or a -//! layer, and a longer record for each drawn object, which holds its matrix, fill, outline and -//! shape. The reader maps the rectangles, ellipses and curves onto one page with one layer and -//! keeps no groups. +//! header of offsets, the settings with the page's box, then one flat list of records: 28 bytes +//! for a group or a layer, and a longer record for each drawn object, which holds its matrix, fill, +//! outline and shape. The reader maps the rectangles, ellipses and curves onto one page with one +//! layer and keeps no groups. use crate::bytes::{Rd, u32_at}; use crate::geometry; @@ -15,38 +15,56 @@ use crate::{Error, Warning}; /// The header: the signature, a marker byte, a zero and five u32 offsets (R40). const HEADER: usize = 28; -/// The marker bytes seen after `WL` (R40). `m` marks version 2. -const MARKERS: [u8; 3] = *b"dem"; -/// Where the page's box lies (R41). -const PAGE_BOX: usize = 30; +/// The marker bytes read after `WL` (R40): `e` for version 1, `l` and `m` for version 2. +const MARKERS: [u8; 3] = *b"elm"; +/// Where the page's box lies after the start of the settings (R41). +const PAGE_BOX: usize = 2; /// The bytes of a group or layer record (R42). const GROUP: usize = 28; -/// The low flag byte, at byte 26, of a drawn object (R42). +/// The u32 that starts every record (R42). +const RECORD_START: u32 = 24; +/// The flag bit, in byte 26, of a drawn object (R42). const DRAWN: u8 = 0x40; -/// The bytes after a drawn object's body size, and the least a drawn object takes with them (R42). +/// The bytes after a drawn object's body size (R42). const AFTER_BODY: usize = 32; -const LEAST: usize = 118; /// Where a drawn object's fields lie (R43, R44). const POINT: usize = 32; const KIND: usize = 60; const OUTLINED: usize = 67; const OUTLINE_WIDTH: usize = 68; const OUTLINE_COLOR: usize = 74; -const FILL_KIND: usize = 94; -const FILL_COLOR: usize = 95; -const SHAPE: usize = 114; + +/// Where a drawn object's fill and shape lie, which differs between the markers (R43, R44). +#[derive(Clone, Copy)] +struct Fields { + fill_kind: usize, + shape: usize, +} + +impl Fields { + fn of(marker: u8) -> Self { + if marker == b'e' { Fields { fill_kind: 94, shape: 114 } } else { Fields { fill_kind: 105, shape: 136 } } + } + + /// The least a drawn object takes: its fixed fields and the 4 bytes of a shape's size. + fn least(self) -> usize { + self.shape + 4 + } +} /// Object types (R43). const RECTANGLE: u8 = 0; const ELLIPSE: u8 = 1; const CURVE: u8 = 2; +const TEXT: u8 = 3; -/// Where the records start and end, when `b` has a `WL` header whose offsets fit the file. -fn header(b: &[u8]) -> Option<(usize, usize)> { +/// Where the settings start, and where the records start and end, when `b` has a `WL` header +/// whose offsets fit the file. +fn header(b: &[u8]) -> Option<(usize, usize, usize)> { if b.get(..2)? != b"WL" || !MARKERS.contains(b.get(2)?) || *b.get(3)? != 0 { return None; } - let (start, end) = (u32_at(b, 8)? as usize, u32_at(b, 12)? as usize); - (start >= HEADER && start <= end && end <= b.len()).then_some((start, end)) + let (settings, start, end) = (u32_at(b, 4)? as usize, u32_at(b, 8)? as usize, u32_at(b, 12)? as usize); + (settings >= HEADER && settings <= start && start <= end && end <= b.len()).then_some((settings, start, end)) } /// Does `b` start like a drawing of version 1 or 2? The two letters alone are not enough: the @@ -56,7 +74,9 @@ pub(crate) fn is_wl(b: &[u8]) -> bool { } pub(crate) fn drawing(b: &[u8], budget: &mut Budget) -> Result { - let (start, end) = header(b).ok_or(Error::NotCdr)?; + let (settings, start, end) = header(b).ok_or(Error::NotCdr)?; + let marker = b.get(2).copied().ok_or(Error::NotCdr)?; + let fields = Fields::of(marker); // Lengths are i16 in 1/1000 inch, as in version 3 (R5). let p = layout::for_version(layout::OLDEST_RIFF).ok_or(Error::NotCdr)?; let mut warn = Vec::new(); @@ -65,12 +85,23 @@ pub(crate) fn drawing(b: &[u8], budget: &mut Budget) -> Result { // Every step takes at least a group record's bytes, so the file's size bounds the steps. while end - at >= GROUP { budget.take_chunk()?; - if b.get(at + 26) != Some(&DRAWN) { + // A record that doesn't start as records do is past one whose length isn't known. + if u32_at(b, at) != Some(RECORD_START) { + break; + } + if b.get(at + 26).is_none_or(|flags| flags & DRAWN == 0) { at += GROUP; continue; } + // A drawn object of another type than rectangle, ellipse, curve and text doesn't say its + // length in the same way, so the records after it can't be found. + if b.get(at + KIND).is_none_or(|kind| *kind > TEXT) { + push(&mut warn, Warning::UnsupportedObjects); + at = end; + break; + } let len = Rd::at(b, at + 28).u16().map_or(0, |body| usize::from(body) + AFTER_BODY); - let Some(rec) = b.get(at..at + len).filter(|_| len >= LEAST && at + len <= end) else { + let Some(rec) = b.get(at..at + len).filter(|_| len >= fields.least() && at + len <= end) else { push(&mut warn, Warning::Damaged); at = end; break; @@ -81,20 +112,20 @@ pub(crate) fn drawing(b: &[u8], budget: &mut Budget) -> Result { at = end; break; } - objects.extend(object(rec, &p, budget, &mut warn)); + objects.extend(object(rec, fields, &p, budget, &mut warn)); } if at != end { push(&mut warn, Warning::Damaged); } - let version = if b.get(2) == Some(&b'm') { 200 } else { 100 }; + let version = if marker == b'e' { 100 } else { 200 }; let layer = Layer { name: String::new(), kind: LayerKind::Drawing, objects, guides: Vec::new() }; let pages = vec![Page { size: None, layers: vec![layer] }]; - Ok(Drawing { version, page_size: page_box(b, &p), pages, master: Vec::new(), guides: Vec::new(), warnings: warn }) + Ok(Drawing { version, page_size: page_box(b, settings, &p), pages, master: Vec::new(), guides: Vec::new(), warnings: warn }) } /// The page's size from its box (R41): left, top, right and bottom around the page's centre. -fn page_box(b: &[u8], p: &Profile) -> Option<(f64, f64)> { - let mut r = Rd::at(b, PAGE_BOX); +fn page_box(b: &[u8], settings: usize, p: &Profile) -> Option<(f64, f64)> { + let mut r = Rd::at(b, settings + PAGE_BOX); let (left, top, right, bottom) = (coord(&mut r, p)?, coord(&mut r, p)?, coord(&mut r, p)?, coord(&mut r, p)?); let (w, h) = ((right - left).abs(), (top - bottom).abs()); (w > 0.0 && h > 0.0).then_some((w, h)) @@ -102,8 +133,8 @@ fn page_box(b: &[u8], p: &Profile) -> Option<(f64, f64)> { /// One drawn object's record (R43, R44): a shape, or `None` with a warning for text, for an object /// of another type and for a shape that can't be read. -fn object(rec: &[u8], p: &Profile, budget: &mut Budget, warn: &mut Vec) -> Option { - let read = shape(rec, p, budget, warn); +fn object(rec: &[u8], fields: Fields, p: &Profile, budget: &mut Budget, warn: &mut Vec) -> Option { + let read = shape(rec, fields, p, budget, warn); if read.is_none() { push(warn, Warning::Damaged); } @@ -112,14 +143,14 @@ fn object(rec: &[u8], p: &Profile, budget: &mut Budget, warn: &mut Vec) /// The shape of a record. The outer `None` is a record that can't be read; the inner one an object /// the reader leaves out, with its warning. -fn shape(rec: &[u8], p: &Profile, budget: &mut Budget, warn: &mut Vec) -> Option> { +fn shape(rec: &[u8], fields: Fields, p: &Profile, budget: &mut Budget, warn: &mut Vec) -> Option> { let k = p.unit(); let mut r = Rd::at(rec, POINT); let (x, y) = (f64::from(r.i16()?), f64::from(r.i16()?)); // x' = a x + b y + tx + x of the point, y' = c x + d y + ty + y of the point. let (a, b, tx, c, d, ty) = (fixed(&mut r)?, fixed(&mut r)?, fixed(&mut r)?, fixed(&mut r)?, fixed(&mut r)?, fixed(&mut r)?); let matrix = Matrix([a, c, b, d, (tx + x) * k, (ty + y) * k]); - let body = rec.get(SHAPE..)?; + let body = rec.get(fields.shape..)?; let geometry = match *rec.get(KIND)? { RECTANGLE => rect(body, p)?, ELLIPSE => { @@ -140,9 +171,9 @@ fn shape(rec: &[u8], p: &Profile, budget: &mut Budget, warn: &mut Vec) } }; let color_at = |at: usize, warn: &mut Vec| Some(records::color5(rec.get(at..at + 5)?.try_into().ok()?, warn)); - let fill = match *rec.get(FILL_KIND)? { + let fill = match *rec.get(fields.fill_kind)? { 0 => None, - 1 => Some(color_at(FILL_COLOR, warn)?), + 1 => Some(color_at(fields.fill_kind + 1, warn)?), _ => { push(warn, Warning::UnsupportedFills); None @@ -157,12 +188,12 @@ fn shape(rec: &[u8], p: &Profile, budget: &mut Budget, warn: &mut Vec) Some(Some(Shape { geometry, matrix, fill, outline })) } -/// A rectangle (R44): its width and height, then after 2 bytes a corner radius, which is kept when -/// it is at most half the shorter side. +/// A rectangle (R44): its width and height, then a corner radius, which is kept when it is at most +/// half the shorter side. fn rect(body: &[u8], p: &Profile) -> Option { let mut r = Rd::new(body); let (w, h) = (coord(&mut r, p)?, coord(&mut r, p)?); - let radius = r.skip(2).and_then(|()| coord(&mut r, p)).filter(|v| *v > 0.0 && *v <= w.abs().min(h.abs()) / 2.0).unwrap_or(0.0); + let radius = coord(&mut r, p).filter(|v| *v > 0.0 && *v <= w.abs().min(h.abs()) / 2.0).unwrap_or(0.0); Some(Geometry::Rect { w, h, radii: [radius; 4] }) } @@ -193,18 +224,76 @@ mod tests { assert!(!is_wl(&with(8, 27))); assert!(!is_wl(&with(8, file.len() as u32))); assert!(!is_wl(&with(12, file.len() as u32 + 1))); + // Settings that start inside the header or after the records. + assert!(!is_wl(&with(4, 27))); + assert!(!is_wl(&with(4, 65))); let mut other = file.clone(); other[2] = b'x'; assert!(!is_wl(&other)); + // No sample shows the marker `d`, so it is not taken. + other[2] = b'd'; + assert!(!is_wl(&other)); + for marker in *b"lm" { + assert!(is_wl(&synth::wl_file(marker, (612.0, 792.0), &[synth::wl_group()]))); + } other[2] = b'e'; other[3] = 1; assert!(!is_wl(&other)); } + #[test] + fn a_version_2_file_keeps_its_page_box_fill_and_shape_where_that_version_puts_them() { + let (red, black) = ([1, 0, 100, 100, 0], [1, 0, 0, 0, 100]); + let rect = synth::wl_object(WlShape::Rect { w: 72.0, h: 36.0, radius: 9.0 }, [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], Some(red), Some((2.0, black))); + for marker in *b"elm" { + let file = synth::wl_file(marker, (720.0, 504.0), std::slice::from_ref(&rect)); + // Version 2 files start their settings after a picture, not after the header. + assert_eq!(u32_at(&file, 4) == Some(28), marker == b'e'); + let d = read(&file).unwrap(); + let near = |a: f64, b: f64| (a - b).abs() < 0.02; + assert_eq!((d.version, d.warnings.clone()), (if marker == b'e' { 100 } else { 200 }, vec![])); + assert!(d.page_size.is_some_and(|(w, h)| near(w, 720.0) && near(h, 504.0)), "{:?}", d.page_size); + let Object::Shape(s) = &d.pages[0].layers[0].objects[0] else { panic!("group") }; + assert!( + matches!(s.geometry, Geometry::Rect { w, h, radii } if near(w, 72.0) && near(h, 36.0) && near(radii[0], 9.0)), + "{}", + marker as char + ); + assert_eq!(s.fill, Some(Color::Cmyk([0.0, 1.0, 1.0, 0.0])), "{}", marker as char); + assert!(s.outline.as_ref().is_some_and(|o| near(o.width, 2.0) && o.color == Color::Cmyk([0.0, 0.0, 0.0, 1.0])), "{}", marker as char); + } + } + + #[test] + fn a_drawn_object_is_marked_by_its_flag_bit() { + let rect = synth::wl_object(WlShape::Rect { w: 72.0, h: 36.0, radius: 0.0 }, [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], None, None); + let mut file = synth::wl_file(b'l', (612.0, 792.0), &[rect]); + // Files with the marker `l` set another flag bit beside it. + file[64 + 26] = 0xc0; + let d = read(&file).unwrap(); + assert_eq!((d.pages[0].layers[0].objects.len(), d.warnings), (1, vec![])); + } + + #[test] + fn the_walk_stops_where_records_can_no_longer_be_found() { + let unit = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + let rect = || synth::wl_object(WlShape::Rect { w: 72.0, h: 36.0, radius: 0.0 }, unit, None, None); + // An object of another type doesn't give its length in the same way. + let mut other = rect(); + other[KIND] = 0xff; + let d = read(&synth::wl_file(b'm', (612.0, 792.0), &[rect(), other, rect()])).unwrap(); + assert_eq!((d.pages[0].layers[0].objects.len(), d.warnings), (1, vec![Warning::UnsupportedObjects])); + // A record that doesn't start as records do. + let mut lost = rect(); + lost[0] = 0x84; + let d = read(&synth::wl_file(b'e', (612.0, 792.0), &[rect(), lost, rect()])).unwrap(); + assert_eq!((d.pages[0].layers[0].objects.len(), d.warnings), (1, vec![Warning::Damaged])); + } + #[test] fn a_rectangle_keeps_a_radius_of_at_most_half_its_shorter_side() { let p = layout::for_version(300).unwrap(); - let body = |radius: i16| [1000i16, 500, 0, radius].iter().flat_map(|v| v.to_le_bytes()).collect::>(); + let body = |radius: i16| [1000i16, 500, radius].iter().flat_map(|v| v.to_le_bytes()).collect::>(); let radius_of = |b: &[u8]| match rect(b, &p) { Some(Geometry::Rect { w, h, radii }) if (w - 72.0).abs() < 1e-9 && (h - 36.0).abs() < 1e-9 => radii[0], other => panic!("{other:?}"), @@ -223,7 +312,7 @@ mod tests { let rect = synth::wl_object(WlShape::Rect { w: 72.0, h: 36.0, radius: 0.0 }, [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], Some([1, 0, 0, 0, 100]), None); let mut short = rect.clone(); // A body size that leaves less than the fixed fields. - short[28..30].copy_from_slice(&((LEAST - AFTER_BODY - 1) as u16).to_le_bytes()); + short[28..30].copy_from_slice(&((Fields::of(b'e').least() - AFTER_BODY - 1) as u16).to_le_bytes()); let d = read(&synth::wl_file(b'e', (612.0, 792.0), &[rect.clone(), short, rect])).unwrap(); assert_eq!(d.pages[0].layers[0].objects.len(), 1); assert_eq!(d.warnings, vec![Warning::Damaged]); @@ -237,7 +326,7 @@ mod tests { let Object::Shape(s) = &d.pages[0].layers[0].objects[0] else { panic!("group") }; assert_eq!(s.fill, Some(Color::Spot { name: "Spot 0:9".into(), tint: 0.5, look: None })); assert_eq!((d.version, d.warnings), (200, vec![Warning::SpotLooks])); - textured[FILL_KIND] = 5; + textured[Fields::of(b'e').fill_kind] = 5; let d = read(&synth::wl_file(b'm', (612.0, 792.0), &[textured])).unwrap(); let Object::Shape(s) = &d.pages[0].layers[0].objects[0] else { panic!("group") }; assert_eq!((s.fill.clone(), d.warnings), (None, vec![Warning::UnsupportedFills])); diff --git a/crates/cdr/tests/read.rs b/crates/cdr/tests/read.rs index 88c015bd89..49d447c170 100644 --- a/crates/cdr/tests/read.rs +++ b/crates/cdr/tests/read.rs @@ -336,7 +336,7 @@ fn wl_objects(d: &vectorcraft_cdr::Drawing) -> &[Object] { #[test] fn versions_1_and_2_read_their_sample() { - for (marker, version) in [(b'e', 100), (b'd', 100), (b'm', 200)] { + for (marker, version) in [(b'e', 100), (b'l', 200), (b'm', 200)] { let file = synth::wl_sample(marker); assert!(file.starts_with(b"WL") && vectorcraft_cdr::is_cdr(&file)); let d = read(&file, Limits::default()).unwrap(); @@ -855,7 +855,7 @@ fn rectangles_from_version_15_give_radii_as_shares_of_half_the_shorter_side() { } #[test] -fn paths_of_version_16_read_their_points() { +fn paths_from_version_10_read_their_points() { let v = V(1700); let mut c = vec![0; 4]; c.extend(2u16.to_le_bytes()); @@ -869,13 +869,15 @@ fn paths_of_version_16_read_their_points() { &[synth::page(false, vec![synth::layer(v, [0, 0, 0, 0x98], "L", vec![synth::object(v, 0x25, &[(30, c.clone())], None)])])], ) }; - let d = read(&objects(1700), Limits::default()).unwrap(); - let s = shapes(&d.pages[0].layers[0].objects); - let Geometry::Path(p) = &s[0].geometry else { panic!("{:?}", s[0].geometry) }; - assert_eq!(p.subpaths[0].segments.len(), 2); - assert!(d.warnings.is_empty(), "{:?}", d.warnings); - // Before 16 the type is not a path. - let d = read(&objects(1500), Limits::default()).unwrap(); + for version in [1000, 1300, 1500, 1700] { + let d = read(&objects(version), Limits::default()).unwrap(); + let s = shapes(&d.pages[0].layers[0].objects); + let Geometry::Path(p) = &s[0].geometry else { panic!("{version}: {:?}", s[0].geometry) }; + assert_eq!(p.subpaths[0].segments.len(), 2); + assert!(d.warnings.is_empty(), "{version}: {:?}", d.warnings); + } + // No sample before version 10 holds the type, so it is not read as a path there. + let d = read(&objects(900), Limits::default()).unwrap(); assert!(d.pages[0].layers[0].objects.is_empty() && d.warnings.contains(&Warning::UnsupportedObjects)); } @@ -900,21 +902,37 @@ fn a_template_takes_its_version_from_the_vrsn_chunk() { } #[test] -fn master_page_layers_take_their_kind_by_byte_0_from_version_14() { - // Flags that mark the master page's guides and desktop layers in versions 7 to 13 mark - // drawing layers from 14. - for version in [1300, 1700] { +fn layers_take_their_kind_from_their_object_type() { + for version in [300, 800, 1300, 1700] { let v = V(version); let rect = || synth::object(v, 1, &[(30, v.rect(10.0, 10.0, 0.0))], None); - let master = - synth::page(true, vec![synth::layer(v, [2, 1, 1, 0x98], "G", vec![rect()]), synth::layer(v, [0, 1, 1, 0x98], "D", vec![rect()])]); - let own = synth::page(false, vec![synth::layer(v, [0, 0, 0, 0x98], "L", vec![rect()])]); + // Version 3 counts its types from 0 (R32). + let ty = |t: u32| if version < 400 { t - 1 } else { t }; + // Master page layers flagged in the older way, with the types of guides and desktop layers, + // and a drawing layer whose flags would make it a desktop layer. + let master = synth::page( + true, + vec![ + synth::layer_of_type(v, [2, 1, 1, 0x98], ty(12), "G", vec![rect()]), + synth::layer_of_type(v, [0, 1, 1, 0x98], ty(17), "D", vec![rect()]), + synth::layer_of_type(v, [8, 1, 0, 0x98], 0, "M", vec![rect()]), + ], + ); + // A drawing layer with flag bit 0x10 set, and a grid layer. + let own = synth::page( + false, + vec![ + synth::layer_of_type(v, [0x10, 0, 0, 0x98], 0, "L", vec![rect()]), + synth::layer_of_type(v, [0x1a, 0, 1, 0x98], ty(11), "Grid", vec![rect()]), + ], + ); let body = [chunk(b"vrsn", version.to_le_bytes().to_vec()), master, own]; let file = if version < 1400 { synth::riff(version, &body) } else { synth::package16(version, &body, true, &[]) }; let d = read(&file, Limits::default()).unwrap(); let kinds: Vec<_> = d.master.iter().map(|l| l.kind).collect(); - let want = if version < 1400 { vec![LayerKind::Guides, LayerKind::Desktop] } else { vec![LayerKind::Drawing, LayerKind::Drawing] }; - assert_eq!(kinds, want, "{version}"); + assert_eq!(kinds, [LayerKind::Guides, LayerKind::Desktop, LayerKind::Drawing], "{version}"); + let kinds: Vec<_> = d.pages[0].layers.iter().map(|l| (l.kind, l.objects.len())).collect(); + assert_eq!(kinds, [(LayerKind::Drawing, 1), (LayerKind::Grid, 1)], "{version}"); } } @@ -1416,7 +1434,7 @@ proptest! { } #[test] - fn never_panics_on_garbage_after_a_wl_header(marker in prop::sample::select(vec![b'd', b'e', b'm']), bytes in prop::collection::vec(any::(), 0..2048)) { + fn never_panics_on_garbage_after_a_wl_header(marker in prop::sample::select(vec![b'e', b'l', b'm']), bytes in prop::collection::vec(any::(), 0..2048)) { // Offsets that fit the file, so the garbage is walked as records. let mut b = vec![b'W', b'L', marker, 0]; b.extend(28u32.to_le_bytes()); @@ -1429,7 +1447,7 @@ proptest! { } #[test] - fn never_panics_on_mutated_wl_files(marker in prop::sample::select(vec![b'd', b'e', b'm']), cut in any::(), edits in prop::collection::vec((any::(), any::()), 1..24)) { + fn never_panics_on_mutated_wl_files(marker in prop::sample::select(vec![b'e', b'l', b'm']), cut in any::(), edits in prop::collection::vec((any::(), any::()), 1..24)) { let mut b = synth::wl_sample(marker); let n = b.len(); for (at, v) in edits { diff --git a/crates/engine/src/cmd/fileio/tests_cdr.rs b/crates/engine/src/cmd/fileio/tests_cdr.rs index 6cd91c3204..9b3d3cdb77 100644 --- a/crates/engine/src/cmd/fileio/tests_cdr.rs +++ b/crates/engine/src/cmd/fileio/tests_cdr.rs @@ -118,7 +118,7 @@ fn versions_3_and_4_open_and_place_as_editable_art() { fn versions_1_and_2_open_and_place_as_editable_art() { use vectorcraft_color::{Color, Paint}; use vectorcraft_doc::appearance::AppearanceItem; - for marker in *b"em" { + for marker in *b"elm" { let bytes = synth::wl_sample(marker); let mut s = Session::new(); for name in ["drawing.cdr", "renamed.bin"] { diff --git a/crates/engine/tests/import_fuzz.rs b/crates/engine/tests/import_fuzz.rs index fdc2be6692..d538db4b1d 100644 --- a/crates/engine/tests/import_fuzz.rs +++ b/crates/engine/tests/import_fuzz.rs @@ -344,7 +344,7 @@ proptest! { // Drawings of versions 1 and 2 are not RIFF files: a header of offsets, then records. #[test] fn cdr_mutated_wl_drawings_render_and_export( - marker in prop::sample::select(vec![b'd', b'e', b'm']), + marker in prop::sample::select(vec![b'e', b'l', b'm']), cut in prop::option::of(0usize..1024), edits in prop::collection::vec((0usize..8192, any::()), 0..24), ) { @@ -366,7 +366,7 @@ proptest! { #[test] fn cdr_garbage_after_a_wl_header_opens_and_places_without_panicking( - marker in prop::sample::select(vec![b'd', b'e', b'm']), + marker in prop::sample::select(vec![b'e', b'l', b'm']), tail in prop::collection::vec(any::(), 0..2048), ) { // Offsets that fit the file, so the garbage is walked as records. diff --git a/docs/mcp.md b/docs/mcp.md index beac1db458..beac6b4b61 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -6852,10 +6852,11 @@ fountain, pattern, texture and PostScript fills, and outline arrowheads are left were left out. Drawings of versions after 27 are refused with a message, and so are packages that are encrypted, ZIP64 archives or split across files. VectorCraft doesn't write `.cdr` files. -No public specification covers versions 1 to 4, so the importer reads what published notes describe. A drawing of -version 1 or 2 opens as one page with one layer: its rectangles, whole ellipses and curves with their matrices, uniform -fills and outlines with their width and color. Its text, its arcs and pies, and its fills of other types are left out -with the same warnings, and its groups are not kept. A drawing of version 3 opens with its pages, layers and groups; +No public specification covers versions 1 to 4, so the importer reads the layouts found in public sample files. A +drawing of version 1 or 2 opens as one page with one layer: its rectangles, whole ellipses and curves with their +matrices, uniform fills and outlines with their width and color. Its text, its arcs and pies, and its fills of other +types are left out with the same warnings, and its groups are not kept. An object of another type ends the read of +its records, with the warning for unsupported objects. A drawing of version 3 opens with its pages, layers and groups; an envelope is not applied, so an object or a group under one keeps its outline, placed by its matrices, and the `warnings` say so. A drawing of version 4 opens as one of version 5 does. A file is taken for version 1 or 2 by its content only when the offsets in diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 2b32650bae..ae116dd6db 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1542,7 +1542,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - DXF export's Include Placed Images and Include Fill Hatches (`includeImages`, `includeHatches`; VectorCraft's own options, on by default and off in CNC / CAM) leave out placed images with their linked files, without decoding them, and the solid hatch after each filled area's outlines, each on its own, with one note in `warnings` when something was left out; R12 keeps its own notes. An open path's fill outline is closed, as the fill is, in every version (`an_open_path_s_fill_outline_keeps_its_closing_edge`, `fills_without_hatches_keep_their_outlines`, `images_can_be_left_out_with_their_files`, `images_and_hatches_are_left_out_independently_and_the_cnc_preset_leaves_out_both`, `placed_images_and_fill_hatches_can_be_left_out`, `include_placed_images_and_fill_hatches_follow_the_preset_and_a_change_shows_custom`). - DXF export writes a path of straight segments and circular arcs (a slot, a D shape, a rounded rectangle, a pie) as one polyline whose vertices carry a bulge (group 42) for each arc: an LWPOLYLINE in R14 and later, a POLYLINE with VERTEX entities in R12 and R13, closed when the path is closed, and it reads back as the same outline (#1299, `crates/cad/src/tests.rs`). Such a path was written as one SPLINE (a flattened polyline in R12); a path with any other curve still is. A shallow curve becomes an ARC or a bulge only when it lies on the circle within 5e-4 of its own extent as well as of the radius, and the drawing extents (`$EXTMIN`, `$EXTMAX`) reach the furthest points of every ARC and bulge. - Affinity documents in (AF.1–AF.4): `.af` (Affinity 3) and `.afdesign`/`.afphoto`/`.afpub` (Affinity 1 and 2, container versions 8–12) open and place as editable art: layers and sublayers, groups (pass-through or isolated), artboards (with their names and backgrounds) and Publisher pages side by side, curves with live corners, parametric shapes (rectangles with corner radii, ellipses, polygons, stars, pies, triangles, trapezoids), compound shapes, solid/linear/elliptical gradient fills and strokes (weight, dashes, alignment, scale-with-object), clipping by nesting, vector and pixel masks, artistic and frame text with fonts, sizes, tracking, leading and alignment, placed images (their original JPEG/PNG) and pixel layers (8/16-bit RGB, CMYK), opacity, visibility, locking and blend modes, at the document's resolution in points. Effects, adjustments, brushes and pressure profiles, transparency gradients, master pages, symbol instances (imported as their cached picture), special shapes (cloud, heart, cog…, imported as ellipses) and Affinity-only blend modes are named in the import warning. A file whose native data can't be read opens as its embedded preview with the reason, and that preview can't be placed. Save never writes back over the Affinity file and there is no Affinity export. AF.5 (from DesignCraft's import, storytold/designcraft#291) adds master pages (on a locked layer behind the pages that show them), tables (area type per cell and a line per edge), each paragraph's alignment, indents, spacing and leading, horizontal text scale, outlined text, frame text sized by its text scale, drop shadows and outer glows as live effects (inner shadows as inner glows), outline effects as strokes, colour and gradient overlays baked into the paint, embedded Affinity documents as editable content (their artboards don't become artboards), pixel layers over 64 MP at a reduced resolution and HEIC photos from their stored pixels; automatic leading (size plus the font's line gap) fitted on thumbnails; underlines and strikethrough; strokes whose style is None (every shape stores one) left out, which also brings 6 public files closer to their thumbnails; line breaks inside a paragraph; table cell text centred as Affinity centres it, also where it is taller than its cell; CMYK pixel layers converted through the document's embedded CMYK profile (relative colorimetric with black-point compensation, as each layer's colour settings name), or kept as CMYK images when there is none; outline effects on stroked shapes around their stroke; artistic text whose paragraphs align differently aligned within its longest line, a frame's first baseline at cap height, and frames whose last lines' descenders hang out made tall enough to show them (all measured on a local document's own PDF export: baselines match to 0.1 pt); a local 56-artboard document's mean thumbnail difference went from 16.2 to 11.4, and its pages' mean difference from the owner's PDF export from 12.9 to 9.3. AF.4 adds Affinity 3 artboard recognition and actual-outline clipping, source-backed JPEG/PNG raster tiles, Unicode text-run recovery, stored fallback/exact installed fonts, OpenType feature flags and compound-geometry limits. 51 public CC0/MIT/Apache-2.0 files (33 current `.af`), pinned and sha256-verified (`cargo xtask corpus --affinity`), test native retention and every-board native/SVG/PDF/PSD output for current files; selected files compare their saved thumbnails. No Affinity reopen or independent paired PSD oracle was available; [source audit and complex-font gaps](affinity-validation.md); [scope, provenance and limits](../crates/affinity/README.md). - - `.cdr` drawings of versions 1 to 27 open and place, and `.cdt` templates open as new untitled documents; the [supported file formats](https://helpx.adobe.com/illustrator/desktop/get-started/learn-the-basics/supported-file-formats-in-illustrator.html) page lists `.cdr` versions 5 to 10. `vectorcraft-cdr` reads the `WL` files of versions 1 and 2 (one page and one layer of rectangles, whole ellipses and curves with uniform fills and outlines), the RIFF chunk tree of versions 3 on and its compressed lists, and the ZIP packages of versions 14 on with the data streams of versions 16 on, and the importer maps pages to artboards side by side, layers by name, the master page's guidelines to guides on every page, groups, curves and paths, rectangles (live, with each corner's radius), ellipses (live), arcs and polygons, uniform fills in CMYK, CMY, RGB, gray and registration, and outlines with their width, caps, joins and dashes. A library color stored with its name and values (version 13 and later) becomes a spot swatch with its tint; one stored only by its number opens as a black spot color named by its palette and number, with a warning. Text, bitmaps, blends, contours, brush strokes, fountain, pattern, texture and PostScript fills, arrowheads and the envelopes of version 3 are left out with a warning, and encrypted, ZIP64 and split packages are refused with a message. Every record layout has a source in `crates/cdr/PROVENANCE.md`. Reading is bounded by limits on input size, inflated size, package members, nesting, chunks, objects, coordinates and dashes, where an outline's dashes count for every shape that uses it (`crates/cdr/tests/read.rs`, `crates/engine/src/cmd/fileio/tests_cdr.rs`, `crates/engine/tests/import_fuzz.rs`). + - `.cdr` drawings of versions 1 to 27 open and place, and `.cdt` templates open as new untitled documents. `vectorcraft-cdr` reads the `WL` files of versions 1 and 2 (one page and one layer of rectangles, whole ellipses and curves with uniform fills and outlines), the RIFF chunk tree of versions 3 on and its compressed lists, and the ZIP packages of versions 14 on with the data streams of versions 16 on, and the importer maps pages to artboards side by side, layers by name, the master page's guidelines to guides on every page, groups, curves and paths, rectangles (live, with each corner's radius), ellipses (live), arcs and polygons, uniform fills in CMYK, CMY, RGB, gray and registration, and outlines with their width, caps, joins and dashes. A library color stored with its name and values (version 13 and later) becomes a spot swatch with its tint; one stored only by its number opens as a black spot color named by its palette and number, with a warning. Text, bitmaps, blends, contours, brush strokes, fountain, pattern, texture and PostScript fills, arrowheads and the envelopes of version 3 are left out with a warning, and encrypted, ZIP64 and split packages are refused with a message. Every record layout has a source in `crates/cdr/PROVENANCE.md`. Reading is bounded by limits on input size, inflated size, package members, nesting, chunks, objects, coordinates and dashes, where an outline's dashes count for every shape that uses it (`crates/cdr/tests/read.rs`, `crates/engine/src/cmd/fileio/tests_cdr.rs`, `crates/engine/tests/import_fuzz.rs`). - Placed documents: File › Place of a `.vectorcraft` file links to it by default (`file.place`'s `link`): one locked object showing an artboard of the document (or its art's bounds), read the way placing it without Link reads it, its symbols, patterns and images kept apart from the document's; without Link (or from bytes, not a file) it is an editable copy as before. It stays vectors in every output (SVG, PDF, EPS, EMF, DXF), and so do placed documents inside it, read from their own files up to 8 levels deep (documents that place each other stop there). On screen they draw from bitmaps cached per file and zoom step, shared by every copy and made in the background (a placeholder until ready), so a sheet of them redraws in about a millisecond; exports and prints draw them exactly. Like linked images, a save keeps only a preview of each (JPEG when opaque) unless Include Linked Files; output, and zooming past the preview, read the file again, falling back to the preview with a warning when it is gone or changed. Saves for older versions write them as plain art. Links check, update, relink, Update Links on open, Link Info (the artboard's size), Placement Options, Break Link (also Embed, Object › Expand: the editable copy placing without Link gives, its resources joining the document), Edit Original (the document in a new tab) and the Links panel cover them; saving a document updates the open documents that place it. The native format (v4) keeps data of 8 KB or more after the JSON instead of base64 in it. - Placed documents' bitmaps on screen: when a bitmap made in the background is ready, the desktop app renders the canvas again only where the objects that show its file are, when the frame shown was rendered on the UI thread and that part is at most half the canvas. Bitmaps drawn or made since a frame began are kept for that frame past the 512 MiB budget, up to 2 GiB, so a view showing more than the budget does not make them again in every frame. A bitmap that would take them past 2 GiB is not kept, and its file draws exactly at that zoom. A bitmap that can't be made (the file can't be read and there is no preview) shows the light placeholder and is not asked for again. Each ready bitmap used to render the whole canvas again, and bitmaps past the budget and bitmaps that could not be made were asked for again in every frame (`crates/render/src/tests/placed_document.rs`). - Linked PDF and `.ai` pages: File › Place with Link (the default) places a page of a PDF, `.ai` or `.ait` file read as PDF pages as a placed document, as it links an image, the page and its box (`crop`) kept with the link: Links check, update and relink it, Break Link and Object › Expand give the group placing it without Link gives, every output draws the page's vectors, and Package copies the file. Without Link, or with a password or `textAs: "outlines"`, the page is embedded. EPS and PostScript `.ai` files are still embedded (`crates/engine/src/tests/placed_document.rs`).