VectorCraft: An open-source, vector art tool written by scratch in pure Rust
Place: Word 97-2003 .doc files place as area type with their formatting
dotCooCoo5fdccdf60255
diff --git a/Cargo.lock b/Cargo.lockindex 934be1b12..248679aac 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -705,6 +705,17 @@ dependencies = ["shlex",]+[[package]]+name = "cfb"+version = "0.15.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0f13298d9178622b7916ec5d01d470425c04273c8ed07e87b1c38b34c4f4612b"+dependencies = [+ "fnv",+ "uuid",+ "web-time",+]+[[package]]name = "cfg-if"version = "1.0.5"@@ -6205,6 +6216,7 @@ dependencies = [name = "vectorcraft-engine"version = "0.19.0"dependencies = [+ "cfb","craft-docpath","crc32fast","flate2",diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.tomlindex 7b1199277..9d4ad7399 100644--- a/crates/engine/Cargo.toml+++ b/crates/engine/Cargo.toml@@ -40,6 +40,8 @@ hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["stflate2 = { workspace = true }# The XML parts of Word documents placed as type (place/docx.rs).roxmltree = "0.21"+# The compound file that holds a Word 97-2003 document placed as type (place/doc.rs).+cfb = "0.15"kurbo = { workspace = true }serde = { workspace = true }serde_json = { workspace = true }diff --git a/crates/engine/src/cmd/fileio.rs b/crates/engine/src/cmd/fileio.rsindex b873f3638..4079b4230 100644--- a/crates/engine/src/cmd/fileio.rs+++ b/crates/engine/src/cmd/fileio.rs@@ -124,8 +124,9 @@ pub(crate) use export_names::{default_name, merge, unique_file_names, with_blackuse export_names::{distinct_names, write_encoded};use formats::{ARTBOARD, ARTBOARDS, RANGE, USE_ARTBOARDS, writable};pub use formats::{- DOCX_EXTS, FORMATS, Format, FormatOption, OPEN_EXTS, PLACE_EXTS, RTF_EXTS, TEXT_EXTS, UNASSOCIATED_EXTS, export_extensions, extension, file_stem,- format, format_for_name, has_own_title, library_open_filters, open_filters, place_filters, plugin_formats, titled_for_export, writable_format,+ DOC_EXTS, DOCX_EXTS, FORMATS, Format, FormatOption, OPEN_EXTS, PLACE_EXTS, RTF_EXTS, TEXT_EXTS, UNASSOCIATED_EXTS, export_extensions, extension,+ file_stem, format, format_for_name, has_own_title, library_open_filters, open_filters, place_filters, plugin_formats, titled_for_export,+ writable_format,};#[cfg(test)]diff --git a/crates/engine/src/cmd/fileio/formats.rs b/crates/engine/src/cmd/fileio/formats.rsindex a1687e214..8df178845 100644--- a/crates/engine/src/cmd/fileio/formats.rs+++ b/crates/engine/src/cmd/fileio/formats.rs@@ -566,8 +566,14 @@ pub const RTF_EXTS: &[&str] = &["rtf"];/// Word documents: File → Place sets them as area type with their formatting.pub const DOCX_EXTS: &[&str] = &["docx"];-/// Every extension File → Place reads: [`OPEN_EXTS`], [`TEXT_EXTS`], [`RTF_EXTS`] and-/// [`DOCX_EXTS`].+/// Word 97-2003 documents: File → Place sets them as area type with their formatting.+pub const DOC_EXTS: &[&str] = &["doc"];++/// The Place dialog's Word filter: Word documents and Word 97-2003 documents.+const WORD_EXTS: &[&str] = &["docx", "doc"];++/// Every extension File → Place reads: [`OPEN_EXTS`], [`TEXT_EXTS`], [`RTF_EXTS`],+/// [`DOCX_EXTS`] and [`DOC_EXTS`].pub const PLACE_EXTS: &[&str] = &["vectorcraft","drawcraft",@@ -610,6 +616,7 @@ pub const PLACE_EXTS: &[&str] = &["txt","rtf","docx",+ "doc",];/// One dialog filter per readable format.@@ -645,13 +652,13 @@ pub fn library_open_filters(}/// File → Place dialog filters: "All placeable files", then one per placeable format, then text,-/// RTF and Word.+/// RTF and Word (`.docx` and `.doc`).pub fn place_filters() -> impl Iterator<Item = (&'static str, &'static [&'static str])> {std::iter::once(("All placeable files", PLACE_EXTS)).chain(format_filters().filter(|(_, exts)| exts.iter().all(|ext| PLACE_EXTS.contains(ext)))).chain(std::iter::once(("Text", TEXT_EXTS))).chain(std::iter::once(("RTF", RTF_EXTS)))- .chain(std::iter::once(("Word", DOCX_EXTS)))+ .chain(std::iter::once(("Word", WORD_EXTS)))}/// A format by id or extension (any case, leading dot allowed; `jpeg` finds `jpg`): a built-indiff --git a/crates/engine/src/cmd/place.rs b/crates/engine/src/cmd/place.rsindex 2ab05f69e..d76de1adb 100644--- a/crates/engine/src/cmd/place.rs+++ b/crates/engine/src/cmd/place.rs@@ -20,6 +20,8 @@//! - An RTF file becomes area type with its character and paragraph formatting ([`rtf`]).//! - A Word document (`.docx`) becomes area type with the character and paragraph formatting its//! styles and direct formatting give ([`docx`]).+//! - A Word 97-2003 document (`.doc`) becomes area type the same way, its main text read from its+//! compound file, with a warning for each kind of content left out ([`doc`]).//! - A VectorCraft document placed from a file with Link (the default) becomes a placed document://! one locked object showing the artboard `page`, linked to the file and read again when it//! changes ([`document`]); without Link, an editable copy of its art.@@ -30,6 +32,7 @@//! `place` tool) with several files, which emits one `file.place` per click or drag.pub(crate) mod adopt;+mod doc;pub(crate) mod document;pub(crate) mod docx;mod rtf;@@ -169,6 +172,15 @@ fn load(p: &Value, cmd: &str, board: Option<Rect>, link_pdf: bool) -> Result<Loa}return Ok(Loaded { name, format: &docx::FORMAT, art: Art::Text(story), natural: TEXT_FRAME, warnings: vec![], link: None, board: None });}+ // A Word 97-2003 document is recognized by its name or by its compound file's WordDocument+ // stream.+ if doc::is_doc(src.name, &src.bytes) {+ let (story, warnings) = doc::import(&src.bytes, cmd)?;+ if story.plain_text().trim().is_empty() {+ return Err(bad(cmd, format!("`{name}` has no text to place")));+ }+ return Ok(Loaded { name, format: &doc::FORMAT, art: Art::Text(story), natural: TEXT_FRAME, warnings, link: None, board: None });+ }let format = fileio::detect(src.name, &src.bytes).ok_or_else(|| bad(cmd, format!("can't place `{name}`: not a format VectorCraft reads (see document.formats)")))?;let page = match p.get("page") {diff --git a/crates/engine/src/cmd/place/doc.rs b/crates/engine/src/cmd/place/doc.rsnew file mode 100644index 000000000..3ae2410ba--- /dev/null+++ b/crates/engine/src/cmd/place/doc.rs@@ -0,0 +1,1161 @@+//! Placing a Word 97-2003 document (`.doc`): its main text set as area type with the character and+//! paragraph formatting VectorCraft has, read as \[MS-DOC\] describes the Word Binary File Format+//! (<https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-doc/262caa01-60a5-479e-b710-1e966936a524>)+//! from the compound file \[MS-CFB\] describes+//! (<https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-cfb/53989ce4-7b05-4f8d-829b-d08d6148375b>).+//!+//! - The `WordDocument` stream starts with the file information block (FIB, §2.5.1), which locates+//! the structures read from the `0Table` or `1Table` stream (`fWhichTblStm`). Documents saved by+//! Word 97 and later (`nFib` 0x00C1 and above) are read; older ones, encrypted or obfuscated ones+//! and ones last saved with fast save (`fComplex`) are refused.+//! - Text: the main document's `ccpText` characters, from the pieces the Clx's PlcPcd lists+//! (§2.9.38, §2.8.35) in their order, each in 16-bit Unicode or in compressed 8-bit text with the+//! mapping §2.9.73 gives. A paragraph mark (U+000D) ends a paragraph, and so does a page or+//! section break (U+000C) at the end of a paragraph's properties (PapxFkp); a line break+//! (U+000B) starts a new paragraph in the same formatting. A table's cells (cell marks, U+0007)+//! are separated by tabs and each row (a table terminating mark) is a paragraph. Tabs are tabs,+//! and U+001E and U+001F are the nonbreaking and optional hyphens. Of a field (U+0013, U+0014 and+//! U+0015), its result is read.+//! - Character formatting: the font (`sprmCRgFtc0`, named by the font table SttbfFfn), the size,+//! bold, italic, underline (every style but none), strikethrough and double strikethrough, all+//! caps, small caps, superscript and subscript, raised and lowered text (`sprmCHpsPos`) and the+//! color (`sprmCIco`, `sprmCCv`).+//! - Paragraph formatting: alignment (`sprmPJc80`, then `sprmPJc`: start and end follow the+//! direction, and the kashida alignments are justified with Short, Medium or Long kashidas), the+//! left and right indents (the physical ones Word 97 writes, unless the logical ones later+//! versions write are given), first-line indent, space before and after, and direction+//! (`sprmPFBiDi`).+//! - Formatting is resolved as "Determining Formatting Properties" describes: the style sheet's+//! default font and 10 pt, then the paragraph style (STSH, §2.9.271) and the styles it is based+//! on, then the run's character style (`sprmCIstd`) and the styles it is based on, then the+//! direct formatting of the run (Chpx) and of the paragraph (Papx). A toggle operand of 0x80 or+//! 0x81 takes or reverses the value the styles give.+//!+//! Hidden text, deleted revisions, field instructions, pictures and embedded objects, note and+//! comment references and the other stories (footnotes, endnotes, headers, footers, comments and+//! text boxes) are left out. The reply's warnings name each kind of content that was left out or+//! changed, once: pictures and objects, fields, the other stories, tracked changes and tables. A+//! stream larger than [`MAX_STREAM`] bytes, a main text longer than [`text::MAX_TEXT`] characters,+//! more than [`MAX_PIECES`] pieces or [`MAX_PAGES`] property pages, and placed type of more than+//! [`MAX_PARAGRAPHS`] paragraphs or [`MAX_RUNS`] runs are refused, and so is a structure that lies+//! outside its stream.++use std::io::{Cursor, Read};++use vectorcraft_doc::text::Kashida;+use vectorcraft_doc::{CharPosition, CharStyle, Justify, ParaDirection, ParaStyle, ScriptMetrics, TextRun};++use super::text::{self, Story};+use super::{EngineError, Paint, Result, bad, fileio};++/// Word 97-2003 documents as `file.place` reports them (not a format `document.open` reads).+pub(super) const FORMAT: fileio::Format = fileio::Format {+ id: "doc",+ label: "Word 97-2003",+ extensions: fileio::DOC_EXTS,+ mime: "application/msword",+ read: true,+ write: false,+ raster: false,+ options: &[],+};++/// The first bytes of a compound file (\[MS-CFB\] §2.2).+const SIGNATURE: [u8; 8] = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];+/// The most bytes a stream read from the compound file may hold.+const MAX_STREAM: u64 = 64 << 20;+/// The most pieces the main text may be made of.+const MAX_PIECES: usize = 100_000;+/// The most 512-byte pages of character or of paragraph properties read.+const MAX_PAGES: usize = 16_384;+/// The most styles read from the style sheet (`Stshif.cstd` is below 0x0FFE).+const MAX_STYLES: usize = 0x0FFE;+/// The most styles one style's chain of `istdBase` takes in.+const MAX_BASED_ON: usize = 32;+/// The most fonts read from the font table (SttbfFfn's `cData` is at most 0x7FF0).+const MAX_FONTS: usize = 0x7FF0;+/// The most bytes of a font name kept.+const MAX_NAME: usize = 256;+/// The most paragraphs placed type takes from one Word document.+const MAX_PARAGRAPHS: usize = 100_000;+/// The most runs of one character formatting placed type takes from one Word document.+const MAX_RUNS: usize = 100_000;+/// The deepest nesting of fields read; deeper ones are left out with their results.+const MAX_FIELDS: usize = 64;+/// `istdBase` of a style that is based on none.+const NO_STYLE: u16 = 0x0FFF;++/// The FibRgFcLcb97 pairs read (§2.5.6), by their index.+const STSHF: usize = 1;+const PLCF_BTE_CHPX: usize = 12;+const PLCF_BTE_PAPX: usize = 13;+const STTBF_FFN: usize = 15;+const CLX: usize = 33;+/// The number of pairs FibRgFcLcb97 holds.+const FC_LCB_97: u16 = 0x005D;++/// Is the file `name` with `bytes` a Word 97-2003 document: a compound file named `.doc`, or one+/// that holds a `WordDocument` stream?+pub(super) fn is_doc(name: &str, bytes: &[u8]) -> bool {+ bytes.starts_with(&SIGNATURE)+ && (fileio::DOC_EXTS.contains(&fileio::extension(name).as_str())+ || cfb::CompoundFile::open(Cursor::new(bytes)).is_ok_and(|f| f.is_stream("/WordDocument")))+}++/// The main text of a Word 97-2003 document's `bytes` (a compound file) with its character and+/// paragraph formatting, and a warning for each kind of content left out or changed.+pub(super) fn import(bytes: &[u8], cmd: &str) -> Result<(Story, Vec<String>)> {+ let mut file = cfb::CompoundFile::open(Cursor::new(bytes)).map_err(|_| damaged(cmd, "its compound file structure is damaged"))?;+ let wd = stream(&mut file, "WordDocument", cmd)?.ok_or_else(|| bad(cmd, "not a Word document: its compound file has no WordDocument stream"))?;+ let fib = Fib::read(&wd, cmd)?;+ let table_name = if fib.table_one { "1Table" } else { "0Table" };+ let table = stream(&mut file, table_name, cmd)?.ok_or_else(|| damaged(cmd, format!("it has no {table_name} stream")))?;+ let clx = fib.part(&table, CLX, cmd)?.ok_or_else(|| damaged(cmd, "it has no piece table"))?;+ let text = main_text(&wd, &pieces(clx, cmd)?, fib.ccp_text, cmd)?;+ let chpx = match fib.part(&table, PLCF_BTE_CHPX, cmd)? {+ Some(plc) => runs(&wd, plc, Fkp::Chpx, cmd)?,+ None => vec![],+ };+ let papx = match fib.part(&table, PLCF_BTE_PAPX, cmd)? {+ Some(plc) => runs(&wd, plc, Fkp::Papx, cmd)?,+ None => vec![],+ };+ let sheet = match fib.part(&table, STSHF, cmd)? {+ Some(stsh) => Sheet::read(stsh, cmd)?,+ None => Sheet::default(),+ };+ let fonts = match fib.part(&table, STTBF_FFN, cmd)? {+ Some(sttb) => fonts(sttb, cmd)?,+ None => vec![],+ };+ let mut r = Reader::new(&sheet, &fonts);+ r.read(&text, &chpx, &papx);+ let warnings = r.warnings(&fib);+ Ok((r.finish(cmd)?, warnings))+}++/// The error for a Word document whose structures can't be read.+fn damaged(cmd: &str, why: impl std::fmt::Display) -> EngineError {+ bad(cmd, format!("can't read this Word document: {why}"))+}++/// The stream `name` of the compound file; None when it has no such stream.+fn stream(file: &mut cfb::CompoundFile<Cursor<&[u8]>>, name: &str, cmd: &str) -> Result<Option<Vec<u8>>> {+ if !file.is_stream(name) {+ return Ok(None);+ }+ let s = file.open_stream(name).map_err(|_| damaged(cmd, format!("its {name} stream is damaged")))?;+ if s.len() > MAX_STREAM {+ return Err(bad(cmd, format!("the Word document is too large to place (its {name} stream holds more than {} MB)", MAX_STREAM >> 20)));+ }+ let mut out = vec![];+ s.take(MAX_STREAM).read_to_end(&mut out).map_err(|_| damaged(cmd, format!("its {name} stream is damaged")))?;+ Ok(Some(out))+}++/// The little-endian 16-bit integer at `at`.+fn u16_at(b: &[u8], at: usize) -> Option<u16> {+ b.get(at..at.checked_add(2)?).and_then(|s| s.try_into().ok()).map(u16::from_le_bytes)+}++/// The little-endian 32-bit integer at `at`.+fn u32_at(b: &[u8], at: usize) -> Option<u32> {+ b.get(at..at.checked_add(4)?).and_then(|s| s.try_into().ok()).map(u32::from_le_bytes)+}++/// `len` bytes of `b` from `at`.+fn bytes_at(b: &[u8], at: usize, len: usize) -> Option<&[u8]> {+ b.get(at..at.checked_add(len)?)+}++/// What the reader takes from the file information block.+struct Fib {+ /// The table stream is `1Table` (else `0Table`).+ table_one: bool,+ /// The main document's length in characters.+ ccp_text: u32,+ /// The lengths of the footnotes, headers and footers, comments, endnotes, text boxes and+ /// header text boxes (`ccpFtn`, `ccpHdd`, `ccpAtn`, `ccpEdn`, `ccpTxbx`, `ccpHdrTxbx`).+ stories: [u32; 6],+ /// FibRgFcLcb's offset and size pairs.+ fc_lcb: Vec<(u32, u32)>,+}++impl Fib {+ /// The FIB at the start of the WordDocument stream `wd` (§2.5.1).+ fn read(wd: &[u8], cmd: &str) -> Result<Self> {+ if u16_at(wd, 0) != Some(0xA5EC) {+ return Err(bad(cmd, "not a Word document: its WordDocument stream doesn't start with a file information block"));+ }+ let field = |at: usize| u16_at(wd, at).ok_or_else(|| damaged(cmd, "its file information block is cut short"));+ if field(2)? < 0x00C1 {+ return Err(bad(+ cmd,+ "the Word document was saved by Word 6.0 or Word 95, which VectorCraft doesn't read; save it as a Word 97-2003 document",+ ));+ }+ let flags = field(10)?;+ // fEncrypted (fObfuscated only says how).+ if flags & 0x0100 != 0 {+ return Err(bad(cmd, "the Word document is encrypted or protected with a password; save it without one to place it"));+ }+ // fComplex: the last save was an incremental (fast) save.+ if flags & 0x0004 != 0 {+ return Err(bad(+ cmd,+ "the Word document was last saved with fast save, which VectorCraft doesn't read; save it again with fast save turned off",+ ));+ }+ // csw (14 16-bit values) and cslw (22 32-bit values) are fixed.+ if field(32)? != 0x000E || field(62)? != 0x0016 {+ return Err(damaged(cmd, "its file information block is damaged"));+ }+ let count = field(152)?;+ if count < FC_LCB_97 {+ return Err(damaged(cmd, "its file information block is damaged"));+ }+ let pair = |i: usize| Some((u32_at(wd, 154 + 8 * i)?, u32_at(wd, 158 + 8 * i)?));+ let fc_lcb =+ (0..usize::from(count)).map(pair).collect::<Option<Vec<_>>>().ok_or_else(|| damaged(cmd, "its file information block is cut short"))?;+ // FibRgLw97 starts at 64: ccpText is its fourth value.+ let lw = |i: usize| u32_at(wd, 64 + 4 * i).unwrap_or_default();+ Ok(Self { table_one: flags & 0x0200 != 0, ccp_text: lw(3), stories: [lw(4), lw(5), lw(7), lw(8), lw(9), lw(10)], fc_lcb })+ }++ /// The structure FibRgFcLcb's pair `index` locates in the table stream `table`; None when it+ /// is empty.+ fn part<'a>(&self, table: &'a [u8], index: usize, cmd: &str) -> Result<Option<&'a [u8]>> {+ let Some(&(fc, lcb)) = self.fc_lcb.get(index).filter(|(_, lcb)| *lcb > 0) else { return Ok(None) };+ bytes_at(table, fc as usize, lcb as usize).map(Some).ok_or_else(|| damaged(cmd, "a table it lists lies outside its table stream"))+ }+}++/// A piece of the text (Pcd): characters `cp..end`, stored from file position `fc`.+struct Piece {+ cp: u32,+ end: u32,+ fc: u32,+ /// Compressed 8-bit text (else 16-bit Unicode).+ compressed: bool,+}++/// The pieces the Clx `clx` lists (§2.9.38), in character order.+fn pieces(clx: &[u8], cmd: &str) -> Result<Vec<Piece>> {+ let broken = || damaged(cmd, "its piece table is damaged");+ let mut at = 0usize;+ // Prc records (property modifiers for pieces) come first.+ while clx.get(at) == Some(&0x01) {+ let size = u16_at(clx, at + 1).map(|v| v as i16).filter(|v| *v >= 0).ok_or_else(broken)?;+ at += 3 + size as usize;+ }+ if clx.get(at) != Some(&0x02) {+ return Err(broken());+ }+ let lcb = u32_at(clx, at + 1).ok_or_else(broken)? as usize;+ let plc = bytes_at(clx, at + 5, lcb).ok_or_else(broken)?;+ if lcb < 4 || !(lcb - 4).is_multiple_of(12) {+ return Err(broken());+ }+ let n = (lcb - 4) / 12;+ if n > MAX_PIECES {+ return Err(bad(cmd, format!("the Word document's text is made of more than {MAX_PIECES} pieces, too many to place")));+ }+ let mut out = Vec::with_capacity(n);+ for i in 0..n {+ let (cp, end) = (u32_at(plc, 4 * i).ok_or_else(broken)?, u32_at(plc, 4 * i + 4).ok_or_else(broken)?);+ // A Pcd: 2 bytes of flags, then FcCompressed (fc in bits 0 to 29, fCompressed in bit 30).+ let raw = u32_at(plc, 4 * (n + 1) + 8 * i + 2).ok_or_else(broken)?;+ if end <= cp || out.last().is_some_and(|p: &Piece| p.end != cp) || (i == 0 && cp != 0) {+ return Err(broken());+ }+ let compressed = raw & 0x4000_0000 != 0;+ let fc = raw & 0x3FFF_FFFF;+ out.push(Piece { cp, end, fc: if compressed { fc / 2 } else { fc }, compressed });+ }+ Ok(out)+}++/// A character of the main text: the character, the file position of its bytes, and their number.+type Char = (char, u32, u8);++/// The first `ccp` characters of the pieces, read from the WordDocument stream `wd`.+fn main_text(wd: &[u8], pieces: &[Piece], ccp: u32, cmd: &str) -> Result<Vec<Char>> {+ if ccp as usize > text::MAX_TEXT {+ return Err(bad(cmd, format!("the Word document has too much text to place as type (at most {} MB)", text::MAX_TEXT >> 20)));+ }+ if pieces.last().is_none_or(|p| p.end < ccp) {+ return Err(damaged(cmd, "its piece table doesn't cover its text"));+ }+ let outside = || damaged(cmd, "a piece of its text lies outside its WordDocument stream");+ let mut out = Vec::with_capacity(ccp as usize);+ for p in pieces.iter().take_while(|p| p.cp < ccp) {+ let n = (p.end.min(ccp) - p.cp) as usize;+ if p.compressed {+ let b = bytes_at(wd, p.fc as usize, n).ok_or_else(outside)?;+ out.extend(b.iter().zip(p.fc..).map(|(&c, fc)| (compressed(c), fc, 1)));+ continue;+ }+ let b = bytes_at(wd, p.fc as usize, n.checked_mul(2).ok_or_else(outside)?).ok_or_else(outside)?;+ let units: Vec<(u16, u32)> = b.as_chunks::<2>().0.iter().zip((p.fc..).step_by(2)).map(|(&u, fc)| (u16::from_le_bytes(u), fc)).collect();+ let mut i = 0;+ while let Some(&(u, fc)) = units.get(i) {+ let pair = units.get(i + 1).map(|&(low, _)| low).filter(|low| (0xD800..0xDC00).contains(&u) && (0xDC00..0xE000).contains(low));+ let c = match pair {+ Some(low) => char::decode_utf16([u, low]).next().and_then(|c| c.ok()),+ None => char::from_u32(u32::from(u)),+ };+ let width = if pair.is_some() { 4 } else { 2 };+ out.push((c.unwrap_or(char::REPLACEMENT_CHARACTER), fc, width));+ i += usize::from(width / 2);+ }+ }+ Ok(out)+}++/// Byte `b` of compressed text (§2.9.73): the byte as a Unicode character, except the ones that+/// stand for these.+fn compressed(b: u8) -> char {+ const MAPPED: [(u8, char); 24] = [+ (0x82, '\u{201A}'),+ (0x83, '\u{0192}'),+ (0x84, '\u{201E}'),+ (0x85, '\u{2026}'),+ (0x86, '\u{2020}'),+ (0x87, '\u{2021}'),+ (0x88, '\u{02C6}'),+ (0x89, '\u{2030}'),+ (0x8A, '\u{0160}'),+ (0x8B, '\u{2039}'),+ (0x8C, '\u{0152}'),+ (0x91, '\u{2018}'),+ (0x92, '\u{2019}'),+ (0x93, '\u{201C}'),+ (0x94, '\u{201D}'),+ (0x95, '\u{2022}'),+ (0x96, '\u{2013}'),+ (0x97, '\u{2014}'),+ (0x98, '\u{02DC}'),+ (0x99, '\u{2122}'),+ (0x9A, '\u{0161}'),+ (0x9B, '\u{203A}'),+ (0x9C, '\u{0153}'),+ (0x9F, '\u{0178}'),+ ];+ MAPPED.iter().find(|(k, _)| *k == b).map_or(char::from(b), |(_, c)| *c)+}++/// Which property pages a bin table points to.+#[derive(Clone, Copy, PartialEq)]+enum Fkp {+ /// ChpxFkp (§2.9.33): character properties.+ Chpx,+ /// PapxFkp (§2.9.174): paragraph properties.+ Papx,+}++/// The file positions `start..end` and the properties a property page gives them.+struct Run {+ start: u32,+ end: u32,+ /// The paragraph style (Papx only).+ istd: u16,+ grpprl: Vec<u8>,+}++/// The runs of the property pages the bin table `plc` (PlcBteChpx or PlcBtePapx) points to, in+/// the WordDocument stream `wd`, ordered by file position.+fn runs(wd: &[u8], plc: &[u8], kind: Fkp, cmd: &str) -> Result<Vec<Run>> {+ let broken = || damaged(cmd, "its character or paragraph properties are damaged");+ if plc.len() < 4 || !(plc.len() - 4).is_multiple_of(8) {+ return Err(broken());+ }+ let n = (plc.len() - 4) / 8;+ if n > MAX_PAGES {+ return Err(bad(cmd, format!("the Word document has more than {MAX_PAGES} pages of properties, too many to place")));+ }+ let mut out = vec![];+ for i in 0..n {+ // PnFkpChpx and PnFkpPapx: the page number in bits 0 to 21.+ let pn = u32_at(plc, 4 * (n + 1) + 4 * i).ok_or_else(broken)? & 0x003F_FFFF;+ let page = bytes_at(wd, pn as usize * 512, 512).ok_or_else(broken)?;+ page_runs(page, kind, &mut out).ok_or_else(broken)?;+ }+ out.sort_by_key(|r| r.start);+ Ok(out)+}++/// The runs of one 512-byte property page; None when it is damaged.+fn page_runs(page: &[u8], kind: Fkp, out: &mut Vec<Run>) -> Option<()> {+ let count = usize::from(*page.get(511)?);+ let most = if kind == Fkp::Chpx { 0x65 } else { 0x1D };+ if count == 0 || count > most {+ return None;+ }+ for i in 0..count {+ let (start, end) = (u32_at(page, 4 * i)?, u32_at(page, 4 * i + 4)?);+ // ChpxFkp's rgb: one byte each; PapxFkp's rgbx: 13-byte BxPap, its first byte the offset.+ let entry = 4 * (count + 1) + if kind == Fkp::Chpx { i } else { 13 * i };+ let offset = usize::from(*page.get(entry)?) * 2;+ let (istd, grpprl) = match (offset, kind) {+ (0, _) => (0, vec![]),+ (_, Fkp::Chpx) => {+ let cb = usize::from(*page.get(offset)?);+ (0, bytes_at(page, offset + 1, cb).filter(|_| offset + 1 + cb <= 511)?.to_vec())+ }+ (_, Fkp::Papx) => {+ // PapxInFkp: 2 × cb − 1 bytes, or (cb = 0) 2 × cb′ bytes after cb′.+ let (at, len) = match usize::from(*page.get(offset)?) {+ 0 => (offset + 2, usize::from(*page.get(offset + 1)?) * 2),+ cb => (offset + 1, 2 * cb - 1),+ };+ let grp = bytes_at(page, at, len).filter(|_| at + len <= 511 && len >= 2)?;+ (u16_at(grp, 0)?, grp.get(2..)?.to_vec())+ }+ };+ if end <= start {+ return None;+ }+ out.push(Run { start, end, istd, grpprl });+ }+ Some(())+}++/// The run of `runs` (ordered by file position) that holds file position `fc`.+fn run_at(runs: &[Run], fc: u32) -> Option<usize> {+ let i = runs.partition_point(|r| r.start <= fc).checked_sub(1)?;+ runs.get(i).filter(|r| fc < r.end).map(|_| i)+}++/// The size of the operand of `sprm` (§2.2.5.1) whose bytes start `rest`.+fn operand_len(sprm: u16, rest: &[u8]) -> Option<usize> {+ Some(match sprm >> 13 {+ 0 | 1 => 1,+ 2 | 4 | 5 => 2,+ 3 => 4,+ 7 => 3,+ // sprmTDefTable: a 2-byte size of the rest, plus 1.+ _ if sprm == 0xD608 => usize::from(u16_at(rest, 0)?) + 1,+ // sprmPChgTabs with cb 255: the deleted tabs with their close tolerances, then the added+ // tabs with their descriptors.+ _ if sprm == 0xC615 && rest.first() == Some(&255) => {+ let deleted = usize::from(*rest.get(1)?);+ let added = usize::from(*rest.get(2 + 4 * deleted)?);+ 3 + 4 * deleted + 3 * added+ }+ _ => usize::from(*rest.first()?) + 1,+ })+}++/// Calls `f` with each Prl of `grpprl`: its sprm and its operand.+fn each_prl(grpprl: &[u8], mut f: impl FnMut(u16, &[u8])) {+ let mut at = 0;+ while let Some(sprm) = u16_at(grpprl, at) {+ let rest = grpprl.get(at + 2..).unwrap_or_default();+ let Some((n, op)) = operand_len(sprm, rest).and_then(|n| Some((n, rest.get(..n)?))) else { return };+ // A variable operand's bytes follow its size.+ let op = if sprm >> 13 == 6 && !matches!(sprm, 0xD608 | 0xC615) { op.get(1..).unwrap_or_default() } else { op };+ f(sprm, op);+ at += 2 + n;+ }+}++/// A style of the style sheet.+struct Style {+ /// 1: paragraph style, 2: character style.+ stk: u8,+ base: u16,+ papx: Vec<u8>,+ chpx: Vec<u8>,+}++/// The style sheet: its styles by istd and its default font.+#[derive(Default)]+struct Sheet {+ styles: Vec<Option<Style>>,+ /// `Stshif.ftcAsci`.+ font: Option<u16>,+}++impl Sheet {+ /// The style sheet STSH (§2.9.271).+ fn read(stsh: &[u8], cmd: &str) -> Result<Self> {+ let broken = || damaged(cmd, "its style sheet is damaged");+ let size = usize::from(u16_at(stsh, 0).ok_or_else(broken)?);+ let stshi = bytes_at(stsh, 2, size).ok_or_else(broken)?;+ let count = usize::from(u16_at(stshi, 0).ok_or_else(broken)?).min(MAX_STYLES);+ // cbSTDBaseInFile: StdfBase, and StdfPost2000 when it is 18.+ let base_size = usize::from(u16_at(stshi, 2).ok_or_else(broken)?).max(10);+ let font = u16_at(stshi, 12);+ let mut at = 2 + size;+ let mut styles = Vec::with_capacity(count);+ for _ in 0..count {+ let Some(len) = u16_at(stsh, at) else { break };+ let std = bytes_at(stsh, at + 2, usize::from(len)).ok_or_else(broken)?;+ styles.push(if std.is_empty() { None } else { Some(style(std, base_size).ok_or_else(broken)?) });+ // LPStd records start on even offsets.+ at += 2 + usize::from(len) + usize::from(len) % 2;+ }+ Ok(Self { styles, font })+ }++ /// Style `istd` of kind `stk` and the styles it is based on, the first of the chain first.+ fn chain(&self, istd: u16, stk: u8) -> Vec<&Style> {+ let mut seen: Vec<u16> = vec![];+ let mut chain = vec![];+ let mut next = istd;+ while next != NO_STYLE && !seen.contains(&next) && seen.len() < MAX_BASED_ON {+ let Some(s) = self.styles.get(usize::from(next)).and_then(Option::as_ref).filter(|s| s.stk == stk) else { break };+ seen.push(next);+ chain.push(s);+ next = s.base;+ }+ chain.reverse();+ chain+ }+}++/// A style definition (STD, §2.9.258) whose Stdf takes `base_size` bytes; None when it is damaged.+fn style(std: &[u8], base_size: usize) -> Option<Style> {+ let kind = u16_at(std, 2)?;+ let (stk, base) = ((kind & 0x000F) as u8, kind >> 4);+ let cupx = usize::from(u16_at(std, 4)? & 0x000F);+ // xstzName: a length, the characters and a terminating zero.+ let name = usize::from(u16_at(std, base_size)?);+ let mut at = base_size + 2 + 2 * name + 2;+ let mut upx = vec![];+ for _ in 0..cupx {+ let len = usize::from(u16_at(std, at)?);+ upx.push(bytes_at(std, at + 2, len)?);+ // Each LPUpx is padded to an even length.+ at += 2 + len + len % 2;+ }+ let (papx, chpx) = match (stk, upx.as_slice()) {+ // UpxPapx: the istd, then the paragraph properties; then UpxChpx.+ (1, [p, c, ..]) => (p.get(2..)?.to_vec(), c.to_vec()),+ (2, [c, ..]) => (vec![], c.to_vec()),+ _ => (vec![], vec![]),+ };+ Some(Style { stk, base, papx, chpx })+}++/// The font names of the font table SttbfFfn (§2.9.286).+fn fonts(sttb: &[u8], cmd: &str) -> Result<Vec<String>> {+ let broken = || damaged(cmd, "its font table is damaged");+ let count = usize::from(u16_at(sttb, 0).ok_or_else(broken)?).min(MAX_FONTS);+ let mut at = 4;+ let mut out = Vec::with_capacity(count);+ for _ in 0..count {+ let len = usize::from(*sttb.get(at).ok_or_else(broken)?);+ let ffn = bytes_at(sttb, at + 1, len).ok_or_else(broken)?;+ // FFN: 39 bytes of family, weight, character set, PANOSE and signature, then the name.+ let units = ffn.get(39..).unwrap_or_default().as_chunks::<2>().0.iter().map(|&u| u16::from_le_bytes(u)).take_while(|&u| u != 0);+ let name: String = char::decode_utf16(units).map(|c| c.unwrap_or(char::REPLACEMENT_CHARACTER)).collect();+ out.push(font_name(&name));+ at += 1 + len;+ }+ Ok(out)+}++/// Font name `name` trimmed, and cut at the last character that ends within [`MAX_NAME`] bytes.+fn font_name(name: &str) -> String {+ let name = name.trim();+ let end = (0..=MAX_NAME.min(name.len())).rev().find(|&i| name.is_char_boundary(i)).unwrap_or_default();+ name.get(..end).unwrap_or_default().to_string()+}++#[derive(Clone, Copy, Debug, Default, PartialEq)]+enum Script {+ #[default]+ Normal,+ Super,+ Sub,+}++/// Character formatting.+#[derive(Clone, Copy, Debug, PartialEq)]+struct Chars {+ /// The font's number in the font table.+ font: Option<u16>,+ half_points: u16,+ bold: bool,+ italic: bool,+ caps: bool,+ small_caps: bool,+ strike: bool,+ dstrike: bool,+ hidden: bool,+ underline: bool,+ script: Script,+ /// Raised (positive) or lowered text in half points.+ shift: i16,+ color: Option<[u8; 3]>,+ /// Deleted and inserted revisions.+ deleted: bool,+ inserted: bool,+ /// A special character (sprmCFSpec): a picture, a note reference or a symbol.+ special: bool,+}++impl Default for Chars {+ fn default() -> Self {+ Self {+ font: None,+ // The default size: 20 half points.+ half_points: 20,+ bold: false,+ italic: false,+ caps: false,+ small_caps: false,+ strike: false,+ dstrike: false,+ hidden: false,+ underline: false,+ script: Script::Normal,+ shift: 0,+ color: None,+ deleted: false,+ inserted: false,+ special: false,+ }+ }+}++/// The colors of `sprmCIco` (Ico, §2.9.119), 1 to 16; 0 is automatic.+const ICO: [[u8; 3]; 16] = [+ [0, 0, 0],+ [0, 0, 0xFF],+ [0, 0xFF, 0xFF],+ [0, 0xFF, 0],+ [0xFF, 0, 0xFF],+ [0xFF, 0, 0],+ [0xFF, 0xFF, 0],+ [0xFF, 0xFF, 0xFF],+ [0, 0, 0x80],+ [0, 0x80, 0x80],+ [0, 0x80, 0],+ [0x80, 0, 0x80],+ [0x80, 0, 0],+ [0x80, 0x80, 0],+ [0x80, 0x80, 0x80],+ [0xC0, 0xC0, 0xC0],+];++impl Chars {+ /// The character properties of `grpprl` applied over this formatting; a toggle operand of+ /// 0x80 or 0x81 takes or reverses the value of `style`, the formatting its styles give.+ fn apply(&mut self, grpprl: &[u8], style: &Chars) {+ each_prl(grpprl, |sprm, op| {+ let byte = op.first().copied();+ let word = u16_at(op, 0);+ let toggle = |state: &mut bool, of_style: bool| match byte {+ Some(0) => *state = false,+ Some(1) => *state = true,+ Some(0x80) => *state = of_style,+ Some(0x81) => *state = !of_style,+ _ => {}+ };+ match sprm {+ 0x0800 => toggle(&mut self.deleted, style.deleted),+ 0x0801 => toggle(&mut self.inserted, style.inserted),+ 0x0835 => toggle(&mut self.bold, style.bold),+ 0x0836 => toggle(&mut self.italic, style.italic),+ 0x0837 => toggle(&mut self.strike, style.strike),+ 0x083A => toggle(&mut self.small_caps, style.small_caps),+ 0x083B => toggle(&mut self.caps, style.caps),+ 0x083C => toggle(&mut self.hidden, style.hidden),+ 0x0855 => toggle(&mut self.special, style.special),+ 0x2A53 => toggle(&mut self.dstrike, style.dstrike),+ 0x2A3E => self.underline = byte.is_some_and(|k| k != 0),+ 0x2A42 => self.color = byte.and_then(|i| usize::from(i).checked_sub(1)).and_then(|i| ICO.get(i)).copied(),+ 0x4A43 => self.half_points = word.filter(|v| (2..=3276).contains(v)).unwrap_or(self.half_points),+ 0x4845 => self.shift = word.map_or(self.shift, |v| v as i16),+ 0x2A48 => {+ self.script = match byte {+ Some(1) => Script::Super,+ Some(2) => Script::Sub,+ _ => Script::Normal,+ }+ }+ 0x4A4F => self.font = word.or(self.font),+ // COLORREF: red, green, blue, then 0xFF for the automatic color.+ 0x6870 => {+ if let [r, g, b, auto] = *op {+ self.color = (auto != 0xFF).then_some([r, g, b]);+ }+ }+ _ => {}+ }+ });+ }+}++#[derive(Clone, Copy, Debug, Default, PartialEq)]+enum Align {+ #[default]+ Left,+ Center,+ Right,+ /// Justified, the last line at the start, with the kashida length of the kashida alignments.+ Both(Kashida),+ /// Justified, the last line too.+ Distribute,+ Start,+ End,+}++/// Paragraph formatting; lengths in twips.+#[derive(Clone, Copy, Debug, Default, PartialEq)]+struct Para {+ align: Align,+ /// The physical left and right indents (`sprmPDxaLeft80`, `sprmPDxaRight80`).+ left: Option<i16>,+ right: Option<i16>,+ /// The logical start and end indents (`sprmPDxaLeft`, `sprmPDxaRight`).+ start: Option<i16>,+ end: Option<i16>,+ first: i16,+ before: u16,+ after: u16,+ rtl: Option<bool>,+ /// The paragraph's mark ends a table row (`sprmPFTtp`, or `sprmPFInnerTtp` in a nested table).+ row_end: bool,+ /// The paragraph's mark ends a cell of a nested table (`sprmPFInnerTableCell`).+ cell_end: bool,+}++impl Para {+ /// The paragraph properties of `grpprl` applied over this formatting.+ fn apply(&mut self, grpprl: &[u8]) {+ each_prl(grpprl, |sprm, op| {+ let byte = op.first().copied();+ let word = u16_at(op, 0);+ match sprm {+ 0x2403 => {+ self.align = match byte {+ Some(1) => Align::Center,+ Some(2) => Align::Right,+ Some(3..=5) => Align::Both(Kashida::None),+ Some(_) => Align::Left,+ None => self.align,+ }+ }+ 0x2461 => {+ self.align = match byte {+ Some(0) => Align::Start,+ Some(1) => Align::Center,+ Some(2) => Align::End,+ Some(3) => Align::Both(Kashida::None),+ Some(5) => Align::Both(Kashida::Medium),+ Some(7) => Align::Both(Kashida::Long),+ Some(8) => Align::Both(Kashida::Short),+ Some(4 | 9) => Align::Distribute,+ _ => self.align,+ }+ }+ 0x840F => self.left = word.map(|v| v as i16),+ 0x840E => self.right = word.map(|v| v as i16),+ 0x845E => self.start = word.map(|v| v as i16),+ 0x845D => self.end = word.map(|v| v as i16),+ 0x8411 | 0x8460 => self.first = word.map_or(self.first, |v| v as i16),+ 0xA413 => self.before = word.unwrap_or(self.before),+ 0xA414 => self.after = word.unwrap_or(self.after),+ 0x2441 => self.rtl = byte.map(|v| v != 0),+ 0x2417 | 0x244C => self.row_end = byte.is_some_and(|v| v != 0),+ 0x244B => self.cell_end = byte.is_some_and(|v| v != 0),+ _ => {}+ }+ });+ }++ /// The paragraph attributes of this formatting.+ fn style(&self) -> ParaStyle {+ let rtl = self.rtl == Some(true);+ let (physical_start, physical_end) = if rtl { (self.right, self.left) } else { (self.left, self.right) };+ let (start, end) = (self.start.or(physical_start).unwrap_or_default(), self.end.or(physical_end).unwrap_or_default());+ let (left, right) = if rtl { (end, start) } else { (start, end) };+ let points = |v: f64| (v / 20.0).clamp(-1296.0, 1296.0);+ ParaStyle {+ justify: match self.align {+ Align::Left => Justify::Left,+ Align::Center => Justify::Center,+ Align::Right => Justify::Right,+ Align::Both(_) if rtl => Justify::JustifyRight,+ Align::Both(_) => Justify::JustifyLeft,+ Align::Distribute => Justify::JustifyAll,+ Align::Start => Justify::Auto,+ Align::End if rtl => Justify::Left,+ Align::End => Justify::Right,+ },+ left_indent: points(f64::from(left)),+ right_indent: points(f64::from(right)),+ first_line_indent: points(f64::from(self.first)),+ space_before: points(f64::from(self.before)),+ space_after: points(f64::from(self.after)),+ direction: self.rtl.map(|rtl| if rtl { ParaDirection::RightToLeft } else { ParaDirection::LeftToRight }),+ kashida: if let Align::Both(k) = self.align { k } else { Kashida::None },+ ..ParaStyle::default()+ }+ }+}++/// The kinds of content a warning names.+#[derive(Clone, Copy, Default)]+struct Seen {+ pictures: bool,+ fields: bool,+ revisions: bool,+ tables: bool,+}++struct Reader<'a> {+ sheet: &'a Sheet,+ fonts: &'a [String],+ /// The paragraph being read.+ para: Para,+ /// A paragraph that has ended (its formatting and its mark's): its break goes before the+ /// next text.+ pending: Option<(Para, Chars)>,+ /// The last cell's paragraph, which its row's break ends.+ cell_end: Option<(Para, Chars)>,+ /// A cell just ended: a tab goes before the next character of its row.+ cell_tab: bool,+ /// The fields open: true while their instructions are being read.+ fields: Vec<bool>,+ /// Fields begun past [`MAX_FIELDS`] and not yet ended.+ field_overflow: usize,+ /// The text, in runs of one character formatting; paragraphs end with `\n`.+ spans: Vec<(String, Chars)>,+ /// The bytes of text in `spans`.+ len: usize,+ /// The formatting of each paragraph that has ended.+ paras: Vec<Para>,+ /// Past [`MAX_PARAGRAPHS`], [`MAX_RUNS`] or [`text::MAX_TEXT`].+ full: bool,+ seen: Seen,+}++impl<'a> Reader<'a> {+ /// A reader of text formatted with the styles of `sheet` and the fonts of `fonts`.+ fn new(sheet: &'a Sheet, fonts: &'a [String]) -> Self {+ Self {+ sheet,+ fonts,+ para: Para::default(),+ pending: None,+ cell_end: None,+ cell_tab: false,+ fields: vec![],+ field_overflow: 0,+ spans: vec![],+ len: 0,+ paras: vec![],+ full: false,+ seen: Seen::default(),+ }+ }++ /// The text `text`, with the character properties of `chpx` and the paragraph properties of+ /// `papx`.+ fn read(&mut self, text: &[Char], chpx: &[Run], papx: &[Run]) {+ let mut start = 0;+ while start < text.len() && !self.full {+ // A paragraph runs to its mark: a paragraph or cell mark, or a page or section break at+ // the end of its paragraph properties.+ let ends = |&(c, fc, width): &Char| {+ matches!(c, '\r' | '\u{7}')+ || (c == '\u{C}' && run_at(papx, fc).and_then(|i| papx.get(i)).is_some_and(|r| r.end == fc + u32::from(width)))+ };+ let end = text.get(start..).and_then(|rest| rest.iter().position(ends)).map_or(text.len(), |at| start + at + 1);+ let segment = text.get(start..end).unwrap_or_default();+ let mark_fc = segment.last().map(|&(_, fc, _)| fc).unwrap_or_default();+ let (para, base) = self.paragraph(run_at(papx, mark_fc).and_then(|i| papx.get(i)));+ self.para = para;+ let mut cached: Option<(Option<usize>, Chars)> = None;+ for (k, ch) in segment.iter().enumerate() {+ let run = run_at(chpx, ch.1);+ let chars = match cached {+ Some((r, chars)) if r == run => chars,+ _ => self.chars(base, run.and_then(|i| chpx.get(i)).map_or(&[][..], |r| r.grpprl.as_slice())),+ };+ cached = Some((run, chars));+ self.character(ch.0, chars, k + 1 == segment.len() && ends(ch));+ }+ start = end;+ }+ }++ /// A paragraph's formatting and its text's formatting before character styles and direct+ /// formatting, from its properties `run`.+ fn paragraph(&self, run: Option<&Run>) -> (Para, Chars) {+ let mut para = Para::default();+ let mut chars = Chars { font: self.sheet.font, ..Chars::default() };+ for s in self.sheet.chain(run.map_or(0, |r| r.istd), 1) {+ para.apply(&s.papx);+ let style = chars;+ chars.apply(&s.chpx, &style);+ }+ if let Some(r) = run {+ para.apply(&r.grpprl);+ }+ (para, chars)+ }++ /// The formatting of text whose paragraph gives `base` and whose properties are `grpprl`: its+ /// character style (`sprmCIstd`), then its direct formatting.+ fn chars(&self, base: Chars, grpprl: &[u8]) -> Chars {+ let mut style = base;+ each_prl(grpprl, |sprm, op| {+ if sprm == 0x4A30+ && let Some(istd) = u16_at(op, 0)+ {+ style = base;+ for s in self.sheet.chain(istd, 2) {+ let before = style;+ style.apply(&s.chpx, &before);+ }+ }+ });+ let mut chars = style;+ chars.apply(grpprl, &style);+ chars+ }++ /// Character `c` of the main text in formatting `chars`; `mark`: it ends its paragraph's+ /// properties (a page or section break that does is a paragraph's end).+ fn character(&mut self, c: char, chars: Chars, mark: bool) {+ if chars.inserted || chars.deleted {+ self.seen.revisions = true;+ }+ if chars.deleted {+ return;+ }+ match c {+ '\u{13}' => {+ self.seen.fields = true;+ if self.fields.len() < MAX_FIELDS && self.field_overflow == 0 {+ self.fields.push(true);+ } else {+ self.field_overflow += 1;+ }+ return;+ }+ '\u{14}' if self.field_overflow == 0 => {+ if let Some(instructions) = self.fields.last_mut() {+ *instructions = false;+ }+ return;+ }+ '\u{15}' => {+ if self.field_overflow > 0 {+ self.field_overflow -= 1;+ } else {+ self.fields.pop();+ }+ return;+ }+ _ => {}+ }+ if self.field_overflow > 0 || self.fields.contains(&true) || chars.hidden {+ return;+ }+ let para = self.para;+ match c {+ '\r' if para.row_end => self.row_end(para, chars),+ '\r' if para.cell_end => self.cell(para, chars),+ '\r' => self.end_paragraph(para, chars),+ '\u{7}' if para.row_end => self.row_end(para, chars),+ '\u{7}' => self.cell(para, chars),+ '\u{C}' if mark => self.end_paragraph(para, chars),+ '\u{B}' => self.end_paragraph(para, chars),+ '\u{1}' | '\u{8}' => self.seen.pictures = true,+ '\u{1E}' => self.put('\u{2011}', chars),+ '\u{1F}' => self.put('\u{AD}', chars),+ '\t' => self.put('\t', chars),+ // Note references, separators and symbols.+ '(' if chars.special => {}+ c if c.is_control() => {}+ c => self.put(c, chars),+ }+ }++ /// The end of a paragraph in formatting `para` whose mark is in formatting `mark`.+ fn end_paragraph(&mut self, para: Para, mark: Chars) {+ self.flush();+ self.pending = Some((para, mark));+ }++ /// The end of a table cell: its paragraph goes on in the next cell, after a tab.+ fn cell(&mut self, para: Para, mark: Chars) {+ self.seen.tables = true;+ self.flush();+ if self.cell_tab {+ self.push('\t', mark);+ }+ self.cell_end = Some((para, mark));+ self.cell_tab = true;+ }++ /// The end of a table row: its cells' paragraph ends.+ fn row_end(&mut self, para: Para, mark: Chars) {+ self.seen.tables = true;+ self.flush();+ self.cell_tab = false;+ self.pending = Some(self.cell_end.take().unwrap_or((para, mark)));+ }++ /// Character `c` of the document's text in formatting `chars`.+ fn put(&mut self, c: char, chars: Chars) {+ self.flush();+ if std::mem::take(&mut self.cell_tab) {+ self.push('\t', chars);+ }+ self.push(c, chars);+ }++ /// Writes the break of the paragraph that has ended.+ fn flush(&mut self) {+ let Some((para, mark)) = self.pending.take() else { return };+ if self.paras.len() >= MAX_PARAGRAPHS {+ self.full = true;+ return;+ }+ self.paras.push(para);+ // A cell that starts with a break is still separated from the cell before it.+ if std::mem::take(&mut self.cell_tab) {+ self.push('\t', mark);+ }+ self.push('\n', mark);+ }++ /// Adds `c` to the text in formatting `chars`.+ fn push(&mut self, c: char, chars: Chars) {+ self.len += c.len_utf8();+ if self.len > text::MAX_TEXT {+ self.full = true;+ return;+ }+ let runs = self.spans.len();+ match self.spans.last_mut() {+ Some((s, f)) if *f == chars => s.push(c),+ _ if runs >= MAX_RUNS => self.full = true,+ _ => self.spans.push((c.to_string(), chars)),+ }+ }++ /// A warning for each kind of content that was left out or changed: what the text held, and+ /// the other stories `fib` gives a length.+ fn warnings(&self, fib: &Fib) -> Vec<String> {+ let mut out = vec![];+ if self.seen.pictures {+ out.push("the Word document's pictures and embedded objects were left out".to_string());+ }+ if self.seen.fields {+ out.push("the Word document's fields were placed as the text of their results".to_string());+ }+ if self.seen.revisions {+ out.push("the Word document's tracked changes were accepted: deleted text was left out and inserted text kept".to_string());+ }+ if self.seen.tables {+ out.push("the Word document's tables were placed as paragraphs with their cells separated by tabs".to_string());+ }+ let names = ["footnotes", "headers and footers", "comments", "endnotes", "text boxes"];+ let [ftn, hdd, atn, edn, txbx, hdr_txbx] = fib.stories;+ let present = [ftn, hdd, atn, edn, txbx.max(hdr_txbx)];+ let stories: Vec<&str> = names.iter().zip(present).filter(|(_, n)| *n > 0).map(|(name, _)| *name).collect();+ if let Some((last, rest)) = stories.split_last() {+ let list = if rest.is_empty() { (*last).to_string() } else { format!("{} and {last}", rest.join(", ")) };+ out.push(format!("the Word document's {list} were left out"));+ }+ out+ }++ /// The character attributes of formatting `c`.+ fn style(&self, c: &Chars) -> CharStyle {+ let mut s = CharStyle::default();+ if let Some(name) = c.font.and_then(|i| self.fonts.get(usize::from(i))).filter(|n| !n.is_empty()) {+ s.font_family.clone_from(name);+ }+ s.font_style = match (c.bold, c.italic) {+ (true, true) => "Bold Italic",+ (true, false) => "Bold",+ (false, true) => "Italic",+ (false, false) => "Regular",+ }+ .into();+ s.size = (f64::from(c.half_points) / 2.0).clamp(0.1, 1296.0);+ s.baseline_shift = (f64::from(c.shift) / 2.0).clamp(-1296.0, 1296.0);+ if let Some([r, g, b]) = c.color {+ s.fill = Paint::solid(vectorcraft_color::Color::rgb8(r, g, b));+ }+ s.underline = c.underline;+ s.strikethrough = c.strike || c.dstrike;+ s.all_caps = c.caps;+ // Document Setup's proportions replace these when the type is placed.+ s.small_caps = c.small_caps.then_some(70.0);+ s.position = match c.script {+ Script::Normal => CharPosition::Normal,+ Script::Super => CharPosition::Superscript(ScriptMetrics::DEFAULT),+ Script::Sub => CharPosition::Subscript(ScriptMetrics::DEFAULT),+ };+ s+ }++ /// The text read, without blank paragraphs at its end, and each paragraph's attributes.+ fn finish(mut self, cmd: &str) -> Result<Story> {+ if self.full {+ return Err(bad(+ cmd,+ format!(+ "the Word document has too much text or formatting to place as type (at most {MAX_PARAGRAPHS} paragraphs, {MAX_RUNS} runs and {} MB of text)",+ text::MAX_TEXT >> 20+ ),+ ));+ }+ let last = self.pending.take().map_or(self.para, |(para, _)| para);+ self.paras.push(last);+ let mut all: String = self.spans.iter().map(|(s, _)| s.as_str()).collect();+ while self.paras.len() > 1+ && let Some(at) = all.rfind('\n')+ && all.get(at + 1..).is_some_and(|tail| tail.trim().is_empty())+ {+ all.truncate(at);+ self.paras.pop();+ }+ let mut keep = all.len();+ let mut runs: Vec<TextRun> = vec![];+ for (s, chars) in &self.spans {+ let piece = s.get(..keep.min(s.len())).unwrap_or_default();+ keep -= piece.len();+ if piece.is_empty() {+ continue;+ }+ let style = self.style(chars);+ match runs.last_mut() {+ Some(r) if r.style == style => r.text.push_str(piece),+ _ => runs.push(TextRun::new(piece, style)),+ }+ }+ let paras = self.paras.iter().map(Para::style).collect();+ Ok(Story { runs, paras })+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn compressed_text_maps_the_bytes_the_specification_lists() {+ let decoded: String = [b'a', 0x80, 0x82, 0x93, 0x94, 0x9E, 0x9F, 0xE9].map(compressed).iter().collect();+ assert_eq!(decoded, "a\u{80}‚“”\u{9E}Ÿé", "not Windows-1252: 0x80 and 0x9E are themselves");+ }++ #[test]+ fn operands_take_the_size_their_sprm_gives() {+ let mut seen = vec![];+ // sprmCFBold 1, sprmCHps 24, sprmCCv, a variable operand of 2 bytes, then a cut-off Prl.+ let grpprl = [0x35, 0x08, 0x01, 0x43, 0x4A, 24, 0, 0x70, 0x68, 1, 2, 3, 0, 0x2A, 0xC6, 2, 9, 9, 0x43, 0x4A, 1];+ each_prl(&grpprl, |sprm, op| seen.push((sprm, op.to_vec())));+ assert_eq!(seen, [(0x0835, vec![1]), (0x4A43, vec![24, 0]), (0x6870, vec![1, 2, 3, 0]), (0xC62A, vec![9, 9])]);+ }+}diff --git a/crates/engine/src/tests/textimport.rs b/crates/engine/src/tests/textimport.rsindex d87909bba..409119161 100644--- a/crates/engine/src/tests/textimport.rs+++ b/crates/engine/src/tests/textimport.rs@@ -467,15 +467,368 @@ fn malformed_docx_fails_cleanly() {assert_eq!(s.doc().unwrap().history.undo.len(), 2, "failed places leave no undo step");}+/// The bytes of the Prls `prls`: each sprm and its operand.+fn prls(prls: &[(u16, &[u8])]) -> Vec<u8> {+ prls.iter().flat_map(|(sprm, op)| sprm.to_le_bytes().into_iter().chain(op.iter().copied())).collect()+}++/// A style of a [`Word97`] document: its kind (1 paragraph, 2 character), the style it is based+/// on, and its paragraph and character properties.+type Word97Style = (u16, u16, Vec<u8>, Vec<u8>);++/// A Word 97-2003 document, built as \[MS-DOC\] lays one out: a file information block, the main+/// text's pieces stored last first, one property page per run of up to 16 character or paragraph+/// property runs, the piece table, style sheet and font table in the table stream, all in a+/// compound file.+#[derive(Default)]+struct Word97 {+ /// The main text's pieces: their text and whether it is stored compressed.+ pieces: Vec<(&'static str, bool)>,+ /// Character properties of characters `start..end`.+ chpx: Vec<(u32, u32, Vec<u8>)>,+ /// The paragraph style and properties of the paragraph whose mark is character `cp`; a+ /// character's paragraph is the first one whose mark is at or after it.+ papx: Vec<(u32, u16, Vec<u8>)>,+ /// The styles by istd; None: an empty slot.+ styles: Vec<Option<Word97Style>>,+ fonts: Vec<&'static str>,+ /// FibBase's flags and `nFib` (0: 0x00C1).+ flags: u16,+ nfib: u16,+ /// The table stream is `0Table` (else `1Table`).+ zero_table: bool,+ /// `ccpFtn`, `ccpHdd`, `ccpAtn`, `ccpEdn`, `ccpTxbx` and `ccpHdrTxbx`.+ stories: [u32; 6],+ /// Characters `ccpText` gives beyond the pieces.+ ccp_extra: u32,+ /// A Clx to write instead of the pieces'.+ clx: Option<Vec<u8>>,+}++/// One 512-byte property page of `runs` (file positions and properties), written to `wd`; its+/// page number.+fn property_page(runs: &[(u32, u32, Vec<u8>)], papx: bool, wd: &mut Vec<u8>) -> u32 {+ let mut page = [0u8; 512];+ let n = runs.len();+ for (i, r) in runs.iter().enumerate() {+ page[4 * i..4 * i + 4].copy_from_slice(&r.0.to_le_bytes());+ }+ page[4 * n..4 * n + 4].copy_from_slice(&runs[n - 1].1.to_le_bytes());+ let mut top = 511;+ for (i, (_, _, props)) in runs.iter().enumerate() {+ let blob = match (papx, props.len() % 2) {+ (false, _) if props.is_empty() => continue,+ (false, _) => [vec![props.len() as u8], props.clone()].concat(),+ // PapxInFkp: cb with 2 × cb − 1 bytes, or 0 and cb′ with 2 × cb′ bytes.+ (true, 1) => [vec![props.len().div_ceil(2) as u8], props.clone()].concat(),+ (true, _) => [vec![0, (props.len() / 2) as u8], props.clone()].concat(),+ };+ top = (top - blob.len()) & !1;+ page[top..top + blob.len()].copy_from_slice(&blob);+ page[4 * (n + 1) + if papx { 13 * i } else { i }] = (top / 2) as u8;+ }+ page[511] = n as u8;+ wd.resize(wd.len().div_ceil(512) * 512, 0);+ let pn = (wd.len() / 512) as u32;+ wd.extend(page);+ pn+}++impl Word97 {+ fn build(&self) -> Vec<u8> {+ let mut wd = vec![0u8; 900];+ let mut stored = vec![0u32; self.pieces.len()];+ for (i, (text, compressed)) in self.pieces.iter().enumerate().rev() {+ stored[i] = wd.len() as u32;+ if *compressed {+ wd.extend(text.chars().map(|c| match c {+ '“' => 0x93,+ '”' => 0x94,+ c => u8::try_from(u32::from(c)).unwrap(),+ }));+ } else {+ wd.extend(text.encode_utf16().flat_map(u16::to_le_bytes));+ }+ }+ // Each character's file position and size, in character order.+ let mut chars: Vec<(u32, u32)> = vec![];+ for ((text, compressed), &at) in self.pieces.iter().zip(&stored) {+ let w = if *compressed { 1 } else { 2 };+ chars.extend((0..text.encode_utf16().count() as u32).map(|k| (at + k * w, w)));+ }+ let chpx_of = |cp: u32| (0u32, self.chpx.iter().rev().find(|(s, e, _)| (*s..*e).contains(&cp)).map(|r| r.2.clone()).unwrap_or_default());+ let papx_of = |cp: u32| {+ let p = self.papx.iter().filter(|(mark, _, _)| *mark >= cp).min_by_key(|(mark, _, _)| *mark);+ p.map_or((u32::MAX, vec![0, 0]), |(mark, istd, g)| (*mark, [istd.to_le_bytes().to_vec(), g.clone()].concat()))+ };+ let mut bins = vec![];+ for (papx, of) in [(false, &chpx_of as &dyn Fn(u32) -> (u32, Vec<u8>)), (true, &papx_of)] {+ let mut runs: Vec<(u32, u32, u32, Vec<u8>)> = vec![];+ for (cp, &(fc, w)) in chars.iter().enumerate() {+ let (key, props) = of(cp as u32);+ match runs.last_mut() {+ Some((_, end, k, p)) if *end == fc && *k == key && *p == props => *end = fc + w,+ _ => runs.push((fc, fc + w, key, props)),+ }+ }+ // Pages of contiguous runs, 16 at most.+ let mut groups: Vec<Vec<(u32, u32, Vec<u8>)>> = vec![];+ for (start, end, _, props) in runs {+ match groups.last_mut() {+ Some(g) if g.len() < 16 && g.last().unwrap().1 == start => g.push((start, end, props)),+ _ => groups.push(vec![(start, end, props)]),+ }+ }+ let mut fcs: Vec<u32> = groups.iter().map(|g| g[0].0).collect();+ fcs.extend(groups.last().map(|g| g.last().unwrap().1));+ let pns: Vec<u32> = groups.iter().map(|g| property_page(g, papx, &mut wd)).collect();+ bins.push([fcs, pns].concat().into_iter().flat_map(u32::to_le_bytes).collect::<Vec<u8>>());+ }+ let mut cps = vec![0u32];+ for (text, _) in &self.pieces {+ cps.push(cps.last().unwrap() + text.encode_utf16().count() as u32);+ }+ let pcds = self.pieces.iter().zip(&stored).flat_map(|((_, compressed), &at)| {+ let fc = if *compressed { (at * 2) | 0x4000_0000 } else { at };+ [&[0u8, 0][..], &fc.to_le_bytes(), &[0, 0]].concat()+ });+ let plc: Vec<u8> = cps.iter().flat_map(|c| c.to_le_bytes()).chain(pcds).collect();+ let clx = self.clx.clone().unwrap_or_else(|| [vec![0x02], (plc.len() as u32).to_le_bytes().to_vec(), plc].concat());+ let words = |v: &[u16]| v.iter().flat_map(|w| w.to_le_bytes()).collect::<Vec<u8>>();+ let mut stsh = words(&[18, self.styles.len() as u16, 10, 1, 0, 15, 0, 0, 0, 0]);+ for (istd, s) in self.styles.iter().enumerate() {+ let Some((stk, base, papx, chpx)) = s else {+ stsh.extend([0, 0]);+ continue;+ };+ let upx = |bytes: Vec<u8>| [words(&[bytes.len() as u16]), bytes.clone(), vec![0; bytes.len() % 2]].concat();+ let grlpupx = match stk {+ 1 => [upx([(istd as u16).to_le_bytes().to_vec(), papx.clone()].concat()), upx(chpx.clone())].concat(),+ _ => upx(chpx.clone()),+ };+ let cupx = if *stk == 1 { 2 } else { 1 };+ let mut std = words(&[0x0FFE, stk | (base << 4), cupx | (0x0FFF << 4), 0, 0, 1, u16::from(b'S'), 0]);+ std.extend(grlpupx);+ let len = std.len() as u16;+ std[6..8].copy_from_slice(&len.to_le_bytes());+ stsh.extend(len.to_le_bytes());+ stsh.extend(std);+ }+ let mut ffn = words(&[self.fonts.len() as u16, 0]);+ for name in &self.fonts {+ let units: Vec<u16> = name.encode_utf16().chain([0]).collect();+ ffn.push((39 + 2 * units.len()) as u8);+ ffn.extend([0u8; 39]);+ ffn.extend(words(&units));+ }+ let mut table = vec![];+ let mut pairs = vec![(0u32, 0u32); 0x5D];+ for (index, part) in [(1, stsh), (12, bins[0].clone()), (13, bins[1].clone()), (15, ffn), (33, clx)] {+ pairs[index] = (table.len() as u32, part.len() as u32);+ table.extend(part);+ }+ let mut put = |at: usize, bytes: &[u8]| wd[at..at + bytes.len()].copy_from_slice(bytes);+ let flags = self.flags | if self.zero_table { 0 } else { 0x0200 };+ put(0, &words(&[0xA5EC, if self.nfib == 0 { 0x00C1 } else { self.nfib }]));+ put(10, &words(&[flags, 0x00BF]));+ put(32, &words(&[14]));+ put(62, &words(&[22]));+ put(76, &(chars.len() as u32 + self.ccp_extra).to_le_bytes());+ for (i, n) in [4, 5, 7, 8, 9, 10].into_iter().zip(self.stories) {+ put(64 + 4 * i, &n.to_le_bytes());+ }+ put(152, &words(&[0x5D]));+ for (i, (fc, lcb)) in pairs.iter().enumerate() {+ put(154 + 8 * i, &[fc.to_le_bytes(), lcb.to_le_bytes()].concat());+ }+ compound_file(&[("WordDocument", &wd), (if self.zero_table { "0Table" } else { "1Table" }, &table)])+ }+}++/// A compound file holding `streams`.+fn compound_file(streams: &[(&str, &[u8])]) -> Vec<u8> {+ let mut cf = cfb::CompoundFile::create(std::io::Cursor::new(Vec::new())).unwrap();+ for (name, bytes) in streams {+ std::io::Write::write_all(&mut cf.create_stream(name).unwrap(), bytes).unwrap();+ }+ cf.flush().unwrap();+ cf.into_inner().into_inner()+}++#[test]+fn doc_places_as_area_type_with_its_styles_and_formatting() {+ let navy: &[u8] = &[0x1F, 0x38, 0x64, 0];+ // Normal: 11 pt with 8 pt after; Heading (based on Normal): centered, 12 pt before and none+ // after, in Arial, bold, 16 pt and navy; Emphasis, a character style: italic.+ let mut styles = vec![+ Some((1, 0x0FFF, prls(&[(0xA414, &[160, 0])]), prls(&[(0x4A43, &[22, 0])]))),+ Some((+ 1,+ 0,+ prls(&[(0x2403, &[1]), (0xA413, &[240, 0]), (0xA414, &[0, 0])]),+ prls(&[(0x4A4F, &[1, 0]), (0x0835, &[1]), (0x4A43, &[32, 0]), (0x6870, navy)]),+ )),+ ];+ styles.resize_with(10, || None);+ styles.push(Some((2, 0x0FFF, vec![], prls(&[(0x0836, &[1])]))));+ // The first piece holds characters 0 to 24 ("notes" at 7, the first paragraph mark at 12), the+ // second "styled" at 25, "2" at 31, "hidden" at 32, "gone" at 38, " café ✓ " at 42, the field+ // at 50 (its result "7" at 58) and the paragraph mark at 60.+ let doc = Word97 {+ pieces: vec![("Report notes\r“Plain” and ", true), ("styled2hiddengone café ✓ \u{13} PAGE \u{14}7\u{15}\r", false)],+ chpx: vec![+ (7, 12, prls(&[(0x4A30, &[10, 0])])),+ (25, 31, prls(&[(0x4A4F, &[2, 0]), (0x0836, &[1]), (0x2A3E, &[1])])),+ (31, 32, prls(&[(0x2A48, &[1])])),+ (32, 38, prls(&[(0x083C, &[1])])),+ (38, 42, prls(&[(0x0800, &[1])])),+ (42, 50, prls(&[(0x0801, &[1])])),+ ],+ papx: vec![(12, 1, vec![]), (60, 0, prls(&[(0x840F, &[0xD0, 0x02]), (0x8411, &(-360i16).to_le_bytes()), (0x2403, &[3])]))],+ styles,+ fonts: vec!["Times New Roman", "Arial", "Courier New"],+ ..Word97::default()+ };+ let mut s = session();+ s.execute("document.setup", &json!({"superscript": {"size": 50, "position": 40}})).unwrap();+ let file = json!({"name": "report.doc", "dataBase64": vectorcraft_format::base64_encode(&doc.build())});+ let r = s.execute("file.place", &file).unwrap();+ assert_eq!((r["format"].as_str(), r["name"].as_str()), (Some("doc"), Some("report.doc")));+ let (text, frame) = placed(&s, &r);+ assert_eq!(text, "Report notes\n“Plain” and styled2 café ✓ 7", "hidden and deleted text and the field's instructions are left out");+ assert!(close(frame, Rect::new(36.0, 36.0, 364.0, 264.0)), "the artboard less a 36 pt margin: {frame:?}");+ let n = s.doc().unwrap().doc.node(NodeId(r["ids"][0].as_u64().unwrap())).unwrap().clone();+ let NodeKind::Text(t) = &n.kind else { panic!("not type") };+ let runs: Vec<_> = t.runs.iter().map(|r| (r.text.as_str(), r.style.font_family.as_str(), r.style.font_style.as_str(), r.style.size)).collect();+ assert_eq!(+ runs,+ [+ ("Report ", "Arial", "Bold", 16.0),+ ("notes", "Arial", "Bold Italic", 16.0),+ ("\n", "Arial", "Bold", 16.0),+ ("“Plain” and ", "Times New Roman", "Regular", 11.0),+ ("styled", "Courier New", "Italic", 11.0),+ ("2", "Times New Roman", "Regular", 11.0),+ (" café ✓ 7", "Times New Roman", "Regular", 11.0),+ ]+ );+ let navy = vectorcraft_color::Paint::solid(vectorcraft_color::Color::rgb8(0x1F, 0x38, 0x64));+ assert_eq!((t.runs[0].style.fill == navy, t.runs[1].style.fill == navy, t.runs[3].style.fill == navy), (true, true, false));+ assert_eq!((t.runs[3].style.underline, t.runs[4].style.underline), (false, true));+ let superscript = vectorcraft_doc::ScriptMetrics { size: 50.0, position: 40.0 };+ assert_eq!(t.runs[5].style.position, vectorcraft_doc::CharPosition::Superscript(superscript), "in Document Setup's proportions");+ let paras = t.paragraph_styles();+ let summary: Vec<_> = paras.iter().map(|p| (p.justify, p.left_indent, p.first_line_indent, p.space_before, p.space_after)).collect();+ assert_eq!(summary, [(vectorcraft_doc::Justify::Center, 0.0, 0.0, 12.0, 0.0), (vectorcraft_doc::Justify::JustifyLeft, 36.0, -18.0, 0.0, 8.0)]);+ let fields = "the Word document's fields were placed as the text of their results";+ let revisions = "the Word document's tracked changes were accepted: deleted text was left out and inserted text kept";+ assert_eq!(r["warnings"], json!([fields, revisions]));+ let st = s.doc().unwrap();+ assert_eq!((st.history.undo.len(), st.selection.objects.len()), (2, 1), "Document Setup and Place, each one undo step");+ // Info and the place cursor report it as a Word 97-2003 document, with the same warnings.+ let info = s.execute("file.place.info", &file).unwrap();+ assert_eq!((info["format"].as_str(), info["width"].as_f64(), &info["warnings"]), (Some("doc"), Some(468.0), &json!([fields, revisions])));+ let q = s.execute("file.place.queue", &json!({"files": [file]})).unwrap();+ assert_eq!((q["count"].as_u64(), q["files"][0]["format"].as_str()), (Some(1), Some("doc")));+}++#[test]+fn doc_tables_breaks_and_other_stories_place_as_paragraphs_and_tabs_with_warnings() {+ let in_table = prls(&[(0x2416, &[1])]);+ // A line break; a table row of two cells; a page break inside a paragraph and a section break+ // that ends one; a picture, a tab and a nonbreaking hyphen; right-to-left justified text.+ let text = "Line one\u{B}line two\ra\u{7}b\u{7}\u{7}x\u{C}y\rsec\u{C}next\rpic\u{1}ture\tend\u{1E}ok\rrtl\r";+ let doc = Word97 {+ pieces: vec![(text, true)],+ // Marks: the first paragraph's at 17, the cells' at 19 and 21, the row's at 22, the page+ // break at 24 inside the paragraph that ends at 26, the section break at 30, and the+ // paragraph marks at 35, 51 and 55.+ papx: vec![+ (17, 0, vec![]),+ (19, 0, in_table.clone()),+ (21, 0, in_table),+ (22, 0, prls(&[(0x2416, &[1]), (0x2417, &[1])])),+ (26, 0, vec![]),+ (30, 0, vec![]),+ (35, 0, vec![]),+ (51, 0, vec![]),+ (55, 0, prls(&[(0x2441, &[1]), (0x2461, &[3])])),+ ],+ zero_table: true,+ stories: [5, 0, 2, 0, 0, 0],+ ..Word97::default()+ };+ let mut s = session();+ // Recognized by its WordDocument stream whatever its name says.+ let r = s.execute("file.place", &json!({"name": "notes.bin", "dataBase64": vectorcraft_format::base64_encode(&doc.build())})).unwrap();+ assert_eq!(r["format"].as_str(), Some("doc"));+ assert_eq!(placed(&s, &r).0, "Line one\nline two\na\tb\nxy\nsec\nnext\npicture\tend\u{2011}ok\nrtl");+ let n = s.doc().unwrap().doc.node(NodeId(r["ids"][0].as_u64().unwrap())).unwrap().clone();+ let NodeKind::Text(t) = &n.kind else { panic!("not type") };+ let last = t.paragraph_styles().last().cloned().unwrap();+ assert_eq!((last.justify, last.direction), (vectorcraft_doc::Justify::JustifyRight, Some(vectorcraft_doc::ParaDirection::RightToLeft)));+ assert_eq!(+ r["warnings"],+ json!([+ "the Word document's pictures and embedded objects were left out",+ "the Word document's tables were placed as paragraphs with their cells separated by tabs",+ "the Word document's footnotes and comments were left out",+ ])+ );+}++#[test]+fn malformed_doc_fails_cleanly() {+ let mut s = session();+ let place = |s: &mut Session, name: &str, bytes: &[u8]| {+ s.execute("file.place", &json!({"name": name, "dataBase64": vectorcraft_format::base64_encode(bytes)})).map_err(|e| e.to_string())+ };+ let words = || Word97 { pieces: vec![("words\r", true)], ..Word97::default() };+ let e = place(&mut s, "a.doc", b"Plain words, not a compound file").unwrap_err();+ assert!(e.contains("not a format VectorCraft reads"), "{e}");+ // An RTF file named `.doc` is still placed as RTF.+ let r = place(&mut s, "a.doc", br"{\rtf1 Rich}").unwrap();+ assert_eq!((r["format"].as_str(), placed(&s, &r).0.as_str()), (Some("rtf"), "Rich"));+ let e = place(&mut s, "a.doc", &compound_file(&[("Other", b"x")])).unwrap_err();+ assert!(e.contains("no WordDocument stream"), "{e}");+ let e = place(&mut s, "a.bin", &compound_file(&[("Other", b"x")])).unwrap_err();+ assert!(e.contains("not a format VectorCraft reads"), "a compound file of something else isn't read as a Word document: {e}");+ let e = place(&mut s, "a.doc", &[&[0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1][..], &[0; 600]].concat()).unwrap_err();+ assert!(e.contains("compound file structure is damaged"), "{e}");+ let e = place(&mut s, "a.doc", &compound_file(&[("WordDocument", &[0; 900])])).unwrap_err();+ assert!(e.contains("doesn't start with a file information block"), "{e}");+ let e = place(&mut s, "a.doc", &Word97 { nfib: 0x0065, ..words() }.build()).unwrap_err();+ assert!(e.contains("Word 6.0 or Word 95"), "{e}");+ let e = place(&mut s, "a.doc", &Word97 { flags: 0x0100, ..words() }.build()).unwrap_err();+ assert!(e.contains("encrypted"), "{e}");+ let e = place(&mut s, "a.doc", &Word97 { flags: 0x0004, ..words() }.build()).unwrap_err();+ assert!(e.contains("fast save"), "{e}");+ let e = place(&mut s, "a.doc", &Word97 { ccp_extra: 3, ..words() }.build()).unwrap_err();+ assert!(e.contains("doesn't cover its text"), "{e}");+ // A piece stored past the end of the WordDocument stream.+ let far = [&0u32.to_le_bytes()[..], &6u32.to_le_bytes(), &[0, 0], &0x00FF_0000u32.to_le_bytes(), &[0, 0]].concat();+ let clx = [&[0x02][..], &(far.len() as u32).to_le_bytes(), &far].concat();+ let e = place(&mut s, "a.doc", &Word97 { clx: Some(clx), ..words() }.build()).unwrap_err();+ assert!(e.contains("lies outside its WordDocument stream"), "{e}");+ let e = place(&mut s, "a.doc", &Word97 { clx: Some(vec![0x02, 0xFF, 0xFF, 0, 0]), ..words() }.build()).unwrap_err();+ assert!(e.contains("piece table is damaged"), "{e}");+ let e = place(&mut s, "a.doc", &Word97 { pieces: vec![("\r\r", true)], ..Word97::default() }.build()).unwrap_err();+ assert!(e.contains("no text to place"), "{e}");+ let r = place(&mut s, "a.doc", &words().build()).unwrap();+ assert_eq!(placed(&s, &r).0, "words");+ assert_eq!(s.doc().unwrap().history.undo.len(), 2, "failed places leave no undo step");+}+#[test]fn place_reads_what_open_reads_and_text_files() {- use cmd::fileio::{DOCX_EXTS, OPEN_EXTS, PLACE_EXTS, RTF_EXTS, TEXT_EXTS};- assert_eq!(PLACE_EXTS, [OPEN_EXTS, TEXT_EXTS, RTF_EXTS, DOCX_EXTS].concat());+ use cmd::fileio::{DOC_EXTS, DOCX_EXTS, OPEN_EXTS, PLACE_EXTS, RTF_EXTS, TEXT_EXTS};+ assert_eq!(PLACE_EXTS, [OPEN_EXTS, TEXT_EXTS, RTF_EXTS, DOCX_EXTS, DOC_EXTS].concat());let filters: Vec<_> = cmd::fileio::place_filters().collect();- assert_eq!((filters.first(), filters.last()), (Some(&("All placeable files", PLACE_EXTS)), Some(&("Word", DOCX_EXTS))));- assert_eq!(filters.get(filters.len() - 3..), Some(&[("Text", TEXT_EXTS), ("RTF", RTF_EXTS), ("Word", DOCX_EXTS)][..]));+ let word: (&str, &[&str]) = ("Word", &["docx", "doc"]);+ assert_eq!((filters.first(), filters.last()), (Some(&("All placeable files", PLACE_EXTS)), Some(&word)));+ assert_eq!(filters.get(filters.len() - 3..), Some(&[("Text", TEXT_EXTS), ("RTF", RTF_EXTS), word][..]));assert!(- !cmd::fileio::open_filters().any(|(_, exts)| exts.contains(&"txt") || exts.contains(&"rtf") || exts.contains(&"docx")),+ !cmd::fileio::open_filters().any(|(_, exts)| ["txt", "rtf", "docx", "doc"].iter().any(|e| exts.contains(e))),"Open doesn't read text, RTF or Word files");}diff --git a/crates/engine/tests/text_import_fuzz.rs b/crates/engine/tests/text_import_fuzz.rsindex 867b016a9..3ea6f94d9 100644--- a/crates/engine/tests/text_import_fuzz.rs+++ b/crates/engine/tests/text_import_fuzz.rs@@ -1,6 +1,6 @@//! Untrusted text files placed with File → Place never crash: any bytes, with any Text Import-//! Options, place as area type or fail with an error, and so do hostile RTF files and Word-//! documents.+//! Options, place as area type or fail with an error, and so do hostile RTF files, Word documents+//! and Word 97-2003 documents.//!//! `PROPTEST_CASES=20000 cargo test -p vectorcraft-engine --test text_import_fuzz` runs a deeper search.// Integration tests: unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md › Robustness).@@ -91,6 +91,164 @@ proptest! {});prop_assert!(r.is_ok(), "panicked: {:?}", r.err());}++ #[test]+ fn any_doc_places_without_panics(+ text in prop::collection::vec(prop::sample::select(DOC_CHARS.chars().collect::<Vec<_>>()), 1..60),+ compressed in any::<bool>(),+ chpx in arb_grpprl(),+ papx in arb_grpprl(),+ istd in 0u16..4,+ stsh in prop_oneof![1 => prop::collection::vec(any::<u8>(), 0..120), 3 => arb_stsh()],+ ffn in prop::collection::vec(any::<u8>(), 0..80),+ changes in prop_oneof![+ 3 => Just(vec![]),+ 1 => prop::collection::vec((any::<bool>(), any::<prop::sample::Index>(), any::<u8>()), 1..4),+ ],+ name in prop::sample::select(vec!["fuzz.doc", "fuzz.bin"]),+ ) {+ vectorcraft_text::FontDb::global().set_system_fallback(false);+ let text: String = text.into_iter().collect();+ let (mut wd, mut table) = word97(&text, compressed, &chpx, istd, &papx, &stsh, &ffn);+ // A changed byte anywhere in either stream: in the FIB, the text, a property page or a+ // table.+ for (in_table, at, byte) in changes {+ let stream = if in_table { &mut table } else { &mut wd };+ let i = at.index(stream.len());+ stream[i] = byte;+ }+ let mut cf = cfb::CompoundFile::create(std::io::Cursor::new(Vec::new())).unwrap();+ std::io::Write::write_all(&mut cf.create_stream("WordDocument").unwrap(), &wd).unwrap();+ std::io::Write::write_all(&mut cf.create_stream("1Table").unwrap(), &table).unwrap();+ cf.flush().unwrap();+ let data = cf.into_inner().into_inner();+ let r = catch_quiet(|| {+ let mut s = vectorcraft_engine::Session::new();+ s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap();+ let p = json!({"name": name, "dataBase64": vectorcraft_format::base64_encode(&data), "thumbnail": 8});+ let _ = s.execute("file.place.info", &p);+ let _ = s.execute("file.place", &p);+ });+ prop_assert!(r.is_ok(), "panicked: {:?}", r.err());+ }+}++/// Characters of a Word document's text: letters, marks of paragraphs, cells and breaks, fields,+/// pictures, hyphens and surrogate-free Unicode.+const DOC_CHARS: &str = "ab \r\u{7}\u{B}\u{C}\u{13}\u{14}\u{15}\u{1}\u{8}\t\u{1E}\u{1F}(é“✓";++/// Character and paragraph properties: Prls the Word 97-2003 reader knows, with operands in and out+/// of range, and raw bytes.+fn arb_grpprl() -> impl Strategy<Value = Vec<u8>> {+ let prl = prop_oneof![+ prop::sample::select(vec![+ &[0x35, 0x08, 0x81][..],+ &[0x35, 0x08, 0x80],+ &[0x3C, 0x08, 0x01],+ &[0x00, 0x08, 0x01],+ &[0x01, 0x08, 0x01],+ &[0x55, 0x08, 0x01],+ &[0x30, 0x4A, 0x0A, 0x00],+ &[0x30, 0x4A, 0xFF, 0xFF],+ &[0x43, 0x4A, 0xFF, 0xFF],+ &[0x4F, 0x4A, 0x07, 0x00],+ &[0x42, 0x2A, 0x63],+ &[0x70, 0x68, 1, 2, 3, 0xFF],+ &[0x45, 0x48, 0x00, 0x80],+ &[0x03, 0x24, 0x09],+ &[0x61, 0x24, 0x08],+ &[0x17, 0x24, 0x01],+ &[0x4B, 0x24, 0x01],+ &[0x41, 0x24, 0x01],+ &[0x0F, 0x84, 0x00, 0x80],+ &[0x13, 0xA4, 0xFF, 0xFF],+ &[0x08, 0xD6, 0xFF, 0xFF],+ &[0x15, 0xC6, 0xFF, 0x7F],+ &[0x2A, 0xC6, 0xFF],+ ])+ .prop_map(<[u8]>::to_vec),+ prop::collection::vec(any::<u8>(), 0..4),+ ];+ prop::collection::vec(prl, 0..6).prop_map(|p| p.concat())+}++/// A style sheet of up to four styles (paragraph or character, based on another or none) with+/// properties of [`arb_grpprl`], or empty slots.+fn arb_stsh() -> impl Strategy<Value = Vec<u8>> {+ let style = prop::option::of((1u16..3, prop_oneof![Just(0x0FFFu16), 0u16..5], arb_grpprl(), arb_grpprl()));+ prop::collection::vec(style, 0..5).prop_map(|styles| {+ let words = |v: &[u16]| v.iter().flat_map(|w| w.to_le_bytes()).collect::<Vec<u8>>();+ let mut stsh = words(&[18, styles.len() as u16, 10, 1, 0, 15, 0, 0, 0, 0]);+ for s in styles {+ let Some((stk, base, papx, chpx)) = s else {+ stsh.extend([0, 0]);+ continue;+ };+ let upx = |b: Vec<u8>| [words(&[b.len() as u16]), b.clone(), vec![0; b.len() % 2]].concat();+ let (cupx, grlpupx) = if stk == 1 { (2, [upx([vec![0, 0], papx].concat()), upx(chpx)].concat()) } else { (1, upx(chpx)) };+ let mut std = words(&[0x0FFE, stk | (base << 4), cupx | (0x0FFF << 4), 0, 0, 1, u16::from(b'S'), 0]);+ std.extend(grlpupx);+ stsh.extend((std.len() as u16).to_le_bytes());+ stsh.extend(std);+ }+ stsh+ })+}++/// The WordDocument and table streams of a Word 97-2003 document whose main text is `text` in one+/// piece, with one page of character properties `chpx` and one of paragraph properties `papx` of+/// style `istd` covering it, and the style sheet `stsh` and font table `ffn`.+fn word97(text: &str, compressed: bool, chpx: &[u8], istd: u16, papx: &[u8], stsh: &[u8], ffn: &[u8]) -> (Vec<u8>, Vec<u8>) {+ let mut wd = vec![0u8; 1024];+ let at = wd.len() as u32;+ if compressed {+ wd.extend(text.chars().map(|c| if c == '“' { 0x93 } else { u32::from(c).min(0xFF) as u8 }));+ } else {+ wd.extend(text.encode_utf16().flat_map(u16::to_le_bytes));+ }+ let ccp = text.encode_utf16().count() as u32;+ let end = wd.len() as u32;+ // One page each: a run of every character, its properties near the page's end.+ let page = |props: Vec<u8>, papx: bool| {+ let mut p = vec![0u8; 512];+ p[0..4].copy_from_slice(&at.to_le_bytes());+ p[4..8].copy_from_slice(&end.to_le_bytes());+ let blob = if papx { [vec![0, (props.len() / 2) as u8], props].concat() } else { [vec![props.len() as u8], props].concat() };+ let top = (511 - blob.len().min(400)) & !1;+ let n = blob.len().min(511 - top);+ p[top..top + n].copy_from_slice(&blob[..n]);+ p[8] = (top / 2) as u8;+ p[511] = 1;+ p+ };+ let papx_props = [istd.to_le_bytes().to_vec(), papx.to_vec(), vec![0; papx.len() % 2]].concat();+ wd.resize(wd.len().div_ceil(512) * 512, 0);+ let chpx_page = (wd.len() / 512) as u32;+ wd.extend(page(chpx.iter().copied().take(200).collect(), false));+ wd.extend(page(papx_props.into_iter().take(400).collect(), true));+ let bte = |pn: u32| [at, end, pn].into_iter().flat_map(u32::to_le_bytes).collect::<Vec<u8>>();+ let fc = if compressed { (at * 2) | 0x4000_0000 } else { at };+ let plc = [&0u32.to_le_bytes()[..], &ccp.to_le_bytes(), &[0, 0], &fc.to_le_bytes(), &[0, 0]].concat();+ let clx = [&[0x02][..], &(plc.len() as u32).to_le_bytes(), &plc].concat();+ let mut table = vec![];+ let mut pairs = vec![(0u32, 0u32); 0x5D];+ for (index, part) in [(1, stsh.to_vec()), (12, bte(chpx_page)), (13, bte(chpx_page + 1)), (15, ffn.to_vec()), (33, clx)] {+ pairs[index] = (table.len() as u32, part.len() as u32);+ table.extend(part);+ }+ let words = |v: &[u16]| v.iter().flat_map(|w| w.to_le_bytes()).collect::<Vec<u8>>();+ let mut put = |at: usize, bytes: &[u8]| wd[at..at + bytes.len()].copy_from_slice(bytes);+ put(0, &words(&[0xA5EC, 0x00C1]));+ put(10, &words(&[0x0200, 0x00BF]));+ put(32, &words(&[14]));+ put(62, &words(&[22]));+ put(76, &ccp.to_le_bytes());+ put(80, &1u32.to_le_bytes());+ put(152, &words(&[0x5D]));+ for (i, (fc, lcb)) in pairs.iter().enumerate() {+ put(154 + 8 * i, &[fc.to_le_bytes(), lcb.to_le_bytes()].concat());+ }+ (wd, table)}/// A well-formed paragraph, or a table row of two such paragraphs.diff --git a/docs/file-format-parity.md b/docs/file-format-parity.mdindex 1d84d3862..a3858ef8e 100644--- a/docs/file-format-parity.md+++ b/docs/file-format-parity.md@@ -17,7 +17,7 @@ from the public specifications, and users' own `.ai` files are used locally only| | Illustrator | VectorCraft | Notes ||---|---:|---:|---|-| Formats opened or placed | ~30 | 27 | missing: FreeHand, CorelDRAW, DWG, HEIC/HEIF, PICT, Pixar, Word `.doc`, PS |+| Formats opened or placed | ~30 | 28 | missing: FreeHand, CorelDRAW, DWG, HEIC/HEIF, PICT, Pixar, PS || Formats saved (Save As) | 6 (AI, AIT, PDF, EPS, SVG, SVGZ) | 6 + native | `.ai` written PDF-compatible (no Illustrator editing data) or as legacy version 7 (layers and groups, one artboard per file) || Formats exported | ~16 | 17 | missing: DWG, 3D (OBJ/USDA/glTF); extra: GIF/PNG-8 palette export, DXF up to 2018, layered PSD || **Overall (weighted by use)** | | **~75%** | `.ai` and PDF dominate the weighting |@@ -113,7 +113,7 @@ the app's own editing copy of a file (an EPS after its `%%EOF`, a `.ai`'s `AIPri| Text `.txt` | place, export | place (Text Import Options) | yes | || 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 |-| Word `.doc` | place | no | no | |+| Word `.doc` | place | place (area type with the same formatting as `.docx`, from the style sheet's styles and direct formatting; Word 97 and later) | no | `tests/textimport.rs`, `place/doc.rs`; fuzzed (`text_import_fuzz.rs`); pictures, headers, footers, notes, comments and text boxes are left out with a warning; encrypted and fast-saved documents are refused || CSS | export (CSS Properties) | — | yes | || OBJ / USDA / glTF | 3D export | — | no | needs 3D (gap G3) || Affinity `.af`, `.afdesign`, `.afpub` | no | yes (beyond Illustrator) | no (#665) | 51 pinned files, [affinity-validation.md](affinity-validation.md) |diff --git a/docs/mcp.md b/docs/mcp.mdindex f40709cb3..d9030f9cf 100644--- a/docs/mcp.md+++ b/docs/mcp.md@@ -3832,6 +3832,18 @@ more than 16 MiB or nests elements more than 256 deep, and a document with moreparagraphs or runs are refused.`file.place` answers `format: "docx"`.+A Word 97-2003 document (a compound file named `.doc`, or one that holds a `WordDocument` stream; an RTF file named+`.doc` is placed as RTF) is placed the same way and keeps the same formatting, taken from the style sheet's default+font, the paragraph style and the character style (and the styles each is based on) and direct formatting. Its main+text is read; table cells are separated by tabs and each row is a paragraph, and a field places as its result. Hidden+and deleted text, field instructions, pictures and embedded objects, headers, footers, notes, comments and text boxes+are left out, and `warnings` names each kind of content left out or changed once (pictures and objects, fields,+tracked changes, tables, and the other stories the document has). Documents saved by Word 6.0 or Word 95, encrypted or+password-protected documents and documents last saved with fast save are refused, and so are a stream larger than+64 MiB, a document with more than 4 MiB of text, 100,000 pieces, 16,384 pages of properties or 100,000 paragraphs or+runs, and one whose structures lie outside their streams.+`file.place` answers `format: "doc"`.+## PDF presets`pdf.preset.list` lists the built-in presets (`VectorCraft Default`, which preserves editing, then High Quality Print,diff --git a/docs/target-app-parity.md b/docs/target-app-parity.mdindex 27ebc9383..0a5187b94 100644--- a/docs/target-app-parity.md+++ b/docs/target-app-parity.md@@ -1093,6 +1093,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur- File › Place reads RTF files as area type with their formatting: each run's font, size, bold and italic, underline, strikethrough, caps, superscript or subscript, baseline shift and color, and each paragraph's alignment, indents, space before and after and direction. Table cells are separated by tabs; hidden text, pictures, headers, footers and notes are left out (`file.place`, `crates/engine/src/tests/textimport.rs`).- File › Place reads Word `.docx` documents as area type with the same formatting, taken from the document defaults, the paragraph and character styles (their toggle properties reversing each other) and direct formatting, the theme's fonts included. Table rows are paragraphs with their cells separated by tabs; hidden and deleted text, field instructions, pictures, headers, footers and notes are left out (`file.place`, `crates/engine/src/tests/textimport.rs`).- A Word paragraph justified with a low, medium or high kashida length places justified with Insert Kashidas set to Short, Medium or Long (`file.place`, `crates/engine/src/tests/textimport.rs`).+ - File › Place reads Word 97-2003 `.doc` documents as area type with the same formatting, read from the main text's pieces, the style sheet's paragraph and character styles and direct formatting. Table rows are paragraphs with their cells separated by tabs and fields place as their results; hidden and deleted text, pictures, headers, footers, notes, comments and text boxes are left out, and `warnings` names each kind left out or changed. Encrypted documents, documents last saved with fast save and Word 6.0 and Word 95 documents are refused (`file.place`, `crates/engine/src/tests/textimport.rs`).- On the desktop, File › Place reads and decodes files off the UI thread, one at a time, and places each once it is read, with "Placing" in the status bar and the busy spinner until then: the Place dialog's OK, a file dropped on the canvas, a click or drag of the place cursor, and the OK of the PDF, DXF and text import options. The files and thumbnails the place cursor is loaded with are read the same way, and the Place dialog's lines about its files are read on a thread of their own. Files are placed in the order they were started; a file is not placed while a dialog is open or a drag is under way, and a file whose document was closed meanwhile is not placed. `file.place` and `file.place.queue` over MCP and scripts reply as before.- The pixels of a PNG, JPEG, GIF or WebP file that Place decodes to check them are kept in the image store the renderers share, so the canvas's first frame with the placed image draws them without decoding the file again on the UI thread: File › Place, a file dropped on the canvas, a click or drag of the place cursor, `file.place`, and Paste of such an image. A CMYK JPEG is still decoded when it is first drawn (`crates/ui-egui/src/placing.rs`, `crates/render/src/tests.rs`).- A photo whose EXIF orientation turns or flips it (a camera held upright or upside down) is placed and opened upright, as it was taken; the image is stored as it is seen (#4284).