From fb33e29bb6e5a63554d5e3f06810d887d3806311 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Mon, 16 Nov 2026 17:52:40 -0500 Subject: [PATCH 1/5] export: write RTF through craft-rtf's full writer, from the word model The pages are mapped onto a craft_wordmodel::Document (as_word.rs, the mapping the DOCX export will share), and craft_rtf::doc::export writes it. Over the small writer this keeps, in RTF: - pictures, inline (the engine no longer leaves them out for RTF); - each page's size, as a section per page (blank pages kept); - Heading 1 and 2 styles; - the title; - hard line breaks inside a paragraph or cell (storytold/pdfcraft#4723). It also fixes two bugs: \cellx was the absolute page x, so every column edge sat one left margin too far right, and tables that touched became one (NIST CSF 2.0 came out with 28 tables instead of 68). craft-rtf, craft-wordmodel, craft-docx and craft-mime are pinned together at one craft-libs commit: doc::export takes its own revision's Document. --- Cargo.lock | 78 +++++++++-- Cargo.toml | 11 +- crates/engine/src/compare.rs | 7 +- crates/export/Cargo.toml | 3 +- crates/export/src/as_rtf.rs | 100 +++----------- crates/export/src/as_word.rs | 252 +++++++++++++++++++++++++++++++++++ crates/export/src/lib.rs | 3 +- crates/export/src/tests.rs | 172 ++++++++++++++++++++---- parity/acrobat-features.toml | 4 +- 9 files changed, 498 insertions(+), 132 deletions(-) create mode 100644 crates/export/src/as_word.rs diff --git a/Cargo.lock b/Cargo.lock index 11a0214d07..4d4328c509 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1211,7 +1211,7 @@ dependencies = [ [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=f29e82f1a7a009e68312bd2e84adab72be23344d#f29e82f1a7a009e68312bd2e84adab72be23344d" +source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" [[package]] name = "craft-csv" @@ -1226,6 +1226,16 @@ name = "craft-docpath" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +[[package]] +name = "craft-flow" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +dependencies = [ + "craft-wordmodel", + "image", + "log", +] + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -1294,6 +1304,15 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-math" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f)", + "serde", +] + [[package]] name = "craft-menu" version = "0.1.0" @@ -1307,10 +1326,21 @@ dependencies = [ "objc2-app-kit 0.3.2", ] +[[package]] +name = "craft-metafile" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +dependencies = [ + "encoding_rs", + "image", + "kurbo 0.13.1", + "thiserror 2.0.21", +] + [[package]] name = "craft-mime" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=f29e82f1a7a009e68312bd2e84adab72be23344d#f29e82f1a7a009e68312bd2e84adab72be23344d" +source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" dependencies = [ "chrono", "craft-codepage", @@ -1322,7 +1352,7 @@ name = "craft-opc" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" dependencies = [ - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "craft-zip", "log", "thiserror 2.0.21", @@ -1416,10 +1446,14 @@ dependencies = [ [[package]] name = "craft-rtf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=f29e82f1a7a009e68312bd2e84adab72be23344d#f29e82f1a7a009e68312bd2e84adab72be23344d" +source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" dependencies = [ "craft-codepage", + "craft-flow", + "craft-metafile", + "craft-wordmodel", "encoding_rs", + "image", ] [[package]] @@ -1518,6 +1552,30 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "craft-wordmodel" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +dependencies = [ + "base64 0.23.1", + "craft-math", + "log", + "serde", + "serde_json", + "thiserror 2.0.21", + "unicode-segmentation", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -3410,6 +3468,7 @@ dependencies = [ "byteorder-lite", "color_quant", "gif", + "image-webp", "moxcms", "num-traits", "png 0.18.1", @@ -4762,7 +4821,8 @@ dependencies = [ "craft-mime", "craft-opc", "craft-rtf", - "craft-xml", + "craft-wordmodel", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "craft-zip", "flate2", ] @@ -4914,7 +4974,7 @@ name = "pdfcraft-preflight" version = "0.21.1" dependencies = [ "craft-cms 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8503f24cb845268f47f97e829491114fe9ad1bfa)", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "flate2", "pdfcraft-content", "pdfcraft-cos", @@ -4942,7 +5002,7 @@ name = "pdfcraft-redact" version = "0.21.1" dependencies = [ "aho-corasick", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "pdfcraft-annot", "pdfcraft-content", "pdfcraft-cos", @@ -5110,7 +5170,7 @@ version = "0.21.1" dependencies = [ "base64 0.23.1", "craft-image-codecs", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "pdfcraft-cos", "pdfcraft-fonts", "sha2 0.11.1", @@ -5123,7 +5183,7 @@ version = "0.21.1" dependencies = [ "base64 0.23.1", "craft-csv", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "pdfcraft-annot", "pdfcraft-cos", "pdfcraft-forms", diff --git a/Cargo.toml b/Cargo.toml index 22af6a1ff5..eb1eb2bcf5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,17 +35,18 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l # craft-update: app-specific pretend versions apply to startup and manual checks. craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "e2dc681a3", features = ["egui"] } craft-pdf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "8e03cd016d42032b867fef6bad86bc69bf2c1920", features = ["cos", "crypt", "filters"] } -# The writer the RTF export maps onto (storytold/craft-libs#793), at storytold/craft-libs#1467: -# its font and colour lookups take constant time, as PdfCraft's own writer did. -craft-rtf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "f29e82f1a7a009e68312bd2e84adab72be23344d" } +# The writers the Word and RTF exports map onto (storytold/craft-libs#793). craft-rtf, craft-wordmodel and +# craft-docx move together: craft_rtf::doc::export takes its own revision's Document. +craft-rtf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "892ef9116c432d7afb5c08aba93e93a4e0b1548f" } # Shared Base64 encoder; use the existing RTF revision to reuse its codepage dependency. -craft-mime = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "f29e82f1a7a009e68312bd2e84adab72be23344d" } +craft-mime = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "892ef9116c432d7afb5c08aba93e93a4e0b1548f" } craft-csv = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-xml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-tiff = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-jpeg = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } -craft-docx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-docx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "892ef9116c432d7afb5c08aba93e93a4e0b1548f" } +craft-wordmodel = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "892ef9116c432d7afb5c08aba93e93a4e0b1548f" } craft-pptx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-drawingml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-opc = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } diff --git a/crates/engine/src/compare.rs b/crates/engine/src/compare.rs index a476e1538b..d9dd0eae57 100644 --- a/crates/engine/src/compare.rs +++ b/crates/engine/src/compare.rs @@ -329,9 +329,8 @@ impl crate::Document { /// [`Self::export_office`], and the pages whose text it had to leave out /// ([`OfficeExport::truncated_pages`]). pub fn export_office_checked(&self, format: OfficeFormat) -> OfficeExport { - // RTF and Markdown leave pictures out. - let (pages, truncated_pages) = - self.export_pages_checked(!matches!(format, OfficeFormat::Rtf | OfficeFormat::Markdown), MAX_EXPORT_IMAGE_BYTES); + // Markdown leaves pictures out. + let (pages, truncated_pages) = self.export_pages_checked(!matches!(format, OfficeFormat::Markdown), MAX_EXPORT_IMAGE_BYTES); let title = self.info.title.clone().unwrap_or_else(|| self.name.trim_end_matches(".pdf").to_string()); // Word and HTML name the document's language (`/Lang`), so spelling is checked in it. let language = self.initial_view().language; @@ -340,7 +339,7 @@ impl crate::Document { OfficeFormat::Pptx => pdfcraft_export::pptx(&pages, &title), OfficeFormat::Html => pdfcraft_export::html_with_language(&pages, &title, language.as_deref()).into_bytes(), OfficeFormat::HtmlSimple => pdfcraft_export::html_simple_with_language(&pages, &title, language.as_deref()).into_bytes(), - OfficeFormat::Rtf => pdfcraft_export::rtf(&pages).into_bytes(), + OfficeFormat::Rtf => pdfcraft_export::rtf_with_title(&pages, &title).into_bytes(), OfficeFormat::Markdown => pdfcraft_export::markdown(&pages).into_bytes(), }; OfficeExport { bytes, truncated_pages } diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index 4ff2f0dfb9..93e16bb00e 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -13,7 +13,8 @@ workspace = true [dependencies] craft-mime.workspace = true craft-opc.workspace = true -craft-rtf.workspace = true +craft-rtf = { workspace = true, features = ["doc"] } +craft-wordmodel.workspace = true craft-xml.workspace = true craft-zip.workspace = true diff --git a/crates/export/src/as_rtf.rs b/crates/export/src/as_rtf.rs index feea5306ee..4ab7a39c2c 100644 --- a/crates/export/src/as_rtf.rs +++ b/crates/export/src/as_rtf.rs @@ -1,90 +1,20 @@ -//! Rich Text Format through craft-rtf's writer: this module maps the pages onto its small model -//! (storytold/craft-libs#793). +//! Rich Text Format through craft-rtf's full writer (storytold/craft-libs#793): the pages are +//! mapped onto a `craft_wordmodel::Document` ([`super::as_word`]), every page flowing. -use super::as_docx::rgb8; -use super::as_html::allowed; -use super::layout::{Item, Page, Table, items}; -use craft_rtf::{Block, Cell, CharFmt, Document, PAGE_BREAK, Para, Run}; +use super::as_word::{Meta, flow_doc}; +use super::layout::Page; -/// The text as RTF may hold it: the characters Word refuses are left out. -fn clean(s: &str) -> String { - s.chars().filter(|c| allowed(*c)).collect() -} +/// The body text's font, as PdfCraft's RTF has always named it (a reader without Helvetica draws +/// it in its sans-serif, which the font table's `\fswiss` asks for). +const FONT: &str = "Helvetica"; -/// A run's formatting: its size in half points, bold, italic and colour (none for black, which -/// is the default). -fn char_fmt(size: f64, bold: bool, italic: bool, color: [f64; 3]) -> CharFmt { - let color = Some(rgb8(color)).filter(|c| *c != [0, 0, 0]); - // A size that is not finite or below zero becomes 0, as the cast saturates. - CharFmt { bold, italic, half_points: Some((size * 2.0).round() as u32), color, ..CharFmt::default() } -} - -/// A table's rows. Each cell ends at the right edge of the last grid column it spans, in twips. -fn table(t: &Table) -> craft_rtf::Table { - let mut edges = t.cols.clone(); - edges.push(t.rect[2]); - let cellx: Vec = edges.iter().skip(1).map(|e| (e * 20.0).round() as i32).collect(); - let rows = t - .rows - .iter() - .map(|row| { - let mut column = 0; - row.iter() - .map(|c| { - column += c.span.max(1); - let right = cellx.get(column.min(cellx.len()).saturating_sub(1)).copied().unwrap_or(0); - let run = Run { text: clean(&c.text), fmt: char_fmt(c.size, c.bold, c.italic, c.color) }; - Cell { right, paras: vec![Para { runs: vec![run], ..Para::default() }] } - }) - .collect() - }) - .collect(); - craft_rtf::Table { rows } -} - -/// Keep a break outside tables, including when no text follows it on its page. -fn standalone_page_break(blocks: &mut Vec) { - let run = Run { text: PAGE_BREAK.to_string(), fmt: CharFmt::default() }; - blocks.push(Block::Para(Para { runs: vec![run], ..Para::default() })); -} - -/// Rich Text Format: paragraphs with their sizes, bold, italic and colours, real table rows, page -/// breaks between pages (images are left out). +/// Rich Text Format: paragraphs with their sizes, bold, italic and colours, headings, tables, +/// pictures, and each page in a section of its own size. pub fn rtf(pages: &[Page]) -> String { - let mut blocks = Vec::new(); - // A page break waits for what follows it: RTF's `\page` is a character in a paragraph, so it - // starts the next one, or stands alone before a table. - let mut page_break = false; - for it in items(pages) { - match it { - Item::Para(runs, _) => { - let mut runs: Vec = runs - .iter() - .map(|r| Run { text: clean(&r.text), fmt: char_fmt(r.block.size, r.block.bold, r.block.italic, r.block.color) }) - .collect(); - let Some(first) = runs.first_mut() else { continue }; - if std::mem::take(&mut page_break) { - first.text.insert(0, PAGE_BREAK); - } - blocks.push(Block::Para(Para { runs, ..Para::default() })); - } - Item::Table(t) => { - if std::mem::take(&mut page_break) { - standalone_page_break(&mut blocks); - } - blocks.push(Block::Table(table(&t))); - } - Item::Img(_) => {} - Item::PageBreak => { - if page_break { - standalone_page_break(&mut blocks); - } - page_break = true; - } - } - } - if page_break { - standalone_page_break(&mut blocks); - } - craft_rtf::write(&Document { blocks, pictures_left_out: 0 }, "Helvetica") + rtf_with_title(pages, "") +} + +/// [`rtf`] with the document's title (`\info\title`). +pub fn rtf_with_title(pages: &[Page], title: &str) -> String { + craft_rtf::doc::export(&flow_doc(pages, &Meta { title, language: None, font: FONT })) } diff --git a/crates/export/src/as_word.rs b/crates/export/src/as_word.rs new file mode 100644 index 0000000000..176a244883 --- /dev/null +++ b/crates/export/src/as_word.rs @@ -0,0 +1,252 @@ +//! The pages as a `craft_wordmodel::Document`, the model craft-libs' word-processing writers take +//! (storytold/craft-libs#793): paragraphs with their runs and heading levels, tables, pictures, +//! and a section per page with that page's size. RTF writes it with craft-rtf's full writer, every +//! page flowing. + +use super::as_docx::{MAX_MEDIA, rgb8}; +use super::as_html::{allowed, language_tag}; +use super::layout::{Image, Item, Page, Table, items}; +use craft_wordmodel::para::{Float, OBJ}; +use craft_wordmodel::props::{Border, BorderStyle, Borders, CellProps, LineSpacing, TableLook, TableProps}; +use craft_wordmodel::{Block, CharProps, Document, InlineObject, ParaProps, Paragraph, Rgb, SectionProps, Style, StyleSheet, TextColor}; + +/// A run's formatting: its size, bold, italic and colour (none for black, the default). +fn chr(size: f64, bold: bool, italic: bool, color: [f64; 3]) -> CharProps { + let [r, g, b] = rgb8(color); + // Half points, as Word and RTF store sizes; one that is not finite gets the 12 pt default. + let half_points = if size.is_finite() { (size * 2.0).round().clamp(2.0, 3276.0) } else { 24.0 }; + CharProps { + size: Some(half_points as f32 / 2.0), + bold: bold.then_some(true), + italic: italic.then_some(true), + color: (r | g | b != 0).then_some(TextColor::Rgb(Rgb(r, g, b))), + ..CharProps::default() + } +} + +/// Appends `text` to `p` as a run, merged into the last one when the formatting is the same. Only +/// the characters Word and RTF readers accept, and never U+FFFC, the model's object mark: in the +/// PDF's text it is a character, not an object. Appending to the fields takes constant time, where +/// `Paragraph::insert_text` walks every run on each call. +fn push_text(p: &mut Paragraph, text: &str, props: CharProps) { + // A hard line ending in the text (LF, CR or CRLF) is a line break, the model's `\n`, which the + // writers keep inside its paragraph or cell (storytold/pdfcraft#4723). + let text = text.replace("\r\n", "\n").replace('\r', "\n"); + let text: String = text.chars().filter(|c| allowed(*c) && *c != OBJ).collect(); + if text.is_empty() { + return; + } + p.text.push_str(&text); + match p.runs.last_mut() { + Some(last) if last.props == props => last.len += text.len(), + _ => p.runs.push(craft_wordmodel::Run { len: text.len(), props }), + } +} + +/// Appends an object to `p` in constant time (`Paragraph::insert_object` counts the objects before +/// its offset). +fn push_object(p: &mut Paragraph, obj: InlineObject) { + let props = CharProps::default(); + p.text.push(OBJ); + p.objects.push(obj); + let len = OBJ.len_utf8(); + match p.runs.last_mut() { + Some(last) if last.props == props => last.len += len, + _ => p.runs.push(craft_wordmodel::Run { len, props }), + } +} + +/// A table: its grid from the column edges, ruled where the PDF draws rules, one paragraph per +/// cell. +fn table(t: &Table) -> craft_wordmodel::Table { + let mut edges = t.cols.clone(); + edges.push(t.rect[2]); + // Whole twips, at least 3 pt wide (Word's narrowest column), as `as_docx` writes them. + let widths: Vec = edges.windows(2).map(|w| (((w[1] - w[0]).max(20.0) * 20.0).round().clamp(60.0, 31680.0) / 20.0) as f32).collect(); + let [top, left, bottom, right] = t.ruled.unwrap_or_default(); + let inside = t.ruled.is_some(); + let rule = |drawn: bool| Some(if drawn { Border { style: BorderStyle::Single, width: 0.5, color: None, space: 0.0 } } else { Border::default() }); + let props = TableProps { + borders: Some(Borders { + top: rule(top), + left: rule(left), + bottom: rule(bottom), + right: rule(right), + between: rule(inside), + inside_v: rule(inside), + }), + // No table style, so none of its options: no header row, bands or first column. (craft-flow + // made the first row of a table with no style a header row from the default look.) + look: TableLook { header_row: false, total_row: false, banded_rows: false, first_column: false, last_column: false, banded_columns: false }, + ..TableProps::default() + }; + let rows = t + .rows + .iter() + .map(|row| { + let mut cells = Vec::new(); + let mut column = 0usize; + for c in row { + let span = c.span.max(1); + let w: f32 = widths.iter().skip(column).take(span).sum(); + let mut p = Paragraph::new(); + push_text(&mut p, &c.text, chr(c.size, c.bold, c.italic, c.color)); + cells.push(craft_wordmodel::Cell { + props: CellProps { width: Some(w), span: span as u32, ..CellProps::default() }, + blocks: vec![craft_wordmodel::para_block(p)], + ..Default::default() + }); + column = column.saturating_add(span); + } + // A short row is padded to the grid, as Word expects every row to fill it. + for w in widths.iter().skip(column) { + cells.push(craft_wordmodel::Cell { + props: CellProps { width: Some(*w), ..CellProps::default() }, + blocks: vec![craft_wordmodel::para_block(Paragraph::new())], + ..Default::default() + }); + } + craft_wordmodel::Row { cells, ..Default::default() } + }) + .collect(); + craft_wordmodel::Table { props, grid: widths, rows, ..Default::default() } +} + +/// A page's section: its size, with margins of an eighth of each side, at most an inch. +fn section(page: &Page) -> SectionProps { + let twips = |pt: f64| if pt.is_finite() { (pt * 20.0).round().clamp(144.0, 31680.0) as i64 } else { 12240 }; + let (w, h) = (twips(page.width), twips(page.height)); + let (mx, my) = ((w / 8).min(1440), (h / 8).min(1440)); + let pt = |t: i64| t as f32 / 20.0; + SectionProps { + page_w: pt(w), + page_h: pt(h), + landscape: false, + margin_top: pt(my), + margin_bottom: pt(my), + margin_left: pt(mx), + margin_right: pt(mx), + header: pt(my / 2), + footer: pt(my / 2), + ..SectionProps::default() + } +} + +/// The styles: the body text in `font`, in the document's language, and two heading levels. +fn styles(font: &str, language: Option<&str>) -> StyleSheet { + let mut s = StyleSheet { styles: Vec::new(), ..StyleSheet::default() }; + s.default_chr = CharProps { font: Some(font.to_string()), lang: language_tag(language).map(str::to_string), ..CharProps::default() }; + // As `as_docx` sets them: lines at 1.16 and 6 pt after each paragraph. + s.default_para = ParaProps { line_spacing: Some(LineSpacing::Multiple(1.16)), ..ParaProps::default() }; + s.default_para_present = true; + let normal = ParaProps { space_after: Some(6.0), ..ParaProps::default() }; + s.styles.push(Style { id: "Normal".into(), name: "Normal".into(), default: true, builtin: true, para: normal, ..Style::default() }); + for (lvl, before) in [(1u8, 12.0f32), (2, 10.0)] { + s.styles.push(Style { + id: format!("Heading{lvl}"), + name: format!("heading {lvl}"), + based_on: Some("Normal".into()), + next: Some("Normal".into()), + para: ParaProps { keep_next: Some(true), space_before: Some(before), outline_level: Some(lvl - 1), ..ParaProps::default() }, + chr: CharProps { bold: Some(true), ..CharProps::default() }, + builtin: true, + ..Style::default() + }); + } + s +} + +/// A paragraph holding a picture, scaled down to fit between the margins of `sec`. +fn picture(doc: &mut Document, im: &Image, sec: &SectionProps) -> Paragraph { + let text_w = f64::from((sec.page_w - sec.margin_left - sec.margin_right).max(1.0)); + let (w, h) = ((im.rect[2] - im.rect[0]).max(1.0), (im.rect[3] - im.rect[1]).max(1.0)); + let k = (text_w / w).min(1.0); + let media = doc.add_media(im.bytes.to_vec(), im.ext); + let mut p = Paragraph::new(); + push_object( + &mut p, + InlineObject::Image { + media, + w: (w * k) as f32, + h: (h * k) as f32, + alt: String::new(), + float: Float::default(), + crop: [0.0; 4], + fx: Default::default(), + ole: None, + }, + ); + p +} + +/// What the document holds besides its pages. +pub(crate) struct Meta<'a> { + pub title: &'a str, + /// The PDF's `/Lang`. + pub language: Option<&'a str>, + /// The body text's font. + pub font: &'a str, +} + +/// The pages as one flowing document: each page a section of its own size (so a page always +/// starts a new one, blank pages included), its paragraphs, tables and pictures in reading order. +pub(crate) fn flow_doc(pages: &[Page], meta: &Meta) -> Document { + let mut doc = Document::new(); + doc.styles = std::sync::Arc::new(styles(meta.font, meta.language)); + doc.core.title = meta.title.to_string(); + let mut body: Vec = Vec::new(); + let mut media = 0usize; + let mut page = 0usize; + let mut sec = pages.first().map(section).unwrap_or_default(); + for it in items(pages) { + match it { + Item::Para(runs, lvl) => { + let mut p = Paragraph::new(); + if matches!(lvl, 1 | 2) { + p.props.style = Some(format!("Heading{lvl}")); + } + for r in &runs { + push_text(&mut p, &r.text, chr(r.block.size, r.block.bold, r.block.italic, r.block.color)); + } + body.push(Block::Para(p)); + } + Item::Table(t) => { + // Tables that touch become one table: a paragraph keeps them apart. + if matches!(body.last(), Some(Block::Table(_))) { + body.push(Block::Para(Paragraph::new())); + } + body.push(Block::Table(table(&t))); + } + Item::Img(im) => { + if im.bytes.is_empty() || media >= MAX_MEDIA { + continue; + } + media += 1; + body.push(Block::Para(picture(&mut doc, im, &sec))); + } + Item::PageBreak => { + // The page ends its section; the next page's section starts on a new page. + let mut p = Paragraph::new(); + p.props = ParaProps { + space_before: Some(0.0), + space_after: Some(0.0), + line_spacing: Some(LineSpacing::Exactly(1.0)), + ..ParaProps::default() + }; + p.section = Some(Box::new(sec)); + body.push(Block::Para(p)); + page += 1; + sec = pages.get(page).map(section).unwrap_or_default(); + } + } + } + doc.last_section = sec; + // A background every page shares is the document's page colour. + let shared = pages.first().and_then(|p| p.background).map(rgb8); + if let Some(c) = shared.filter(|c| *c != [255; 3] && pages.iter().all(|p| p.background.map(rgb8) == Some(*c))) { + doc.settings.page_color = Some(Rgb(c[0], c[1], c[2])); + } + doc.body = body.into_iter().map(std::sync::Arc::new).collect(); + doc.ensure_nonempty(); + doc +} diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index 5f50122f90..6baa913126 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -20,6 +20,7 @@ mod as_docx; mod as_html; mod as_markdown; mod as_rtf; +mod as_word; mod clip; mod flow; mod layout; @@ -35,7 +36,7 @@ pub use as_html::{html, html_simple, html_simple_with_language, html_with_langua pub use as_markdown::markdown; #[cfg(test)] use as_markdown::{emphasis, paragraph}; -pub use as_rtf::rtf; +pub use as_rtf::{rtf, rtf_with_title}; pub use clip::{ClipBuilder, ClipSpan, ClipStyle, ClipText, MAX_CLIP_HTML_BYTES, MAX_CLIP_SPANS, MAX_CLIP_TEXT_BYTES, clip_html, font_family}; #[cfg(test)] use flow::list_item; diff --git a/crates/export/src/tests.rs b/crates/export/src/tests.rs index 68d3a569ee..f9ea2ba72a 100644 --- a/crates/export/src/tests.rs +++ b/crates/export/src/tests.rs @@ -388,50 +388,85 @@ fn rtf_escapes_and_sizes() { space_around: [false; 2], }); let r = rtf(&[p]); - assert!(r.starts_with("{\\rtf1") && r.ends_with('}')); + assert!(r.starts_with("{\\rtf1") && r.trim_end().ends_with('}')); assert!(r.contains("Caf\\u233? \\{x\\}"), "escaped: {r}"); let runs: Vec<_> = rtf_paras(&r).into_iter().flatten().collect(); assert!(runs.contains(&("Annual Report".into(), true, false, Some(48))), "{runs:?}"); assert!(runs.contains(&("Café {x}".into(), false, true, Some(20))), "{runs:?}"); } -/// A page break starts the next page's first paragraph, and stands in a paragraph of its own -/// before a table, rather than in the table's first cell. -#[test] -fn rtf_page_breaks_stay_out_of_tables() { - let r = rtf(&[page(), table_page(), page()]); - let back = craft_rtf::read(r.as_bytes()).unwrap(); - let first_cell = back.blocks.iter().find_map(|b| match b { - craft_rtf::Block::Table(t) => t.rows.first().and_then(|row| row.first()).map(|c| c.paras.iter().map(|p| p.text()).collect::()), - craft_rtf::Block::Para(_) => None, - }); - assert_eq!(first_cell.as_deref(), Some("Meter"), "{r}"); - let texts: Vec = back.blocks.iter().map(|b| if let craft_rtf::Block::Para(p) = b { p.text() } else { "".into() }).collect(); - let table = texts.iter().position(|t| t == "
"); - assert_eq!(table.and_then(|i| i.checked_sub(1)).and_then(|i| texts.get(i)).map(String::as_str), Some("\u{000C}"), "{texts:?}"); - assert_eq!(texts.iter().filter(|t| t.starts_with('\u{000C}')).count(), 2, "one break before each later page: {texts:?}"); +/// The RTF read back into the word model: each section's page size (points) and the text of its +/// blocks, tables as ``. +fn rtf_sections(r: &str) -> Vec<([f32; 2], Vec)> { + let doc = craft_rtf::doc::import(r.as_bytes()).unwrap(); + let mut out: Vec<([f32; 2], Vec)> = Vec::new(); + let mut texts = Vec::new(); + for b in &doc.body { + match &**b { + craft_wordmodel::Block::Para(p) => { + if !p.plain_text().is_empty() || !p.objects.is_empty() { + texts.push(if p.objects.is_empty() { p.plain_text() } else { "".into() }); + } + if let Some(s) = &p.section { + out.push(([s.page_w, s.page_h], std::mem::take(&mut texts))); + } + } + craft_wordmodel::Block::Table(t) => { + let first = t + .rows + .first() + .and_then(|r| r.cells.first()) + .map(|c| { + c.blocks + .iter() + .filter_map(|b| if let craft_wordmodel::Block::Para(p) = &**b { Some(p.plain_text()) } else { None }) + .collect::() + }) + .unwrap_or_default(); + texts.push(format!("", first.trim())); + } + } + } + out.push(([doc.last_section.page_w, doc.last_section.page_h], texts)); + out } -/// Empty and image-only pages still occupy a page when pictures are omitted from RTF. +/// Each page is a section of its own, so it starts a new page, and a table on a page is in that +/// page's section rather than after a break character in its first cell. #[test] -fn rtf_preserves_breaks_for_pages_without_text() { +fn rtf_pages_are_sections_and_tables_stay_on_theirs() { + let r = rtf(&[page(), table_page(), page()]); + let sections = rtf_sections(&r); + assert_eq!(sections.len(), 3, "{sections:?}"); + assert!(sections[1].1.first().is_some_and(|t| t == ""), "{sections:?}"); + assert!(!r.contains("\\page"), "sections break the pages: {r}"); +} + +/// Empty and image-only pages still occupy a page; each keeps its own size. +#[test] +fn rtf_keeps_pages_without_text() { let mut blank = page(); blank.blocks.clear(); blank.images.clear(); blank.rules.clear(); let mut image_only = page(); image_only.blocks.clear(); + let mut small = blank.clone(); + small.width = 400.0; + small.height = 500.0; for pages in [ vec![blank.clone(), blank.clone(), page()], vec![page(), blank.clone(), blank.clone()], vec![page(), blank.clone(), blank.clone(), table_page()], - vec![blank.clone(), blank.clone(), blank], + vec![blank.clone(), blank.clone(), blank.clone()], vec![page(), image_only.clone(), image_only.clone(), page()], vec![page(), image_only], + vec![page(), small, blank], ] { let r = rtf(&pages); - let breaks = rtf_paras(&r).iter().flatten().map(|run| run.0.matches(craft_rtf::PAGE_BREAK).count()).sum::(); - assert_eq!(breaks, pages.len() - 1, "{r}"); + let sizes: Vec<[f32; 2]> = rtf_sections(&r).iter().map(|s| s.0).collect(); + let want: Vec<[f32; 2]> = pages.iter().map(|p| [p.width as f32, p.height as f32]).collect(); + assert_eq!(sizes, want, "{r}"); } } @@ -501,8 +536,10 @@ fn grid_text_becomes_a_real_table_in_every_format() { let r = rtf(std::slice::from_ref(&p)); assert!(r.contains("\\trowd"), "{r}"); assert_eq!(r.matches("\\row").count(), 4); - // Each cell ends at its last column's right edge; "Totals" spans two columns, so it ends at - // the second of the three. + // Each cell ends at its last column's right edge, measured from the left margin (`\cellx`): + // the columns start at x = 72, 232 and 392 and the table ends at 496, so 160, 160 and 104 pt + // wide. "Totals" spans two columns, so it ends at the second edge, and its row is padded to + // the grid with an empty cell. let back = craft_rtf::read(r.as_bytes()).unwrap(); let rows: Vec> = back .blocks @@ -511,8 +548,8 @@ fn grid_text_becomes_a_real_table_in_every_format() { .flat_map(|t| &t.rows) .map(|row| row.iter().map(|c| (c.right, c.paras.iter().map(|p| p.text()).collect())).collect()) .collect(); - assert_eq!(rows.first().map(|r| r.iter().map(|c| c.0).collect::>()), Some(vec![4640, 7840, 9920]), "{rows:?}"); - assert!(rows.contains(&vec![(7840, "Totals".to_string())]), "{rows:?}"); + assert_eq!(rows.first().map(|r| r.iter().map(|c| c.0).collect::>()), Some(vec![3200, 6400, 8480]), "{rows:?}"); + assert!(rows.contains(&vec![(6400, "Totals".to_string()), (8480, String::new())]), "{rows:?}"); let pt = pptx(std::slice::from_ref(&p), "Bill"); assert!(pt.starts_with(b"PK")); @@ -1442,3 +1479,88 @@ fn shared_base64_keeps_padded_unwrapped_export_bytes() { assert!(!encoded.contains(['\r', '\n'])); assert!(encoded.ends_with("/w==")); } + +/// RTF keeps a page's pictures, inline where they sit in the reading order (it left them out). +#[test] +fn rtf_keeps_pictures() { + let r = rtf(&[page()]); + assert!(r.contains(r"\pngblip"), "{r}"); + let doc = craft_rtf::doc::import(r.as_bytes()).unwrap(); + let pictures = doc + .body + .iter() + .filter_map(|b| if let craft_wordmodel::Block::Para(p) = &**b { Some(p) } else { None }) + .flat_map(|p| &p.objects) + .filter(|o| matches!(o, craft_wordmodel::InlineObject::Image { .. })) + .count(); + assert_eq!(pictures, 1, "{r}"); +} + +/// Headings are Word's Heading 1 and 2 styles, which a word processor shows in its outline. +#[test] +fn rtf_headings_have_heading_styles() { + let r = rtf(&[page()]); + let doc = craft_rtf::doc::import(r.as_bytes()).unwrap(); + let heading = doc.body.iter().find_map(|b| match &**b { + craft_wordmodel::Block::Para(p) if p.plain_text() == "Annual Report" => Some(p.props.style.clone()), + _ => None, + }); + let style = heading.flatten().and_then(|id| doc.styles.styles.iter().find(|s| s.id == id).map(|s| s.para.outline_level)); + assert_eq!(style, Some(Some(0)), "Annual Report is a level 1 heading: {r}"); +} + +/// The document's title goes into the RTF's information group. +#[test] +fn rtf_names_the_title() { + let r = crate::rtf_with_title(&[page()], "Annual Report"); + assert_eq!(craft_rtf::doc::import(r.as_bytes()).unwrap().core.title, "Annual Report", "{r}"); +} + +/// Two tables one under the other stay two tables: in RTF, rows that follow each other are one +/// table, so a paragraph keeps them apart. +#[test] +fn rtf_keeps_tables_that_touch_apart() { + // Two grids on one page with nothing between them. + let mut one = table_page(); + one.blocks.retain(|b| !b.text.starts_with("Notes")); + let mut lower = one.blocks.clone(); + for b in &mut lower { + b.rect[1] -= 300.0; + b.rect[3] -= 300.0; + } + one.blocks.extend(lower); + let r = rtf(std::slice::from_ref(&one)); + let (found, _) = tables(&one.blocks, &one.rules); + assert_eq!(found.len(), 2, "the page has two grids"); + let read = craft_rtf::doc::import(r.as_bytes()).unwrap().body.iter().filter(|b| matches!(&***b, craft_wordmodel::Block::Table(_))).count(); + assert_eq!(read, 2, "{r}"); +} + +/// A hard line ending in the text (LF, CR or CRLF) is a line break inside its paragraph or table +/// cell, which RTF readers keep (storytold/pdfcraft#4723, by KyleKassen): they ignore a bare line +/// ending as whitespace. +#[test] +fn rtf_keeps_hard_line_breaks_in_paragraphs_and_cells() { + let mut paragraph = page(); + paragraph.blocks.truncate(1); + paragraph.blocks[0].text = "First\nSecond\r\nThird\rFourth".into(); + paragraph.blocks[0].italic = true; + paragraph.images.clear(); + let r = rtf(&[paragraph]); + let texts: Vec = rtf_sections(&r).into_iter().flat_map(|s| s.1).collect(); + assert_eq!(texts, ["First\nSecond\nThird\nFourth"], "one paragraph, four lines: {r}"); + + let mut table = table_page(); + table.blocks[0].text = "Meter\nnumber".into(); + table.blocks[4].text = "kWh\r\nunit".into(); + let r = rtf(&[table]); + let back = craft_rtf::read(r.as_bytes()).unwrap(); + let cells: Vec = back + .blocks + .iter() + .filter_map(|b| if let craft_rtf::Block::Table(t) = b { Some(t) } else { None }) + .flat_map(|t| t.rows.iter().flatten()) + .map(|c| c.paras.iter().map(|p| p.text()).collect()) + .collect(); + assert!(cells.contains(&"Meter\nnumber".to_string()) && cells.contains(&"kWh\nunit".to_string()), "{cells:?}"); +} diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml index 69e78702fc..b3808b68a9 100644 --- a/parity/acrobat-features.toml +++ b/parity/acrobat-features.toml @@ -7241,8 +7241,8 @@ milestone = "M10" status = "shipped" commands = ["export.rtf"] tools = ["doc_export_office"] -evidence = ["crates/export/src/tests.rs::table_spans_do_not_consume_text_they_do_not_emit", "crates/export/src/tests.rs::rtf_escapes_and_sizes", "crates/export/src/tests.rs::text_colour_survives_every_format", "crates/export/src/tests.rs::word_opens_text_with_control_characters_and_long_pages", "crates/automation/tests/it/automation.rs::exporting_to_word_html_and_rtf", "crates/ui-egui/tests/it/compare.rs::export_to_word_html_and_rtf", "crates/export/src/tests.rs::a_sentence_that_wraps_after_a_style_change_is_one_paragraph", "crates/export/src/tests.rs::a_bulleted_list_is_paragraphs_not_a_table"] -notes = "Text in slots covered by a preceding table-cell span remains in paragraph flow instead of being discarded. Paragraphs with sizes, bold, italic, text colour (colortbl), Unicode; a block that continues another on its line is a formatted group inside that paragraph (#4158), and a paragraph that wraps onto the next line after a style change runs on there, with the same rules and limits as Word export; a list whose bullets are blocks of their own is a paragraph per item, not a table, as in Word export; page breaks. Images are left out." +evidence = ["crates/export/src/tests.rs::table_spans_do_not_consume_text_they_do_not_emit", "crates/export/src/tests.rs::rtf_escapes_and_sizes", "crates/export/src/tests.rs::text_colour_survives_every_format", "crates/export/src/tests.rs::word_opens_text_with_control_characters_and_long_pages", "crates/automation/tests/it/automation.rs::exporting_to_word_html_and_rtf", "crates/ui-egui/tests/it/compare.rs::export_to_word_html_and_rtf", "crates/export/src/tests.rs::a_sentence_that_wraps_after_a_style_change_is_one_paragraph", "crates/export/src/tests.rs::a_bulleted_list_is_paragraphs_not_a_table", "crates/export/src/tests.rs::rtf_keeps_pictures", "crates/export/src/tests.rs::rtf_keeps_pages_without_text", "crates/export/src/tests.rs::rtf_headings_have_heading_styles", "crates/export/src/tests.rs::rtf_keeps_hard_line_breaks_in_paragraphs_and_cells"] +notes = "Text in slots covered by a preceding table-cell span remains in paragraph flow instead of being discarded. Paragraphs with sizes, bold, italic, text colour (colortbl), Unicode; a block that continues another on its line is a formatted group inside that paragraph (#4158), and a paragraph that wraps onto the next line after a style change runs on there, with the same rules and limits as Word export; a list whose bullets are blocks of their own is a paragraph per item, not a table, as in Word export; written by craft-rtf's full writer from the word model: each page a section of its own size (blank pages kept), pictures inline, Heading 1 and 2 styles, tables that touch kept apart, hard line breaks kept inside their paragraph or cell, and the title. Not kept: the PDF's language (the writer names English), and positioned pages flow." [[feature]] From 5555b1a2493e7263ec50af5cd1d22b9526af1642 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Mon, 16 Nov 2026 21:36:55 -0500 Subject: [PATCH 2/5] export: pin craft-libs main with push_text, the header-row fix and the RTF generator name craft-rtf, craft-wordmodel, craft-docx and craft-mime move to craft-libs b8eaa8e2, which has storytold/craft-libs#1946, #1947 and #1950. The mapping appends through Paragraph::push_text/push_object, tables keep the default look, and the RTF names PdfCraft as its generator. --- Cargo.lock | 18 ++++++++-------- Cargo.toml | 8 +++---- crates/export/src/as_rtf.rs | 5 ++++- crates/export/src/as_word.rs | 42 ++++++++---------------------------- crates/export/src/tests.rs | 6 ++++-- 5 files changed, 30 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d4328c509..2ea38f655b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1211,7 +1211,7 @@ dependencies = [ [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" [[package]] name = "craft-csv" @@ -1229,7 +1229,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8 [[package]] name = "craft-flow" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ "craft-wordmodel", "image", @@ -1307,9 +1307,9 @@ dependencies = [ [[package]] name = "craft-math" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "serde", ] @@ -1329,7 +1329,7 @@ dependencies = [ [[package]] name = "craft-metafile" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ "encoding_rs", "image", @@ -1340,7 +1340,7 @@ dependencies = [ [[package]] name = "craft-mime" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ "chrono", "craft-codepage", @@ -1446,7 +1446,7 @@ dependencies = [ [[package]] name = "craft-rtf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ "craft-codepage", "craft-flow", @@ -1555,7 +1555,7 @@ dependencies = [ [[package]] name = "craft-wordmodel" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ "base64 0.23.1", "craft-math", @@ -1569,7 +1569,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=892ef9116c432d7afb5c08aba93e93a4e0b1548f#892ef9116c432d7afb5c08aba93e93a4e0b1548f" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ "quick-xml 0.41.0", "serde", diff --git a/Cargo.toml b/Cargo.toml index eb1eb2bcf5..5d6c178550 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,16 +37,16 @@ craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0. craft-pdf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "8e03cd016d42032b867fef6bad86bc69bf2c1920", features = ["cos", "crypt", "filters"] } # The writers the Word and RTF exports map onto (storytold/craft-libs#793). craft-rtf, craft-wordmodel and # craft-docx move together: craft_rtf::doc::export takes its own revision's Document. -craft-rtf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "892ef9116c432d7afb5c08aba93e93a4e0b1548f" } +craft-rtf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" } # Shared Base64 encoder; use the existing RTF revision to reuse its codepage dependency. -craft-mime = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "892ef9116c432d7afb5c08aba93e93a4e0b1548f" } +craft-mime = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" } craft-csv = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-xml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-tiff = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-jpeg = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } -craft-docx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "892ef9116c432d7afb5c08aba93e93a4e0b1548f" } -craft-wordmodel = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "892ef9116c432d7afb5c08aba93e93a4e0b1548f" } +craft-docx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" } +craft-wordmodel = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" } craft-pptx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-drawingml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-opc = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } diff --git a/crates/export/src/as_rtf.rs b/crates/export/src/as_rtf.rs index 4ab7a39c2c..b5ae44d137 100644 --- a/crates/export/src/as_rtf.rs +++ b/crates/export/src/as_rtf.rs @@ -8,6 +8,9 @@ use super::layout::Page; /// it in its sans-serif, which the font table's `\fswiss` asks for). const FONT: &str = "Helvetica"; +/// The application the RTF names as its writer (`{\*\generator}`). +pub(crate) const GENERATOR: &str = "PdfCraft"; + /// Rich Text Format: paragraphs with their sizes, bold, italic and colours, headings, tables, /// pictures, and each page in a section of its own size. pub fn rtf(pages: &[Page]) -> String { @@ -16,5 +19,5 @@ pub fn rtf(pages: &[Page]) -> String { /// [`rtf`] with the document's title (`\info\title`). pub fn rtf_with_title(pages: &[Page], title: &str) -> String { - craft_rtf::doc::export(&flow_doc(pages, &Meta { title, language: None, font: FONT })) + craft_rtf::doc::export_with_generator(&flow_doc(pages, &Meta { title, language: None, font: FONT }), GENERATOR) } diff --git a/crates/export/src/as_word.rs b/crates/export/src/as_word.rs index 176a244883..01d9d3723c 100644 --- a/crates/export/src/as_word.rs +++ b/crates/export/src/as_word.rs @@ -6,8 +6,8 @@ use super::as_docx::{MAX_MEDIA, rgb8}; use super::as_html::{allowed, language_tag}; use super::layout::{Image, Item, Page, Table, items}; -use craft_wordmodel::para::{Float, OBJ}; -use craft_wordmodel::props::{Border, BorderStyle, Borders, CellProps, LineSpacing, TableLook, TableProps}; +use craft_wordmodel::para::Float; +use craft_wordmodel::props::{Border, BorderStyle, Borders, CellProps, LineSpacing, TableProps}; use craft_wordmodel::{Block, CharProps, Document, InlineObject, ParaProps, Paragraph, Rgb, SectionProps, Style, StyleSheet, TextColor}; /// A run's formatting: its size, bold, italic and colour (none for black, the default). @@ -24,36 +24,15 @@ fn chr(size: f64, bold: bool, italic: bool, color: [f64; 3]) -> CharProps { } } -/// Appends `text` to `p` as a run, merged into the last one when the formatting is the same. Only -/// the characters Word and RTF readers accept, and never U+FFFC, the model's object mark: in the -/// PDF's text it is a character, not an object. Appending to the fields takes constant time, where -/// `Paragraph::insert_text` walks every run on each call. +/// Appends `text` to `p` as a run (`Paragraph::push_text` drops U+FFFC, the model's object mark, +/// which in the PDF's text is a character, not an object), keeping only the characters Word and +/// RTF readers accept. fn push_text(p: &mut Paragraph, text: &str, props: CharProps) { // A hard line ending in the text (LF, CR or CRLF) is a line break, the model's `\n`, which the // writers keep inside its paragraph or cell (storytold/pdfcraft#4723). let text = text.replace("\r\n", "\n").replace('\r', "\n"); - let text: String = text.chars().filter(|c| allowed(*c) && *c != OBJ).collect(); - if text.is_empty() { - return; - } - p.text.push_str(&text); - match p.runs.last_mut() { - Some(last) if last.props == props => last.len += text.len(), - _ => p.runs.push(craft_wordmodel::Run { len: text.len(), props }), - } -} - -/// Appends an object to `p` in constant time (`Paragraph::insert_object` counts the objects before -/// its offset). -fn push_object(p: &mut Paragraph, obj: InlineObject) { - let props = CharProps::default(); - p.text.push(OBJ); - p.objects.push(obj); - let len = OBJ.len_utf8(); - match p.runs.last_mut() { - Some(last) if last.props == props => last.len += len, - _ => p.runs.push(craft_wordmodel::Run { len, props }), - } + let text: String = text.chars().filter(|c| allowed(*c)).collect(); + p.push_text(&text, &props); } /// A table: its grid from the column edges, ruled where the PDF draws rules, one paragraph per @@ -75,9 +54,6 @@ fn table(t: &Table) -> craft_wordmodel::Table { between: rule(inside), inside_v: rule(inside), }), - // No table style, so none of its options: no header row, bands or first column. (craft-flow - // made the first row of a table with no style a header row from the default look.) - look: TableLook { header_row: false, total_row: false, banded_rows: false, first_column: false, last_column: false, banded_columns: false }, ..TableProps::default() }; let rows = t @@ -163,8 +139,7 @@ fn picture(doc: &mut Document, im: &Image, sec: &SectionProps) -> Paragraph { let k = (text_w / w).min(1.0); let media = doc.add_media(im.bytes.to_vec(), im.ext); let mut p = Paragraph::new(); - push_object( - &mut p, + p.push_object( InlineObject::Image { media, w: (w * k) as f32, @@ -175,6 +150,7 @@ fn picture(doc: &mut Document, im: &Image, sec: &SectionProps) -> Paragraph { fx: Default::default(), ole: None, }, + &CharProps::default(), ); p } diff --git a/crates/export/src/tests.rs b/crates/export/src/tests.rs index f9ea2ba72a..c37bbdbb62 100644 --- a/crates/export/src/tests.rs +++ b/crates/export/src/tests.rs @@ -1509,11 +1509,13 @@ fn rtf_headings_have_heading_styles() { assert_eq!(style, Some(Some(0)), "Annual Report is a level 1 heading: {r}"); } -/// The document's title goes into the RTF's information group. +/// The document's title goes into the RTF's information group, and PdfCraft is named as the +/// application that wrote it. #[test] -fn rtf_names_the_title() { +fn rtf_names_the_title_and_pdfcraft() { let r = crate::rtf_with_title(&[page()], "Annual Report"); assert_eq!(craft_rtf::doc::import(r.as_bytes()).unwrap().core.title, "Annual Report", "{r}"); + assert!(r.contains(r"{\*\generator PdfCraft;}") && !r.contains("WordCraft"), "{r}"); } /// Two tables one under the other stay two tables: in RTF, rows that follow each other are one From 0b50bddc846bc07049e23863e8c1f879ec798cdd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 14:05:40 -0500 Subject: [PATCH 3/5] Bound RTF picture expansion and bake image rotations before embedding --- Cargo.lock | 1 + crates/export/Cargo.toml | 1 + crates/export/src/as_word.rs | 13 +- crates/export/src/as_word/pictures.rs | 234 ++++++++++++++++++++++++++ 4 files changed, 246 insertions(+), 3 deletions(-) create mode 100644 crates/export/src/as_word/pictures.rs diff --git a/Cargo.lock b/Cargo.lock index 2ea38f655b..d4574f9694 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4825,6 +4825,7 @@ dependencies = [ "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "craft-zip", "flate2", + "image", ] [[package]] diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index 93e16bb00e..388a5c5309 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -15,6 +15,7 @@ craft-mime.workspace = true craft-opc.workspace = true craft-rtf = { workspace = true, features = ["doc"] } craft-wordmodel.workspace = true +image = { version = "0.25", default-features = false, features = ["png", "jpeg"] } craft-xml.workspace = true craft-zip.workspace = true diff --git a/crates/export/src/as_word.rs b/crates/export/src/as_word.rs index 01d9d3723c..3c5dfb41e8 100644 --- a/crates/export/src/as_word.rs +++ b/crates/export/src/as_word.rs @@ -3,6 +3,8 @@ //! and a section per page with that page's size. RTF writes it with craft-rtf's full writer, every //! page flowing. +mod pictures; + use super::as_docx::{MAX_MEDIA, rgb8}; use super::as_html::{allowed, language_tag}; use super::layout::{Image, Item, Page, Table, items}; @@ -133,11 +135,11 @@ fn styles(font: &str, language: Option<&str>) -> StyleSheet { } /// A paragraph holding a picture, scaled down to fit between the margins of `sec`. -fn picture(doc: &mut Document, im: &Image, sec: &SectionProps) -> Paragraph { +fn picture(doc: &mut Document, im: &Image, bytes: Vec, ext: &str, sec: &SectionProps) -> Paragraph { let text_w = f64::from((sec.page_w - sec.margin_left - sec.margin_right).max(1.0)); let (w, h) = ((im.rect[2] - im.rect[0]).max(1.0), (im.rect[3] - im.rect[1]).max(1.0)); let k = (text_w / w).min(1.0); - let media = doc.add_media(im.bytes.to_vec(), im.ext); + let media = doc.add_media(bytes, ext); let mut p = Paragraph::new(); p.push_object( InlineObject::Image { @@ -167,6 +169,10 @@ pub(crate) struct Meta<'a> { /// The pages as one flowing document: each page a section of its own size (so a page always /// starts a new one, blank pages included), its paragraphs, tables and pictures in reading order. pub(crate) fn flow_doc(pages: &[Page], meta: &Meta) -> Document { + flow_doc_with_budget(pages, meta, pictures::Budget::default()) +} + +fn flow_doc_with_budget(pages: &[Page], meta: &Meta, mut pictures: pictures::Budget) -> Document { let mut doc = Document::new(); doc.styles = std::sync::Arc::new(styles(meta.font, meta.language)); doc.core.title = meta.title.to_string(); @@ -197,8 +203,9 @@ pub(crate) fn flow_doc(pages: &[Page], meta: &Meta) -> Document { if im.bytes.is_empty() || media >= MAX_MEDIA { continue; } + let Some((bytes, ext)) = pictures.prepare(im) else { continue }; media += 1; - body.push(Block::Para(picture(&mut doc, im, &sec))); + body.push(Block::Para(picture(&mut doc, im, bytes, ext, &sec))); } Item::PageBreak => { // The page ends its section; the next page's section starts on a new page. diff --git a/crates/export/src/as_word/pictures.rs b/crates/export/src/as_word/pictures.rs new file mode 100644 index 0000000000..f9a881a069 --- /dev/null +++ b/crates/export/src/as_word/pictures.rs @@ -0,0 +1,234 @@ +//! RTF repeats encoded image data at every placement. Bound that expansion before copying, +//! and bake rotations because its inline picture writer does not apply DrawingML transforms. +use super::Image; +use image::{ImageEncoder, RgbaImage}; +use std::io::{self, Write}; + +const MAX_ENCODED: usize = 512 << 20; +const MAX_PIXELS: u64 = 16 << 20; +const MAX_RASTER_WORK: u64 = 64 << 20; + +#[derive(Default)] +pub(super) struct Budget { + encoded: usize, + raster: u64, +} + +fn rtf_bytes(bytes: usize) -> Option { + bytes.checked_mul(2)?.checked_add(bytes.div_ceil(64))?.checked_add(256) +} + +impl Budget { + pub(super) fn prepare(&mut self, im: &Image) -> Option<(Vec, &'static str)> { + let rotation = im.rotation.rem_euclid(360.0); + if !rotation.is_finite() || !matches!(im.ext, "png" | "jpg" | "jpeg") { + return None; + } + let remaining = MAX_ENCODED.checked_sub(self.encoded)?; + if im.bytes.is_empty() || rtf_bytes(im.bytes.len())? > remaining { + return None; + } + let (bytes, ext) = if rotation == 0.0 { + (im.bytes.to_vec(), im.ext) + } else { + let (w, h) = image::ImageReader::new(io::Cursor::new(&im.bytes)).with_guessed_format().ok()?.into_dimensions().ok()?; + let pixels = u64::from(w).checked_mul(u64::from(h))?; + if w == 0 || h == 0 || w > 4096 || h > 4096 || pixels > MAX_PIXELS { + return None; + } + let [pw, ph] = im.size; + if !pw.is_finite() || !ph.is_finite() || pw <= 0.0 || ph <= 0.0 { + return None; + } + let (sin, cos) = rotation.to_radians().sin_cos(); + let bw = pw * cos.abs() + ph * sin.abs(); + let bh = pw * sin.abs() + ph * cos.abs(); + let density = (f64::from(w) / pw).min(f64::from(h) / ph); + // Exact quarter turns preserve every source pixel even when the PDF stretches + // the image anisotropically. Its physical aspect remains in the displayed rect. + let (ow, oh) = if rotation == 90.0 || rotation == 270.0 { + (f64::from(h), f64::from(w)) + } else if rotation == 180.0 { + (f64::from(w), f64::from(h)) + } else { + // Arbitrary angles use bounded nearest-neighbour resampling at the lower + // source density; unlike quarter turns this can lose detail when stretched. + ((bw * density).round().max(1.0), (bh * density).round().max(1.0)) + }; + if !ow.is_finite() || !oh.is_finite() || ow > 4096.0 || oh > 4096.0 { + return None; + } + let (ow, oh) = (ow as u32, oh as u32); + let output_pixels = u64::from(ow) * u64::from(oh); + if output_pixels > MAX_PIXELS { + return None; + } + self.raster = self.raster.checked_add(pixels)?.checked_add(output_pixels)?; + if self.raster > MAX_RASTER_WORK { + return None; + } + let mut limits = image::Limits::default(); + limits.max_image_width = Some(4096); + limits.max_image_height = Some(4096); + limits.max_alloc = Some(64 << 20); + let mut reader = image::ImageReader::new(io::Cursor::new(&im.bytes)).with_guessed_format().ok()?; + reader.limits(limits); + let source = reader.decode().ok()?.into_rgba8(); + let mut output = RgbaImage::new(ow, oh); + for (x, y, pixel) in output.enumerate_pixels_mut() { + let exact = if rotation == 90.0 { + Some((w - 1 - y, x)) + } else if rotation == 180.0 { + Some((w - 1 - x, h - 1 - y)) + } else if rotation == 270.0 { + Some((y, h - 1 - x)) + } else { + None + }; + if let Some((sx, sy)) = exact { + *pixel = *source.get_pixel(sx, sy); + continue; + } + let dx = (f64::from(x) + 0.5) * bw / f64::from(ow) - bw / 2.0; + let dy = (f64::from(y) + 0.5) * bh / f64::from(oh) - bh / 2.0; + // PDF rotation is counterclockwise in y-up space; raster rows are y-down. + let sx = (cos * dx - sin * dy + pw / 2.0) * f64::from(w) / pw; + let sy = (sin * dx + cos * dy + ph / 2.0) * f64::from(h) / ph; + if sx >= 0.0 && sy >= 0.0 && sx < f64::from(w) && sy < f64::from(h) { + *pixel = *source.get_pixel(sx as u32, sy as u32); + } + } + let mut sink = Limited { bytes: Vec::new(), cap: remaining.saturating_sub(256) / 3 }; + image::codecs::png::PngEncoder::new(&mut sink).write_image(output.as_raw(), ow, oh, image::ExtendedColorType::Rgba8).ok()?; + (sink.bytes, "png") + }; + self.encoded = self.encoded.checked_add(rtf_bytes(bytes.len())?)?; + if self.encoded > MAX_ENCODED { + return None; + } + Some((bytes, ext)) + } +} + +struct Limited { + bytes: Vec, + cap: usize, +} +impl Write for Limited { + fn write(&mut self, data: &[u8]) -> io::Result { + if data.len() > self.cap.saturating_sub(self.bytes.len()) { + return Err(io::Error::other("RTF picture byte budget")); + } + self.bytes.extend_from_slice(data); + Ok(data.len()) + } + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sample(rotation: f64) -> Image { + let pixels = RgbaImage::from_raw(2, 1, vec![255, 0, 0, 255, 0, 0, 255, 255]).expect("two pixels"); + let mut bytes = Vec::new(); + image::codecs::png::PngEncoder::new(&mut bytes).write_image(pixels.as_raw(), 2, 1, image::ExtendedColorType::Rgba8).expect("encode tiny PNG"); + Image { ext: "png", bytes: bytes.into(), rect: [0.0, 0.0, 1.0, 2.0], size: [2.0, 1.0], rotation } + } + + #[test] + fn pdf_counterclockwise_rotations_are_baked_into_asymmetric_pixels() { + for (angle, dims, first, last) in [ + (90.0, (1, 2), [0, 0, 255, 255], [255, 0, 0, 255]), + (180.0, (2, 1), [0, 0, 255, 255], [255, 0, 0, 255]), + (270.0, (1, 2), [255, 0, 0, 255], [0, 0, 255, 255]), + (-90.0, (1, 2), [255, 0, 0, 255], [0, 0, 255, 255]), + ] { + let (bytes, ext) = Budget::default().prepare(&sample(angle)).expect("rotated picture"); + assert_eq!(ext, "png"); + let pixels = image::load_from_memory(&bytes).expect("PNG").into_rgba8(); + assert_eq!(pixels.dimensions(), dims); + assert_eq!(pixels.get_pixel(0, 0).0, first); + assert_eq!(pixels.get_pixel(dims.0 - 1, dims.1 - 1).0, last); + } + } + + #[test] + fn quarter_turns_preserve_pixels_when_physical_size_is_stretched() { + for (angle, dims, first, last) in [ + (90.0, (1, 2), [0, 0, 255, 255], [255, 0, 0, 255]), + (180.0, (2, 1), [0, 0, 255, 255], [255, 0, 0, 255]), + (270.0, (1, 2), [255, 0, 0, 255], [0, 0, 255, 255]), + ] { + let mut im = sample(angle); + im.size = [1.0, 1.0]; + let (bytes, _) = Budget::default().prepare(&im).expect("stretched rotated picture"); + let pixels = image::load_from_memory(&bytes).expect("PNG").into_rgba8(); + assert_eq!(pixels.dimensions(), dims); + assert_eq!(pixels.get_pixel(0, 0).0, first); + assert_eq!(pixels.get_pixel(dims.0 - 1, dims.1 - 1).0, last); + } + } + + #[test] + fn flow_document_embeds_rotated_pixels_not_original_bytes() { + let page = + crate::layout::Page { width: 10.0, height: 10.0, blocks: Vec::new(), images: vec![sample(90.0)], background: None, rules: Vec::new() }; + let doc = super::super::flow_doc(&[page], &super::super::Meta { title: "", language: None, font: "Helvetica" }); + let bytes = doc.media.values().next().expect("picture retained"); + let pixels = image::load_from_memory(bytes).expect("PNG").into_rgba8(); + assert_eq!(pixels.dimensions(), (1, 2)); + assert_eq!(pixels.get_pixel(0, 0).0, [0, 0, 255, 255]); + assert_eq!(pixels.get_pixel(0, 1).0, [255, 0, 0, 255]); + } + + #[test] + fn repeated_allocations_pay_each_hex_expansion_before_copying() { + let im = sample(0.0); + let cost = rtf_bytes(im.bytes.len()).expect("small"); + let mut budget = Budget { encoded: MAX_ENCODED - cost * 2, raster: 0 }; + assert_eq!(budget.prepare(&im).expect("first").0, im.bytes.as_ref()); + assert!(budget.prepare(&im).is_some()); + assert!(budget.prepare(&im).is_none()); + assert_eq!(budget.encoded, MAX_ENCODED); + assert!(rtf_bytes(usize::MAX).is_none()); + } + + #[test] + fn flow_budget_counts_reused_media_at_each_placement() { + let im = sample(0.0); + let cost = rtf_bytes(im.bytes.len()).expect("small"); + let page = crate::layout::Page { width: 10.0, height: 10.0, blocks: Vec::new(), images: vec![im; 3], background: None, rules: Vec::new() }; + let doc = super::super::flow_doc_with_budget( + &[page], + &super::super::Meta { title: "", language: None, font: "Helvetica" }, + Budget { encoded: MAX_ENCODED - 2 * cost, raster: 0 }, + ); + let count: usize = doc + .body + .iter() + .map(|block| match &**block { + craft_wordmodel::Block::Para(p) => p.objects.len(), + _ => 0, + }) + .sum(); + assert_eq!(count, 2); + let rtf = craft_rtf::doc::export_with_generator(&doc, "PdfCraft"); + assert_eq!(rtf.matches("\\pngblip").count(), 2); + } + + #[test] + fn raster_work_and_encoded_writer_are_bounded() { + assert!(Budget { encoded: 0, raster: MAX_RASTER_WORK }.prepare(&sample(45.0)).is_none()); + assert!(Budget::default().prepare(&sample(f64::NAN)).is_none()); + let mut bad = sample(45.0); + bad.size[0] = f64::INFINITY; + assert!(Budget::default().prepare(&bad).is_none()); + let mut sink = Limited { bytes: Vec::new(), cap: 3 }; + assert!(sink.write_all(&[1, 2, 3]).is_ok()); + assert!(sink.write_all(&[4]).is_err()); + assert_eq!(sink.bytes, [1, 2, 3]); + } +} From ac8c44517d4a505e68834a7bdb956e89015c8d21 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 14:57:09 -0500 Subject: [PATCH 4/5] Fit RTF pictures within page bounds and report omitted media --- crates/engine/src/compare.rs | 13 +++++++--- crates/export/src/as_rtf.rs | 8 ++++++ crates/export/src/as_word.rs | 25 ++++++++++++++----- crates/export/src/as_word/pictures.rs | 35 +++++++++++++++++++++++++-- crates/export/src/lib.rs | 2 +- crates/export/src/tests.rs | 15 ++++++++++++ 6 files changed, 86 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/compare.rs b/crates/engine/src/compare.rs index d9dd0eae57..5d5dba94dc 100644 --- a/crates/engine/src/compare.rs +++ b/crates/engine/src/compare.rs @@ -197,7 +197,8 @@ pub struct OfficeExport { /// Pages (0-based) that draw forms nested deeper than [`MAX_FORM_DEPTH`] or more of them /// than [`MAX_FORM_VISITS`], or pictures past [`MAX_EXPORT_IMAGE_BYTES`] or past what one export /// may decode: export stops reading there to stay bounded on hostile files, so the text and images of those forms, or - /// those pictures, are missing from `bytes`. + /// those pictures, are missing from `bytes`. RTF also reports pages whose pictures could + /// not be encoded or fit its output/raster budgets; remaining page content is preserved. pub truncated_pages: Vec, } @@ -330,7 +331,7 @@ impl crate::Document { /// ([`OfficeExport::truncated_pages`]). pub fn export_office_checked(&self, format: OfficeFormat) -> OfficeExport { // Markdown leaves pictures out. - let (pages, truncated_pages) = self.export_pages_checked(!matches!(format, OfficeFormat::Markdown), MAX_EXPORT_IMAGE_BYTES); + let (pages, mut truncated_pages) = self.export_pages_checked(!matches!(format, OfficeFormat::Markdown), MAX_EXPORT_IMAGE_BYTES); let title = self.info.title.clone().unwrap_or_else(|| self.name.trim_end_matches(".pdf").to_string()); // Word and HTML name the document's language (`/Lang`), so spelling is checked in it. let language = self.initial_view().language; @@ -339,7 +340,13 @@ impl crate::Document { OfficeFormat::Pptx => pdfcraft_export::pptx(&pages, &title), OfficeFormat::Html => pdfcraft_export::html_with_language(&pages, &title, language.as_deref()).into_bytes(), OfficeFormat::HtmlSimple => pdfcraft_export::html_simple_with_language(&pages, &title, language.as_deref()).into_bytes(), - OfficeFormat::Rtf => pdfcraft_export::rtf_with_title(&pages, &title).into_bytes(), + OfficeFormat::Rtf => { + let (rtf, omitted) = pdfcraft_export::rtf_with_title_checked(&pages, &title); + truncated_pages.extend(omitted); + truncated_pages.sort_unstable(); + truncated_pages.dedup(); + rtf.into_bytes() + } OfficeFormat::Markdown => pdfcraft_export::markdown(&pages).into_bytes(), }; OfficeExport { bytes, truncated_pages } diff --git a/crates/export/src/as_rtf.rs b/crates/export/src/as_rtf.rs index b5ae44d137..93b6bd7849 100644 --- a/crates/export/src/as_rtf.rs +++ b/crates/export/src/as_rtf.rs @@ -21,3 +21,11 @@ pub fn rtf(pages: &[Page]) -> String { pub fn rtf_with_title(pages: &[Page], title: &str) -> String { craft_rtf::doc::export_with_generator(&flow_doc(pages, &Meta { title, language: None, font: FONT }), GENERATOR) } + +/// RTF plus zero-based pages where nonempty pictures could not be embedded. Page text and +/// other surviving content are retained; this is partial-output reporting, not a page drop. +pub fn rtf_with_title_checked(pages: &[Page], title: &str) -> (String, Vec) { + let mut omitted = Vec::new(); + let doc = super::as_word::flow_doc_with_omissions(pages, &Meta { title, language: None, font: FONT }, &mut omitted); + (craft_rtf::doc::export_with_generator(&doc, GENERATOR), omitted) +} diff --git a/crates/export/src/as_word.rs b/crates/export/src/as_word.rs index 3c5dfb41e8..cbadf9de5c 100644 --- a/crates/export/src/as_word.rs +++ b/crates/export/src/as_word.rs @@ -42,7 +42,7 @@ fn push_text(p: &mut Paragraph, text: &str, props: CharProps) { fn table(t: &Table) -> craft_wordmodel::Table { let mut edges = t.cols.clone(); edges.push(t.rect[2]); - // Whole twips, at least 3 pt wide (Word's narrowest column), as `as_docx` writes them. + // Whole twips, at least 20 pt wide before the Word-range clamp, as `as_docx` writes them. let widths: Vec = edges.windows(2).map(|w| (((w[1] - w[0]).max(20.0) * 20.0).round().clamp(60.0, 31680.0) / 20.0) as f32).collect(); let [top, left, bottom, right] = t.ruled.unwrap_or_default(); let inside = t.ruled.is_some(); @@ -138,7 +138,8 @@ fn styles(font: &str, language: Option<&str>) -> StyleSheet { fn picture(doc: &mut Document, im: &Image, bytes: Vec, ext: &str, sec: &SectionProps) -> Paragraph { let text_w = f64::from((sec.page_w - sec.margin_left - sec.margin_right).max(1.0)); let (w, h) = ((im.rect[2] - im.rect[0]).max(1.0), (im.rect[3] - im.rect[1]).max(1.0)); - let k = (text_w / w).min(1.0); + let text_h = f64::from((sec.page_h - sec.margin_top - sec.margin_bottom).max(1.0)); + let k = (text_w / w).min(text_h / h).min(1.0); let media = doc.add_media(bytes, ext); let mut p = Paragraph::new(); p.push_object( @@ -169,10 +170,14 @@ pub(crate) struct Meta<'a> { /// The pages as one flowing document: each page a section of its own size (so a page always /// starts a new one, blank pages included), its paragraphs, tables and pictures in reading order. pub(crate) fn flow_doc(pages: &[Page], meta: &Meta) -> Document { - flow_doc_with_budget(pages, meta, pictures::Budget::default()) + flow_doc_with_omissions(pages, meta, &mut Vec::new()) } -fn flow_doc_with_budget(pages: &[Page], meta: &Meta, mut pictures: pictures::Budget) -> Document { +pub(crate) fn flow_doc_with_omissions(pages: &[Page], meta: &Meta, omitted: &mut Vec) -> Document { + flow_doc_with_budget(pages, meta, pictures::Budget::default(), omitted) +} + +fn flow_doc_with_budget(pages: &[Page], meta: &Meta, mut pictures: pictures::Budget, omitted: &mut Vec) -> Document { let mut doc = Document::new(); doc.styles = std::sync::Arc::new(styles(meta.font, meta.language)); doc.core.title = meta.title.to_string(); @@ -200,10 +205,18 @@ fn flow_doc_with_budget(pages: &[Page], meta: &Meta, mut pictures: pictures::Bud body.push(Block::Table(table(&t))); } Item::Img(im) => { - if im.bytes.is_empty() || media >= MAX_MEDIA { + if im.bytes.is_empty() { continue; } - let Some((bytes, ext)) = pictures.prepare(im) else { continue }; + let prepared = (media < MAX_MEDIA).then(|| pictures.prepare(im)).flatten(); + let Some((bytes, ext)) = prepared else { + // Keep readable page content, but tell checked callers which pages lost + // pictures to encoding, transform, or resource limits. + if omitted.last() != Some(&page) { + omitted.push(page); + } + continue; + }; media += 1; body.push(Block::Para(picture(&mut doc, im, bytes, ext, &sec))); } diff --git a/crates/export/src/as_word/pictures.rs b/crates/export/src/as_word/pictures.rs index f9a881a069..4671f0926e 100644 --- a/crates/export/src/as_word/pictures.rs +++ b/crates/export/src/as_word/pictures.rs @@ -63,10 +63,12 @@ impl Budget { if output_pixels > MAX_PIXELS { return None; } - self.raster = self.raster.checked_add(pixels)?.checked_add(output_pixels)?; - if self.raster > MAX_RASTER_WORK { + let raster = self.raster.checked_add(pixels)?.checked_add(output_pixels)?; + if raster > MAX_RASTER_WORK { return None; } + // Charge only accepted work, but retain the charge if decoding later fails. + self.raster = raster; let mut limits = image::Limits::default(); limits.max_image_width = Some(4096); limits.max_image_height = Some(4096); @@ -201,11 +203,14 @@ mod tests { let im = sample(0.0); let cost = rtf_bytes(im.bytes.len()).expect("small"); let page = crate::layout::Page { width: 10.0, height: 10.0, blocks: Vec::new(), images: vec![im; 3], background: None, rules: Vec::new() }; + let mut omitted = Vec::new(); let doc = super::super::flow_doc_with_budget( &[page], &super::super::Meta { title: "", language: None, font: "Helvetica" }, Budget { encoded: MAX_ENCODED - 2 * cost, raster: 0 }, + &mut omitted, ); + assert_eq!(omitted, [0]); let count: usize = doc .body .iter() @@ -219,6 +224,32 @@ mod tests { assert_eq!(rtf.matches("\\pngblip").count(), 2); } + #[test] + fn rejected_raster_charge_leaves_room_for_a_smaller_picture() { + let mut budget = Budget { encoded: 0, raster: MAX_RASTER_WORK - 4 }; + assert!(budget.prepare(&sample(45.0)).is_none()); + assert_eq!(budget.raster, MAX_RASTER_WORK - 4); + assert!(budget.prepare(&sample(90.0)).is_some()); + assert_eq!(budget.raster, MAX_RASTER_WORK); + } + + #[test] + fn picture_fits_both_text_area_dimensions() { + let mut im = sample(0.0); + im.rect = [0.0, 0.0, 100.0, 1000.0]; + let section = craft_wordmodel::SectionProps { + page_w: 600.0, + page_h: 400.0, + margin_top: 50.0, + margin_bottom: 50.0, + margin_left: 50.0, + margin_right: 50.0, + ..Default::default() + }; + let p = super::super::picture(&mut craft_wordmodel::Document::new(), &im, im.bytes.to_vec(), im.ext, §ion); + assert!(matches!(p.objects.first(), Some(craft_wordmodel::InlineObject::Image { w, h, .. }) if *w == 30.0 && *h == 300.0)); + } + #[test] fn raster_work_and_encoded_writer_are_bounded() { assert!(Budget { encoded: 0, raster: MAX_RASTER_WORK }.prepare(&sample(45.0)).is_none()); diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index 6baa913126..7b44475c47 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -36,7 +36,7 @@ pub use as_html::{html, html_simple, html_simple_with_language, html_with_langua pub use as_markdown::markdown; #[cfg(test)] use as_markdown::{emphasis, paragraph}; -pub use as_rtf::{rtf, rtf_with_title}; +pub use as_rtf::{rtf, rtf_with_title, rtf_with_title_checked}; pub use clip::{ClipBuilder, ClipSpan, ClipStyle, ClipText, MAX_CLIP_HTML_BYTES, MAX_CLIP_SPANS, MAX_CLIP_TEXT_BYTES, clip_html, font_family}; #[cfg(test)] use flow::list_item; diff --git a/crates/export/src/tests.rs b/crates/export/src/tests.rs index d438db8899..c92ff23bb0 100644 --- a/crates/export/src/tests.rs +++ b/crates/export/src/tests.rs @@ -1578,3 +1578,18 @@ fn html_bounds_the_pictures_it_embeds() { assert_eq!(h.matches(" Date: Wed, 18 Nov 2026 18:07:30 -0500 Subject: [PATCH 5/5] Bound aggregate input work for rotated RTF picture probes --- crates/export/src/as_word/pictures.rs | 36 ++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/export/src/as_word/pictures.rs b/crates/export/src/as_word/pictures.rs index 61857575c4..d3b93d31ba 100644 --- a/crates/export/src/as_word/pictures.rs +++ b/crates/export/src/as_word/pictures.rs @@ -7,11 +7,13 @@ use std::io::{self, Write}; const MAX_ENCODED: usize = 512 << 20; const MAX_PIXELS: u64 = 16 << 20; const MAX_RASTER_WORK: u64 = 64 << 20; +const MAX_PROBE_INPUT: usize = 512 << 20; #[derive(Default)] pub(super) struct Budget { encoded: usize, raster: u64, + probe_input: usize, } fn rtf_bytes(bytes: usize) -> Option { @@ -31,6 +33,13 @@ impl Budget { let (bytes, ext) = if rotation == 0.0 { (im.bytes.to_vec(), im.ext) } else { + // JPEG dimension probes copy the input. Charge every attempt before probing, + // including failures, so repeated rejected placements cannot multiply that work. + let probe_input = self.probe_input.checked_add(im.bytes.len())?; + if probe_input > MAX_PROBE_INPUT { + return None; + } + self.probe_input = probe_input; let (w, h) = image::ImageReader::new(io::Cursor::new(&im.bytes)).with_guessed_format().ok()?.into_dimensions().ok()?; let pixels = u64::from(w).checked_mul(u64::from(h))?; if w == 0 || h == 0 || w > 4096 || h > 4096 || pixels > MAX_PIXELS { @@ -190,7 +199,7 @@ mod tests { fn repeated_allocations_pay_each_hex_expansion_before_copying() { let im = sample(0.0); let cost = rtf_bytes(im.bytes.len()).expect("small"); - let mut budget = Budget { encoded: MAX_ENCODED - cost * 2, raster: 0 }; + let mut budget = Budget { encoded: MAX_ENCODED - cost * 2, raster: 0, ..Default::default() }; assert_eq!(budget.prepare(&im).expect("first").0, im.bytes.as_ref()); assert!(budget.prepare(&im).is_some()); assert!(budget.prepare(&im).is_none()); @@ -206,7 +215,7 @@ mod tests { let (doc, omitted) = super::super::flow_doc_with_budget( &[page], &super::super::Meta { title: "", language: None, font: "Helvetica" }, - Budget { encoded: MAX_ENCODED - 2 * cost, raster: 0 }, + Budget { encoded: MAX_ENCODED - 2 * cost, raster: 0, ..Default::default() }, ); assert_eq!(omitted, [0]); let count: usize = doc @@ -224,13 +233,32 @@ mod tests { #[test] fn rejected_raster_charge_leaves_room_for_a_smaller_picture() { - let mut budget = Budget { encoded: 0, raster: MAX_RASTER_WORK - 4 }; + let mut budget = Budget { encoded: 0, raster: MAX_RASTER_WORK - 4, ..Default::default() }; assert!(budget.prepare(&sample(45.0)).is_none()); assert_eq!(budget.raster, MAX_RASTER_WORK - 4); assert!(budget.prepare(&sample(90.0)).is_some()); assert_eq!(budget.raster, MAX_RASTER_WORK); } + #[test] + fn rejected_probes_pay_input_budget_before_retrying() { + let mut im = sample(90.0); + im.ext = "jpg"; + im.bytes = vec![0xff, 0xd8, 0xff, 0xe0, 0, 2, 0xff, 0xd9].into(); + let cost = im.bytes.len(); + let mut budget = Budget { probe_input: MAX_PROBE_INPUT - 2 * cost, ..Default::default() }; + assert!(budget.prepare(&im).is_none()); + assert_eq!(budget.probe_input, MAX_PROBE_INPUT - cost); + assert!(budget.prepare(&im).is_none()); + assert_eq!(budget.probe_input, MAX_PROBE_INPUT); + assert!(budget.prepare(&im).is_none()); + assert_eq!(budget.probe_input, MAX_PROBE_INPUT); + assert_eq!(budget.encoded, 0); + assert_eq!(budget.raster, 0); + // Exhausting rotated probes must not suppress unrotated byte-preserving pictures. + assert!(budget.prepare(&sample(0.0)).is_some()); + } + #[test] fn picture_fits_both_text_area_dimensions() { let mut im = sample(0.0); @@ -250,7 +278,7 @@ mod tests { #[test] fn raster_work_and_encoded_writer_are_bounded() { - assert!(Budget { encoded: 0, raster: MAX_RASTER_WORK }.prepare(&sample(45.0)).is_none()); + assert!(Budget { encoded: 0, raster: MAX_RASTER_WORK, ..Default::default() }.prepare(&sample(45.0)).is_none()); assert!(Budget::default().prepare(&sample(f64::NAN)).is_none()); let mut bad = sample(45.0); bad.size[0] = f64::INFINITY;