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]]