From 147b3f475056967353606b90fa0dc8bb39de7f27 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 9 Nov 2026 06:30:13 -0500 Subject: [PATCH 001/153] Release 0.19.0 --- Cargo.lock | 42 +++++++++++++++++++++--------------------- Cargo.toml | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3bc1e94a2..2cef88cfb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1306,7 +1306,7 @@ dependencies = [ [[package]] name = "designcraft" -version = "0.17.0" +version = "0.19.0" dependencies = [ "arboard", "craft-update", @@ -1331,7 +1331,7 @@ dependencies = [ [[package]] name = "designcraft-cli" -version = "0.17.0" +version = "0.19.0" dependencies = [ "designcraft-color", "designcraft-compose", @@ -1348,7 +1348,7 @@ dependencies = [ [[package]] name = "designcraft-color" -version = "0.17.0" +version = "0.19.0" dependencies = [ "craft-cms", "kurbo", @@ -1360,7 +1360,7 @@ dependencies = [ [[package]] name = "designcraft-compose" -version = "0.17.0" +version = "0.19.0" dependencies = [ "designcraft-color", "designcraft-doc", @@ -1380,7 +1380,7 @@ dependencies = [ [[package]] name = "designcraft-doc" -version = "0.17.0" +version = "0.19.0" dependencies = [ "chrono", "designcraft-color", @@ -1393,7 +1393,7 @@ dependencies = [ [[package]] name = "designcraft-engine" -version = "0.17.0" +version = "0.19.0" dependencies = [ "caseless", "craft-atomic", @@ -1433,7 +1433,7 @@ dependencies = [ [[package]] name = "designcraft-epub" -version = "0.17.0" +version = "0.19.0" dependencies = [ "chrono", "designcraft-color", @@ -1443,7 +1443,7 @@ dependencies = [ [[package]] name = "designcraft-fonts" -version = "0.17.0" +version = "0.19.0" dependencies = [ "core-text", "harfrust 0.13.3", @@ -1456,7 +1456,7 @@ dependencies = [ [[package]] name = "designcraft-format" -version = "0.17.0" +version = "0.19.0" dependencies = [ "designcraft-doc", "designcraft-images", @@ -1468,7 +1468,7 @@ dependencies = [ [[package]] name = "designcraft-geom" -version = "0.17.0" +version = "0.19.0" dependencies = [ "flo_curves", "kurbo", @@ -1481,7 +1481,7 @@ dependencies = [ [[package]] name = "designcraft-idml" -version = "0.17.0" +version = "0.19.0" dependencies = [ "designcraft-color", "designcraft-compose", @@ -1496,7 +1496,7 @@ dependencies = [ [[package]] name = "designcraft-images" -version = "0.17.0" +version = "0.19.0" dependencies = [ "craft-cms", "designcraft-color", @@ -1514,7 +1514,7 @@ dependencies = [ [[package]] name = "designcraft-mcp" -version = "0.17.0" +version = "0.19.0" dependencies = [ "designcraft-engine", "designcraft-geom", @@ -1527,7 +1527,7 @@ dependencies = [ [[package]] name = "designcraft-pdf" -version = "0.17.0" +version = "0.19.0" dependencies = [ "chrono", "craft-cms", @@ -1553,7 +1553,7 @@ dependencies = [ [[package]] name = "designcraft-pptx" -version = "0.17.0" +version = "0.19.0" dependencies = [ "chrono", "designcraft-color", @@ -1571,7 +1571,7 @@ dependencies = [ [[package]] name = "designcraft-render" -version = "0.17.0" +version = "0.19.0" dependencies = [ "craft-pdf", "designcraft-color", @@ -1590,7 +1590,7 @@ dependencies = [ [[package]] name = "designcraft-textimport" -version = "0.17.0" +version = "0.19.0" dependencies = [ "designcraft-doc", "quick-xml 0.41.0", @@ -1599,7 +1599,7 @@ dependencies = [ [[package]] name = "designcraft-tools" -version = "0.17.0" +version = "0.19.0" dependencies = [ "designcraft-color", "designcraft-compose", @@ -1611,7 +1611,7 @@ dependencies = [ [[package]] name = "designcraft-ui-egui" -version = "0.17.0" +version = "0.19.0" dependencies = [ "craft-dialogs", "craft-keymap", @@ -1645,7 +1645,7 @@ dependencies = [ [[package]] name = "designcraft-web" -version = "0.17.0" +version = "0.19.0" dependencies = [ "designcraft-engine", "designcraft-format", @@ -7679,7 +7679,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" [[package]] name = "xtask" -version = "0.17.0" +version = "0.19.0" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 6a07b01b1..5a4143f12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"] default-members = ["crates/*", "apps/designcraft", "apps/designcraft-cli", "xtask"] [workspace.package] -version = "0.17.0" +version = "0.19.0" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.95" From 741ccbbabb7bde2a60c1f40d2f32b2b2a71477dd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 14:27:23 -0500 Subject: [PATCH 002/153] Release 0.20.1: repair Flatpak update checks and self-updates (#2627) * Release 0.20.1: fix Flatpak update permissions * test: avoid unchecked option parsing in Flatpak regression --- Cargo.lock | 42 +++++++++---------- Cargo.toml | 2 +- crates/ui-egui/tests/flatpak_updates.rs | 27 ++++++++++++ .../ai.storyteller.designcraft.bundle.yml | 2 + .../flatpak/ai.storyteller.designcraft.yml | 2 + 5 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 crates/ui-egui/tests/flatpak_updates.rs diff --git a/Cargo.lock b/Cargo.lock index 5505fc5f3..48cd5dd5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1306,7 +1306,7 @@ dependencies = [ [[package]] name = "designcraft" -version = "0.20.0" +version = "0.20.1" dependencies = [ "arboard", "craft-update", @@ -1331,7 +1331,7 @@ dependencies = [ [[package]] name = "designcraft-cli" -version = "0.20.0" +version = "0.20.1" dependencies = [ "designcraft-color", "designcraft-compose", @@ -1348,7 +1348,7 @@ dependencies = [ [[package]] name = "designcraft-color" -version = "0.20.0" +version = "0.20.1" dependencies = [ "craft-cms", "kurbo", @@ -1360,7 +1360,7 @@ dependencies = [ [[package]] name = "designcraft-compose" -version = "0.20.0" +version = "0.20.1" dependencies = [ "designcraft-color", "designcraft-doc", @@ -1380,7 +1380,7 @@ dependencies = [ [[package]] name = "designcraft-doc" -version = "0.20.0" +version = "0.20.1" dependencies = [ "chrono", "designcraft-color", @@ -1393,7 +1393,7 @@ dependencies = [ [[package]] name = "designcraft-engine" -version = "0.20.0" +version = "0.20.1" dependencies = [ "caseless", "craft-atomic", @@ -1433,7 +1433,7 @@ dependencies = [ [[package]] name = "designcraft-epub" -version = "0.20.0" +version = "0.20.1" dependencies = [ "chrono", "designcraft-color", @@ -1443,7 +1443,7 @@ dependencies = [ [[package]] name = "designcraft-fonts" -version = "0.20.0" +version = "0.20.1" dependencies = [ "core-text", "harfrust 0.13.3", @@ -1456,7 +1456,7 @@ dependencies = [ [[package]] name = "designcraft-format" -version = "0.20.0" +version = "0.20.1" dependencies = [ "designcraft-doc", "designcraft-images", @@ -1468,7 +1468,7 @@ dependencies = [ [[package]] name = "designcraft-geom" -version = "0.20.0" +version = "0.20.1" dependencies = [ "flo_curves", "kurbo", @@ -1481,7 +1481,7 @@ dependencies = [ [[package]] name = "designcraft-idml" -version = "0.20.0" +version = "0.20.1" dependencies = [ "designcraft-color", "designcraft-compose", @@ -1496,7 +1496,7 @@ dependencies = [ [[package]] name = "designcraft-images" -version = "0.20.0" +version = "0.20.1" dependencies = [ "craft-cms", "designcraft-color", @@ -1514,7 +1514,7 @@ dependencies = [ [[package]] name = "designcraft-mcp" -version = "0.20.0" +version = "0.20.1" dependencies = [ "designcraft-engine", "designcraft-geom", @@ -1527,7 +1527,7 @@ dependencies = [ [[package]] name = "designcraft-pdf" -version = "0.20.0" +version = "0.20.1" dependencies = [ "chrono", "craft-cms", @@ -1553,7 +1553,7 @@ dependencies = [ [[package]] name = "designcraft-pptx" -version = "0.20.0" +version = "0.20.1" dependencies = [ "chrono", "designcraft-color", @@ -1571,7 +1571,7 @@ dependencies = [ [[package]] name = "designcraft-render" -version = "0.20.0" +version = "0.20.1" dependencies = [ "craft-pdf", "designcraft-color", @@ -1590,7 +1590,7 @@ dependencies = [ [[package]] name = "designcraft-textimport" -version = "0.20.0" +version = "0.20.1" dependencies = [ "designcraft-doc", "quick-xml 0.41.0", @@ -1599,7 +1599,7 @@ dependencies = [ [[package]] name = "designcraft-tools" -version = "0.20.0" +version = "0.20.1" dependencies = [ "designcraft-color", "designcraft-compose", @@ -1611,7 +1611,7 @@ dependencies = [ [[package]] name = "designcraft-ui-egui" -version = "0.20.0" +version = "0.20.1" dependencies = [ "craft-dialogs", "craft-keymap", @@ -1645,7 +1645,7 @@ dependencies = [ [[package]] name = "designcraft-web" -version = "0.20.0" +version = "0.20.1" dependencies = [ "designcraft-engine", "designcraft-format", @@ -7679,7 +7679,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" [[package]] name = "xtask" -version = "0.20.0" +version = "0.20.1" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index ee27e5fbd..496998bcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"] default-members = ["crates/*", "apps/designcraft", "apps/designcraft-cli", "xtask"] [workspace.package] -version = "0.20.0" +version = "0.20.1" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.95" diff --git a/crates/ui-egui/tests/flatpak_updates.rs b/crates/ui-egui/tests/flatpak_updates.rs new file mode 100644 index 000000000..01d835f42 --- /dev/null +++ b/crates/ui-egui/tests/flatpak_updates.rs @@ -0,0 +1,27 @@ +//! Release and source Flatpaks must allow release checks and host update helpers. +fn finish_args(manifest: &str) -> Vec<&str> { + manifest + .split("finish-args:") + .nth(1) + .unwrap_or_default() + .lines() + .take_while(|line| line.is_empty() || line.starts_with(' ') || line.starts_with('#')) + .filter_map(|line| line.trim().strip_prefix("- ")) + .map(|arg| arg.trim_matches(['\'', '"'])) + .collect() +} + +#[test] +fn flatpak_update_permissions() { + let manifests = [ + include_str!("../../../packaging/linux/flatpak/ai.storyteller.designcraft.bundle.yml"), + include_str!("../../../packaging/linux/flatpak/ai.storyteller.designcraft.yml"), + ]; + let args = manifests.map(finish_args); + assert_eq!(args[0], args[1], "source and release permissions must agree"); + for permissions in args { + for required in ["--share=network", "--talk-name=org.freedesktop.Flatpak"] { + assert!(permissions.contains(&required), "missing update permission: {required}"); + } + } +} diff --git a/packaging/linux/flatpak/ai.storyteller.designcraft.bundle.yml b/packaging/linux/flatpak/ai.storyteller.designcraft.bundle.yml index 750400657..eac2602c6 100644 --- a/packaging/linux/flatpak/ai.storyteller.designcraft.bundle.yml +++ b/packaging/linux/flatpak/ai.storyteller.designcraft.bundle.yml @@ -17,6 +17,8 @@ runtime-version: '25.08' sdk: org.freedesktop.Sdk command: designcraft finish-args: + # The shared updater inspects and updates this installation via flatpak-spawn --host. + - --talk-name=org.freedesktop.Flatpak - --filesystem=xdg-data/ArtCraft/versions:ro # Display: Wayland first; X11 only when no Wayland compositor is running. - --socket=wayland diff --git a/packaging/linux/flatpak/ai.storyteller.designcraft.yml b/packaging/linux/flatpak/ai.storyteller.designcraft.yml index fd56910ce..4cbee1c12 100644 --- a/packaging/linux/flatpak/ai.storyteller.designcraft.yml +++ b/packaging/linux/flatpak/ai.storyteller.designcraft.yml @@ -18,6 +18,8 @@ sdk-extensions: - org.freedesktop.Sdk.Extension.rust-stable command: designcraft finish-args: + # The shared updater inspects and updates this installation via flatpak-spawn --host. + - --talk-name=org.freedesktop.Flatpak - --filesystem=xdg-data/ArtCraft/versions:ro # Display: Wayland first; X11 only when no Wayland compositor is running. - --socket=wayland From 7a0c256601c55240ab47cd46c3d2adbc02f24e31 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 19:33:39 -0500 Subject: [PATCH 003/153] Move native bundle and EPUB packaging to craft-libs --- Cargo.lock | 50 ++++- Cargo.toml | 3 + crates/epub/Cargo.toml | 4 +- crates/epub/src/chapters.rs | 2 + crates/epub/src/lib.rs | 3 - crates/epub/src/package.rs | 325 +++++++++++---------------- crates/epub/src/tests.rs | 20 -- crates/format/Cargo.toml | 7 +- crates/format/src/lib.rs | 423 +++++++++--------------------------- crates/format/src/tests.rs | 100 +-------- 10 files changed, 290 insertions(+), 647 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9760f61ff..8229e7d80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1035,6 +1035,32 @@ name = "craft-docpath" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +[[package]] +name = "craft-document-bundle" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +dependencies = [ + "craft-zip", + "serde_json", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-eps" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" + +[[package]] +name = "craft-epub" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +dependencies = [ + "craft-xml", + "craft-zip", + "thiserror 2.0.21", +] + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -1181,6 +1207,25 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + [[package]] name = "crc32fast" version = "1.5.3" @@ -1436,7 +1481,7 @@ dependencies = [ name = "designcraft-epub" version = "0.20.1" dependencies = [ - "chrono", + "craft-epub", "designcraft-color", "designcraft-doc", "zip 3.0.0", @@ -1459,8 +1504,9 @@ dependencies = [ name = "designcraft-format" version = "0.20.1" dependencies = [ + "craft-document-bundle", + "craft-eps", "designcraft-doc", - "designcraft-images", "serde", "serde_json", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index dcf2c7235..702ff232c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,9 @@ repository = "https://github.com/storytold/designcraft" [workspace.dependencies] # Shared reader and writer; publish the immutable revision before this consumer. craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false } +craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "dc20b58f0f6059bd408d6f14ab130f343", version = "=0.1.0" } +craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "dc20b58f0f6059bd408d6f14ab130f343", version = "=0.1.0" } +craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "dc20b58f0f6059bd408d6f14ab130f343", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/epub/Cargo.toml b/crates/epub/Cargo.toml index 2836b0a92..96a95faf1 100644 --- a/crates/epub/Cargo.toml +++ b/crates/epub/Cargo.toml @@ -9,7 +9,9 @@ description = "DesignCraft EPUB 3 (reflowable) export: stories in reading order, workspace = true [dependencies] +craft-epub = { workspace = true } designcraft-doc = { workspace = true } designcraft-color = { workspace = true } + +[dev-dependencies] zip = { workspace = true } -chrono = { workspace = true } diff --git a/crates/epub/src/chapters.rs b/crates/epub/src/chapters.rs index ede96f390..93afea958 100644 --- a/crates/epub/src/chapters.rs +++ b/crates/epub/src/chapters.rs @@ -1,5 +1,6 @@ use super::css::{export_mapping, para_classes}; use super::html::Block; +#[cfg(test)] use super::markup::xml_text; use super::*; @@ -118,6 +119,7 @@ pub(crate) fn toc_entries(doc: &Document, order: &[Block]) -> Vec<(String, Strin /// The table of contents as nested `li`/`ol` items (the content of the navigation's outer /// `ol`): a deeper level goes into the previous item, skipped levels count as one. +#[cfg(test)] pub(crate) fn nav_list(entries: &[(String, String, u8)], files: &BTreeMap) -> String { let mut out = String::new(); let mut open: Vec = Vec::new(); diff --git a/crates/epub/src/lib.rs b/crates/epub/src/lib.rs index c102a54fe..6fa60cd08 100644 --- a/crates/epub/src/lib.rs +++ b/crates/epub/src/lib.rs @@ -11,7 +11,6 @@ use std::collections::BTreeMap; use std::fmt::Write as _; -use std::io::Write; use designcraft_doc::geom::{Affine, Rect}; use designcraft_doc::{ @@ -39,8 +38,6 @@ use fonts::{css_weight_style, generic_family}; use html::base64; pub use html::{Block, HtmlOptions, export_html, images_without_alt, reading_order}; pub use markup::{EpubError, slug}; -#[cfg(test)] -use package::iso8601; pub use package::{FixedPage, contents_title, export_epub, export_fixed_epub}; pub use story_writer::story_html; diff --git a/crates/epub/src/package.rs b/crates/epub/src/package.rs index 63e0399f0..9b33d356b 100644 --- a/crates/epub/src/package.rs +++ b/crates/epub/src/package.rs @@ -1,72 +1,16 @@ -use super::chapters::{chapter_file, link_across_chapters, nav_list, split_chapters}; +use std::borrow::Cow; + +use super::chapters::{chapter_file, link_across_chapters, split_chapters}; use super::css::stylesheet_with; use super::fonts::font_file; use super::html::{Body, body_html}; use super::markup::{EpubError, language_tag, story_markup, xml_attr, xml_text}; -use super::*; - -/// An ISO 8601 UTC timestamp (`2026-10-13T09:30:00Z`) for Unix seconds `t`. -/// A time outside chrono's year range still converts: the calendar repeats every 400 years, so the -/// instant is moved into range by whole cycles and only the year is carried (matches the old -/// `civil_from_days`), rather than resetting to the epoch. -pub(crate) fn iso8601(t: i64) -> String { - let (y, dt) = designcraft_doc::time::utc_parts(t, 0); - // `{:04}` rather than `%Y`, which writes a `+` before years past 9999. - format!("{y:04}-{}", dt.format("%m-%dT%H:%M:%SZ")) -} - -/// The package's `dcterms:modified`: the given time, else the document's last save or creation. -fn modified(doc: &Document, opts: &EpubOptions) -> String { - let t = opts.modified.or((doc.modified > 0).then_some(doc.modified)).or((doc.created > 0).then_some(doc.created)); - t.map_or_else(|| "2026-01-01T00:00:00Z".to_string(), iso8601) -} - -/// Characters of the title the package keeps: it is copied into every content file, so a huge one -/// would multiply by the number of chapters or pages. -const MAX_TITLE_CHARS: usize = 1024; - -fn capped_title(title: String) -> String { - match title.char_indices().nth(MAX_TITLE_CHARS) { - Some((at, _)) => title[..at].to_string(), - None => title, - } -} - -/// The book's identifier: the given one, else a `urn:uuid` derived from the title and the -/// document's creation time, so a book keeps its identifier from export to export and two books -/// with the same title don't share one. -fn identifier(doc: &Document, opts: &EpubOptions, title: &str) -> String { - if let Some(id) = opts.identifier.clone().filter(|i| !i.trim().is_empty()) { - return id; - } - // Two FNV-1a 64-bit hashes (different offsets) as 128 bits, shaped as a version-5-style UUID. - let seed = format!("{title}\n{}", doc.created); - let fnv = |offset: u64| seed.bytes().fold(offset, |h, b| (h ^ u64::from(b)).wrapping_mul(0x0000_0100_0000_01b3)); - let (a, b) = (fnv(0xcbf2_9ce4_8422_2325), fnv(0x84222325_cbf29ce4)); - let hi = (a & 0xffff_ffff_ffff_0fff) | 0x5000; - let lo = (b & 0x3fff_ffff_ffff_ffff) | 0x8000_0000_0000_0000; - format!("urn:uuid:{:08x}-{:04x}-{:04x}-{:04x}-{:012x}", hi >> 32, (hi >> 16) & 0xffff, hi & 0xffff, lo >> 48, lo & 0xffff_ffff_ffff) -} - -/// Package name and media type of a cover image: JPEG by its signature, else PNG. -fn cover_file(data: &[u8]) -> (&'static str, &'static str) { - if data.starts_with(&[0xFF, 0xD8, 0xFF]) { ("images/cover.jpg", "image/jpeg") } else { ("images/cover.png", "image/png") } -} +use super::{Document, EpubOptions}; /// The table of contents' title in `language` (a BCP 47 tag or a language name's code), English /// for languages without one here. pub fn contents_title(language: &str) -> &'static str { - let code = language.split(['-', '_']).next().unwrap_or("").to_ascii_lowercase(); - match code.as_str() { - "nl" => "Inhoud", - "de" => "Inhalt", - "fr" => "Table des matières", - "es" | "pt" => "Índice", - "it" => "Indice", - "ja" => "目次", - "zh" => "目录", - _ => "Contents", - } + craft_epub::contents_title(language) } /// A page of a fixed-layout EPUB: its image (PNG), size in points and text (kept for search, @@ -79,105 +23,85 @@ pub struct FixedPage { pub text: String, } +fn capped_title(title: String) -> String { + match title.char_indices().nth(1024) { + Some((at, _)) => title.get(..at).unwrap_or(&title).to_owned(), + None => title, + } +} + +fn identifier(doc: &Document, opts: &EpubOptions, title: &str) -> String { + opts.identifier.clone().filter(|id| !id.trim().is_empty()).unwrap_or_else(|| craft_epub::urn_uuid(&format!("{title}\n{}", doc.created))) +} + +fn modified(doc: &Document, opts: &EpubOptions) -> String { + let timestamp = opts.modified.or((doc.modified > 0).then_some(doc.modified)).or((doc.created > 0).then_some(doc.created)); + timestamp.map_or_else( + || "2026-01-01T00:00:00Z".to_owned(), + |time| { + let (year, date) = designcraft_doc::time::utc_parts(time, 0); + format!("{year:04}-{}", date.format("%m-%dT%H:%M:%SZ")) + }, + ) +} + +fn metadata(doc: &Document, opts: &EpubOptions, title: &str, language: &str) -> craft_epub::Metadata { + craft_epub::Metadata { + identifier: identifier(doc, opts, title), + title: title.to_owned(), + language: language.to_owned(), + creators: opts.author.as_deref().or(doc.metadata_author()).into_iter().map(str::to_owned).collect(), + modified: modified(doc, opts), + generator: Some("DesignCraft".into()), + ..Default::default() + } +} + /// A pre-paginated (fixed-layout) EPUB: one XHTML page per document page. pub fn export_fixed_epub(doc: &Document, pages: &[FixedPage], opts: &EpubOptions) -> Result, EpubError> { let title = capped_title(opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string())); let lang = language_tag(&opts.language); - let id = identifier(doc, opts, &title); - let modified = modified(doc, opts); - let mut manifest = String::from("\n"); - let mut spine = String::new(); - let mut files: Vec<(String, Vec)> = Vec::new(); - let mut nav_items = String::new(); - for (i, p) in pages.iter().enumerate() { - let n = i + 1; - let (w, h) = (p.width.round().max(1.0) as u32, p.height.round().max(1.0) as u32); + let mut resources = Vec::with_capacity(pages.len().saturating_mul(2)); + let mut spine = Vec::with_capacity(pages.len()); + let mut navigation = Vec::with_capacity(pages.len()); + for (index, page) in pages.iter().enumerate() { + let number = index + 1; + let width = page.width.round().max(1.0) as u32; + let height = page.height.round().max(1.0) as u32; let xhtml = format!( - "\n\n\n{} — {n}\n\n\"\"/
{}
\n\n", + "\n\n\n{} — {number}\n\n\"\"/
{}
\n\n", xml_text(&title), - story_markup(&p.text).replace('\n', "
") + story_markup(&page.text).replace('\n', "
") ); - let _ = writeln!( - manifest, - "\n", - if n == 1 { " properties=\"cover-image\"" } else { "" } - ); - let _ = writeln!(spine, ""); - let _ = write!(nav_items, "
  • {n}
  • "); - files.push((format!("OEBPS/page{n}.xhtml"), xhtml.into_bytes())); - files.push((format!("OEBPS/pages/p{n}.png"), p.png.clone())); + resources.push(craft_epub::Resource::new( + format!("page{number}"), + format!("page{number}.xhtml"), + "application/xhtml+xml", + xhtml.into_bytes(), + )); + let image = + craft_epub::Resource::new(format!("pimg{number}"), format!("pages/p{number}.png"), "image/png", Cow::Borrowed(page.png.as_slice())); + resources.push(if number == 1 { image.with_properties("cover-image").stored() } else { image.stored() }); + spine.push(format!("page{number}")); + navigation.push(craft_epub::NavItem { href: format!("page{number}.xhtml"), label: number.to_string(), level: 0 }); } - let (vw, vh) = pages.first().map_or((612, 792), |p| (p.width.round() as u32, p.height.round() as u32)); - let nav = format!( - "\n\n\n{}\n\n\n", - xml_text(&title) - ); - let rendition = format!( - "\npre-paginated\nauto\n" - ); - let head = PackageHead { lang, id: &id, title: &title, author: opts.author.as_deref().or(doc.metadata_author()), modified: &modified }; - let opf = package_opf(&head, " prefix=\"rendition: http://www.idpf.org/vocab/rendition/#\"", &rendition, &manifest, &spine); - let entries = files.iter().map(|(name, data)| (name.clone(), data.as_slice(), name.ends_with(".png"))); - write_package(&opf, &nav, entries) -} - -/// The package metadata both EPUB flavours share. -struct PackageHead<'a> { - lang: &'a str, - id: &'a str, - title: &'a str, - author: Option<&'a str>, - modified: &'a str, -} - -/// The OPF package document. `prefix` goes on `` (with its leading space), `meta` after -/// `dcterms:modified` (with its leading newline); `manifest` and `spine` are the item lines. -fn package_opf(h: &PackageHead, prefix: &str, meta: &str, manifest: &str, spine: &str) -> String { - let PackageHead { lang, id, title, author, modified } = h; - let author = author.map(|a| format!("{}", xml_text(a))).unwrap_or_default(); - format!( - "\n\n\n{}\n{}\n{lang}{author}\n{modified}{meta}\n\n\n\n{manifest}\n\n{spine}\n\n", - xml_text(id), - xml_text(title) - ) -} - -/// Zips an EPUB: the stored `mimetype`, `container.xml`, the OPF and nav, then `entries` in order -/// as (path, bytes, stored uncompressed). -fn write_package<'a>(opf: &str, nav: &str, entries: impl IntoIterator) -> Result, EpubError> { - let io = |e: std::io::Error| EpubError(e.to_string()); - let zerr = |e: zip::result::ZipError| EpubError(e.to_string()); - let container = "\n\n"; - let mut buf = std::io::Cursor::new(Vec::new()); - { - let mut z = zip::ZipWriter::new(&mut buf); - let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); - let deflate = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); - z.start_file("mimetype", stored).map_err(zerr)?; - z.write_all(b"application/epub+zip").map_err(io)?; - for (name, data) in - [("META-INF/container.xml", container.as_bytes()), ("OEBPS/content.opf", opf.as_bytes()), ("OEBPS/nav.xhtml", nav.as_bytes())] - { - z.start_file(name, deflate).map_err(zerr)?; - z.write_all(data).map_err(io)?; - } - for (name, data, store) in entries { - z.start_file(name, if store { stored } else { deflate }).map_err(zerr)?; - z.write_all(data).map_err(io)?; - } - z.finish().map_err(zerr)?; - } - Ok(buf.into_inner()) + let (width, height) = pages.first().map_or((612, 792), |page| (page.width.round() as u32, page.height.round() as u32)); + let book = craft_epub::Book { + metadata: metadata(doc, opts, &title, lang), + layout: craft_epub::Layout::PrePaginated { width, height }, + resources, + spine, + nav: craft_epub::Nav { kind: craft_epub::NavKind::PageList, heading: "Pages".into(), items: navigation }, + ncx: false, + }; + craft_epub::write(&book).map_err(|error| EpubError(error.to_string())) } /// Export the document as a reflowable EPUB 3. pub fn export_epub(doc: &Document, opts: &EpubOptions) -> Result, EpubError> { let title = capped_title(opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string())); - let id = identifier(doc, opts, &title); - let modified = modified(doc, opts); - let Body { html: body, images, toc, .. } = body_html(doc, true, opts.split.as_ref(), &|name, _, _| name.to_string()); let lang = language_tag(&opts.language); - // One file per chapter (see `starts_chapter`); one file in all when Split Document is off. + let Body { html: body, images, toc, .. } = body_html(doc, true, opts.split.as_ref(), &|name, _, _| name.to_string()); let chapters: Vec = split_chapters(&body) .into_iter() .map(|part| { @@ -187,69 +111,66 @@ pub fn export_epub(doc: &Document, opts: &EpubOptions) -> Result, EpubEr ) }) .collect(); - // The file each navigation target (an element id) ended up in. - let mut files: BTreeMap = BTreeMap::new(); - for (k, c) in chapters.iter().enumerate() { - for rest in c.split(" id=\"").skip(1) { + let mut files = std::collections::BTreeMap::::new(); + for (index, chapter) in chapters.iter().enumerate() { + for rest in chapter.split(" id=\"").skip(1) { if let Some(id) = rest.split('"').next() { - files.entry(id.to_string()).or_insert_with(|| chapter_file(k)); + files.entry(id.to_owned()).or_insert_with(|| chapter_file(index)); } } } - // Note links whose target ended up in another file (endnotes end the book) point at it. - let chapters: Vec = chapters.iter().enumerate().map(|(k, c)| link_across_chapters(c, &chapter_file(k), &files)).collect(); - let mut nav_items = nav_list(&toc, &files); - if nav_items.is_empty() { - nav_items = format!("
  • {}
  • ", xml_text(&title)); - } - let nav = format!( - "\n\n\n{}\n\n\n", - xml_text(&title), - xml_text(opts.toc_title.as_deref().filter(|t| !t.trim().is_empty()).unwrap_or(contents_title(&opts.language))) - ); - let mut manifest = String::from( - "\n\n", - ); - let mut spine = String::new(); - for k in 0..chapters.len() { - let item = if k == 0 { "content".to_string() } else { format!("content{}", k + 1) }; - let _ = writeln!(manifest, "", chapter_file(k)); - let _ = writeln!(spine, ""); - } - for (k, (name, (mime, _))) in images.iter().enumerate() { - let _ = writeln!(manifest, "", xml_attr(name), xml_attr(mime)); - } - for (k, f) in opts.fonts.iter().enumerate() { - let (name, mime) = font_file(k, f); - let _ = writeln!(manifest, "", xml_attr(&name)); - } - // Cover: an image page first in reading order. - if let Some(c) = &opts.cover { - let (name, mime) = cover_file(c); - let _ = writeln!( - manifest, - "\n" - ); - } - let cover_spine = if opts.cover.is_some() { "\n" } else { "" }; - let head = PackageHead { lang, id: &id, title: &title, author: opts.author.as_deref().or(doc.metadata_author()), modified: &modified }; - let opf = package_opf(&head, "", "", &manifest, &format!("{cover_spine}{spine}")); + let chapters: Vec = + chapters.iter().enumerate().map(|(index, chapter)| link_across_chapters(chapter, &chapter_file(index), &files)).collect(); + let nav_items = if toc.is_empty() { + vec![craft_epub::NavItem { href: "content.xhtml".into(), label: title.clone(), level: 0 }] + } else { + toc.iter() + .map(|(target, label, level)| { + let file = files.get(target).map_or("content.xhtml", String::as_str); + craft_epub::NavItem { href: format!("{file}#{target}"), label: label.clone(), level: *level } + }) + .collect() + }; + let heading = opts.toc_title.as_deref().filter(|value| !value.trim().is_empty()).unwrap_or(contents_title(&opts.language)); + let mut resources = Vec::new(); let css = stylesheet_with(doc, &opts.generic_families, &opts.fonts); - let mut entries: Vec<(String, &[u8], bool)> = vec![("OEBPS/style.css".into(), css.as_bytes(), false)]; - entries.extend(chapters.iter().enumerate().map(|(k, c)| (format!("OEBPS/{}", chapter_file(k)), c.as_bytes(), false))); - entries.extend(images.iter().map(|(name, (_, data))| (format!("OEBPS/{name}"), *data, true))); - entries.extend(opts.fonts.iter().enumerate().map(|(k, f)| (format!("OEBPS/{}", font_file(k, f).0), f.data.as_slice(), false))); - let cover_xhtml = opts.cover.as_ref().map(|c| { - format!( + resources.push(craft_epub::Resource::new("css", "style.css", "text/css", css.into_bytes())); + let mut spine = Vec::new(); + for (index, chapter) in chapters.into_iter().enumerate() { + let id = if index == 0 { "content".to_owned() } else { format!("content{}", index + 1) }; + resources.push(craft_epub::Resource::new(id.clone(), chapter_file(index), "application/xhtml+xml", chapter.into_bytes())); + spine.push(id); + } + for (index, (path, (mime, data))) in images.iter().enumerate() { + resources.push(craft_epub::Resource::new(format!("img{index}"), path.clone(), *mime, Cow::Borrowed(*data)).stored()); + } + for (index, font) in opts.fonts.iter().enumerate() { + let (path, mime) = font_file(index, font); + resources.push(craft_epub::Resource::new(format!("font{index}"), path, mime, Cow::Borrowed(font.data.as_slice()))); + } + if let Some(cover) = &opts.cover { + let (path, mime) = cover_file(cover); + resources.push(craft_epub::Resource::new("cover-img", path, mime, Cow::Borrowed(cover.as_slice())).with_properties("cover-image").stored()); + let xhtml = format!( "\n\n\n{}\n\"{}\"/\n\n", xml_text(&title), - cover_file(c).0, + path, xml_attr(&title) - ) - }); - if let (Some(c), Some(xhtml)) = (&opts.cover, &cover_xhtml) { - entries.push((format!("OEBPS/{}", cover_file(c).0), c.as_slice(), true)); - entries.push(("OEBPS/cover.xhtml".into(), xhtml.as_bytes(), false)); + ); + resources.push(craft_epub::Resource::new("cover", "cover.xhtml", "application/xhtml+xml", xhtml.into_bytes())); + spine.insert(0, "cover".into()); } - write_package(&opf, &nav, entries) + let book = craft_epub::Book { + metadata: metadata(doc, opts, &title, lang), + layout: craft_epub::Layout::Reflowable, + resources, + spine, + nav: craft_epub::Nav { kind: craft_epub::NavKind::Toc, heading: heading.to_owned(), items: nav_items }, + ncx: false, + }; + craft_epub::write(&book).map_err(|error| EpubError(error.to_string())) +} + +fn cover_file(data: &[u8]) -> (&'static str, &'static str) { + if data.starts_with(&[0xFF, 0xD8, 0xFF]) { ("images/cover.jpg", "image/jpeg") } else { ("images/cover.png", "image/png") } } diff --git a/crates/epub/src/tests.rs b/crates/epub/src/tests.rs index 62683f91a..b08a97ff0 100644 --- a/crates/epub/src/tests.rs +++ b/crates/epub/src/tests.rs @@ -122,26 +122,6 @@ fn anchored_objects_are_exported_in_place() { assert!(!story_html(&d, sid).contains(" = craft_document_bundle::Spec { + mimetype_entry: "mimetype", + mimetype: MIME, + metadata_entry: "meta.json", + document_entry: "document.json", + preview_entry: PREVIEW, + assets_directory: "assets", + format_name: "designcraft", + legacy_assets_field: Some("assetData"), +}; #[derive(Debug, thiserror::Error)] pub enum FormatError { @@ -36,260 +41,85 @@ pub enum FormatError { TooNew(u64), #[error("{0}")] Io(String), - /// The document holds something the file can't represent; nothing is written. - #[error("{0}")] + #[error("document cannot be saved: {0}")] Unsaveable(String), } -fn ext_for(mime: &str) -> &'static str { - match mime { - "image/png" => "png", - "image/jpeg" => "jpg", - "image/gif" => "gif", - "image/webp" => "webp", - "image/tiff" => "tif", - "application/pdf" => "pdf", - "application/postscript" => "eps", - "image/svg+xml" => "svg", - _ => "bin", +impl From for FormatError { + fn from(error: craft_document_bundle::Error) -> Self { + match error { + craft_document_bundle::Error::TooNew(version) => FormatError::TooNew(version), + craft_document_bundle::Error::NotBundle(message) => FormatError::NotOurs(message), + craft_document_bundle::Error::Read(message) | craft_document_bundle::Error::Write(message) => FormatError::Io(message), + } } } -/// Serialize a document to `.designcraft` bytes. The file holds each distinct placed file once -/// and leaves out assets nothing references; `doc` itself keeps them (undo can bring them back). +/// Serialize a document to `.designcraft` bytes. pub fn save(doc: &Document) -> Result, FormatError> { save_with_preview(doc, None) } -/// [`save`], plus `preview` (PNG bytes) as `preview.png`. +/// [`save`], plus a PNG preview of the first page. pub fn save_with_preview(doc: &Document, preview: Option<&[u8]>) -> Result, FormatError> { - save_with_large_entries_from(doc, preview, LARGE_ENTRY) -} - -/// Asset entries this size or larger are written with zip64 sizes. Zip rejects an entry over -/// 4 GiB written without them; the margin covers deflate output that is larger than its input. -const LARGE_ENTRY: usize = (u32::MAX as usize).saturating_sub(64 << 20); - -/// The oldest reader capability that keeps everything in `doc`. Documents that need nothing newer -/// keep their older requirement so existing readers still open them. -fn required_version(doc: &Document) -> u32 { - // Version 4 covers both of these: older readers ignore the unknown fields, so they would - // silently drop retained graphic layer choices, or read an explicit all-on PDF layer choice - // (empty hidden list) as "use the file's defaults". A nonempty hidden list stays readable. - if doc.has_graphic_layer_options() || doc.has_explicit_all_on_pdf_layers() { - return 4; - } - // Older readers would silently omit EPS artwork. Preview-only legacy assets and documents - // without EPS stay at 2 so their existing readers keep working; writing 1 would make a - // reader re-run the version 2 paragraph upgrade over current defaults. - if doc.assets.values().any(|a| designcraft_images::is_eps(&a.data)) { 3 } else { 2 } -} - -fn save_with_large_entries_from(doc: &Document, preview: Option<&[u8]>, large_entry: usize) -> Result, FormatError> { - // JSON writes infinity and NaN as null, which no load accepts: the file would not open. if let Some(id) = doc.non_finite_item() { return Err(FormatError::Unsaveable(format!("object {} has a position or size that is not a finite number", id.0))); } let mut compact = doc.clone(); compact.compact_assets(); - let doc = &compact; - let io = |e: zip::result::ZipError| FormatError::Io(e.to_string()); - let mut buf = Cursor::new(Vec::new()); - { - let mut z = zip::ZipWriter::new(&mut buf); - let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); - let deflate = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); - z.start_file("mimetype", stored).map_err(io)?; - z.write_all(MIME.as_bytes()).map_err(|e| FormatError::Io(e.to_string()))?; - z.start_file("meta.json", deflate).map_err(io)?; - let version = required_version(doc); - let meta = json!({"format": "designcraft", "version": version, "generator": concat!("DesignCraft ", env!("CARGO_PKG_VERSION"))}); - z.write_all(meta.to_string().as_bytes()).map_err(|e| FormatError::Io(e.to_string()))?; - if let Some(png) = preview { - z.start_file(PREVIEW, stored).map_err(io)?; - z.write_all(png).map_err(|e| FormatError::Io(e.to_string()))?; - } - z.start_file("document.json", deflate).map_err(io)?; - let json = serde_json::to_vec_pretty(doc).map_err(|e| FormatError::Io(e.to_string()))?; - z.write_all(&json).map_err(|e| FormatError::Io(e.to_string()))?; - for (id, a) in &doc.assets { - if a.data.is_empty() { - continue; - } - // Already-compressed images are stored as-is. - let opts = if matches!(a.mime.as_str(), "image/png" | "image/jpeg" | "image/gif" | "image/webp") { stored } else { deflate }; - let opts = opts.large_file(a.data.len() >= large_entry); - z.start_file(format!("assets/{}.{}", id.0, ext_for(&a.mime)), opts).map_err(io)?; - z.write_all(&a.data).map_err(|e| FormatError::Io(e.to_string()))?; - } - z.finish().map_err(io)?; - } - Ok(buf.into_inner()) + let version = required_version(&compact); + let json = serde_json::to_vec_pretty(&compact).map_err(|e| FormatError::Io(e.to_string()))?; + let owned_assets = compact.assets.values().map(|asset| (asset.id.0.to_string(), asset.mime.clone(), asset.data.clone())).collect::>(); + let assets = owned_assets.iter().map(|(id, mime, data)| craft_document_bundle::Asset { id, mime, data }).collect::>(); + craft_document_bundle::write(&json, version, concat!("DesignCraft ", env!("CARGO_PKG_VERSION")), preview, &assets, BUNDLE).map_err(Into::into) } -/// The file name extension of a DesignCraft document. -pub const EXTENSION: &str = "designcraft"; +fn required_version(doc: &Document) -> u32 { + if doc.has_graphic_layer_options() || doc.has_explicit_all_on_pdf_layers() { + return 4; + } + if doc.assets.values().any(|asset| craft_eps::is_eps(&asset.data)) { 3 } else { 2 } +} -/// Whether `bytes` are a DesignCraft document package: a zip holding `document.json`. Files -/// without an extension are recognised by this (the legacy JSON form is too generic to sniff). +/// Whether `bytes` are a native DesignCraft ZIP package. pub fn is_designcraft(bytes: &[u8]) -> bool { - bytes.starts_with(b"PK") && zip::ZipArchive::new(Cursor::new(bytes)).is_ok_and(|z| z.index_for_name("document.json").is_some()) + craft_document_bundle::has_document(bytes, BUNDLE) } -/// Whether `prefix`, the start of a file, opens a DesignCraft package: a zip whose first, stored -/// entry is the `mimetype` naming this format. Packages saved before they had one are recognised -/// only whole ([`is_designcraft`]). +/// Whether `prefix` begins with this format's stored mimetype entry. pub fn starts_designcraft(prefix: &[u8]) -> bool { - prefix.starts_with(b"PK\x03\x04") && prefix.get(26..28) == Some(&8u16.to_le_bytes()[..]) && prefix.get(30..38) == Some(b"mimetype") && { - let extra = prefix.get(28..30).map_or(0, |e| usize::from(u16::from_le_bytes([e[0], e[1]]))); - prefix.get(38 + extra..).is_some_and(|rest| rest.starts_with(MIME.as_bytes())) - } + craft_document_bundle::starts_with_mimetype(prefix, BUNDLE) } -/// The zip entry holding the first page's picture. -pub const PREVIEW: &str = "preview.png"; - -/// The PNG bytes of a `.designcraft` file's preview, reading only the zip's directory and that -/// entry. None for files without one (saved before previews, or legacy JSON) or that aren't ours. -pub fn read_preview(file: R) -> Option> { - let mut z = zip::ZipArchive::new(file).ok()?; - let f = z.by_name(PREVIEW).ok()?; - // A preview is small; a huge entry is not one we wrote. - if f.size() > MAX_PREVIEW_BYTES { - return None; - } - // The declared size can lie: bound what the entry inflates to as well. - let mut v = Vec::new(); - f.take(MAX_PREVIEW_BYTES + 1).read_to_end(&mut v).ok()?; - (u64::try_from(v.len()).ok()? <= MAX_PREVIEW_BYTES).then_some(v) +/// Read the first-page PNG preview from a seekable package file, with a fixed 4 MiB cap. +pub fn read_preview(reader: R) -> Option> { + craft_document_bundle::read_preview_from(reader, PREVIEW) } -/// The largest preview read back (a first-page PNG is far smaller). -const MAX_PREVIEW_BYTES: u64 = 4 << 20; - -/// Read `.designcraft` bytes (zip, or the legacy single JSON file). +/// Read `.designcraft` bytes (ZIP, or the legacy single JSON file). pub fn load(bytes: &[u8]) -> Result { - if bytes.starts_with(b"PK") { - return load_zip(bytes); - } - load_legacy_json(bytes) -} - -/// Most entries a `.designcraft` archive may hold. -const MAX_ENTRIES: usize = 100_000; - -/// How much a `.designcraft` archive may expand to when read. The sizes an archive claims are -/// not trusted, so deflated entries are capped; a *stored* entry can't expand (its size is its -/// bytes in the archive, which is already in memory, and all stored entries read together are -/// held to the archive's own size), so a placed image — stored as is by [`save`] — opens whatever -/// its size and doesn't count against the budget. Saving has no cap, and this keeps every saved -/// document openable (#2166). -#[derive(Clone, Copy, Debug)] -struct Limits { - /// Most bytes `meta.json` or `document.json` may expand to. - json: u64, - /// Most bytes one deflated asset may expand to. - asset: u64, - /// Most bytes all the deflated entries read from one archive may expand to together. - total: u64, -} - -impl Limits { - const DEFAULT: Limits = Limits { json: 256 << 20, asset: 1 << 30, total: 4 << 30 }; -} - -/// Read entry `name` of `z`, or `None` when it is missing or unreadable. Refuses a deflated entry -/// that expands past `limit` bytes or past what is `left` of the archive's budget, without -/// decompressing more than that: the sizes the archive claims are not trusted. A stored entry -/// is read at its size in the archive and charged to `stored_left`, the archive's own size; only a -/// stored asset is free of `limit`. -fn read_entry( - z: &mut zip::ZipArchive>, - name: &str, - limit: u64, - left: &mut u64, - stored_left: &mut u64, -) -> Result>, FormatError> { - let Ok(f) = z.by_name(name) else { return Ok(None) }; - let stored = f.compression() == zip::CompressionMethod::Stored; - let limit = if !stored { - limit.min(*left) - } else if name.starts_with("assets/") { - f.compressed_size().min(*stored_left) - } else { - f.compressed_size().min(*stored_left).min(limit) - }; - let too_big = || FormatError::Io(format!("{name} is larger than the {limit}-byte limit (or the archive expands past its {}-byte budget)", *left)); - if f.size() > limit { - return Err(too_big()); - } - let mut v = Vec::new(); - if f.take(limit.saturating_add(1)).read_to_end(&mut v).is_err() { - return Ok(None); - } - let n = v.len() as u64; - if n > limit { - return Err(too_big()); - } - if stored { - *stored_left = stored_left.saturating_sub(n); - } else { - *left = left.saturating_sub(n); - } - Ok(Some(v)) -} - -fn load_zip(bytes: &[u8]) -> Result { - load_zip_within(bytes, Limits::DEFAULT) -} - -fn load_zip_within(bytes: &[u8], limits: Limits) -> Result { - let mut z = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| FormatError::NotOurs(e.to_string()))?; - if z.len() > MAX_ENTRIES { - return Err(FormatError::NotOurs(format!("{} entries (at most {MAX_ENTRIES})", z.len()))); - } - let mut left = limits.total; - // Stored entries can't expand, but a crafted zip can point several at the same bytes: together - // they get the archive's own size. - let mut stored_left = bytes.len() as u64; - let version = read_entry(&mut z, "meta.json", limits.json, &mut left, &mut stored_left)? - .and_then(|meta| serde_json::from_slice::(&meta).ok()) - .and_then(|m| m.get("version").and_then(Value::as_u64)) - .unwrap_or(1); - if version > u64::from(VERSION) { - return Err(FormatError::TooNew(version)); - } - let json = read_entry(&mut z, "document.json", limits.json, &mut left, &mut stored_left)? - .ok_or_else(|| FormatError::NotOurs("missing document.json".into()))?; - let mut v: Value = serde_json::from_slice(&json).map_err(|e| FormatError::NotOurs(e.to_string()))?; - upgrade_json(&mut v); - let mut doc: Document = serde_json::from_value(v).map_err(|e| FormatError::NotOurs(e.to_string()))?; - let names: Vec = z.file_names().map(str::to_string).collect(); - // Files saved with one copy per placement: identical copies share one buffer as they are - // read, so memory holds each distinct file once. + let package = craft_document_bundle::read(bytes, VERSION, BUNDLE)?; + let mut value = package.document; + upgrade_json(&mut value); + let mut doc: Document = serde_json::from_value(value).map_err(|e| FormatError::NotOurs(e.to_string()))?; let fingerprint = std::collections::hash_map::RandomState::new(); let mut loaded: HashMap>>> = HashMap::new(); - for name in names { - let Some(stem) = name.strip_prefix("assets/") else { continue }; - let Some(id) = stem.split('.').next().and_then(|s| s.parse::().ok()) else { continue }; - // Entries for assets the document doesn't have are never decompressed. - if !doc.assets.contains_key(&AssetId(id)) { - continue; - } - if let (Some(data), Some(a)) = (read_entry(&mut z, &name, limits.asset, &mut left, &mut stored_left)?, doc.assets.get_mut(&AssetId(id))) { - let same = loaded.entry(fingerprint.hash_one(data.as_slice())).or_default(); - let data = match same.iter().find(|d| ***d == data) { - Some(d) => d.clone(), - None => { - let d = Arc::new(data); - same.push(d.clone()); - d - } - }; - Arc::make_mut(a).data = data; - } + for (raw_id, bytes) in package.assets { + let Ok(id) = raw_id.parse::() else { continue }; + let asset_id = AssetId(id); + let Some(asset) = doc.assets.get_mut(&asset_id) else { continue }; + let same = loaded.entry(fingerprint.hash_one(bytes.as_slice())).or_default(); + let data = match same.iter().find(|data| ***data == bytes) { + Some(data) => data.clone(), + None => { + let data = Arc::new(bytes); + same.push(data.clone()); + data + } + }; + Arc::make_mut(asset).data = data; } - upgrade(&mut doc, u32::try_from(version).unwrap_or(u32::MAX)); + upgrade(&mut doc, u32::try_from(package.version).unwrap_or(u32::MAX)); doc.merge_duplicate_assets(); doc.mark_differing_margins_local(); doc.renumber_duplicate_items(); @@ -300,16 +130,12 @@ fn load_zip_within(bytes: &[u8], limits: Limits) -> Result(doc: &mut Document, field: fn(&mut ParaAttrs) -> &mut Option, old: T) { let unset = |a: &ParaAttrs| field(&mut a.clone()).is_none(); // Every style is looked up by name: through one index, not a search of all styles each time. @@ -324,8 +150,8 @@ fn pin_para_default(doc: &mut Document, field: fn(&mut ParaAttrs) -> & }) .collect(); if !roots.is_empty() { - for s in doc.styles_mut().paragraph.iter_mut().filter(|s| roots.contains(&s.name)) { - *field(&mut s.para) = Some(old.clone()); + for style in doc.styles_mut().paragraph.iter_mut().filter(|style| roots.contains(&style.name)) { + *field(&mut style.para) = Some(old.clone()); } } // Borrowed names: one entry per style already in memory, so no copy and no extra bound needed. @@ -338,105 +164,58 @@ fn pin_para_default(doc: &mut Document, field: fn(&mut ParaAttrs) -> & } } }; - for st in doc.stories.values_mut() { - let st = Arc::make_mut(st); - st.for_each_text_mut(&mut pin); - for n in &mut st.endnotes { - Arc::make_mut(n).text.for_each_text_mut(&mut pin); + for story in doc.stories.values_mut() { + let story = Arc::make_mut(story); + story.for_each_text_mut(&mut pin); + for note in &mut story.endnotes { + Arc::make_mut(note).text.for_each_text_mut(&mut pin); } } } -fn load_legacy_json(bytes: &[u8]) -> Result { - let mut v: Value = serde_json::from_slice(bytes).map_err(|e| FormatError::NotOurs(e.to_string()))?; - upgrade_json(&mut v); - let mut doc: Document = serde_json::from_value(v.clone()).map_err(|e| FormatError::NotOurs(e.to_string()))?; - if let Some(data) = v.get("assetData").and_then(Value::as_object) { - for (k, s) in data { - if let (Ok(id), Some(s)) = (k.parse::(), s.as_str()) - && let Some(a) = doc.assets.get_mut(&AssetId(id)) - { - Arc::make_mut(a).data = Arc::new(base64_decode(s)); - } - } - } - upgrade(&mut doc, 1); - doc.merge_duplicate_assets(); - doc.mark_differing_margins_local(); - doc.renumber_duplicate_items(); - doc.check().map_err(|e| FormatError::NotOurs(e.to_string()))?; - Ok(doc) -} - -/// Bring older documents' JSON up to the current model. Vertical Type was a text frame option -/// (`content.options.vertical`); it is the story's direction, which a story takes from its first -/// frame. -fn upgrade_json(doc: &mut Value) { +/// Upgrade the old vertical-text representation before deserializing the current model. +fn upgrade_json(document: &mut Value) { let mut vertical = HashSet::new(); - vertical_frames(doc, &mut vertical, 0); + vertical_frames(document, &mut vertical, 0); if vertical.is_empty() { return; } - let Some(stories) = doc.get_mut("stories").and_then(Value::as_object_mut) else { return }; - for st in stories.values_mut() { - let first = st.get("frames").and_then(|f| f.get(0)).and_then(Value::as_u64); - if let (Some(f), Some(st)) = (first, st.as_object_mut()) - && vertical.contains(&f) - && !st.contains_key("vertical") + let Some(stories) = document.get_mut("stories").and_then(Value::as_object_mut) else { return }; + for story in stories.values_mut() { + let first = story.get("frames").and_then(|frames| frames.get(0)).and_then(Value::as_u64); + if let (Some(frame), Some(story)) = (first, story.as_object_mut()) + && vertical.contains(&frame) + && !story.contains_key("vertical") { - st.insert("vertical".into(), Value::Bool(true)); + story.insert("vertical".into(), Value::Bool(true)); } } } -/// Ids of the text frames in `v` (any depth: groups, anchored objects) whose options say vertical. -fn vertical_frames(v: &Value, out: &mut HashSet, depth: usize) { - // serde_json already refuses JSON nested deeper than 128 levels. +fn vertical_frames(value: &Value, out: &mut HashSet, depth: usize) { if depth > 256 { return; } - match v { - Value::Object(m) => { - if let (Some(id), Some(c)) = (m.get("id").and_then(Value::as_u64), m.get("content")) - && c.get("type").and_then(Value::as_str) == Some("text") - && c.get("options").and_then(|o| o.get("vertical")).and_then(Value::as_bool) == Some(true) + match value { + Value::Object(map) => { + if let (Some(id), Some(content)) = (map.get("id").and_then(Value::as_u64), map.get("content")) + && content.get("type").and_then(Value::as_str) == Some("text") + && content.get("options").and_then(|options| options.get("vertical")).and_then(Value::as_bool) == Some(true) { out.insert(id); } - for c in m.values() { - vertical_frames(c, out, depth + 1); + for child in map.values() { + vertical_frames(child, out, depth + 1); } } - Value::Array(a) => { - for c in a { - vertical_frames(c, out, depth + 1); + Value::Array(children) => { + for child in children { + vertical_frames(child, out, depth + 1); } } _ => {} } } -fn base64_decode(s: &str) -> Vec { - let mut out = Vec::with_capacity(s.len() * 3 / 4); - let (mut buf, mut bits) = (0u32, 0); - for b in s.bytes() { - let v = match b { - b'A'..=b'Z' => b - b'A', - b'a'..=b'z' => b - b'a' + 26, - b'0'..=b'9' => b - b'0' + 52, - b'+' | b'-' => 62, - b'/' | b'_' => 63, - _ => continue, - } as u32; - buf = buf << 6 | v; - bits += 6; - if bits >= 8 { - bits -= 8; - out.push((buf >> bits) as u8); - } - } - out -} - #[cfg(test)] mod tests; diff --git a/crates/format/src/tests.rs b/crates/format/src/tests.rs index ca57c7770..0a62e0939 100644 --- a/crates/format/src/tests.rs +++ b/crates/format/src/tests.rs @@ -1,7 +1,10 @@ use super::*; +use std::io::{Cursor, Read, Write}; + use designcraft_doc::build::NewDocument; use designcraft_doc::geom::Rect; use designcraft_doc::{Asset, ParaFormat, SpreadRef}; +use serde_json::json; fn png_asset(d: &mut Document, data: &[u8]) -> AssetId { let aid = AssetId(d.alloc()); @@ -54,85 +57,6 @@ fn save_uncompacted(d: &Document) -> Vec { buf.into_inner() } -/// A `.designcraft` with the given asset entries, each stored as is or deflated. -fn archive_with(d: &Document, entries: &[(AssetId, &[u8], bool)]) -> Vec { - let mut buf = Cursor::new(Vec::new()); - let mut z = zip::ZipWriter::new(&mut buf); - let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); - let deflate = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); - z.start_file("mimetype", stored).unwrap(); - z.write_all(MIME.as_bytes()).unwrap(); - z.start_file("meta.json", deflate).unwrap(); - z.write_all(json!({"format": "designcraft", "version": VERSION}).to_string().as_bytes()).unwrap(); - z.start_file("document.json", deflate).unwrap(); - z.write_all(&serde_json::to_vec(d).unwrap()).unwrap(); - for (id, data, as_is) in entries { - z.start_file(format!("assets/{}.png", id.0), if *as_is { stored } else { deflate }).unwrap(); - z.write_all(data).unwrap(); - } - z.finish().unwrap(); - buf.into_inner() -} - -/// Bytes that don't compress (a simple generator, so the test needs no crate). -fn noise(n: usize) -> Vec { - let mut x: u32 = 0x9e37_79b9; - (0..n) - .map(|_| { - x ^= x << 13; - x ^= x >> 17; - x ^= x << 5; - (x >> 24) as u8 - }) - .collect() -} - -/// #2166: saving has no size cap, so opening must keep every saved document openable. A stored -/// entry (how placed images are saved) can't expand and opens at any size; the total budget is -/// never below the archive's own size. Deflated entries stay capped, so a zip bomb is refused. -#[test] -fn stored_assets_open_at_any_size_while_deflated_ones_stay_capped() { - let mut d = Document::new(&NewDocument::default()); - let big = noise(50_000); - let a = png_asset(&mut d, &big); - let b = png_asset(&mut d, &big); - d.assets.values_mut().for_each(|a| Arc::make_mut(a).data = Arc::new(Vec::new())); - place(&mut d, a, 0.0); - place(&mut d, b, 0.0); - // A budget that fits the document's JSON but not the two 50 000-byte assets together. - let json_len = serde_json::to_vec(&d).unwrap().len() as u64; - let tight = Limits { json: 256 << 20, asset: 1_000, total: json_len + 10_000 }; - assert!(tight.total < 100_000, "the JSON is {json_len} bytes; the budget must stay under the two assets"); - - // Two stored assets, each over the asset cap and together over the total: both open. - let bytes = archive_with(&d, &[(a, &big, true), (b, &big, true)]); - let doc = load_zip_within(&bytes, tight).expect("stored entries open at their own size"); - assert_eq!(doc.assets[&a].data.len(), 50_000); - // (Identical bytes come back as one asset: `load` merges duplicates.) - assert_eq!(doc.assets.len(), 1); - assert!(doc.all_items().iter().filter_map(|i| doc.item(*i)?.graphic()).all(|g| g.asset == a), "both placements show it"); - - // A deflated entry past the cap is refused, whether it really is that big ... - let bytes = archive_with(&d, &[(a, &big, false), (b, &big, true)]); - let e = load_zip_within(&bytes, tight).expect_err("a deflated entry over the cap").to_string(); - assert!(e.contains("larger than the 1000-byte limit"), "{e}"); - // ... or claims to be (a few KB that expand to megabytes). - let bomb = vec![0u8; 3 << 20]; - let bytes = archive_with(&d, &[(a, &bomb, false), (b, &big, true)]); - assert!(bytes.len() < 64 << 10, "the bomb is small on disk: {}", bytes.len()); - assert!(load_zip_within(&bytes, tight).is_err(), "a zip bomb is refused"); - - // The default limits read the same stored archive, and what `save` writes round-trips. - let bytes = archive_with(&d, &[(a, &big, true), (b, &big, true)]); - assert_eq!(load_zip(&bytes).unwrap().assets[&a].data.len(), 50_000); - Arc::make_mut(d.assets.get_mut(&a).unwrap()).data = Arc::new(big.clone()); - Arc::make_mut(d.assets.get_mut(&b).unwrap()).data = Arc::new(big.clone()); - let saved = save(&d).unwrap(); - let doc = load_zip_within(&saved, tight).expect("what save writes opens under a cap smaller than its images"); - // (`save` merges the two identical assets into one.) - assert!(!doc.assets.is_empty() && doc.assets.values().all(|x| x.data.len() == 50_000), "every saved image is back"); -} - #[test] fn explicit_all_on_pdf_layers_require_a_capable_reader_only_when_needed() { let mut d = Document::new(&NewDocument::default()); @@ -421,20 +345,6 @@ fn format_1_drop_caps_stay_unaligned() { assert!(now.styles.resolve_para(&now.story(sid).unwrap().paras[0]).0.drop_cap_align_left); } -/// Assets over the zip size limit are written with zip64 sizes (scaled down here: the real -/// threshold is near 4 GiB); smaller ones are written exactly as before. -#[test] -fn large_assets_are_written_with_zip64_sizes_and_open_again() { - let mut d = Document::new(&NewDocument::default()); - let aid = png_asset(&mut d, &[5; 64]); - place(&mut d, aid, 0.0); - let normal = save(&d).unwrap(); - assert_eq!(normal, save_with_large_entries_from(&d, None, 65).unwrap(), "an asset under the threshold is unchanged"); - let large = save_with_large_entries_from(&d, None, 64).unwrap(); - assert!(large.len() > normal.len(), "the asset entry carries zip64 sizes"); - assert_eq!(*load(&large).unwrap().assets[&aid].data, vec![5; 64]); -} - #[test] fn saves_hold_each_used_file_once() { let mut d = Document::new(&NewDocument::default()); @@ -825,7 +735,7 @@ fn oversized_document_json_is_refused() { assert!(bytes.len() < 4 << 20, "{}", bytes.len()); claim_size(&mut bytes, "document.json", 1); let Err(err) = load(&bytes) else { panic!("an oversized document.json loaded") }; - assert!(err.to_string().contains("limit"), "{err}"); + assert!(err.to_string().contains("larger"), "{err}"); } /// An embedded file that expands past the 1 GiB limit (and says so) is refused too. @@ -840,7 +750,7 @@ fn oversized_asset_is_refused() { z.start_file(format!("assets/{}.png", aid.0), zip::write::SimpleFileOptions::default()).unwrap(); write_fill(&mut z, (1 << 30) + 1, 0); let Err(err) = load(&z.finish().unwrap().into_inner()) else { panic!("an oversized asset loaded") }; - assert!(err.to_string().contains("limit"), "{err}"); + assert!(err.to_string().contains("larger"), "{err}"); } /// An archive with more entries than any document needs is refused before any is read. From 188371969998276906d15d8d3df27e9706222eb4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:15:39 -0500 Subject: [PATCH 004/153] Move IDML package IO and SVG preflight to craft-libs --- Cargo.lock | 41 +++++++-- Cargo.toml | 9 +- crates/idml/Cargo.toml | 5 +- crates/idml/src/export.rs | 20 +--- crates/idml/src/import.rs | 38 ++------ crates/idml/src/import/item.rs | 29 ++---- crates/idml/src/import/package.rs | 129 +++----------------------- crates/idml/src/lib.rs | 2 +- crates/idml/src/tests.rs | 2 +- crates/idml/src/xml.rs | 148 ++++++++++-------------------- crates/images/Cargo.toml | 5 +- crates/images/src/eps.rs | 12 +-- crates/images/src/lib.rs | 78 ++++------------ 13 files changed, 147 insertions(+), 371 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8229e7d80..80ccd7840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1038,7 +1038,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13af [[package]] name = "craft-document-bundle" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" dependencies = [ "craft-zip", "serde_json", @@ -1049,12 +1049,12 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" [[package]] name = "craft-epub" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" dependencies = [ "craft-xml", "craft-zip", @@ -1089,6 +1089,18 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-idml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +dependencies = [ + "craft-xml", + "craft-zip", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] [[package]] name = "craft-keymap" version = "0.1.0" @@ -1140,6 +1152,19 @@ dependencies = [ "winsafe", ] +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +dependencies = [ + "craft-xml", + "flate2", + "kurbo 0.13.1", + "roxmltree 0.21.2", + "thiserror 2.0.21", +] + +[[package]] [[package]] name = "craft-ui" version = "0.1.0" @@ -1210,7 +1235,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1219,7 +1244,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=dc20b58f0f6059bd408d6f14ab130f343#dc20b58f0f6059bd408d6f14ab130f343" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" dependencies = [ "flate2", "thiserror 2.0.21", @@ -1530,13 +1555,14 @@ dependencies = [ name = "designcraft-idml" version = "0.20.1" dependencies = [ + "craft-idml", + "craft-xml", "designcraft-color", "designcraft-compose", "designcraft-doc", "designcraft-geom", "designcraft-images", "log", - "quick-xml 0.41.0", "thiserror 2.0.21", "zip 3.0.0", ] @@ -1545,13 +1571,14 @@ dependencies = [ name = "designcraft-images" version = "0.20.1" dependencies = [ + "craft-eps", + "craft-svg", "craft-cms", "designcraft-color", "designcraft-fonts", "flate2", "image", "log", - "quick-xml 0.41.0", "resvg", "skrifa 0.47.0", "tiff", diff --git a/Cargo.toml b/Cargo.toml index 702ff232c..03d750c98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,12 @@ repository = "https://github.com/storytold/designcraft" [workspace.dependencies] # Shared reader and writer; publish the immutable revision before this consumer. craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false } -craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "dc20b58f0f6059bd408d6f14ab130f343", version = "=0.1.0" } -craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "dc20b58f0f6059bd408d6f14ab130f343", version = "=0.1.0" } -craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "dc20b58f0f6059bd408d6f14ab130f343", version = "=0.1.0" } +craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } +craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } +craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } +craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } +craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } +craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/idml/Cargo.toml b/crates/idml/Cargo.toml index e9fcca616..217e29329 100644 --- a/crates/idml/Cargo.toml +++ b/crates/idml/Cargo.toml @@ -13,10 +13,11 @@ designcraft-geom = { workspace = true } designcraft-color = { workspace = true } designcraft-doc = { workspace = true } designcraft-images = { workspace = true } +craft-idml = { workspace = true } +craft-xml = { workspace = true } thiserror = { workspace = true } log = { workspace = true } -quick-xml = { workspace = true } -zip = { workspace = true } [dev-dependencies] designcraft-compose = { workspace = true } +zip = { workspace = true } diff --git a/crates/idml/src/export.rs b/crates/idml/src/export.rs index 717bb5d6c..6bb014b26 100644 --- a/crates/idml/src/export.rs +++ b/crates/idml/src/export.rs @@ -1,7 +1,6 @@ //! Document → IDML package. use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; -use std::io::Write; use designcraft_color::{Color, ColorType, GradientKind, SwatchValue, swatch}; use designcraft_doc::{ @@ -12,7 +11,7 @@ use designcraft_geom::{Affine, PathData}; use crate::names::{self, CHAR_BUILTINS, OBJECT_BUILTINS, PARA_BUILTINS, escape_id, num, pt}; use crate::xml::{El, Node, document, document_with_pi}; -use crate::{DOM_VERSION, MIMETYPE, base64_encode}; +use crate::{DOM_VERSION, base64_encode}; mod common; mod designmap; @@ -71,22 +70,7 @@ pub fn export_idml_with(doc: &Document, opts: &ExportOptions) -> Vec { } fn zip_parts(parts: &[(String, Vec)]) -> Vec { - use zip::write::SimpleFileOptions; - let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let stored = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); - let deflated = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); - let mut write = || -> zip::result::ZipResult<()> { - w.start_file("mimetype", stored)?; - w.write_all(MIMETYPE.as_bytes())?; - for (name, data) in parts { - w.start_file(name.as_str(), deflated)?; - w.write_all(data)?; - } - Ok(()) - }; - // Writing into memory cannot fail except on internal errors; return what we have then. - let _ = write(); - w.finish().map(|c| c.into_inner()).unwrap_or_default() + craft_idml::write_package(parts).unwrap_or_default() } struct Ex<'a> { diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs index 980c38058..8a2f49759 100644 --- a/crates/idml/src/import.rs +++ b/crates/idml/src/import.rs @@ -1,7 +1,6 @@ //! IDML package → Document. use std::collections::{BTreeMap, HashMap, HashSet}; -use std::io::Read; use std::sync::Arc; use designcraft_color::cms::lab; @@ -17,8 +16,8 @@ use designcraft_geom::corners::{Corner, CornerOptions}; use designcraft_geom::{Affine, Anchor, PathData, Point, Rect, SubPath}; use crate::names::{self, CHAR_BUILTINS, OBJECT_BUILTINS, PARA_BUILTINS, unescape_id}; -use crate::xml::{El, Node, parse}; -use crate::{IdmlError, MIMETYPE, Result, base64_decode, sniff_image}; +use crate::xml::{El, Node}; +use crate::{IdmlError, Result, base64_decode, sniff_image}; mod document; mod frame; @@ -79,35 +78,12 @@ pub fn import_idml_report_with_previews( previews: &HashMap>>, ) -> Result { let mut pkg = Package::open(bytes)?; - if let Some(m) = pkg.part("mimetype")? - && String::from_utf8_lossy(&m).trim() != MIMETYPE - { - return Err(IdmlError::NotIdml(format!("unexpected mimetype `{}`", String::from_utf8_lossy(&m).trim()))); - } - let dm = pkg.part("designmap.xml")?.ok_or_else(|| IdmlError::NotIdml("missing designmap.xml".into()))?; - let root = parse(&dm).map_err(|msg| IdmlError::Part { part: "designmap.xml".into(), msg })?; - if root.local() != "Document" { - return Err(IdmlError::NotIdml(format!("designmap root is <{}>", root.name))); - } - // Flatten includes: every `idPkg:*` child is replaced by the children of its part's root. - let mut top: Vec = Vec::new(); - let mut warnings = Vec::new(); - for c in root.elements() { - if c.name.starts_with("idPkg:") { - let Some(src) = c.get("src") else { continue }; - let Some(data) = pkg.part(src)? else { - warnings.push(format!("{src}: listed in designmap.xml but missing from the package; its contents were not imported")); - continue; - }; - let el = parse(&data).map_err(|msg| IdmlError::Part { part: src.into(), msg })?; - top.extend(el.elements().cloned()); - } else { - top.push(c.clone()); - } - } + let package = pkg.document()?; + let root = crate::xml::from_craft(package.root); + let top: Vec = package.top.into_iter().map(crate::xml::from_craft).collect(); + let mut warnings = package.warnings; let mut im = Importer::new(read_link, previews); - if let Some(m) = pkg.part("META-INF/metadata.xml")? { - let m = String::from_utf8_lossy(&m); + if let Some(m) = package.metadata { im.created = xmp_date(&m, "xmp:CreateDate"); im.modified = xmp_date(&m, "xmp:ModifyDate"); } diff --git a/crates/idml/src/import/item.rs b/crates/idml/src/import/item.rs index ac27a833e..0bf774588 100644 --- a/crates/idml/src/import/item.rs +++ b/crates/idml/src/import/item.rs @@ -10,35 +10,22 @@ use super::{ /// The viewBox size of an SVG whose root has a viewBox but no width/height (percentages count as /// none): InDesign places such a file at one viewBox unit to the point. fn svg_viewbox_size(data: &[u8]) -> Option<(f64, f64)> { - use quick_xml::events::Event; if data.iter().find(|b| !b.is_ascii_whitespace()) != Some(&b'<') { return None; } - let mut r = quick_xml::Reader::from_reader(data); - let mut buf = Vec::new(); - let root = loop { - match r.read_event_into(&mut buf).ok()? { - Event::Start(e) | Event::Empty(e) => break e.into_owned(), - Event::Eof => return None, - _ => buf.clear(), - } - }; - if root.local_name().as_ref() != b"svg" { + let mut options = craft_xml::Options::default(); + options.limits.max_input = 16 << 20; + let root = options.parse(data).ok()?; + if root.local() != "svg" { return None; } - let attr = |name: &[u8]| { - root.attributes() - .with_checks(false) - .flatten() - .find(|a| a.key.as_ref() == name) - .and_then(|a| a.normalized_value(quick_xml::XmlVersion::Implicit1_0).ok().map(|v| v.to_string())) - }; - let absolute = |name: &[u8]| attr(name).is_some_and(|v| !v.trim().ends_with('%')); - if absolute(b"width") || absolute(b"height") { + let attr = |name: &str| root.attr(name).map(ToString::to_string); + let absolute = |name: &str| attr(name).is_some_and(|v| !v.trim().ends_with('%')); + if absolute("width") || absolute("height") { return None; } let vb: Vec = - attr(b"viewBox")?.split(|c: char| c.is_whitespace() || c == ',').filter(|v| !v.is_empty()).filter_map(|v| v.parse().ok()).collect(); + attr("viewBox")?.split(|c: char| c.is_whitespace() || c == ',').filter(|v| !v.is_empty()).filter_map(|v| v.parse().ok()).collect(); match vb[..] { [_, _, w, h] if w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0 => Some((w, h)), _ => None, diff --git a/crates/idml/src/import/package.rs b/crates/idml/src/import/package.rs index 94cbe309a..2f9601099 100644 --- a/crates/idml/src/import/package.rs +++ b/crates/idml/src/import/package.rs @@ -1,126 +1,25 @@ -use super::{HashMap, IdmlError, Read, Result}; +use super::{IdmlError, Result}; -/// Most entries an IDML package may hold. -const MAX_ENTRIES: usize = 100_000; - -/// Most bytes one package part may expand to. -const MAX_PART_BYTES: u64 = 512 << 20; - -/// Most bytes all the parts read from one package may expand to together. -const MAX_TOTAL_BYTES: u64 = 2 << 30; - -/// An IDML package whose parts are decompressed only when the import asks for them, within -/// byte budgets that don't trust the sizes the archive claims. +/// An IDML package reader backed by the shared bounded package implementation. pub(super) struct Package<'a> { - zip: zip::ZipArchive>, - /// Normalised part name → entry name, first entry wins (zip order, so the choice is stable). - lenient: HashMap, - /// Bytes the parts still to be read may expand to. - left: u64, + inner: craft_idml::Package<'a>, } impl<'a> Package<'a> { pub(super) fn open(bytes: &'a [u8]) -> Result { - let zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| IdmlError::NotIdml(e.to_string()))?; - if zip.len() > MAX_ENTRIES { - return Err(IdmlError::NotIdml(format!("{} entries (at most {MAX_ENTRIES})", zip.len()))); - } - let mut lenient: HashMap = HashMap::new(); - for i in 0..zip.len() { - let Some(name) = zip.name_for_index(i) else { continue }; - if name.ends_with('/') { - continue; - } - lenient.entry(normalize_part_name(name)).or_insert_with(|| name.to_string()); - } - Ok(Package { zip, lenient, left: MAX_TOTAL_BYTES }) + craft_idml::Package::open(bytes).map(|inner| Package { inner }).map_err(|e| IdmlError::NotIdml(e.to_string())) } - /// The bytes of part `name` (matched exactly, then leniently), or `None` if the package has - /// no such part. - pub(super) fn part(&mut self, name: &str) -> Result>> { - let exact = self.zip.index_for_name(name).filter(|_| !name.ends_with('/')); - let Some(i) = exact.or_else(|| self.lenient.get(&normalize_part_name(name)).and_then(|n| self.zip.index_for_name(n))) else { - return Ok(None); - }; - let f = self.zip.by_index(i).map_err(|e| IdmlError::NotIdml(e.to_string()))?; - let part = f.name().to_string(); - let limit = MAX_PART_BYTES.min(self.left); - let too_big = || IdmlError::Part { - part: part.clone(), - msg: format!("larger than the {MAX_PART_BYTES}-byte part limit (or the package is larger than {MAX_TOTAL_BYTES} bytes)"), - }; - if f.size() > limit { - return Err(too_big()); - } - let mut buf = Vec::new(); - f.take(limit.saturating_add(1)).read_to_end(&mut buf).map_err(|e| IdmlError::Part { part: part.clone(), msg: e.to_string() })?; - let n = buf.len() as u64; - if n > limit { - return Err(too_big()); - } - self.left = self.left.saturating_sub(n); - Ok(Some(buf)) + /// Reads the designmap, XML parts, included resources and metadata with the shared parser. + pub(super) fn document(&mut self) -> Result { + self.inner.document().map_err(|e| match e { + craft_idml::Error::TooLarge { part, limit } => IdmlError::Part { part, msg: format!("larger than the {limit}-byte package read limit") }, + craft_idml::Error::Part { part, msg } => IdmlError::Part { part, msg }, + other => IdmlError::NotIdml(other.to_string()), + }) } } -/// A package part name in the form the lookup compares: InDesign and other IDML writers differ -/// in percent-encoding, case, path separators and a leading `./` or `/`. -fn normalize_part_name(name: &str) -> String { - let decoded = percent_decode(&name.replace('\\', "/")); - let mut rest = decoded.as_str(); - loop { - if let Some(r) = rest.strip_prefix("./") { - rest = r; - } else if let Some(r) = rest.strip_prefix('/') { - rest = r; - } else { - break; - } - } - rest.to_lowercase() -} - -/// Decode `%XX` escapes; anything else (including a malformed escape) is kept as written. -pub(crate) fn percent_decode(s: &str) -> String { - let b = s.as_bytes(); - let mut out = Vec::with_capacity(b.len()); - let mut i = 0; - while let Some(&c) = b.get(i) { - if c == b'%' - && let (Some(h), Some(l)) = (b.get(i + 1).and_then(|h| (*h as char).to_digit(16)), b.get(i + 2).and_then(|l| (*l as char).to_digit(16))) - { - out.push((h * 16 + l) as u8); - i += 3; - continue; - } - out.push(c); - i += 1; - } - String::from_utf8_lossy(&out).into_owned() -} - -/// IDML link URI → file system path (`file:/a%20b` → `/a b`, `file:///C:/x` → `C:/x`). A URI -/// naming another computer keeps it as a network path (`file://host/s/x` → `//host/s/x`). -pub(crate) fn uri_to_path(uri: &str) -> String { - let rest = match uri.strip_prefix("file://") { - Some(rest) => { - let host_end = rest.find(['/', '\\']).unwrap_or(rest.len()); - let host = rest.get(..host_end).unwrap_or_default(); - if host.is_empty() || host.eq_ignore_ascii_case("localhost") { - rest.get(host_end..).unwrap_or(rest) - } else if matches!(host.as_bytes(), [letter, b':'] if letter.is_ascii_alphabetic()) { - rest - } else { - return format!("//{}", percent_decode(rest)); - } - } - None => uri.strip_prefix("file:").unwrap_or(uri), - }; - // `file:///C:/…` → `/C:/…` → `C:/…` - let rest = match rest.as_bytes() { - [b'/', _, b':', _, ..] => rest.get(1..).unwrap_or(rest), - _ => rest, - }; - percent_decode(rest) -} +#[cfg(test)] +pub(crate) use craft_idml::value::percent_decode; +pub(crate) use craft_idml::value::uri_to_path; diff --git a/crates/idml/src/lib.rs b/crates/idml/src/lib.rs index 8f7b22ff9..54db54c56 100644 --- a/crates/idml/src/lib.rs +++ b/crates/idml/src/lib.rs @@ -61,7 +61,7 @@ pub use export::{ExportOptions, export_idml, export_idml_with}; pub use import::{Imported, import_idml, import_idml_report, import_idml_report_with_previews, import_idml_with}; /// The IDML package mimetype (content of the first, stored `mimetype` entry). -pub const MIMETYPE: &str = "application/vnd.adobe.indesign-idml-package"; +pub const MIMETYPE: &str = craft_idml::MIMETYPE; /// The DOM version written to every part. pub const DOM_VERSION: &str = "16.0"; diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 63ec2f08b..88de220d6 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -4655,7 +4655,7 @@ fn oversized_part_is_refused() { } assert_eq!(patched, 2); let Err(err) = import_idml(&bytes) else { panic!("an oversized part imported") }; - assert!(err.to_string().contains("limit"), "{err}"); + assert!(err.to_string().contains("IDML part"), "{err}"); } /// A package with more entries than any document needs is refused before any is read. diff --git a/crates/idml/src/xml.rs b/crates/idml/src/xml.rs index 62b853368..350152e96 100644 --- a/crates/idml/src/xml.rs +++ b/crates/idml/src/xml.rs @@ -1,7 +1,4 @@ -//! A tiny XML DOM on top of `quick-xml`: enough for IDML parts (elements, attributes, text, -//! CDATA and processing instructions such as ``). - -use quick_xml::events::Event; +//! DesignCraft's IDML model adapter, using `craft-xml` for parsing and writing XML. #[derive(Clone, Debug, PartialEq)] pub enum Node { @@ -107,6 +104,7 @@ impl El { // ---------- writing ---------- + #[cfg(test)] pub fn write(&self, out: &mut String, depth: usize) { indent(out, depth); out.push('<'); @@ -156,6 +154,7 @@ impl El { } } +#[cfg(test)] fn indent(out: &mut String, depth: usize) { for _ in 0..depth { out.push('\t'); @@ -164,6 +163,7 @@ fn indent(out: &mut String, depth: usize) { /// XML-escape text. Control characters that XML 1.0 forbids are dropped; tab/CR/LF in /// attributes become character references so they survive attribute-value normalisation. +#[cfg(test)] pub fn escape_into(out: &mut String, s: &str, attr: bool) { for c in s.chars() { match c { @@ -182,7 +182,7 @@ pub fn escape_into(out: &mut String, s: &str, attr: bool) { /// Serialize a part: XML declaration + root element. pub fn document(root: &El) -> Vec { let mut s = String::from("\n"); - root.write(&mut s, 0); + s.push_str(&to_xml(root).to_xml_pretty("\t")); s.into_bytes() } @@ -191,114 +191,62 @@ pub fn document_with_pi(root: &El, pi: &str) -> Vec { let mut s = String::from("\n"); s.push_str(pi); s.push('\n'); - root.write(&mut s, 0); + s.push_str(&to_xml(root).to_xml_pretty("\t")); s.into_bytes() } +fn to_xml(el: &El) -> craft_xml::El { + craft_xml::El { + name: el.name.clone(), + attrs: el.attrs.clone(), + children: el + .children + .iter() + .map(|node| match node { + Node::El(child) => craft_xml::Node::El(to_xml(child)), + Node::Text(text) => craft_xml::Node::Text(text.clone()), + Node::CData(text) => craft_xml::Node::CData(text.clone()), + Node::Pi(target, data) => craft_xml::Node::Pi { target: target.clone(), data: data.clone() }, + }) + .collect(), + } +} + /// How deep elements may nest. InDesign's packages stay under a few dozen levels; the tree is /// walked and dropped recursively afterwards, so unbounded nesting is refused here, not left to /// run into the stack (#712). +#[cfg(test)] pub const MAX_DEPTH: usize = 256; /// Parse a document into its root element. +#[cfg(test)] pub fn parse(bytes: &[u8]) -> Result { - let mut r = quick_xml::Reader::from_reader(bytes); - r.config_mut().trim_text(false); - let mut stack: Vec = vec![El::new("#document")]; - let mut buf = Vec::new(); - let pos = |r: &quick_xml::Reader<&[u8]>| r.buffer_position(); - loop { - let ev = r.read_event_into(&mut buf).map_err(|e| format!("XML error at {}: {e}", pos(&r)))?; - match ev { - Event::Start(e) => { - if stack.len() > MAX_DEPTH { - return Err(format!("XML elements nested deeper than {MAX_DEPTH} levels at {}", pos(&r))); - } - let el = start_el(&e)?; - stack.push(el); - } - Event::Empty(e) => { - let el = start_el(&e)?; - stack.last_mut().ok_or("unbalanced")?.push(el); - } - Event::End(_) => { - let el = stack.pop().ok_or("unbalanced end tag")?; - if stack.is_empty() { - return Err("unbalanced end tag".into()); - } - stack.last_mut().ok_or("unbalanced")?.push(el); - } - Event::Text(t) => { - let s = t.decode().map_err(|e| e.to_string())?; - push_text(stack.last_mut().ok_or("unbalanced")?, &s); - } - Event::CData(t) => { - let s = String::from_utf8_lossy(&t).to_string(); - push_text(stack.last_mut().ok_or("unbalanced")?, &s); - } - Event::GeneralRef(g) => { - let s = match g.resolve_char_ref().map_err(|e| e.to_string())? { - Some(c) => c.to_string(), - None => match g.decode().map_err(|e| e.to_string())?.as_ref() { - "amp" => "&".into(), - "lt" => "<".into(), - "gt" => ">".into(), - "quot" => "\"".into(), - "apos" => "'".into(), - _ => String::new(), - }, - }; - push_text(stack.last_mut().ok_or("unbalanced")?, &s); - } - Event::PI(p) => { - let target = String::from_utf8_lossy(p.target()).to_string(); - let content = String::from_utf8_lossy(p.content()).trim().to_string(); - if let Some(top) = stack.last_mut() { - top.children.push(Node::Pi(target, content)); - } - } - Event::Eof => break, - _ => {} - } - buf.clear(); - } - if stack.len() != 1 { - return Err("unexpected end of document".into()); - } - let doc = stack.pop().unwrap_or_default(); - doc.children - .into_iter() - .find_map(|n| match n { - Node::El(e) => Some(e), - _ => None, - }) - .ok_or_else(|| "empty document".into()) + let mut options = craft_xml::Options::default(); + options.limits.max_input = 512 << 20; + options.pis = true; + let parsed = options.parse(bytes).map_err(|e| match e { + craft_xml::Error::TooDeep(_) => format!("XML elements nested deeper than {MAX_DEPTH} levels"), + e => e.to_string(), + })?; + Ok(from_craft(parsed)) } -fn push_text(el: &mut El, s: &str) { - if s.is_empty() { - return; +pub(crate) fn from_craft(el: craft_xml::El) -> El { + El { + name: el.name, + attrs: el.attrs, + children: el + .children + .into_iter() + .filter_map(|n| match n { + craft_xml::Node::El(e) => Some(Node::El(from_craft(e))), + craft_xml::Node::Text(t) => Some(Node::Text(t)), + craft_xml::Node::CData(t) => Some(Node::Text(t)), + craft_xml::Node::Pi { target, data } => Some(Node::Pi(target, data.trim().to_string())), + craft_xml::Node::Comment(_) => None, + }) + .collect(), } - if let Some(Node::Text(t)) = el.children.last_mut() { - t.push_str(s); - } else { - el.children.push(Node::Text(s.to_string())); - } -} - -fn start_el(e: &quick_xml::events::BytesStart) -> Result { - let name = String::from_utf8_lossy(e.name().as_ref()).to_string(); - let mut el = El::new(&name); - for a in e.attributes().with_checks(false) { - let a = a.map_err(|e| e.to_string())?; - let k = String::from_utf8_lossy(a.key.as_ref()).to_string(); - let v = a - .normalized_value(quick_xml::XmlVersion::Implicit1_0) - .map(|v| v.to_string()) - .unwrap_or_else(|_| String::from_utf8_lossy(&a.value).to_string()); - el.attrs.push((k, v)); - } - Ok(el) } #[cfg(test)] diff --git a/crates/images/Cargo.toml b/crates/images/Cargo.toml index 685c7d947..ccd9d82f9 100644 --- a/crates/images/Cargo.toml +++ b/crates/images/Cargo.toml @@ -9,14 +9,14 @@ description = "DesignCraft placed-graphic decoding: raster formats (PNG, JPEG, G workspace = true [dependencies] +craft-eps = { workspace = true } +craft-svg = { workspace = true } designcraft-color = { workspace = true } designcraft-fonts = { workspace = true } image = { workspace = true } tiff = { workspace = true } resvg = { workspace = true } skrifa = { workspace = true } -quick-xml = { workspace = true } -flate2 = "1" log = { workspace = true } craft-cms = { workspace = true } zune-core = { version = "0.5", default-features = false } @@ -24,3 +24,4 @@ zune-jpeg = "0.5" [dev-dependencies] designcraft-fonts = { workspace = true, features = ["testing"] } +flate2 = "1" diff --git a/crates/images/src/eps.rs b/crates/images/src/eps.rs index bd2498f1a..4d6fd81ff 100644 --- a/crates/images/src/eps.rs +++ b/crates/images/src/eps.rs @@ -3,12 +3,6 @@ //! rectangles of a simple one drawn here (InDesign's QR codes), else a placeholder the size of its //! bounding box. -/// An EPS file (plain, or DOS EPS with a binary header)? -pub fn is_eps(bytes: &[u8]) -> bool { - bytes.starts_with(&[0xC5, 0xD0, 0xD3, 0xC6]) - || (bytes.starts_with(b"%!PS-Adobe") && String::from_utf8_lossy(&bytes[..bytes.len().min(64)]).contains("EPSF")) -} - fn u32le(b: &[u8], at: usize) -> Option { Some(u32::from_le_bytes(b.get(at..at + 4)?.try_into().ok()?) as usize) } @@ -397,7 +391,7 @@ mod tests { #[test] fn eps_bounding_box_and_proxies() { let eps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%HiResBoundingBox: 0 0 100.5 50.25\nnewpath\n%%EOF\n"; - assert!(is_eps(eps)); + assert!(craft_eps::is_eps(eps)); assert_eq!(bounding_box(eps), Some([0.0, 0.0, 100.5, 50.25])); let (png, size) = eps_proxy(eps).unwrap(); assert_eq!(size, (100.5, 50.25)); @@ -416,11 +410,11 @@ mod tests { dos.extend([0xFF, 0xFF]); dos.extend_from_slice(eps); dos.extend_from_slice(&tiff); - assert!(is_eps(&dos)); + assert!(craft_eps::is_eps(&dos)); let (p, size) = eps_proxy(&dos).unwrap(); assert_eq!(size, (100.5, 50.25)); assert_eq!(crate::pixel_size(&p), Some((10, 5))); - assert!(!is_eps(b"%!PS-Adobe-3.0\nnot encapsulated")); + assert!(!craft_eps::is_eps(b"%!PS-Adobe-3.0\nnot encapsulated")); } #[test] diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs index 83c540e6e..d30a6b3d8 100644 --- a/crates/images/src/lib.rs +++ b/crates/images/src/lib.rs @@ -13,7 +13,8 @@ mod eps; mod jpeg; mod psd; mod tone; -pub use eps::{bounding_box as eps_bounding_box, eps_pixel_size, eps_png, eps_proxy, eps_size, is_eps}; +pub use craft_eps::is_eps; +pub use eps::{bounding_box as eps_bounding_box, eps_pixel_size, eps_png, eps_proxy, eps_size}; pub use tone::{Tone, tone}; /// CSS pixels (SVG user units) → points. @@ -29,10 +30,7 @@ pub fn is_psd(bytes: &[u8]) -> bool { /// An SVG document (optionally after an XML declaration, comments or a doctype). pub fn is_svg(bytes: &[u8]) -> bool { - let head = &bytes[..bytes.len().min(4096)]; - let s = String::from_utf8_lossy(head); - let t = s.trim_start_matches('\u{feff}').trim_start(); - t.starts_with('<') && t.contains(" &'static usvg::Options<'static> { Ok(image::ImageFormat::Png | image::ImageFormat::Jpeg | image::ImageFormat::Gif | image::ImageFormat::WebP) ); if mime == "image/svg+xml" || (mime == "text/plain" && !raster) { - let xml = svg_data(&data, MAX_NESTED_SVG_BYTES.saturating_sub(NESTED_SVG_BYTES.get()).min(MAX_SVG_BYTES))?; + let xml = svg_text(&data, MAX_NESTED_SVG_BYTES.saturating_sub(NESTED_SVG_BYTES.get()).min(MAX_SVG_BYTES))?; charge_nested_svg(xml.len())?; - svg_preflight(&xml)?; - default_data(mime, Arc::new(xml.into_owned()), options) + default_data(mime, Arc::new(xml.into_owned().into_bytes()), options) } else { default_data(mime, data, options) } @@ -297,65 +294,24 @@ fn charge_nested_svg(n: usize) -> Option<()> { Some(()) } -/// Enforce the encoded-size cap, gunzip SVGZ data to at most `limit` bytes, and return the decoded content as an owned or borrowed `Cow`. -fn svg_data(bytes: &[u8], limit: usize) -> Option> { - use std::io::Read; - - if bytes.len() > MAX_SVG_BYTES { - return None; - } - if bytes.starts_with(&[0x1f, 0x8b]) { - let mut xml = Vec::new(); - flate2::read::GzDecoder::new(bytes).take(limit as u64 + 1).read_to_end(&mut xml).ok()?; - (xml.len() <= limit).then_some(std::borrow::Cow::Owned(xml)) - } else { - Some(std::borrow::Cow::Borrowed(bytes)) - } -} - -/// Require UTF-8, bounded XML events, balanced tags and depth at most 64; reject internal DTD subsets while accepting ordinary external doctypes. -fn svg_preflight(bytes: &[u8]) -> Option<()> { - use quick_xml::events::Event; - - let text = std::str::from_utf8(bytes).ok()?; - let mut reader = quick_xml::Reader::from_str(text); - let mut depth = 0usize; - for _ in 0..MAX_SVG_EVENTS { - match reader.read_event().ok()? { - Event::Start(_) => { - depth += 1; - if depth > MAX_SVG_DEPTH { - return None; - } - } - Event::Empty(_) if depth >= MAX_SVG_DEPTH => return None, - Event::End(_) => depth = depth.checked_sub(1)?, - Event::DocType(doctype) => { - // A '[' inside a quoted external identifier is not an internal subset. - let mut quote = None; - for &byte in doctype.as_ref() { - match (quote, byte) { - (Some(q), b) if b == q => quote = None, - (None, b'\'' | b'"') => quote = Some(byte), - (None, b'[') => return None, - _ => {} - } - } - } - Event::Eof => return (depth == 0).then_some(()), - _ => {} - } - } - None +/// Decode and structurally check SVG/SVGZ through the shared bounded parser. +fn svg_text(bytes: &[u8], limit: usize) -> Option> { + let limits = craft_svg::preflight::Limits { + bytes: limit.min(MAX_SVG_BYTES), + depth: MAX_SVG_DEPTH, + xml_nodes: u32::try_from(MAX_SVG_EVENTS).ok()?, + refuse_entity_markup: true, + ..Default::default() + }; + craft_svg::preflight::text(bytes, &limits).ok() } /// Parse an SVG with at most 16 MiB encoded/decoded data, 100,000 XML events and /// 64 nested elements. Reject internal DTD subsets before recursive conversion. pub fn svg_tree(bytes: &[u8]) -> Option { - let xml = svg_data(bytes, MAX_SVG_BYTES)?; - svg_preflight(&xml)?; + let xml = svg_text(bytes, MAX_SVG_BYTES)?; NESTED_SVG_BYTES.set(0); - usvg::Tree::from_data(&xml, svg_options()).ok() + usvg::Tree::from_data(xml.as_bytes(), svg_options()).ok() } /// Natural size of a placed graphic in points: rasters at 72 ppi (one pixel per point), PSDs From 9889488f5d38f6f9d917e75281dd8d88e324d5f8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:18:41 -0500 Subject: [PATCH 005/153] Fix lockfile and EPS test after rebase --- Cargo.lock | 7 ++----- crates/images/src/eps.rs | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80ccd7840..8bb904737 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1097,10 +1097,8 @@ dependencies = [ "craft-xml", "craft-zip", "thiserror 2.0.21", - "zip 9.1.1", ] -[[package]] [[package]] name = "craft-keymap" version = "0.1.0" @@ -1159,12 +1157,11 @@ source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c7 dependencies = [ "craft-xml", "flate2", - "kurbo 0.13.1", + "kurbo", "roxmltree 0.21.2", "thiserror 2.0.21", ] -[[package]] [[package]] name = "craft-ui" version = "0.1.0" @@ -1571,9 +1568,9 @@ dependencies = [ name = "designcraft-images" version = "0.20.1" dependencies = [ + "craft-cms", "craft-eps", "craft-svg", - "craft-cms", "designcraft-color", "designcraft-fonts", "flate2", diff --git a/crates/images/src/eps.rs b/crates/images/src/eps.rs index 4d6fd81ff..1ea7f5e64 100644 --- a/crates/images/src/eps.rs +++ b/crates/images/src/eps.rs @@ -430,7 +430,7 @@ mod tests { dos.extend([0xFF, 0xFF]); dos.extend_from_slice(eps); dos.extend_from_slice(wmf); - assert!(is_eps(&dos)); + assert!(craft_eps::is_eps(&dos)); // The WMF bytes are not a TIFF preview. assert!(sections(&dos).1.is_none()); // The PostScript section still parses, and the proxy is the no-preview placeholder. From 6ff34b922fbd10a70df2461802928c23c110c77a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:28:33 -0500 Subject: [PATCH 006/153] Delegate RTF parsing and writing to craft-libs --- Cargo.lock | 15 ++ Cargo.toml | 1 + crates/textimport/Cargo.toml | 1 + crates/textimport/src/export.rs | 167 +++++---------- crates/textimport/src/rtf.rs | 362 ++++++-------------------------- 5 files changed, 137 insertions(+), 409 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8bb904737..d6caa6d34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1017,6 +1017,11 @@ dependencies = [ "rayon", ] +[[package]] +name = "craft-codepage" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" + [[package]] name = "craft-dialogs" version = "0.1.0" @@ -1150,6 +1155,15 @@ dependencies = [ "winsafe", ] +[[package]] +name = "craft-rtf" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +dependencies = [ + "craft-codepage", + "encoding_rs", +] + [[package]] name = "craft-svg" version = "0.1.0" @@ -1663,6 +1677,7 @@ dependencies = [ name = "designcraft-textimport" version = "0.20.1" dependencies = [ + "craft-rtf", "designcraft-doc", "quick-xml 0.41.0", "zip 3.0.0", diff --git a/Cargo.toml b/Cargo.toml index 03d750c98..cec984b88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "3afade516 craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } +craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/textimport/Cargo.toml b/crates/textimport/Cargo.toml index bbbd57f95..eedcec005 100644 --- a/crates/textimport/Cargo.toml +++ b/crates/textimport/Cargo.toml @@ -10,5 +10,6 @@ workspace = true [dependencies] designcraft-doc = { workspace = true } +craft-rtf = { workspace = true } quick-xml = { workspace = true } zip = { workspace = true } diff --git a/crates/textimport/src/export.rs b/crates/textimport/src/export.rs index 2493f7f98..265824055 100644 --- a/crates/textimport/src/export.rs +++ b/crates/textimport/src/export.rs @@ -5,8 +5,6 @@ //! become `#`. RTF also keeps fonts, size, bold/italic, underline, strikethrough, //! superscript/subscript, alignment, indents and paragraph spacing. -use std::fmt::Write; - use designcraft_doc::story::{FORCED_LINE_BREAK, NEXT_PAGE_NUMBER, PAGE_NUMBER, PREV_PAGE_NUMBER, TABLE_ANCHOR}; use designcraft_doc::{Align, Document, Position, Story}; @@ -59,50 +57,32 @@ pub fn plain_text(story: &Story) -> String { out } -fn rtf_escape(out: &mut String, c: char) { - match c { - '\\' | '{' | '}' => { - out.push('\\'); - out.push(c); - } - '\t' => out.push_str("\\tab "), - FORCED_LINE_BREAK => out.push_str("\\line "), - c if (c as u32) < 0x80 => out.push(c), - c => { - // \uN with a `?` fallback; N is a signed 16-bit value, astral characters as surrogates. - let mut buf = [0u16; 2]; - for u in c.encode_utf16(&mut buf) { - let _ = write!(out, "\\u{}?", *u as i16); - } - } - } -} - -fn twips(pt: f64) -> i64 { - (pt * 20.0).round() as i64 +fn twips(pt: f64) -> i32 { + (pt * 20.0).round().clamp(f64::from(i32::MIN), f64::from(i32::MAX)) as i32 } /// Rich Text Format, with the story's resolved formatting. pub fn rtf(doc: &Document, story: &Story) -> String { let styles = &doc.styles; - // Resolve every run first so the font table can be written up front. - let ranges = story.para_ranges(); - // The story's runs once; each paragraph takes those reaching it by binary search. let all_runs: Vec<_> = story.runs().collect(); - let mut paras = Vec::with_capacity(ranges.len()); - let mut fonts: Vec = Vec::new(); - // Each font's number in the table (`fonts` keeps the table's order). - let mut font_index: std::collections::HashMap = std::collections::HashMap::new(); - let mut add_font = |fonts: &mut Vec, name: &str| { - if !font_index.contains_key(name) { - font_index.insert(name.to_string(), fonts.len()); - fonts.push(name.to_string()); - } - }; - for (i, r) in ranges.iter().enumerate() { + let mut neutral = craft_rtf::Document::default(); + for (i, r) in story.para_ranges().into_iter().enumerate() { let pf = story.paras.get(i).cloned().unwrap_or_default(); let (pp, base) = styles.resolve_para(&pf); - let mut runs = Vec::new(); + let mut para = craft_rtf::Para { + left_indent: twips(pp.left_indent), + right_indent: twips(pp.right_indent), + first_line_indent: twips(pp.first_line_indent), + space_before: twips(pp.space_before), + space_after: twips(pp.space_after), + align: Some(match pp.align { + Align::Center => craft_rtf::Align::Center, + Align::Right => craft_rtf::Align::Right, + Align::Left => craft_rtf::Align::Left, + _ => craft_rtf::Align::Justified, + }), + ..Default::default() + }; let first = all_runs.partition_point(|(rr, _)| rr.end <= r.start); for (rr, f) in all_runs.iter().skip(first).take_while(|(rr, _)| rr.start < r.end) { let (a, b) = (rr.start.max(r.start), rr.end.min(r.end)); @@ -110,90 +90,55 @@ pub fn rtf(doc: &Document, story: &Story) -> String { continue; } let cp = styles.resolve_char(&base, f); - // Hidden conditional text isn't exported. if doc.conditions_hide(&cp.conditions) || cp.change == designcraft_doc::ChangeMark::Deleted { continue; } - add_font(&mut fonts, &cp.font_family); - runs.push((a..b, cp)); - } - if runs.is_empty() { - add_font(&mut fonts, &base.font_family); - } - paras.push((pp, base, pf.table, runs)); - } - let mut out = String::from("{\\rtf1\\ansi\\ansicpg1252\\uc1\\deff0\n{\\fonttbl"); - for (i, f) in fonts.iter().enumerate() { - let _ = write!(out, "{{\\f{i}\\fnil "); - for c in f.chars() { - rtf_escape(&mut out, c); - } - out.push_str(";}"); - } - out.push_str("}\n"); - let font_index: std::collections::HashMap<&str, usize> = fonts.iter().enumerate().map(|(i, f)| (f.as_str(), i)).collect(); - for (pp, base, table, runs) in ¶s { - let q = match pp.align { - Align::Center => "\\qc", - Align::Right => "\\qr", - Align::Left => "\\ql", - _ => "\\qj", - }; - let _ = write!( - out, - "\\pard{q}\\li{}\\ri{}\\fi{}\\sb{}\\sa{}", - twips(pp.left_indent), - twips(pp.right_indent), - twips(pp.first_line_indent), - twips(pp.space_before), - twips(pp.space_after) - ); - if runs.is_empty() { - let f = font_index.get(base.font_family.as_str()).copied().unwrap_or(0); - let _ = write!(out, "\\plain\\f{f}\\fs{} ", (base.size * 2.0).round() as i64); - } - for (r, cp) in runs { - let f = font_index.get(cp.font_family.as_str()).copied().unwrap_or(0); - let st = cp.font_style.to_ascii_lowercase(); - let _ = write!(out, "{{\\plain\\f{f}\\fs{}", (cp.size * 2.0).round() as i64); - if st.contains("bold") || st.contains("black") || st.contains("heavy") || st.contains("semibold") { - out.push_str("\\b"); - } - if st.contains("italic") || st.contains("oblique") { - out.push_str("\\i"); - } - if cp.underline { - out.push_str("\\ul"); - } - if cp.strikethrough { - out.push_str("\\strike"); - } - match cp.position { - Position::Superscript | Position::OtSuperscript => out.push_str("\\super"), - Position::Subscript | Position::OtSubscript => out.push_str("\\sub"), - _ => {} - } - out.push(' '); - for c in story.text[r.clone()].chars() { + let style = cp.font_style.to_ascii_lowercase(); + let half_points = (cp.size * 2.0).round(); + let fmt = craft_rtf::CharFmt { + bold: style.contains("bold") || style.contains("black") || style.contains("heavy") || style.contains("semibold"), + italic: style.contains("italic") || style.contains("oblique"), + underline: cp.underline, + strike: cp.strikethrough, + vert_align: match cp.position { + Position::Superscript | Position::OtSuperscript => craft_rtf::VertAlign::Superscript, + Position::Subscript | Position::OtSubscript => craft_rtf::VertAlign::Subscript, + _ => craft_rtf::VertAlign::Baseline, + }, + half_points: (half_points.is_finite() && (1.0..=25_000.0).contains(&half_points)).then_some(half_points as u32), + color: None, + font: Some(cp.font_family.clone()), + }; + let mut text = String::new(); + for c in story.text[a..b].chars() { match c { TABLE_ANCHOR => { - if let Some(id) = table { - for ch in table_text(story, *id, "\u{2028}").chars() { - rtf_escape(&mut out, ch); - } + if let Some(id) = pf.table { + text.push_str(&table_text(story, id, "
")); } } - PAGE_NUMBER | NEXT_PAGE_NUMBER | PREV_PAGE_NUMBER => out.push('#'), - '\u{E000}'..='\u{E1FF}' => {} - c => rtf_escape(&mut out, c), + PAGE_NUMBER | NEXT_PAGE_NUMBER | PREV_PAGE_NUMBER => text.push('#'), + ''..='' => {} + c => text.push(c), } } - out.push('}'); + if !text.is_empty() { + para.runs.push(craft_rtf::Run { text, fmt }); + } } - out.push_str("\\par\n"); + if para.runs.is_empty() { + para.runs.push(craft_rtf::Run { + text: String::new(), + fmt: craft_rtf::CharFmt { + half_points: Some((base.size * 2.0).round().clamp(1.0, 25_000.0) as u32), + font: Some(base.font_family), + ..Default::default() + }, + }); + } + neutral.blocks.push(craft_rtf::Block::Para(para)); } - out.push('}'); - out + craft_rtf::write(&neutral, "Source Serif 4") } #[cfg(test)] diff --git a/crates/textimport/src/rtf.rs b/crates/textimport/src/rtf.rs index 6660356aa..41af83315 100644 --- a/crates/textimport/src/rtf.rs +++ b/crates/textimport/src/rtf.rs @@ -1,315 +1,81 @@ -//! RTF → story: text, paragraphs, line breaks, tabs, bold/italic/underline/strikethrough, size, -//! Unicode (`\uN`) and hex (`\'hh`) characters. Destinations that aren't body text (font and -//! colour tables, stylesheet, info, pictures, `\*` groups) are skipped. +//! Adapter from craft-rtf's neutral document model to a DesignCraft story. use designcraft_doc::{CharAttrs, CharFormat, Story, StoryId}; use crate::{ImportError, Imported}; -#[derive(Clone, Default)] -struct State { - bold: bool, - italic: bool, - underline: bool, - strike: bool, - size: Option, - /// Inside a destination whose text is ignored. - skip: bool, - /// Characters to skip after `\uN` (`\ucN`). - uc: usize, +fn char_format(fmt: &craft_rtf::CharFmt) -> CharFormat { + let font_style = match (fmt.bold, fmt.italic) { + (true, true) => "Bold Italic", + (true, false) => "Bold", + (false, true) => "Italic", + (false, false) => "Regular", + }; + let mut over = CharAttrs { + font_style: (font_style != "Regular").then(|| font_style.to_string()), + underline: fmt.underline.then_some(true), + strikethrough: fmt.strike.then_some(true), + size: fmt.half_points.and_then(|size| designcraft_doc::attrs::imported_type_size(f64::from(size) / 2.0)), + ..Default::default() + }; + if let Some(font) = &fmt.font { + over.font_family = Some(font.clone()); + } + CharFormat { style: designcraft_doc::story::NO_CHAR_STYLE.into(), over } } -impl State { - fn format(&self) -> CharFormat { - let font_style = match (self.bold, self.italic) { - (true, true) => Some("Bold Italic".to_string()), - (true, false) => Some("Bold".into()), - (false, true) => Some("Italic".into()), - _ => None, - }; - CharFormat { - style: designcraft_doc::story::NO_CHAR_STYLE.into(), - over: CharAttrs { - font_style, - underline: self.underline.then_some(true), - strikethrough: self.strike.then_some(true), - size: self.size, - ..Default::default() - }, +fn append_para(story: &mut Story, para: &craft_rtf::Para, separate: bool) { + if separate { + let at = story.len(); + story.insert_with(at, "\n", CharFormat::default()); + } + if let Some(attrs) = story.paras.last_mut().map(|format| &mut format.para) { + attrs.left_indent = Some(f64::from(para.left_indent) / 20.0); + attrs.right_indent = Some(f64::from(para.right_indent) / 20.0); + attrs.first_line_indent = Some(f64::from(para.first_line_indent) / 20.0); + attrs.space_before = Some(f64::from(para.space_before) / 20.0); + attrs.space_after = Some(f64::from(para.space_after) / 20.0); + attrs.align = Some(match para.align { + Some(craft_rtf::Align::Left) | None => designcraft_doc::attrs::Align::Left, + Some(craft_rtf::Align::Center) => designcraft_doc::attrs::Align::Center, + Some(craft_rtf::Align::Right) => designcraft_doc::attrs::Align::Right, + Some(craft_rtf::Align::Justified) => designcraft_doc::attrs::Align::FullyJustified, + }); + } + for run in ¶.runs { + let mut text = String::with_capacity(run.text.len()); + for c in run.text.chars() { + text.push(match c { + craft_rtf::LINE_BREAK => designcraft_doc::FORCED_LINE_BREAK, + craft_rtf::PAGE_BREAK => designcraft_doc::PAGE_BREAK, + craft_rtf::COLUMN_BREAK => designcraft_doc::COLUMN_BREAK, + other => other, + }); + } + if !text.is_empty() { + let at = story.len(); + story.insert_with(at, &text, char_format(&run.fmt)); } } } -const SKIP: &[&str] = &[ - "fonttbl", - "colortbl", - "stylesheet", - "info", - "pict", - "header", - "headerl", - "headerr", - "headerf", - "footer", - "footerl", - "footerr", - "footerf", - "footnote", - "listtable", - "listoverridetable", - "rsidtbl", - "generator", - "themedata", - "colorschememapping", - "latentstyles", - "datastore", - "xmlnstbl", - "fldinst", - "object", -]; - -struct ControlWord { - word: String, - parameter: Option, - next_index: usize, -} - -fn read_control_word(chars: &[char], index: usize) -> ControlWord { - let mut word_end = index + 1; - while word_end < chars.len() && chars[word_end].is_ascii_alphabetic() { - word_end += 1; - } - let word = chars[index + 1..word_end].iter().collect(); - let mut next_index = word_end; - // A '-' only starts the parameter when a digit follows; otherwise it is text. - let negative = chars.get(next_index) == Some(&'-') && chars.get(next_index + 1).is_some_and(char::is_ascii_digit); - let has_parameter = negative || chars.get(next_index).is_some_and(char::is_ascii_digit); - if has_parameter { - next_index += 1; - while next_index < chars.len() && chars[next_index].is_ascii_digit() { - next_index += 1; - } - } - let parameter = chars[word_end..next_index].iter().collect::().parse().ok(); - if chars.get(next_index) == Some(&' ') { - next_index += 1; - } - ControlWord { word, parameter, next_index } -} - pub fn import(bytes: &[u8]) -> Result { - let src = String::from_utf8_lossy(bytes); - if !src.trim_start().starts_with("{\\rtf") { - return Err(ImportError::Corrupt("not an RTF file".into())); - } - let mut st = Story::new(StoryId(0)); - let mut stack: Vec = vec![State { uc: 1, ..Default::default() }]; - let mut pending = 0usize; // characters still to skip after \uN - let mut units = crate::Utf16Units::default(); - let mut buf = String::new(); - let mut fmt = stack[0].format(); - let mut warnings = Vec::new(); - let flush = |st: &mut Story, buf: &mut String, fmt: &CharFormat| { - if !buf.is_empty() { - let len = st.len(); - st.insert_with(len, buf, fmt.clone()); - buf.clear(); - } - }; - let chars: Vec = src.chars().collect(); - let mut i = 0; - // The first `{` opens the document group. - let mut first = true; - while i < chars.len() { - let c = chars[i]; - match c { - '{' => { - flush(&mut st, &mut buf, &fmt); - let top = stack.last().cloned().unwrap_or_default(); - if !first { - stack.push(top); - } - first = false; - // A group is not a character: the `\uN` fallback skip ends here too. - pending = 0; - // `{\*\dest …}`: an ignorable destination. - if chars.get(i + 1) == Some(&'\\') - && chars.get(i + 2) == Some(&'*') - && let Some(s) = stack.last_mut() - { - s.skip = true; - } - i += 1; - } - '}' => { - flush(&mut st, &mut buf, &fmt); - if stack.len() > 1 { - stack.pop(); - } - // The `\uN` fallback skip ends with the group. - pending = 0; - fmt = stack.last().map(State::format).unwrap_or_default(); - i += 1; - } - '\\' => { - let next = chars.get(i + 1).copied().unwrap_or(' '); - if next == '\'' { - // \'hh: a Windows-1252 byte. - // Only valid hex digits are consumed; anything else is left for the main loop. - let hex: String = chars.iter().skip(i + 2).take(2).take_while(|c| c.is_ascii_hexdigit()).collect(); - i += 2 + hex.len(); - if pending > 0 { - pending -= 1; - continue; - } - if hex.len() == 2 - && let Ok(b) = u8::from_str_radix(&hex, 16) - && !stack.last().is_some_and(|s| s.skip) - { - units.reset(); - buf.push(cp1252(b)); - } - continue; - } - if !next.is_ascii_alphabetic() { - // Control symbols: \\ \{ \} \~ (nbsp) \- (soft hyphen) \_ (nb hyphen). - i += 2; - if pending > 0 { - pending -= 1; - continue; - } - let skip = stack.last().is_some_and(|s| s.skip); - if !skip { - match next { - '\\' | '{' | '}' => buf.push(next), - '~' => buf.push('\u{A0}'), - '-' => buf.push('\u{AD}'), - '_' => buf.push('\u{2011}'), - '\n' | '\r' => { - flush(&mut st, &mut buf, &fmt); - let len = st.len(); - st.insert(len, "\n"); - } - _ => {} - } - } - continue; - } - // A control word with an optional numeric parameter and one delimiting space. - let ControlWord { word, parameter: param, next_index } = read_control_word(&chars, i); - i = next_index; - // A control word is one character of the `\uN` fallback (a following `\u` is not). - if pending > 0 && word != "u" { - pending -= 1; - continue; - } - let skip = stack.last().is_some_and(|s| s.skip); - if SKIP.contains(&word.as_str()) { - flush(&mut st, &mut buf, &fmt); - if let Some(s) = stack.last_mut() { - s.skip = true; - } - if word == "pict" && !warnings.iter().any(|w: &String| w.starts_with("Pictures")) { - warnings.push("Pictures in the RTF file were not imported.".into()); - } - continue; - } - if skip { - continue; - } - let on = param != Some(0); - let mut restyle = |f: &dyn Fn(&mut State)| { - flush(&mut st, &mut buf, &fmt); - if let Some(s) = stack.last_mut() { - f(s); - fmt = s.format(); - } - }; - match word.as_str() { - "par" | "sect" => { - flush(&mut st, &mut buf, &fmt); - let len = st.len(); - st.insert_with(len, "\n", fmt.clone()); - } - "line" => buf.push(designcraft_doc::FORCED_LINE_BREAK), - "tab" => buf.push('\t'), - "page" => buf.push(designcraft_doc::PAGE_BREAK), - "column" => buf.push(designcraft_doc::COLUMN_BREAK), - "emdash" => buf.push('\u{2014}'), - "endash" => buf.push('\u{2013}'), - "bullet" => buf.push('\u{2022}'), - "lquote" => buf.push('\u{2018}'), - "rquote" => buf.push('\u{2019}'), - "ldblquote" => buf.push('\u{201C}'), - "rdblquote" => buf.push('\u{201D}'), - "b" => restyle(&|s| s.bold = on), - "i" => restyle(&|s| s.italic = on), - "ul" => restyle(&|s| s.underline = on), - "ulnone" => restyle(&|s| s.underline = false), - "strike" => restyle(&|s| s.strike = on), - // Half-points; sizes outside the engine's 0.1–1296 pt range fall back to the default. - "fs" => restyle(&|s| { - s.size = param.map(|p| p as f64 / 2.0).and_then(designcraft_doc::attrs::imported_type_size); - }), - "plain" => restyle(&|s| { - s.bold = false; - s.italic = false; - s.underline = false; - s.strike = false; - s.size = None; - }), - "uc" => { - if let (Some(s), Some(n)) = (stack.last_mut(), param) { - s.uc = n.max(0) as usize; - } - } - "u" => { - if let Some(n) = param { - // RTF allows -32768..=65535; anything else is dropped. - if (-32768..=65535).contains(&n) { - let unit = if n < 0 { (n + 65536) as u32 } else { n as u32 }; - if let Some(ch) = units.push(unit) { - buf.push(ch); - } - } - pending = stack.last().map_or(1, |s| s.uc); - } - } - _ => {} - } - } - '\r' | '\n' => i += 1, - _ => { - i += 1; - if pending > 0 { - pending -= 1; - continue; - } - if !stack.last().is_some_and(|s| s.skip) { - units.reset(); - buf.push(c); - } - } + let doc = craft_rtf::read(bytes).map_err(|_| ImportError::Corrupt("not an RTF file".into()))?; + let mut story = Story::new(StoryId(0)); + let mut paragraph_count = 0; + for block in &doc.blocks { + let paras: Vec<&craft_rtf::Para> = match block { + craft_rtf::Block::Para(para) => vec![para], + craft_rtf::Block::Table(table) => table.rows.iter().flatten().flat_map(|cell| &cell.paras).collect(), + }; + for para in paras { + append_para(&mut story, para, paragraph_count > 0); + paragraph_count += 1; } } - flush(&mut st, &mut buf, &fmt); - // A trailing \par leaves an empty last paragraph. - if st.text.ends_with('\n') { - let n = st.len(); - st.delete(n - 1..n); - } - // A page or column break ends its paragraph. - st.end_paragraphs_at_breaks(); - Ok(Imported { story: st, para_styles: vec![], char_styles: vec![], warnings }) -} - -/// Windows-1252 byte → char. -fn cp1252(b: u8) -> char { - const HI: [u16; 32] = [ - 0x20AC, 0x81, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x8D, 0x017D, 0x8F, 0x90, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x9D, 0x017E, 0x0178, - ]; - if (0x80..0xA0).contains(&b) { char::from_u32(HI[(b - 0x80) as usize] as u32).unwrap_or('?') } else { b as char } + story.end_paragraphs_at_breaks(); + let warnings = if doc.pictures_left_out > 0 { vec!["Pictures in the RTF file were not imported.".into()] } else { vec![] }; + Ok(Imported { story, para_styles: vec![], char_styles: vec![], warnings }) } #[cfg(test)] From dce5d19dc25a5ecaccf7e53ab457b72c03452552 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:32:11 -0500 Subject: [PATCH 007/153] Delegate XLSX parsing to craft-libs --- Cargo.lock | 40 ++++++ Cargo.toml | 1 + crates/textimport/Cargo.toml | 1 + crates/textimport/src/xlsx.rs | 252 ++++++++++------------------------ 4 files changed, 112 insertions(+), 182 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6caa6d34..288950c98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1051,6 +1051,17 @@ dependencies = [ "zip 9.1.1", ] +[[package]] +name = "craft-drawingml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +dependencies = [ + "craft-xml", + "kurbo", + "log", + "serde", +] + [[package]] name = "craft-eps" version = "0.1.0" @@ -1116,6 +1127,17 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-opc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +dependencies = [ + "craft-xml", + "craft-zip", + "log", + "thiserror 2.0.21", +] + [[package]] name = "craft-pdf" version = "0.1.0" @@ -1164,6 +1186,11 @@ dependencies = [ "encoding_rs", ] +[[package]] +name = "craft-sheet" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" + [[package]] name = "craft-svg" version = "0.1.0" @@ -1243,6 +1270,18 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "craft-xlsx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +dependencies = [ + "craft-drawingml", + "craft-opc", + "craft-sheet", + "craft-xml", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -1678,6 +1717,7 @@ name = "designcraft-textimport" version = "0.20.1" dependencies = [ "craft-rtf", + "craft-xlsx", "designcraft-doc", "quick-xml 0.41.0", "zip 3.0.0", diff --git a/Cargo.toml b/Cargo.toml index cec984b88..881e9a19d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "3afade516 craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } +craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/textimport/Cargo.toml b/crates/textimport/Cargo.toml index eedcec005..34302939b 100644 --- a/crates/textimport/Cargo.toml +++ b/crates/textimport/Cargo.toml @@ -11,5 +11,6 @@ workspace = true [dependencies] designcraft-doc = { workspace = true } craft-rtf = { workspace = true } +craft-xlsx = { workspace = true } quick-xml = { workspace = true } zip = { workspace = true } diff --git a/crates/textimport/src/xlsx.rs b/crates/textimport/src/xlsx.rs index 8ad1bb004..997448de5 100644 --- a/crates/textimport/src/xlsx.rs +++ b/crates/textimport/src/xlsx.rs @@ -6,85 +6,63 @@ use std::collections::BTreeMap; use designcraft_doc::{CharFormat, ParaFormat, Story, StoryId, Table}; -use crate::docx::{El, parse, part}; use crate::{ImportError, Imported}; -/// "B12" → (row 11, column 1). -fn cell_ref(r: &str) -> Option<(usize, usize)> { - let letters: String = r.chars().take_while(char::is_ascii_alphabetic).collect(); - let digits = &r[letters.len()..]; - if !digits.bytes().all(|b| b.is_ascii_digit()) { - return None; - } - let row: usize = digits.parse().ok()?; - let col = letters.chars().try_fold(0usize, |acc, c| { - let digit = (c.to_ascii_uppercase() as u8).checked_sub(b'A')? as usize + 1; - acc.checked_mul(26)?.checked_add(digit) - })?; - Some((row.checked_sub(1)?, col.checked_sub(1)?)) -} - -/// The worksheet part of the first sheet (workbook order), via the workbook relationships. -fn first_sheet(zip: &mut zip::ZipArchive>) -> Result, ImportError> { - let Some(wb) = part(zip, "xl/workbook.xml")? else { return Ok(None) }; - let Some(rels) = part(zip, "xl/_rels/workbook.xml.rels")? else { return Ok(None) }; - Ok(first_sheet_target(&wb, &rels)) -} - -fn first_sheet_target(wb: &str, rels: &str) -> Option { - let wb = parse(wb).ok()?; - let rid = wb.child("sheets")?.els().find(|e| e.name == "sheet")?.attr("id")?.to_string(); - let rels = parse(rels).ok()?; - let target = rels.els().find(|r| r.attr("Id") == Some(rid.as_str()))?.attr("Target")?.trim_start_matches('/').to_string(); - Some(if target.starts_with("xl/") { target } else { format!("xl/{target}") }) -} - -/// The text of a shared string (``) or inline string (``): its own `` and the `` of its -/// `` runs. Phonetic guides (``, ``) are the reading, not the cell text. -fn string_item(si: &El) -> String { - si.els() - .map(|e| match e.name.as_str() { - "t" => e.text(), - "r" => e.els().filter(|t| t.name == "t").map(El::text).collect(), - _ => String::new(), - }) - .collect() -} - -fn value(c: &El, shared: &[String]) -> String { - let v = || c.child("v").map(El::text).unwrap_or_default(); - match c.attr("t") { - Some("s") => v().trim().parse::().ok().and_then(|i| shared.get(i).cloned()).unwrap_or_default(), - Some("inlineStr") => c.child("is").map(string_item).unwrap_or_default(), - // A cached formula string is text even when it looks like a number. - Some("str") => v(), - Some("b") => { - if v().trim() == "1" { - "TRUE".into() - } else { - "FALSE".into() - } - } - _ => { - let raw = v(); - match raw.trim().parse::() { - Ok(f) if f.is_finite() => general(f), - _ => raw, - } - } +fn display(value: craft_xlsx::Value) -> String { + match value { + craft_xlsx::Value::Empty => String::new(), + craft_xlsx::Value::Number(n) => general(n), + craft_xlsx::Value::Text(s) => s.to_string(), + craft_xlsx::Value::Bool(b) => if b { "TRUE" } else { "FALSE" }.into(), + craft_xlsx::Value::Error(e) => e.to_string(), } } -/// A number as Excel's General format shows it: at most 15 significant digits (Excel stores -/// doubles with 17, so `=0.1+0.2` is cached as 0.30000000000000004 and shown as 0.3), whole -/// numbers without ".0", and an exponent for very large or very small values. +fn sheet_cells( + bytes: &[u8], + wanted: Option<&str>, + max_cells: usize, + max_text_bytes: usize, +) -> Result<(Vec<(usize, usize, String)>, bool, Vec), ImportError> { + let mut package = craft_xlsx::Package::open(bytes, &craft_xlsx::Limits::default()).map_err(|e| ImportError::Corrupt(e.to_string()))?; + let workbook = package.workbook().map_err(|e| ImportError::Corrupt(e.to_string()))?; + let entry = match wanted { + Some(name) => workbook.sheets.iter().find(|sheet| sheet.name == name), + None => workbook.sheets.first(), + } + .ok_or_else(|| ImportError::Corrupt(wanted.map_or("the workbook has no sheets".into(), |name| format!("no worksheet \"{name}\""))))?; + let date_1904 = workbook.date_1904; + let strings = package.shared_strings().map_err(|e| ImportError::Corrupt(e.to_string()))?; + let mut cells = Vec::new(); + let mut text_bytes = 0usize; + let mut cut = false; + let result = package.sheet(entry, |row| { + for cell in row.cells { + let value = display(cell.value.resolve(&strings, date_1904)); + text_bytes = text_bytes.saturating_add(value.len()); + if cells.len() >= max_cells || text_bytes > max_text_bytes { + cut = true; + return Err(craft_xlsx::Error::TooLarge("worksheet text or cell limit reached".into())); + } + cells.push((cell.at.row as usize, cell.at.col as usize, value)); + } + Ok(()) + }); + if let Err(error) = result + && !cut + { + return Err(ImportError::Corrupt(error.to_string())); + } + let warnings = package.warnings().to_vec(); + Ok((cells, cut, warnings)) +} + fn general(f: f64) -> String { let r = format!("{f:.14e}").parse::().unwrap_or(f); let a = r.abs(); if r == 0.0 { "0".into() } else if (1e-9..1e15).contains(&a) { - // Shortest text that reads back as the rounded value; whole numbers drop the ".0". r.to_string() } else { let s = format!("{r:E}"); @@ -95,7 +73,11 @@ fn general(f: f64) -> String { } } -/// The first and last column used on the header row, if that row has any cells. +#[cfg(test)] +fn cell_ref(reference: &str) -> Option<(usize, usize)> { + craft_xlsx::CellRef::parse(reference).map(|cell| (cell.row as usize, cell.col as usize)) +} + fn header_span(cells: &[(usize, usize, String)], header_row: usize) -> Option<(usize, usize)> { cells.iter().filter(|(r, _, _)| *r == header_row).fold(None, |span, (_, c, _)| match span { None => Some((*c, *c)), @@ -103,20 +85,13 @@ fn header_span(cells: &[(usize, usize, String)], header_row: usize) -> Option<(u }) } -/// One row's values laid into `width` columns starting at column `min_c`; values outside that -/// span come back separately (only the non-empty ones), so the caller can warn. fn place_row(cols: &[(usize, String)], min_c: usize, width: usize) -> (Vec, Vec) { let mut row = vec![String::new(); width]; - let mut extra: Vec = Vec::new(); - for (c, v) in cols { - let at = c.checked_sub(min_c).filter(|at| *at < width); - match at { - Some(at) => { - if let Some(cell) = row.get_mut(at) { - *cell = v.clone(); - } - } - None if !v.is_empty() => extra.push(v.clone()), + let mut extra = Vec::new(); + for (c, value) in cols { + match c.checked_sub(min_c).filter(|at| *at < width) { + Some(at) => row[at] = value.clone(), + None if !value.is_empty() => extra.push(value.clone()), None => {} } } @@ -133,38 +108,17 @@ const MAX_RECORD_TEXT_BYTES: usize = 256 << 20; const MAX_RECORD_CELLS: usize = 2 * crate::DATA_MERGE_MAX_ROWS * crate::DATA_MERGE_MAX_COLS; pub fn records(bytes: &[u8], sheet: Option<&str>) -> Result>, ImportError> { - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| ImportError::Corrupt(e.to_string()))?; - let sheet_part = sheet_target(&mut zip, sheet)?; - let sheet_xml = parse(&part(&mut zip, &sheet_part)?.ok_or_else(|| ImportError::Corrupt(format!("no {sheet_part} (not an Excel workbook)")))?)?; - let shared: Vec = part(&mut zip, "xl/sharedStrings.xml")? - .map(|x| parse(&x)) - .transpose()? - .map(|sst| sst.els().filter(|e| e.name == "si").map(string_item).collect()) - .unwrap_or_default(); - let mut cells: Vec<(usize, usize, String)> = Vec::new(); - // Each `t="s"` cell is its own copy of a shared string: bounded as `import` bounds it, before - // the row and column limits below can apply (one large string referenced by many cells). - let mut text_bytes = 0usize; - if let Some(data) = sheet_xml.child("sheetData") { - for (ri, row) in data.els().filter(|e| e.name == "row").enumerate() { - for (ci, c) in row.els().filter(|e| e.name == "c").enumerate() { - let (r, col) = c.attr("r").and_then(cell_ref).unwrap_or((ri, ci)); - let v = value(c, &shared); - text_bytes = text_bytes.saturating_add(v.len()); - if text_bytes > MAX_RECORD_TEXT_BYTES || cells.len() >= MAX_RECORD_CELLS { - return Err(ImportError::Corrupt(format!( - "the sheet holds more than {} MB of text or {MAX_RECORD_CELLS} cells; the limit for a data source", - MAX_RECORD_TEXT_BYTES >> 20 - ))); - } - cells.push((r, col, v)); - } - } + let (cells, cut, _) = sheet_cells(bytes, sheet, MAX_RECORD_CELLS, MAX_RECORD_TEXT_BYTES)?; + if cut { + return Err(ImportError::Corrupt(format!( + "the sheet holds more than {} MB of text or {MAX_RECORD_CELLS} cells; the limit for a data source", + MAX_RECORD_TEXT_BYTES >> 20 + ))); } if cells.is_empty() { return Err(ImportError::Corrupt("the worksheet is empty".into())); } - let header_row = cells.iter().map(|c| c.0).min().unwrap_or(0); + let header_row = cells.iter().map(|cell| cell.0).min().unwrap_or(0); let Some((min_c, max_c)) = header_span(&cells, header_row) else { return Err(ImportError::Corrupt("the worksheet has no header".into())); }; @@ -172,21 +126,11 @@ pub fn records(bytes: &[u8], sheet: Option<&str>) -> Result>, Im if width > crate::DATA_MERGE_MAX_COLS { return Err(ImportError::Corrupt(format!("the sheet has {width} columns; the limit is {}", crate::DATA_MERGE_MAX_COLS))); } - // Group by row. Gaps (empty rows) are not materialised. let mut by_row: BTreeMap> = BTreeMap::new(); for (r, c, v) in cells { by_row.entry(r).or_default().push((c, v)); } - let mut data_rows = 0usize; - for (r, cols) in &by_row { - if *r == header_row { - continue; - } - let any = cols.iter().any(|(_, v)| !v.is_empty()); - if any { - data_rows = data_rows.saturating_add(1); - } - } + let data_rows = by_row.iter().filter(|(r, cols)| **r != header_row && cols.iter().any(|(_, v)| !v.is_empty())).count(); if data_rows > crate::DATA_MERGE_MAX_ROWS { return Err(ImportError::Corrupt(format!("the sheet has {data_rows} data rows; the limit is {}", crate::DATA_MERGE_MAX_ROWS))); } @@ -202,64 +146,10 @@ pub fn records(bytes: &[u8], sheet: Option<&str>) -> Result>, Im Ok(out) } -/// Workbook order, or the sheet named `wanted`. -fn sheet_target(zip: &mut zip::ZipArchive>, wanted: Option<&str>) -> Result { - let wb = parse(&part(zip, "xl/workbook.xml")?.ok_or_else(|| ImportError::Corrupt("no xl/workbook.xml (not an Excel workbook)".into()))?)?; - let sheets: Vec<(String, String)> = wb - .child("sheets") - .map(|s| s.els().filter(|e| e.name == "sheet").filter_map(|e| Some((e.attr("name")?.to_string(), e.attr("id")?.to_string()))).collect()) - .unwrap_or_default(); - let (name, rid) = match wanted { - None => sheets.first().cloned().ok_or_else(|| ImportError::Corrupt("the workbook has no sheets".into()))?, - Some(w) => sheets.into_iter().find(|(n, _)| n == w).ok_or_else(|| ImportError::Corrupt(format!("no worksheet \"{w}\"")))?, - }; - let rels = parse(&part(zip, "xl/_rels/workbook.xml.rels")?.ok_or_else(|| ImportError::Corrupt("no workbook relationships".into()))?)?; - let target = rels - .els() - .find(|r| r.attr("Id") == Some(rid.as_str())) - .and_then(|r| r.attr("Target")) - .ok_or_else(|| ImportError::Corrupt(format!("worksheet \"{name}\" has no file")))?; - let target = target.trim_start_matches('/'); - Ok(if target.starts_with("xl/") { target.to_string() } else { format!("xl/{target}") }) -} - pub fn import(bytes: &[u8]) -> Result { - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| ImportError::Corrupt(e.to_string()))?; - let sheet_part = first_sheet(&mut zip)?.unwrap_or_else(|| "xl/worksheets/sheet1.xml".into()); - let sheet = parse(&part(&mut zip, &sheet_part)?.ok_or_else(|| ImportError::Corrupt(format!("no {sheet_part} (not an Excel workbook)")))?)?; - let shared: Vec = part(&mut zip, "xl/sharedStrings.xml")? - .map(|x| parse(&x)) - .transpose()? - .map(|sst| sst.els().filter(|e| e.name == "si").map(string_item).collect()) - .unwrap_or_default(); - // A `t="s"` cell holds an index into the shared strings and each one is materialised as its - // own `String`, so one large shared string referenced by many cells multiplies: a 1 MB string - // and 3M references is terabytes, allocated before the grid cap below ever applies, and an - // allocation that large aborts the process rather than failing (#1584). Collection carries a - // byte budget and a cell count of its own. const MAX_TEXT_BYTES: usize = 64 << 20; const MAX_COLLECTED_CELLS: usize = 400_000; - let mut cells: Vec<((usize, usize), String)> = Vec::new(); - let mut text_bytes = 0usize; - let mut cut = false; - if let Some(data) = sheet.child("sheetData") { - 'rows: for (ri, row) in data.els().filter(|e| e.name == "row").enumerate() { - for (ci, c) in row.els().filter(|e| e.name == "c").enumerate() { - let at = c.attr("r").and_then(cell_ref).unwrap_or((ri, ci)); - let v = value(c, &shared); - if v.is_empty() { - continue; - } - if cells.len() >= MAX_COLLECTED_CELLS || text_bytes.saturating_add(v.len()) > MAX_TEXT_BYTES { - cut = true; - break 'rows; - } - text_bytes = text_bytes.saturating_add(v.len()); - cells.push((at, v)); - } - } - } - let mut warnings = Vec::new(); + let (cells, cut, mut warnings) = sheet_cells(bytes, None, MAX_COLLECTED_CELLS, MAX_TEXT_BYTES)?; if cut { warnings.push(format!( "the worksheet holds more than {} cells or {} MB of text: the rest was not read", @@ -270,12 +160,10 @@ pub fn import(bytes: &[u8]) -> Result { if cells.is_empty() { warnings.push("the worksheet is empty".into()); } - // The used range, from the first used row/column. Cell references come from the file: the - // table is capped before anything is allocated for it. const MAX_CELLS: usize = 100_000; const MAX_COLS: usize = 1_000; - let (r0, c0) = cells.iter().fold((usize::MAX, usize::MAX), |(r, c), ((a, b), _)| (r.min(*a), c.min(*b))); - let (r1, c1) = cells.iter().fold((0, 0), |(r, c), ((a, b), _)| (r.max(*a), c.max(*b))); + let (r0, c0) = cells.iter().fold((usize::MAX, usize::MAX), |(r, c), (a, b, _)| (r.min(*a), c.min(*b))); + let (r1, c1) = cells.iter().fold((0, 0), |(r, c), (a, b, _)| (r.max(*a), c.max(*b))); let (nrows, ncols) = if cells.is_empty() { (1, 1) } else { ((r1 - r0).saturating_add(1), (c1 - c0).saturating_add(1)) }; let ncols_kept = ncols.min(MAX_COLS); if ncols_kept < ncols { @@ -286,15 +174,15 @@ pub fn import(bytes: &[u8]) -> Result { warnings.push(format!("only the first {nrows_kept} rows were placed")); } let mut rows: Vec> = vec![vec![String::new(); ncols_kept]; nrows_kept]; - for ((r, c), v) in cells { + for (r, c, v) in cells.into_iter().filter(|(_, _, value)| !value.is_empty()) { if let Some(cell) = rows.get_mut(r - r0).and_then(|row| row.get_mut(c - c0)) { *cell = v; } } let mut story = Story::new(StoryId(0)); let width = 72.0 * ncols_kept as f64; - let t = Table::from_strings(1, &rows, width, &ParaFormat::default(), &CharFormat::default()); - story.insert_table(0, t); + let table = Table::from_strings(1, &rows, width, &ParaFormat::default(), &CharFormat::default()); + story.insert_table(0, table); Ok(Imported { story, para_styles: vec![], char_styles: vec![], warnings }) } From 3617464d73e1482a486cbcc24e91083606b8350f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:35:54 -0500 Subject: [PATCH 008/153] Use craft-eps for EPS section and box parsing --- crates/images/src/eps.rs | 78 +++------------------------------------- 1 file changed, 4 insertions(+), 74 deletions(-) diff --git a/crates/images/src/eps.rs b/crates/images/src/eps.rs index 1ea7f5e64..2d5c85867 100644 --- a/crates/images/src/eps.rs +++ b/crates/images/src/eps.rs @@ -3,85 +3,15 @@ //! rectangles of a simple one drawn here (InDesign's QR codes), else a placeholder the size of its //! bounding box. -fn u32le(b: &[u8], at: usize) -> Option { - Some(u32::from_le_bytes(b.get(at..at + 4)?.try_into().ok()?) as usize) -} - -/// The PostScript section and the TIFF preview (DOS EPS). +/// The PostScript section and the TIFF preview (DOS EPS), read by craft-eps. fn sections(bytes: &[u8]) -> (&[u8], Option<&[u8]>) { - if bytes.starts_with(&[0xC5, 0xD0, 0xD3, 0xC6]) { - let ps = u32le(bytes, 4).zip(u32le(bytes, 8)).and_then(|(o, l)| bytes.get(o..o.checked_add(l)?)).unwrap_or(&[]); - let tiff = u32le(bytes, 20).zip(u32le(bytes, 24)).filter(|(o, l)| *o > 0 && *l > 0).and_then(|(o, l)| bytes.get(o..o.checked_add(l)?)); - return (ps, tiff); - } - (bytes, None) + craft_eps::sections(bytes).map_or((&[][..], None), |parts| (parts.postscript, parts.tiff)) } /// The bounding box in points: (x0, y0, x1, y1), the high-resolution one when given. pub fn bounding_box(bytes: &[u8]) -> Option<[f64; 4]> { - let (ps, _) = sections(bytes); - let head = String::from_utf8_lossy(&ps[..ps.len().min(64 * 1024)]); - // DSC allows (atend): scan a bounded trailer as well, without reading the whole program. - let tail = String::from_utf8_lossy(&ps[ps.len().saturating_sub(64 * 1024)..]); - let header_value = |key: &str| { - // Lines may end in `\r` alone (classic Mac files). - head.split(['\n', '\r']) - .take_while(|l| { - (l.starts_with('%') || l.trim().is_empty()) - && ![ - "%%EndComments", - "%%BeginDocument", - "%%BeginProlog", - "%%BeginSetup", - "%%BeginData", - "%%BeginBinary", - "%%BeginResource", - "%%Page:", - ] - .iter() - .any(|k| l.starts_with(k)) - }) - .find_map(|l| l.strip_prefix(key)) - }; - let hires = header_value("%%HiResBoundingBox:"); - let bbox = header_value("%%BoundingBox:"); - let at_end = |v: Option<&str>| v.is_some_and(|v| v.trim() == "(atend)"); - // An outer header box must not be replaced by a nested document's trailer comments. - // Inspect the trailer only when the outer header delegates its bounds there. - hires - .and_then(parse_box) - .or_else(|| (at_end(hires) || at_end(bbox)).then(|| trailer_box(&tail, "%%HiResBoundingBox:")).flatten()) - .or_else(|| bbox.and_then(parse_box)) - .or_else(|| at_end(bbox).then(|| trailer_box(&tail, "%%BoundingBox:")).flatten()) -} - -fn parse_box(value: &str) -> Option<[f64; 4]> { - let mut words = value.split_whitespace(); - let mut bb = [0.0f64; 4]; - for n in &mut bb { - *n = words.next()?.parse().ok()?; - } - // Finite corners and a finite size (subtracting extreme corners can overflow to infinity). - let finite = [bb[0], bb[1], bb[2], bb[3], bb[2] - bb[0], bb[3] - bb[1]].iter().all(|n| n.is_finite()); - (words.next().is_none() && finite && bb[2] > bb[0] && bb[3] > bb[1]).then_some(bb) -} - -fn trailer_box(tail: &str, key: &str) -> Option<[f64; 4]> { - // Work backwards from the outer EOF so nesting remains known even when the bounded - // window begins inside a child document. Opaque DSC data/resource blocks are skipped too. - let mut nested = 0usize; - for line in tail.split(['\n', '\r']).rev() { - if ["%%EndDocument", "%%EndData", "%%EndBinary", "%%EndResource"].iter().any(|k| line.starts_with(k)) { - nested = nested.checked_add(1)?; - } else if ["%%BeginDocument", "%%BeginData", "%%BeginBinary", "%%BeginResource"].iter().any(|k| line.starts_with(k)) { - nested = nested.checked_sub(1)?; - } else if nested == 0 - && let Some(bb) = line.strip_prefix(key).and_then(parse_box) - { - return Some(bb); - } - } - None + let parts = craft_eps::sections(bytes).ok()?; + craft_eps::Comments::read(parts.postscript).page_box() } /// Natural EPS dimensions in points. Bound sizes before fitting or allocating a proxy: From 9b0c6ee2a79f361da04d0c6cd27b0339bc383a50 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:40:26 -0500 Subject: [PATCH 009/153] Delegate EPS preview parsing to craft-eps --- Cargo.lock | 26 +++---- Cargo.toml | 16 ++--- crates/images/src/eps.rs | 143 +++++---------------------------------- 3 files changed, 39 insertions(+), 146 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 288950c98..80043a3d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1020,7 +1020,7 @@ dependencies = [ [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" [[package]] name = "craft-dialogs" @@ -1043,7 +1043,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13af [[package]] name = "craft-document-bundle" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "craft-zip", "serde_json", @@ -1054,7 +1054,7 @@ dependencies = [ [[package]] name = "craft-drawingml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "craft-xml", "kurbo", @@ -1065,12 +1065,12 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" [[package]] name = "craft-epub" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "craft-xml", "craft-zip", @@ -1108,7 +1108,7 @@ dependencies = [ [[package]] name = "craft-idml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "craft-xml", "craft-zip", @@ -1130,7 +1130,7 @@ dependencies = [ [[package]] name = "craft-opc" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "craft-xml", "craft-zip", @@ -1180,7 +1180,7 @@ dependencies = [ [[package]] name = "craft-rtf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "craft-codepage", "encoding_rs", @@ -1189,12 +1189,12 @@ dependencies = [ [[package]] name = "craft-sheet" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "craft-xml", "flate2", @@ -1273,7 +1273,7 @@ dependencies = [ [[package]] name = "craft-xlsx" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "craft-drawingml", "craft-opc", @@ -1285,7 +1285,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1294,7 +1294,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3afade51630fcdb02662c761e0805bc6147bb0ac#3afade51630fcdb02662c761e0805bc6147bb0ac" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" dependencies = [ "flate2", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index 881e9a19d..121b553b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,14 +13,14 @@ repository = "https://github.com/storytold/designcraft" [workspace.dependencies] # Shared reader and writer; publish the immutable revision before this consumer. craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false } -craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } -craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } -craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } -craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } -craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } -craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } -craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } -craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "3afade51630fcdb02662c761e0805bc6147bb0ac", version = "=0.1.0" } +craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } +craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } +craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } +craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } +craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } +craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } +craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } +craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/images/src/eps.rs b/crates/images/src/eps.rs index 2d5c85867..63f0e6b09 100644 --- a/crates/images/src/eps.rs +++ b/crates/images/src/eps.rs @@ -91,34 +91,12 @@ pub fn eps_proxy(bytes: &[u8]) -> Option<(Vec, (f64, f64))> { /// Pixels per point for rendered EPS (a QR code's modules stay crisp when scaled up on screen). const RENDER_SCALE: f64 = 8.0; -/// The most rectangles and operators read from an EPS before giving up on drawing it. -const MAX_RECTS: usize = 200_000; -const MAX_TOKENS: usize = 2_000_000; /// The most pixels painted, in multiples of the image's area (overlapping rectangles repaint). const MAX_PAINT_AREAS: u64 = 4; -/// The deepest `gsave`/`save` nesting read. -const MAX_SAVE_DEPTH: usize = 64; -/// PostScript tokens without comments, `{` and `}` as tokens of their own. Lazy and borrowed, so -/// a huge input costs no memory beyond itself; lines may end in `\n`, `\r\n` or `\r`. -fn ps_tokens(text: &str) -> impl Iterator { - text.split(['\n', '\r']).flat_map(|line| { - line.split('%').next().unwrap_or("").split_whitespace().flat_map(|w| w.split_inclusive(['{', '}'])).flat_map(|piece| { - let (body, brace) = match piece.char_indices().last() { - Some((at, '{' | '}')) => (piece.get(..at).unwrap_or(""), piece.get(at..)), - _ => (piece, None), - }; - [Some(body).filter(|b| !b.is_empty()), brace].into_iter().flatten() - }) - }) -} - -/// PostScript that only fills rectangles in solid colours (`setcmykcolor`, `setrgbcolor`, -/// `setgray`, `rectfill`, and procedures that stand for one of them, such as InDesign's -/// `/rf { rectfill } bind def`), drawn as a PNG of the bounding box `bb`. None for anything else -/// (paths, text, images, transforms), which keeps its placeholder, or when nothing is drawn. +/// Rasterize rectangles read by craft-eps; unsupported PostScript keeps the placeholder. fn render_rectangles(ps: &[u8], bb: [f64; 4]) -> Option> { - let text = std::str::from_utf8(ps).ok()?; + let rectangles = craft_eps::simple_rectangles(ps)?; let (w, h) = (bb[2] - bb[0], bb[3] - bb[1]); if !(w > 0.0 && h > 0.0) { return None; @@ -126,110 +104,25 @@ fn render_rectangles(ps: &[u8], bb: [f64; 4]) -> Option> { let scale = RENDER_SCALE.min(4000.0 / w.max(h)); let (pw, ph) = ((w * scale).round().max(1.0) as u32, (h * scale).round().max(1.0) as u32); let mut img = image::RgbaImage::from_pixel(pw, ph, image::Rgba([0, 0, 0, 0])); - let mut aliases: std::collections::HashMap = std::collections::HashMap::new(); - let mut stack: Vec = Vec::new(); - let mut color = [0u8, 0, 0, 255]; - let mut drawn = 0usize; let mut painted = 0u64; let paint_budget = u64::from(pw) * u64::from(ph) * MAX_PAINT_AREAS; - let mut saved: Vec<[u8; 4]> = Vec::new(); - let mut tokens = ps_tokens(text); - let mut read = 0usize; - let mut next = || { - read += 1; - if read > MAX_TOKENS { None } else { Some(tokens.next()) } - }; - let unit = |v: f64| v.clamp(0.0, 1.0); - // `None` from `next`: over the token limit; `Some(None)`: the end. - while let Some(tok) = next()? { - if let Ok(v) = tok.parse::() { - if !v.is_finite() { - return None; - } - stack.push(v); - continue; + for rect in rectangles { + let (x0, x1) = ((rect.x - bb[0]) * scale, (rect.x + rect.width - bb[0]) * scale); + let (y0, y1) = ((bb[3] - (rect.y + rect.height)) * scale, (bb[3] - rect.y) * scale); + let px = |a: f64, b: f64, max: u32| { + let (lo, hi) = (a.min(b).round().max(0.0), a.max(b).round().min(max as f64)); + (lo as u32, hi.max(lo) as u32) + }; + let ((xa, xb), (ya, yb)) = (px(x0, x1, pw), px(y0, y1, ph)); + painted = painted.saturating_add(u64::from(xb - xa) * u64::from(yb - ya)); + if painted > paint_budget { + return None; } - // `/name { op } bind def`: name stands for op. - if let Some(name) = tok.strip_prefix('/') { - // At most `{ op } bind`: anything longer isn't an alias this reader knows. - let mut body: Vec<&str> = Vec::new(); - while body.len() < 5 { - match next()? { - Some("def") | None => break, - Some(t) => body.push(t), - } + for y in ya..yb { + for x in xa..xb { + img.put_pixel(x, y, image::Rgba(rect.color)); } - let op = match body.as_slice() { - ["{", op, "}"] | ["{", op, "}", _] => (*op).to_string(), - _ => return None, - }; - aliases.insert(name.to_string(), op); - continue; } - let op = aliases.get(tok).map_or(tok, String::as_str); - let mut pop = |n: usize| -> Option> { (stack.len() >= n).then(|| stack.split_off(stack.len() - n)) }; - match op { - "setcmykcolor" => { - let v = pop(4)?; - let k = unit(v[3]); - let ch = |c: f64| ((1.0 - unit(c)) * (1.0 - k) * 255.0).round() as u8; - color = [ch(v[0]), ch(v[1]), ch(v[2]), 255]; - } - "setrgbcolor" => { - let v = pop(3)?; - let ch = |c: f64| (unit(c) * 255.0).round() as u8; - color = [ch(v[0]), ch(v[1]), ch(v[2]), 255]; - } - "setgray" => { - let v = pop(1)?; - let g = (unit(v[0]) * 255.0).round() as u8; - color = [g, g, g, 255]; - } - "rectfill" => { - let v = pop(4)?; - drawn += 1; - if drawn > MAX_RECTS { - return None; - } - // PostScript y runs up; the image's down. Edges round to whole pixels, so - // touching rectangles (QR code modules) meet without gaps. - let (x0, x1) = ((v[0] - bb[0]) * scale, (v[0] + v[2] - bb[0]) * scale); - let (y0, y1) = ((bb[3] - (v[1] + v[3])) * scale, (bb[3] - v[1]) * scale); - let px = |a: f64, b: f64, max: u32| { - let (lo, hi) = (a.min(b).round().max(0.0), a.max(b).round().min(max as f64)); - (lo as u32, hi.max(lo) as u32) - }; - let ((xa, xb), (ya, yb)) = (px(x0, x1, pw), px(y0, y1, ph)); - painted = painted.saturating_add(u64::from(xb - xa) * u64::from(yb - ya)); - if painted > paint_budget { - return None; - } - for y in ya..yb { - for x in xa..xb { - img.put_pixel(x, y, image::Rgba(color)); - } - } - } - "gsave" | "save" => { - if saved.len() >= MAX_SAVE_DEPTH { - return None; - } - saved.push(color); - } - "grestore" | "restore" => { - if let Some(c) = saved.pop() { - color = c; - } - } - "pop" => { - pop(1)?; - } - "bind" | "newpath" | "showpage" | "end" | "begin" => {} - _ => return None, - } - } - if drawn == 0 { - return None; } let mut out = Vec::new(); image::DynamicImage::ImageRgba8(img).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).ok()?; @@ -305,14 +198,14 @@ mod tests { assert_eq!(drawn(eps, 28, 28), [0, 0, 0, 255], "bottom right: the colour from before gsave"); assert_eq!(drawn(eps, 28, 4)[3], 0, "top right: nothing (pop removed the extra operand)"); // Unbounded nesting isn't drawn. - let deep = format!("{} 0 0 2 2 rectfill", "gsave ".repeat(MAX_SAVE_DEPTH + 1)); + let deep = format!("{} 0 0 2 2 rectfill", "gsave ".repeat(craft_eps::MAX_SIMPLE_RECTANGLE_SAVE_DEPTH + 1)); assert_eq!(render_rectangles(deep.as_bytes(), [0.0, 0.0, 4.0, 4.0]), None); } #[test] fn a_huge_token_stream_on_one_line_is_refused() { // One line of more operands than the token limit, then a rectangle. - let ps = format!("{}\n0 0 2 2 rectfill\n", "0 ".repeat(MAX_TOKENS)); + let ps = format!("{}\n0 0 2 2 rectfill\n", "0 ".repeat(craft_eps::MAX_SIMPLE_RECTANGLE_TOKENS)); assert_eq!(render_rectangles(ps.as_bytes(), [0.0, 0.0, 4.0, 4.0]), None); let ps = format!("{}\n0 0 2 2 rectfill\n", "0 ".repeat(1000)); assert!(render_rectangles(ps.as_bytes(), [0.0, 0.0, 4.0, 4.0]).is_some(), "under the limit it is drawn"); From fcb571c52a5d6791cdf08f50c29aebf73f0e976b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:11:49 -0500 Subject: [PATCH 010/153] Delegate raster codecs and text interchange to craft-libs --- Cargo.lock | 408 +++++++++- Cargo.toml | 20 +- crates/images/Cargo.toml | 6 +- crates/images/src/eps.rs | 18 +- crates/images/src/lib.rs | 279 ++----- crates/images/src/psd.rs | 397 ++-------- crates/images/src/tone.rs | 46 +- crates/render/Cargo.toml | 1 + crates/render/src/lib.rs | 29 +- crates/textimport/Cargo.toml | 6 +- crates/textimport/src/docx.rs | 949 ++++++++---------------- crates/textimport/src/docx/tests.rs | 52 +- crates/textimport/src/docx_numbering.rs | 357 --------- crates/textimport/src/lib.rs | 39 - crates/textimport/src/tagged.rs | 484 ++---------- 15 files changed, 893 insertions(+), 2198 deletions(-) delete mode 100644 crates/textimport/src/docx_numbering.rs diff --git a/Cargo.lock b/Cargo.lock index 80043a3d8..e046d2799 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,6 +116,17 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aes" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fc36d7440d015b45468ba08a21e80e056ebbd014c611c6298bb7503e04bd1" +dependencies = [ + "cipher", + "cpubits", + "cpufeatures 0.3.1", +] + [[package]] name = "ahash" version = "0.8.12" @@ -230,7 +241,7 @@ dependencies = [ "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", - "windows-sys 0.61.2", + "windows-sys 0.59.0", "wl-clipboard-rs", "x11rb", ] @@ -536,6 +547,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bit_field" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1" + [[package]] name = "bitflags" version = "1.3.2" @@ -557,6 +574,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "block2" version = "0.5.1" @@ -794,6 +820,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cipher" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" +dependencies = [ + "crypto-common 0.2.2", + "inout", +] + [[package]] name = "clipboard-win" version = "5.4.3" @@ -812,6 +848,12 @@ dependencies = [ "cc", ] +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + [[package]] name = "cmpv2" version = "0.2.0" @@ -830,7 +872,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b77c319abfd5219629c45c34c89ba945ed3c5e49fcde9d16b6c3885f118a730" dependencies = [ - "const-oid", + "const-oid 0.9.6", "der", "spki", "x509-cert", @@ -906,6 +948,12 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" +[[package]] +name = "const-oid" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" + [[package]] name = "core-foundation" version = "0.9.4" @@ -983,6 +1031,12 @@ dependencies = [ "libm", ] +[[package]] +name = "cpubits" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -1009,6 +1063,11 @@ dependencies = [ "rustix 1.1.5", ] +[[package]] +name = "craft-cfb" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" + [[package]] name = "craft-cms" version = "0.1.0" @@ -1020,7 +1079,7 @@ dependencies = [ [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" [[package]] name = "craft-dialogs" @@ -1043,7 +1102,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13af [[package]] name = "craft-document-bundle" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-zip", "serde_json", @@ -1051,10 +1110,30 @@ dependencies = [ "zip 9.1.1", ] +[[package]] +name = "craft-docx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +dependencies = [ + "base64 0.23.1", + "chrono", + "craft-drawingml", + "craft-math", + "craft-offcrypto", + "craft-opc", + "craft-wordmodel", + "craft-xml", + "log", + "quick-xml 0.41.0", + "thiserror 2.0.21", + "unicode-bidi", + "zeroize", +] + [[package]] name = "craft-drawingml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-xml", "kurbo", @@ -1065,12 +1144,12 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" [[package]] name = "craft-epub" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-xml", "craft-zip", @@ -1108,13 +1187,43 @@ dependencies = [ [[package]] name = "craft-idml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-xml", "craft-zip", "thiserror 2.0.21", ] +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=85145d8dfd52966eacd045683a86a3737883aeea#85145d8dfd52966eacd045683a86a3737883aeea" +dependencies = [ + "craft-jpeg", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder", + "png", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=85145d8dfd52966eacd045683a86a3737883aeea#85145d8dfd52966eacd045683a86a3737883aeea" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-keymap" version = "0.1.0" @@ -1127,10 +1236,36 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-math" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +dependencies = [ + "craft-xml", + "serde", +] + +[[package]] +name = "craft-offcrypto" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +dependencies = [ + "aes", + "base64 0.23.1", + "craft-cfb", + "craft-xml", + "getrandom 0.3.4", + "hmac", + "sha1 0.11.0", + "sha2 0.11.1", + "thiserror 2.0.21", + "zeroize", +] + [[package]] name = "craft-opc" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-xml", "craft-zip", @@ -1177,10 +1312,21 @@ dependencies = [ "winsafe", ] +[[package]] +name = "craft-psd" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=85145d8dfd52966eacd045683a86a3737883aeea#85145d8dfd52966eacd045683a86a3737883aeea" +dependencies = [ + "encoding_rs", + "flate2", + "rayon", + "thiserror 2.0.21", +] + [[package]] name = "craft-rtf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-codepage", "encoding_rs", @@ -1189,12 +1335,12 @@ dependencies = [ [[package]] name = "craft-sheet" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-xml", "flate2", @@ -1241,7 +1387,7 @@ dependencies = [ "semver", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "tar", "tempfile", "ureq", @@ -1270,10 +1416,24 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "craft-wordmodel" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +dependencies = [ + "base64 0.23.1", + "craft-math", + "log", + "serde", + "serde_json", + "thiserror 2.0.21", + "unicode-segmentation", +] + [[package]] name = "craft-xlsx" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-drawingml", "craft-opc", @@ -1285,7 +1445,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1294,7 +1454,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe243c90e0f95c56eb6f82424be02a3ed77f#ba24fe243c90e0f95c56eb6f82424be02a3ed77f" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "flate2", "thiserror 2.0.21", @@ -1372,6 +1532,24 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "ctutils" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" +dependencies = [ + "cmov", +] + [[package]] name = "cursor-icon" version = "1.2.0" @@ -1390,7 +1568,7 @@ version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" dependencies = [ - "const-oid", + "const-oid 0.9.6", "der_derive", "flagset", "pem-rfc7468", @@ -1623,6 +1801,8 @@ version = "0.20.1" dependencies = [ "craft-cms", "craft-eps", + "craft-image-codecs", + "craft-psd", "craft-svg", "designcraft-color", "designcraft-fonts", @@ -1632,8 +1812,6 @@ dependencies = [ "resvg", "skrifa 0.47.0", "tiff", - "zune-core", - "zune-jpeg", ] [[package]] @@ -1697,6 +1875,7 @@ dependencies = [ name = "designcraft-render" version = "0.20.1" dependencies = [ + "craft-image-codecs", "craft-pdf", "designcraft-color", "designcraft-compose", @@ -1716,10 +1895,12 @@ dependencies = [ name = "designcraft-textimport" version = "0.20.1" dependencies = [ + "craft-docx", + "craft-idml", "craft-rtf", + "craft-wordmodel", "craft-xlsx", "designcraft-doc", - "quick-xml 0.41.0", "zip 3.0.0", ] @@ -1807,8 +1988,20 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "crypto-common", + "block-buffer 0.10.4", + "crypto-common 0.1.7", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid 0.10.3", + "crypto-common 0.2.2", + "ctutils", ] [[package]] @@ -2149,7 +2342,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -2187,6 +2380,22 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "exr" +version = "1.74.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6" +dependencies = [ + "bit_field", + "half", + "lebe", + "miniz_oxide 0.8.9", + "multiversion", + "rayon-core", + "smallvec", + "zune-inflate", +] + [[package]] name = "fancy-regex" version = "0.19.2" @@ -2553,9 +2762,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi 5.3.0", "wasip2", + "wasm-bindgen", ] [[package]] @@ -2934,6 +3145,15 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hmac" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" +dependencies = [ + "digest 0.11.3", +] + [[package]] name = "http" version = "1.5.0" @@ -2973,6 +3193,15 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + [[package]] name = "hyper" version = "1.12.0" @@ -3277,6 +3506,7 @@ dependencies = [ "moxcms", "num-traits", "png", + "qoi", "tiff", "zune-core", "zune-jpeg", @@ -3313,6 +3543,15 @@ dependencies = [ "hashbrown 0.17.1", ] +[[package]] +name = "inout" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9" +dependencies = [ + "hybrid-array", +] + [[package]] name = "ipnet" version = "2.12.2" @@ -3430,6 +3669,12 @@ dependencies = [ "libc", ] +[[package]] +name = "jpeg-encoder" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f" + [[package]] name = "js-sys" version = "0.3.106" @@ -3541,6 +3786,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" +[[package]] +name = "lebe" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8" + [[package]] name = "libc" version = "0.2.191" @@ -3741,6 +3992,28 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "multiversion" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076" +dependencies = [ + "multiversion-macros", + "target-features", +] + +[[package]] +name = "multiversion-macros" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "target-features", +] + [[package]] name = "multiversion_no_op" version = "1.0.0" @@ -3848,7 +4121,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -4273,7 +4546,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -4736,6 +5009,15 @@ version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + [[package]] name = "qrcodegen" version = "1.8.0" @@ -4826,7 +5108,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -5215,7 +5497,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -5274,7 +5556,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -5523,7 +5805,18 @@ checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" dependencies = [ "cfg-if", "cpufeatures 0.2.17", - "digest", + "digest 0.10.7", +] + +[[package]] +name = "sha1" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", ] [[package]] @@ -5534,7 +5827,18 @@ checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", "cpufeatures 0.2.17", - "digest", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", ] [[package]] @@ -5588,12 +5892,12 @@ checksum = "da90c3d9af638898a5fdc347479b18f950bb0dde11ecf2244f94cd28135da53b" dependencies = [ "aws-lc-rs", "base64 0.22.1", - "const-oid", + "const-oid 0.9.6", "der", - "digest", + "digest 0.10.7", "pem", "rand_core 0.10.1", - "sha2", + "sha2 0.10.9", "signature", "sigstore-types", "spki", @@ -5662,7 +5966,7 @@ dependencies = [ "chrono", "cmpv2", "cms", - "const-oid", + "const-oid 0.9.6", "der", "hex", "rand", @@ -5701,7 +6005,7 @@ dependencies = [ "base64 0.22.1", "chrono", "cms", - "const-oid", + "const-oid 0.9.6", "hex", "pem", "rustls-pki-types", @@ -6091,6 +6395,12 @@ dependencies = [ "xattr", ] +[[package]] +name = "target-features" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5" + [[package]] name = "tempfile" version = "3.27.0" @@ -6098,10 +6408,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix 1.1.5", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7313,7 +7623,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7725,9 +8035,9 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1301e935010a701ae5f8655edc0ad17c44bad3ac5ce8c39185f75453b720ae94" dependencies = [ - "const-oid", + "const-oid 0.9.6", "der", - "sha1", + "sha1 0.10.7", "signature", "spki", "tls_codec", @@ -8103,6 +8413,15 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] + [[package]] name = "zune-jpeg" version = "0.5.15" @@ -8112,6 +8431,15 @@ dependencies = [ "zune-core", ] +[[package]] +name = "zune-jpegxl" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4" +dependencies = [ + "zune-core", +] + [[package]] name = "zvariant" version = "5.15.1" diff --git a/Cargo.toml b/Cargo.toml index 121b553b1..955a05a94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,14 +13,18 @@ repository = "https://github.com/storytold/designcraft" [workspace.dependencies] # Shared reader and writer; publish the immutable revision before this consumer. craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false } -craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } -craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } -craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } -craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } -craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } -craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } -craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } -craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243c90e0f95c56eb6f82424be02a3ed77f", version = "=0.1.0" } +craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "85145d8dfd52966eacd045683a86a3737883aeea", version = "=0.1.0" } +craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "85145d8dfd52966eacd045683a86a3737883aeea", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/images/Cargo.toml b/crates/images/Cargo.toml index ccd9d82f9..6b58daccb 100644 --- a/crates/images/Cargo.toml +++ b/crates/images/Cargo.toml @@ -14,14 +14,14 @@ craft-svg = { workspace = true } designcraft-color = { workspace = true } designcraft-fonts = { workspace = true } image = { workspace = true } -tiff = { workspace = true } resvg = { workspace = true } skrifa = { workspace = true } log = { workspace = true } craft-cms = { workspace = true } -zune-core = { version = "0.5", default-features = false } -zune-jpeg = "0.5" +craft-image-codecs = { workspace = true } +craft-psd = { workspace = true } [dev-dependencies] designcraft-fonts = { workspace = true, features = ["testing"] } flate2 = "1" +tiff = { workspace = true } diff --git a/crates/images/src/eps.rs b/crates/images/src/eps.rs index 63f0e6b09..b311f82f1 100644 --- a/crates/images/src/eps.rs +++ b/crates/images/src/eps.rs @@ -48,9 +48,7 @@ pub fn eps_png(bytes: &[u8]) -> Option> { return Some(proxy); } let rgba = crate::decode_rgba(&proxy)?; - let mut png = Vec::new(); - image::DynamicImage::ImageRgba8(rgba).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).ok()?; - Some(png) + encode_png(rgba) } /// The proxy for a placed EPS: (image bytes, its size in points). The TIFF preview when present, @@ -84,9 +82,13 @@ pub fn eps_proxy(bytes: &[u8]) -> Option<(Vec, (f64, f64))> { img.put_pixel(0, y, image::Rgba([150, 150, 150, 255])); img.put_pixel(pw - 1, y, image::Rgba([150, 150, 150, 255])); } - let mut out = Vec::new(); - image::DynamicImage::ImageRgba8(img).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).ok()?; - Some((out, (w, h))) + Some((encode_png(img)?, (w, h))) +} + +fn encode_png(img: image::RgbaImage) -> Option> { + let (width, height) = img.dimensions(); + let image = craft_image_codecs::Image::from_u8(width, height, craft_image_codecs::ChannelLayout::Rgba, img.into_raw()).ok()?; + craft_image_codecs::encode(&image, craft_image_codecs::Format::Png, &Default::default()).ok() } /// Pixels per point for rendered EPS (a QR code's modules stay crisp when scaled up on screen). @@ -124,9 +126,7 @@ fn render_rectangles(ps: &[u8], bb: [f64; 4]) -> Option> { } } } - let mut out = Vec::new(); - image::DynamicImage::ImageRgba8(img).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).ok()?; - Some(out) + encode_png(img) } #[cfg(test)] diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs index d30a6b3d8..b9209e65e 100644 --- a/crates/images/src/lib.rs +++ b/crates/images/src/lib.rs @@ -10,7 +10,6 @@ pub use resvg::usvg; pub mod colour; mod eps; -mod jpeg; mod psd; mod tone; pub use craft_eps::is_eps; @@ -25,7 +24,7 @@ pub fn is_pdf(bytes: &[u8]) -> bool { } pub fn is_psd(bytes: &[u8]) -> bool { - bytes.starts_with(b"8BPS") + craft_psd::head::sniff(bytes) } /// An SVG document (optionally after an XML declaration, comments or a doctype). @@ -47,13 +46,13 @@ pub fn mime(bytes: &[u8]) -> &'static str { if is_svg(bytes) { return "image/svg+xml"; } - match image::guess_format(bytes) { - Ok(image::ImageFormat::Png) => "image/png", - Ok(image::ImageFormat::Jpeg) => "image/jpeg", - Ok(image::ImageFormat::Gif) => "image/gif", - Ok(image::ImageFormat::WebP) => "image/webp", - Ok(image::ImageFormat::Tiff) => "image/tiff", - Ok(image::ImageFormat::Bmp) => "image/bmp", + match craft_image_codecs::detect(bytes) { + Some(craft_image_codecs::Format::Png) => "image/png", + Some(craft_image_codecs::Format::Jpeg) => "image/jpeg", + Some(craft_image_codecs::Format::Gif) => "image/gif", + Some(craft_image_codecs::Format::WebP) => "image/webp", + Some(craft_image_codecs::Format::Tiff) => "image/tiff", + Some(craft_image_codecs::Format::Bmp) => "image/bmp", _ => "application/octet-stream", } } @@ -80,8 +79,13 @@ fn svg_options() -> &'static usvg::Options<'static> { let image_href_resolver = usvg::ImageHrefResolver { resolve_data: Box::new(move |mime, data, options| { let raster = matches!( - image::guess_format(&data), - Ok(image::ImageFormat::Png | image::ImageFormat::Jpeg | image::ImageFormat::Gif | image::ImageFormat::WebP) + craft_image_codecs::detect(&data), + Some( + craft_image_codecs::Format::Png + | craft_image_codecs::Format::Jpeg + | craft_image_codecs::Format::Gif + | craft_image_codecs::Format::WebP + ) ); if mime == "image/svg+xml" || (mime == "text/plain" && !raster) { let xml = svg_text(&data, MAX_NESTED_SVG_BYTES.saturating_sub(NESTED_SVG_BYTES.get()).min(MAX_SVG_BYTES))?; @@ -331,21 +335,19 @@ pub fn natural_size(bytes: &[u8]) -> Option<(f64, f64)> { /// Pixel dimensions of a raster. pub fn pixel_size(bytes: &[u8]) -> Option<(u32, u32)> { - if let Some(p) = palette_tiff(bytes) { - return Some((p.width, p.height)); + if is_psd(bytes) { + let header = craft_psd::header::Header::from_bytes(bytes).ok()?; + return Some((header.width, header.height)); } - if is_psd(bytes) && bytes.len() >= 26 { - // Header: signature, version, 6 reserved, channels, height, width (big-endian). - let be = |i: usize| u32::from_be_bytes([bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]); - return Some((be(18), be(14))); - } - image::ImageReader::new(std::io::Cursor::new(bytes)) - .with_guessed_format() - .ok()? - .into_dimensions() + craft_image_codecs::inspect_raster(bytes) .ok() - // `image` doesn't read five-sample CMYK with alpha. - .or_else(|| tiff_decoder(bytes)?.dimensions().ok()) + .flatten() + .map(|image| (image.width, image.height)) + .or_else(|| craft_image_codecs::probe_size(bytes).ok()) + .or_else(|| { + let image = craft_image_codecs::decode_with(bytes, &decode_options()).ok()?; + Some(image.dimensions()) + }) } /// The largest decoded raster, in RGBA bytes: 256 Mpx, a 300 ppi scan of an A0 sheet fits. @@ -367,72 +369,43 @@ pub fn decode_rgba(bytes: &[u8]) -> Option { if is_psd(bytes) { return psd::decode(bytes); } - // `image` rejects indexed-colour TIFFs (an old GIF logo saved from Photoshop). - if let Some(img) = decode_palette_tiff(bytes) { - return Some(img); - } // An EPS is drawn as its proxy (preview, simple PostScript or placeholder); the document // keeps the EPS itself. if is_eps(bytes) { return decode_rgba(&eps_proxy(bytes)?.0); } - if let Some((raster, icc)) = jpeg::decode_cmyk(bytes) { - return to_display_rgba(Pixels::Cmyk(raster), icc.as_deref(), &DisplayColour::active()); + let decoded = craft_image_codecs::decode_with(bytes, &decode_options()).ok()?; + if decoded.layout().is_cmyk() { + let raster = cmyk_raster(&decoded)?; + return to_display_rgba(Pixels::Cmyk(raster), decoded.icc.as_deref(), &DisplayColour::active()); } - if let Some(raster) = decode_cmyk_tiff(bytes) { - return to_display_rgba(Pixels::Cmyk(raster), embedded_icc(bytes).as_deref(), &DisplayColour::active()); + let rgba = image::RgbaImage::from_raw(decoded.width(), decoded.height(), decoded.to_rgba8())?; + let Some(icc) = decoded.icc.as_deref() else { return Some(rgba) }; + let px = if decoded.layout().is_gray() { Pixels::Gray(rgba) } else { Pixels::Rgb(rgba) }; + to_display_rgba(px, Some(icc), &DisplayColour::active()) +} + +fn decode_options() -> craft_image_codecs::DecodeOptions { + craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits { max_alloc: MAX_RGBA_BYTES, ..Default::default() }, ..Default::default() } +} + +fn cmyk_raster(image: &craft_image_codecs::Image) -> Option { + let converted = image.convert(craft_image_codecs::ChannelLayout::CmykA, craft_image_codecs::SampleType::U8); + let pixels = usize::try_from(converted.width()).ok()?.checked_mul(usize::try_from(converted.height()).ok()?)?; + let data = converted.as_u8()?; + if data.len() != pixels.checked_mul(5)? { + return None; } - let img = image::load_from_memory(bytes).ok()?; - let rgba = decode_to_rgba8(&img); - let Some(icc) = embedded_icc(bytes) else { return Some(rgba) }; - let px = if img.color().has_color() { Pixels::Rgb(rgba) } else { Pixels::Gray(rgba) }; - to_display_rgba(px, Some(&icc), &DisplayColour::active()) -} - -/// The ICC profile embedded in a PNG, JPEG, TIFF or WebP. -fn embedded_icc(bytes: &[u8]) -> Option> { - use image::ImageDecoder; - let mut d = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?.into_decoder().ok()?; - d.icc_profile().ok().flatten() -} - -/// Fresh raster decodes are sRGB in the pinned image decoder. Expanding an L8 value to -/// [gray, gray, gray, 255] is exact and avoids its generic floating-point conversion. -fn decode_to_rgba8(img: &image::DynamicImage) -> image::RgbaImage { - let prepared = match img { - image::DynamicImage::ImageLuma8(gray) => prepare_luma8_rgba(gray), - _ => None, - }; - rgba_or_original(img, prepared) -} - -// Keep preparation failure on the existing conversion/error path. This small boundary also -// lets tests force failure deterministically without exhausting memory or changing an allocator. -fn rgba_or_original(img: &image::DynamicImage, prepared: Option) -> image::RgbaImage { - prepared.unwrap_or_else(|| img.to_rgba8()) -} - -fn prepare_luma8_rgba(gray: &image::GrayImage) -> Option { - let (width, height) = gray.dimensions(); - let pixels = usize::try_from(width).ok()?.checked_mul(usize::try_from(height).ok()?)?; - let length = pixels.checked_mul(4)?; - let mut bytes = Vec::new(); - // Unlike the generic ConvertBuffer API's infallible allocation, preserve a route back to - // the original to_rgba8 behavior when the requested buffer cannot be reserved. - bytes.try_reserve_exact(length).ok()?; - bytes.resize(length, 0); - for (output, input) in bytes.as_chunks_mut::<4>().0.iter_mut().zip(gray.pixels()) { - let value = input.0[0]; - output.copy_from_slice(&[value, value, value, 255]); + let mut cmyk = Vec::with_capacity(pixels.checked_mul(4)?); + let mut alpha = Vec::with_capacity(pixels); + for &[c, m, y, k, a] in data.as_chunks::<5>().0 { + cmyk.extend([c, m, y, k]); + alpha.push(a); } - let mut rgba = image::RgbaImage::from_raw(width, height, bytes)?; - rgba.set_color_space(gray.color_space()).ok()?; - Some(rgba) + let alpha = alpha.iter().any(|&a| a < 255).then_some(alpha); + Some(CmykRaster { width: converted.width(), height: converted.height(), cmyk, alpha }) } -#[cfg(test)] -mod luma8_tests; - /// A CMYK raster's ink values, 8 bits per ink (0 = no ink, 255 = solid), as PDF's DeviceCMYK /// reads them. #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -445,109 +418,12 @@ pub struct CmykRaster { pub alpha: Option>, } -fn tiff_decoder(bytes: &[u8]) -> Option>> { - let tiff = [b"II*\0", b"MM\0*", b"II+\0", b"MM\0+"].iter().any(|m| bytes.starts_with(*m)); - if !tiff { - return None; - } - tiff::decoder::Decoder::new(std::io::Cursor::new(bytes)).ok() -} - -/// An indexed-colour TIFF (PhotometricInterpretation 3): its size, bits per index and ColorMap -/// in 8 bits per channel. -struct PaletteTiff { - width: u32, - height: u32, - bits: u8, - colours: Vec<[u8; 3]>, -} - -/// The header of a palette TIFF with 1-, 2-, 4- or 8-bit indices and a full ColorMap, which -/// neither `image` nor `tiff` decodes. `None` for anything else. -fn palette_tiff(bytes: &[u8]) -> Option { - use tiff::tags::Tag; - let mut d = tiff_decoder(bytes)?; - if d.find_tag_unsigned::(Tag::PhotometricInterpretation).ok()?? != 3 { - return None; - } - if d.find_tag_unsigned::(Tag::SamplesPerPixel).ok()?.unwrap_or(1) != 1 { - return None; - } - let bits = d.find_tag_unsigned::(Tag::BitsPerSample).ok()?.unwrap_or(1); - if !matches!(bits, 1 | 2 | 4 | 8) { - return None; - } - // 2^bits reds, then greens, then blues; 16-bit entries whose high byte is the 8-bit value. - let map = d.find_tag_unsigned_vec::(Tag::ColorMap).ok()??; - let n = 1usize << bits; - let (r, gb) = map.split_at_checked(n)?; - let (g, b) = gb.split_at_checked(n)?; - if b.len() != n { - return None; - } - let colours = r.iter().zip(g).zip(b).map(|((&r, &g), &b)| [r, g, b].map(|v| (v >> 8) as u8)).collect(); - let (width, height) = d.dimensions().ok()?; - Some(PaletteTiff { width, height, bits, colours }) -} - -/// A palette TIFF as RGBA, each index looked up in the ColorMap. -fn decode_palette_tiff(bytes: &[u8]) -> Option { - let p = palette_tiff(bytes)?; - // Read as gray, `tiff` hands back the indices themselves, whatever the compression or layout. - let gray = with_photometric(bytes, 1)?; - let tiff::decoder::DecodingResult::U8(px) = tiff_decoder(&gray)?.read_image().ok()? else { return None }; - let (w, h) = (usize::try_from(p.width).ok()?, usize::try_from(p.height).ok()?); - let bits = usize::from(p.bits); - // Indices below 8 bits are packed high bits first, each row starting on a byte. - let row = w.checked_mul(bits)?.div_ceil(8); - if row == 0 || px.len() < row.checked_mul(h)? { - return None; - } - let per_byte = 8 / bits; - let mask = ((1u16 << bits) - 1) as u8; - let mut out = Vec::new(); - out.try_reserve_exact(w.checked_mul(h)?.checked_mul(4)?).ok()?; - for line in px.chunks_exact(row).take(h) { - for x in 0..w { - let shift = 8 - bits * (x % per_byte + 1); - let i = (line.get(x / per_byte)? >> shift) & mask; - let [r, g, b] = *p.colours.get(usize::from(i))?; - out.extend([r, g, b, 255]); - } - } - image::RgbaImage::from_raw(p.width, p.height, out) -} - -/// A copy of a TIFF with its first image's PhotometricInterpretation (tag 262, one SHORT) set -/// to `value`. Classic and BigTIFF, either byte order. -fn with_photometric(bytes: &[u8], value: u16) -> Option> { - let le = bytes.starts_with(b"II"); - let int = |at: usize, len: usize| -> Option { - let b = bytes.get(at..at.checked_add(len)?)?; - let v = |acc: u64, &x: &u8| acc << 8 | u64::from(x); - usize::try_from(if le { b.iter().rev().fold(0, v) } else { b.iter().fold(0, v) }).ok() - }; - // Classic: 4-byte offsets and value counts, 2-byte entry count, 12-byte entries. BigTIFF: - // 8, 8, 8 and 20. - let (ifd, word, count_len, entry_len) = if int(2, 2)? == 43 { (int(8, 8)?, 8, 8, 20) } else { (int(4, 4)?, 4, 2, 12) }; - let count = int(ifd, count_len)?; - for k in 0..count { - let at = ifd.checked_add(count_len)?.checked_add(k.checked_mul(entry_len)?)?; - // Tag, type (3 = SHORT), value count, then the value itself. - if int(at, 2)? == 262 && int(at + 2, 2)? == 3 && int(at + 4, word)? == 1 { - let mut out = bytes.to_vec(); - let v = if le { value.to_le_bytes() } else { value.to_be_bytes() }; - let value_at = at + 4 + word; - out.get_mut(value_at..value_at + 2)?.copy_from_slice(&v); - return Some(out); - } - } - None -} - /// A TIFF whose pixels are CMYK (PhotometricInterpretation Separated, four inks). pub fn is_cmyk_tiff(bytes: &[u8]) -> bool { - tiff_decoder(bytes).and_then(|mut d| d.colortype().ok()).is_some_and(|c| matches!(c, tiff::ColorType::CMYK(_) | tiff::ColorType::CMYKA(_))) + craft_image_codecs::tiff_info(bytes) + .ok() + .and_then(|info| info.default_page().and_then(|i| info.pages.get(i).map(|page| (page.photometric, page.samples_per_pixel)))) + .is_some_and(|(photometric, samples)| photometric == Some(5) && matches!(samples, 4 | 5)) } /// The ink values of a CMYK TIFF, which [`decode_rgba`] converts to RGB. 8- or 16-bit CMYK, @@ -555,48 +431,11 @@ pub fn is_cmyk_tiff(bytes: &[u8]) -> bool { /// format, another ink set (InkSet 2), planar storage or premultiplied alpha — and for files /// over the decoder's 256 MiB limit; [`is_cmyk_tiff`] tells those apart from RGB files. pub fn decode_cmyk_tiff(bytes: &[u8]) -> Option { - use tiff::tags::Tag; if too_large_to_decode(bytes) { return None; } - let mut d = tiff_decoder(bytes)?; - let has_alpha = match d.colortype().ok()? { - tiff::ColorType::CMYK(8 | 16) => false, - tiff::ColorType::CMYKA(8 | 16) => true, - _ => return None, - }; - // InkSet (tag 332): 1 or absent = CMYK; 2 = other inks, named in InkNames. - if d.find_tag_unsigned::(Tag::Unknown(332)).ok()?.is_some_and(|s| s != 1) { - return None; - } - // ExtraSamples 2 = unassociated alpha. Premultiplied CMYK has no meaning as ink values. - if has_alpha && d.find_tag_unsigned_vec::(Tag::ExtraSamples).ok()?.and_then(|v| v.first().copied()) != Some(2) { - return None; - } - let (w, h) = d.dimensions().ok()?; - let pixels = usize::try_from(w).ok()?.checked_mul(usize::try_from(h).ok()?)?; - let samples = if has_alpha { 5 } else { 4 }; - let px = match d.read_image().ok()? { - tiff::decoder::DecodingResult::U8(v) => v, - tiff::decoder::DecodingResult::U16(v) => v.iter().map(|&s| ((u32::from(s) * 255 + 32_767) / 65_535) as u8).collect(), - _ => return None, - }; - // A planar file reads back as its first plane only, and extra non-alpha samples make the - // pixels wider: either way the length is not `pixels * samples`. - if px.len() != pixels.checked_mul(samples)? { - return None; - } - if !has_alpha { - return Some(CmykRaster { width: w, height: h, cmyk: px, alpha: None }); - } - let mut cmyk = Vec::with_capacity(pixels.checked_mul(4)?); - let mut alpha = Vec::with_capacity(pixels); - for &[c, m, y, k, a] in px.as_chunks::<5>().0 { - cmyk.extend([c, m, y, k]); - alpha.push(a); - } - let alpha = alpha.iter().any(|&a| a < 255).then_some(alpha); - Some(CmykRaster { width: w, height: h, cmyk, alpha }) + let image = craft_image_codecs::decode_with(bytes, &decode_options()).ok()?; + image.layout().is_cmyk().then(|| cmyk_raster(&image)).flatten() } /// An SVG rasterised so its longer side is `max_side` pixels: premultiplied RGBA8. diff --git a/crates/images/src/psd.rs b/crates/images/src/psd.rs index 90977d36e..3e0d8f101 100644 --- a/crates/images/src/psd.rs +++ b/crates/images/src/psd.rs @@ -1,356 +1,83 @@ -//! Photoshop documents (.psd, and .psb large documents): the merged composite in the image data -//! section, decoded by its colour mode per the public file format specification. Extra alpha and -//! spot channels don't change the placed image; when the layer count is negative, the first extra -//! channel is the composite's merged transparency. +//! Photoshop's merged composite, decoded by the shared PSD implementation and colour managed by +//! DesignCraft for its active display profile. -use designcraft_color::cms::{Lab, lab::lab_to_srgb}; +use craft_psd::header::ColorMode; use crate::CmykRaster; use crate::colour::{DisplayColour, Pixels, to_display_rgba}; -/// The largest decoded image, in RGBA bytes (the `image` crate's default allocation limit). -const MAX_RGBA_BYTES: usize = 512 << 20; - -/// Tagged blocks whose length is 8 bytes in a PSB. -const LONG_KEYS: [&[u8; 4]; 13] = - [b"LMsk", b"Lr16", b"Lr32", b"Layr", b"Mt16", b"Mt32", b"Mtrn", b"Alph", b"FMsk", b"lnk2", b"FEid", b"FXid", b"PxSD"]; - -/// Big-endian reads; each one fails at the end of the data. -struct Reader<'a> { - b: &'a [u8], -} - -impl<'a> Reader<'a> { - fn take(&mut self, n: usize) -> Option<&'a [u8]> { - let (head, rest) = self.b.split_at_checked(n)?; - self.b = rest; - Some(head) - } - fn u16(&mut self) -> Option { - Some(u16::from_be_bytes(self.take(2)?.try_into().ok()?)) - } - fn u32(&mut self) -> Option { - Some(u32::from_be_bytes(self.take(4)?.try_into().ok()?)) - } - /// A length-prefixed block: 4-byte length, or 8 bytes when `long`. - fn block(&mut self, long: bool) -> Option<&'a [u8]> { - let n = if long { u64::from_be_bytes(self.take(8)?.try_into().ok()?) } else { u64::from(self.u32()?) }; - self.take(usize::try_from(n).ok()?) - } -} - -#[derive(Clone, Copy, PartialEq)] -enum Mode { - Bitmap, - /// Grayscale, and Duotone (its data is the gray channel; the inks are in the colour mode data). - Gray, - Indexed, - Rgb, - Cmyk, - Lab, -} - -impl Mode { - fn parse(v: u16) -> Option { - Some(match v { - 0 => Mode::Bitmap, - 1 | 8 => Mode::Gray, - 2 => Mode::Indexed, - 3 => Mode::Rgb, - 4 => Mode::Cmyk, - 9 => Mode::Lab, - _ => return None, - }) - } - fn colour_channels(self) -> usize { - match self { - Mode::Bitmap | Mode::Gray | Mode::Indexed => 1, - Mode::Rgb | Mode::Lab => 3, - Mode::Cmyk => 4, - } - } - /// The stored channel values of white: a composite with transparency is matted against it. - fn white(self) -> Option<[f32; 4]> { - match self { - Mode::Gray | Mode::Rgb | Mode::Cmyk => Some([255.0; 4]), - Mode::Lab => Some([255.0, 128.0, 128.0, 0.0]), - Mode::Bitmap | Mode::Indexed => None, - } - } -} - -/// The merged composite as straight RGBA8, `None` when the file is damaged or unsupported (32-bit, -/// Multichannel, ZIP-compressed composite). -/// Whether the header's depth, channel count and size are all within what the decoder supports. -fn header_in_range(mode: Mode, depth: u16, channels: usize, (w, h): (u32, u32), max_side: u32) -> bool { - let depth_ok = match mode { - Mode::Bitmap => depth == 1, - Mode::Indexed => depth == 8, - _ => depth == 8 || depth == 16, - }; - (1..=56).contains(&channels) && channels >= mode.colour_channels() && (1..=max_side).contains(&w) && (1..=max_side).contains(&h) && depth_ok -} +/// The largest decoded raster in RGBA bytes. +const MAX_RGBA_BYTES: u64 = 512 << 20; pub(crate) fn decode(bytes: &[u8]) -> Option { - let mut r = Reader { b: bytes }; - if r.take(4)? != b"8BPS" { + let (file, _) = craft_psd::PsdFile::from_bytes_salvaging(bytes).ok()?; + let header = &file.header; + let pixels = usize::try_from(header.width).ok()?.checked_mul(usize::try_from(header.height).ok()?)?; + if u64::try_from(pixels).ok()?.checked_mul(4)? > MAX_RGBA_BYTES { return None; } - let psb = match r.u16()? { - 1 => false, - 2 => true, - _ => return None, - }; - r.take(6)?; - let channels = usize::from(r.u16()?); - let (h, w) = (r.u32()?, r.u32()?); - let depth = r.u16()?; - let mode = Mode::parse(r.u16()?)?; - let max_side = if psb { 300_000 } else { 30_000 }; - let colours = mode.colour_channels(); - if !header_in_range(mode, depth, channels, (w, h), max_side) { + if header.color_mode == ColorMode::Cmyk { + let raster = cmyk(&file, pixels)?; + return to_display_rgba(Pixels::Cmyk(raster), file.icc_profile(), &DisplayColour::active()); + } + let composite = file.composite_rgba8().ok()?; + let rgba = image::RgbaImage::from_raw(composite.width, composite.height, composite.data)?; + match (header.color_mode, file.icc_profile()) { + (ColorMode::Grayscale | ColorMode::Duotone, Some(icc)) => to_display_rgba(Pixels::Gray(rgba), Some(icc), &DisplayColour::active()), + (ColorMode::Rgb, Some(icc)) => to_display_rgba(Pixels::Rgb(rgba), Some(icc), &DisplayColour::active()), + _ => Some(rgba), + } +} + +fn cmyk(file: &craft_psd::PsdFile, pixels: usize) -> Option { + let header = &file.header; + if !matches!(header.depth, 8 | 16) || header.channels < 4 { return None; } - let (wu, hu) = (usize::try_from(w).ok()?, usize::try_from(h).ok()?); - let pixels = wu.checked_mul(hu)?; - let (stride, len) = (wu.checked_mul(4)?, pixels.checked_mul(4)?); - if len > MAX_RGBA_BYTES { + let plane = header.row_bytes().checked_mul(usize::try_from(header.height).ok()?)?; + let decoded = file.decode_merged().ok()?; + let needed = plane.checked_mul(4)?; + if decoded.len() < needed { return None; } - - let colour_mode_data = r.block(false)?; - let icc = icc_profile(r.block(false)?); - let layers = r.block(psb)?; - let transparency = channels > colours && layer_count(layers, psb).is_some_and(|n| n < 0); - let row_bytes = wu.checked_mul(usize::from(depth))?.div_ceil(8); - let data = ImageData::parse(r, psb, channels, row_bytes, hu)?; - let palette = if mode == Mode::Indexed { Some(palette(colour_mode_data)?) } else { None }; - // Header sizes are untrusted: allocate only once the data to fill the image is present. - data.holds(colours + usize::from(transparency))?; - - let mut rgba = vec![0u8; len]; - for c in 0..colours { - let mut rows = rgba.chunks_exact_mut(stride); - data.channel(c, |src| { - if let Some(dst) = rows.next() { - fill(dst, c, 4, src, depth); + let alpha_plane = if file.merged_has_alpha() { Some(decoded.get(needed..needed.checked_add(plane)?)?) } else { None }; + let sample = |channel: usize, pixel: usize| -> Option { + let bytes_per_sample = usize::from(header.depth / 8); + let at = channel.checked_mul(plane)?.checked_add(pixel.checked_mul(bytes_per_sample)?)?; + let value = match header.depth { + 8 => *decoded.get(at)?, + 16 => { + let v = u16::from_be_bytes(decoded.get(at..at.checked_add(2)?)?.try_into().ok()?); + ((u32::from(v) * 255 + 32_767) / 65_535) as u8 } - })?; - } - let mut alpha = Vec::new(); - if transparency { - alpha = vec![0u8; pixels]; - let mut rows = alpha.chunks_exact_mut(wu); - data.channel(colours, |src| { - if let Some(dst) = rows.next() { - fill(dst, 0, 1, src, depth); - } - })?; - } - - let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - let byte = |v: f32| v.round().clamp(0.0, 255.0) as u8; - let white = mode.white(); - for (i, px) in rgba.as_chunks_mut::<4>().0.iter_mut().enumerate() { - let a = alpha.get(i).copied().unwrap_or(255); - let mut v = px.map(f32::from); - // The composite is matted against white where it's partly transparent: un-matte it. - if let Some(white) = white - && a > 0 - && a < 255 - { - for (v, w) in v.iter_mut().zip(white).take(colours) { - *v = (w + (*v - w) * 255.0 / f32::from(a)).round().clamp(0.0, 255.0); - } - } - let [v0, v1, v2, v3] = v; - *px = match mode { - Mode::Bitmap | Mode::Gray => [byte(v0), byte(v0), byte(v0), a], - Mode::Indexed => { - let Some([pr, pg, pb]) = &palette else { return None }; - let i = usize::from(px[0]); - [pr[i], pg[i], pb[i], a] - } - Mode::Rgb => [byte(v0), byte(v1), byte(v2), a], - // Stored inverted (255 is no ink): ink values, converted below. - Mode::Cmyk => [v0, v1, v2, v3].map(|v| 255 - byte(v)), - Mode::Lab => { - let [r, g, b] = lab_to_srgb(Lab::new(v0 * 100.0 / 255.0, v1 - 128.0, v2 - 128.0)); - [q(r), q(g), q(b), a] - } - }; - } - let icc = icc.filter(|_| matches!(mode, Mode::Gray | Mode::Rgb | Mode::Cmyk)); - if mode == Mode::Cmyk { - let alpha = transparency.then_some(alpha); - let raster = CmykRaster { width: w, height: h, cmyk: rgba, alpha }; - return to_display_rgba(Pixels::Cmyk(raster), icc, &DisplayColour::active()); - } - let img = image::RgbaImage::from_raw(w, h, rgba)?; - match icc { - Some(icc) if mode == Mode::Gray => to_display_rgba(Pixels::Gray(img), Some(icc), &DisplayColour::active()), - Some(icc) => to_display_rgba(Pixels::Rgb(img), Some(icc), &DisplayColour::active()), - None => Some(img), - } -} - -/// The ICC profile in the image resources section (resource 1039), if any. -fn icc_profile(resources: &[u8]) -> Option<&[u8]> { - let mut r = Reader { b: resources }; - while r.take(4)? == b"8BIM" { - let id = r.u16()?; - // Pascal name, padded to an even length including its length byte. - let name_len = usize::from(*r.b.first()?); - r.take((name_len + 2) & !1)?; - let data = r.block(false)?; - if data.len() % 2 == 1 { - r.take(1)?; - } - if id == 1039 { - return Some(data); - } - } - None -} - -/// The layer count from the layer and mask section. 16- and 32-bit documents keep their layer -/// info in an `Lr16` / `Lr32` tagged block after the global layer mask info. -fn layer_count(section: &[u8], psb: bool) -> Option { - let count = |info: &[u8]| Some(i16::from_be_bytes(info.get(..2)?.try_into().ok()?)); - let mut r = Reader { b: section }; - let info = r.block(psb)?; - if !info.is_empty() { - return count(info); - } - r.block(false)?; // global layer mask info - while !r.b.is_empty() { - let sig = r.take(4)?; - if sig != b"8BIM" && sig != b"8B64" { - return None; - } - let key = r.take(4)?; - let data = r.block(psb && LONG_KEYS.iter().any(|k| k.as_slice() == key))?; - if matches!(key, b"Lr16" | b"Lr32" | b"Layr") { - return count(data); - } - // Padding to 2 or 4 bytes (a signature never starts with 0). - while r.b.first() == Some(&0) { - r.take(1)?; - } - } - None -} - -/// An indexed-colour palette: 256 reds, then greens, then blues. -fn palette(data: &[u8]) -> Option<[[u8; 256]; 3]> { - let part = |i: usize| data.get(i * 256..(i + 1) * 256)?.try_into().ok(); - Some([part(0)?, part(1)?, part(2)?]) -} - -/// The composite's channels: raw planes, or PackBits-compressed rows. -struct ImageData<'a> { - /// Each row's compressed length, channel after channel (2 bytes each, 4 in a PSB); empty for raw. - counts: &'a [u8], - count_size: usize, - data: &'a [u8], - row_bytes: usize, - height: usize, -} - -impl<'a> ImageData<'a> { - fn parse(mut r: Reader<'a>, psb: bool, channels: usize, row_bytes: usize, height: usize) -> Option { - let count_size = if psb { 4 } else { 2 }; - let counts = match r.u16()? { - 0 => &[][..], - 1 => r.take(channels.checked_mul(height)?.checked_mul(count_size)?)?, - // ZIP appears only in layers, never in the composite. _ => return None, }; - Some(ImageData { counts, count_size, data: r.b, row_bytes, height }) + Some(255 - value) + }; + let mut inks = Vec::new(); + inks.try_reserve_exact(pixels.checked_mul(4)?).ok()?; + let mut alpha = Vec::new(); + if alpha_plane.is_some() { + alpha.try_reserve_exact(pixels).ok()?; } - - /// `Some` when the data can hold the first `channels` channels: whole raw planes, or row - /// counts that fit in the data and are each long enough to unpack to a full row (PackBits - /// writes at most 128 bytes per 2 input bytes). - fn holds(&self, channels: usize) -> Option<()> { - let rows = channels.checked_mul(self.height)?; - if self.counts.is_empty() { - let need = self.row_bytes.checked_mul(rows)?; - return (need <= self.data.len()).then_some(()); + for p in 0..pixels { + for channel in 0..4 { + inks.push(sample(channel, p)?); } - let min_row = self.row_bytes.div_ceil(128).checked_mul(2)?; - let mut total = 0usize; - for c in self.counts.chunks_exact(self.count_size).take(rows) { - let n = c.iter().fold(0usize, |n, &b| n << 8 | usize::from(b)); - if n < min_row { - return None; - } - total = total.checked_add(n)?; + if let Some(plane) = alpha_plane { + let bytes_per_sample = usize::from(header.depth / 8); + let at = p.checked_mul(bytes_per_sample)?; + let a = match header.depth { + 8 => *plane.get(at)?, + 16 => { + let v = u16::from_be_bytes(plane.get(at..at.checked_add(2)?)?.try_into().ok()?); + ((u32::from(v) * 255 + 32_767) / 65_535) as u8 + } + _ => return None, + }; + alpha.push(a); } - (total <= self.data.len()).then_some(()) - } - - /// Channel `ch`'s rows, uncompressed, to `row` from top to bottom. - fn channel(&self, ch: usize, mut row: impl FnMut(&[u8])) -> Option<()> { - if self.row_bytes == 0 { - return None; - } - if self.counts.is_empty() { - let plane = self.row_bytes.checked_mul(self.height)?; - let start = ch.checked_mul(plane)?; - self.data.get(start..start.checked_add(plane)?)?.chunks_exact(self.row_bytes).for_each(row); - return Some(()); - } - let mut counts = self.counts.chunks_exact(self.count_size).map(|c| c.iter().fold(0usize, |n, &b| n << 8 | usize::from(b))); - // This channel starts after every earlier channel's rows. - let mut at = counts.by_ref().take(ch.checked_mul(self.height)?).try_fold(0usize, usize::checked_add)?; - for _ in 0..self.height { - let n = counts.next()?; - let end = at.checked_add(n)?; - row(&unpack_bits(self.data.get(at..end)?, self.row_bytes)?); - at = end; - } - Some(()) } + let alpha = alpha.iter().any(|&a| a < 255).then_some(alpha); + Some(CmykRaster { width: header.width, height: header.height, cmyk: inks, alpha }) } - -/// One PackBits row; `None` unless it unpacks to exactly `len` bytes. -fn unpack_bits(mut src: &[u8], len: usize) -> Option> { - let mut out = Vec::with_capacity(len); - while let Some((&head, rest)) = src.split_first() { - src = rest; - match head as i8 { - -128 => {} - n @ 0.. => { - let (literal, rest) = src.split_at_checked(n as usize + 1)?; - out.extend_from_slice(literal); - src = rest; - } - n => { - let (&v, rest) = src.split_first()?; - out.resize(out.len() + (1 - n as isize) as usize, v); - src = rest; - } - } - if out.len() > len { - return None; - } - } - (out.len() == len).then_some(out) -} - -/// A row of `depth`-bit samples as 8-bit values, into every `step`-th byte of `dst` from `slot`. -/// Bitmap mode stores black as 1. -fn fill(dst: &mut [u8], slot: usize, step: usize, src: &[u8], depth: u16) { - let dst = dst.iter_mut().skip(slot).step_by(step); - match depth { - 1 => dst.zip(src.iter().flat_map(|&b| (0..8).rev().map(move |i| if (b >> i) & 1 == 1 { 0 } else { 255 }))).for_each(|(d, s)| *d = s), - 16 => dst - .zip(src.as_chunks::<2>().0.iter().map(|c| c.iter().fold(0u32, |n, &b| n << 8 | u32::from(b)))) - .for_each(|(d, s)| *d = ((s * 255 + 32_767) / 65_535) as u8), - _ => dst.zip(src).for_each(|(d, &s)| *d = s), - } -} - -#[cfg(test)] -mod tests; diff --git a/crates/images/src/tone.rs b/crates/images/src/tone.rs index 3d0b945ce..a4b1515fe 100644 --- a/crates/images/src/tone.rs +++ b/crates/images/src/tone.rs @@ -13,49 +13,9 @@ pub enum Tone { /// The [`Tone`] of an image file, `None` when a swatch doesn't colour it. pub fn tone(bytes: &[u8]) -> Option { - use image::{ExtendedColorType as E, ImageDecoder, ImageFormat as F}; - if crate::is_psd(bytes) { - // Header: channels at 12, colour mode at 24 (big-endian). A second channel is alpha or spot. - let be = |i: usize| Some(u16::from_be_bytes([*bytes.get(i)?, *bytes.get(i + 1)?])); - return match (be(12)?, be(24)?) { - (1, 0) => Some(Tone::Bitmap), - (1, 1) => Some(Tone::Gray), - _ => None, - }; - } - let reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?; - match reader.format()? { - F::Png => { - // `image` expands 1-bit greys to 8 bits: the depth is IHDR's (signature, length, type, - // width, height, then the bit depth). - let gray = matches!(reader.into_decoder().ok()?.color_type(), image::ColorType::L8 | image::ColorType::L16); - let one_bit = *bytes.get(24)? == 1; - gray.then_some(if one_bit { Tone::Bitmap } else { Tone::Gray }) - } - F::Tiff | F::Jpeg | F::Bmp => match reader.into_decoder().ok()?.original_color_type() { - E::L1 => Some(Tone::Bitmap), - E::L2 | E::L4 | E::L8 | E::L16 => Some(Tone::Gray), - _ => None, - }, - // Indexed: what the palette holds decides. Transparent pixels are left out of a bitmap. - F::Gif => { - if crate::too_large_to_decode(bytes) { - return None; - } - let img = image::load_from_memory_with_format(bytes, F::Gif).ok()?.into_rgba8(); - let mut opaque = true; - let mut two_level = true; - for p in img.pixels() { - let [r, g, b, a] = p.0; - match a { - 0 => opaque = false, - 255 if r == g && g == b => two_level &= r == 0 || r == 255, - _ => return None, - } - } - if two_level { Some(Tone::Bitmap) } else { opaque.then_some(Tone::Gray) } - } - _ => None, + match craft_image_codecs::classify_tone(bytes)? { + craft_image_codecs::ToneClass::Bitmap => Some(Tone::Bitmap), + craft_image_codecs::ToneClass::Gray => Some(Tone::Gray), } } diff --git a/crates/render/Cargo.toml b/crates/render/Cargo.toml index 8cd446af1..4f93f62ee 100644 --- a/crates/render/Cargo.toml +++ b/crates/render/Cargo.toml @@ -22,6 +22,7 @@ designcraft-images = { workspace = true } log = { workspace = true } thiserror = { workspace = true } serde = { workspace = true } +craft-image-codecs = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] vello_cpu = { workspace = true, features = ["multithreading"] } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 25207b248..e6e5d04e8 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -76,15 +76,17 @@ impl Rendered { let mut px = self.to_straight(); // `pixels` is public: a mismatched buffer is padded or cut to the stated size. px.resize(self.width as usize * self.height as usize * 4, 0); - let Some(img) = image::RgbaImage::from_raw(self.width, self.height, px) else { + let Ok(img) = craft_image_codecs::Image::from_u8(self.width, self.height, craft_image_codecs::ChannelLayout::Rgba, px) else { log::error!("PNG encode: {}×{} image is too large", self.width, self.height); - return Vec::new(); + return buf; }; - if let Err(e) = img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png) { - log::error!("PNG encode: {e}"); - return Vec::new(); + match craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &Default::default()) { + Ok(bytes) => bytes, + Err(e) => { + log::error!("PNG encode: {e}"); + buf + } } - buf } pub fn to_jpeg(&self, quality: u8) -> Vec { let rgba = self.to_straight(); @@ -99,9 +101,18 @@ impl Rendered { }) .collect(); let mut buf = Vec::new(); - let enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut buf, quality.clamp(1, 100)); - let _ = image::ImageEncoder::write_image(enc, &rgb, self.width, self.height, image::ExtendedColorType::Rgb8); - buf + let Ok(img) = craft_image_codecs::Image::from_u8(self.width, self.height, craft_image_codecs::ChannelLayout::Rgb, rgb) else { + log::error!("JPEG encode: {}×{} image is too large", self.width, self.height); + return buf; + }; + let options = craft_image_codecs::EncodeOptions { jpeg_quality: quality.clamp(1, 100), ..Default::default() }; + match craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &options) { + Ok(bytes) => bytes, + Err(e) => { + log::error!("JPEG encode: {e}"); + buf + } + } } /// Straight-alpha RGBA at (x, y). pub fn pixel(&self, x: u32, y: u32) -> [u8; 4] { diff --git a/crates/textimport/Cargo.toml b/crates/textimport/Cargo.toml index 34302939b..460458ddf 100644 --- a/crates/textimport/Cargo.toml +++ b/crates/textimport/Cargo.toml @@ -12,5 +12,9 @@ workspace = true designcraft-doc = { workspace = true } craft-rtf = { workspace = true } craft-xlsx = { workspace = true } -quick-xml = { workspace = true } +craft-docx = { workspace = true } +craft-wordmodel = { workspace = true } +craft-idml = { workspace = true } + +[dev-dependencies] zip = { workspace = true } diff --git a/crates/textimport/src/docx.rs b/crates/textimport/src/docx.rs index fb76d366a..ba869a599 100644 --- a/crates/textimport/src/docx.rs +++ b/crates/textimport/src/docx.rs @@ -1,680 +1,331 @@ -//! Word (.docx, Office Open XML) → story. +//! DOCX to DesignCraft story adapter. Package, XML, styles and numbering are read by craft-docx; +//! this module maps the neutral Word model into the app's story and style model. -use std::collections::HashMap; -use std::io::Read; +use std::collections::{HashMap, HashSet}; +use craft_wordmodel::Block; +use craft_wordmodel::para::{InlineObject, NoteKind, Paragraph}; +use craft_wordmodel::props::{Align as WordAlign, CharProps as WordCharProps, ParaProps as WordParaProps, Underline, VertAlign}; +use craft_wordmodel::section::NumFormat; +use craft_wordmodel::styles::{StyleKind, StyleSheet}; use designcraft_doc::story::{BASIC_PARAGRAPH, NO_CHAR_STYLE}; -use designcraft_doc::{CharAttrs, CharFormat, ListCounter, ListType, ParaAttrs, ParaFormat, ParaProps, Story, StoryId, Table}; -use quick_xml::events::Event; +use designcraft_doc::{CharAttrs, CharFormat, ListType, NumberStyle, ParaAttrs, ParaFormat, Story, StoryId, Table}; -use crate::docx_numbering::{ListPara, Numbering}; use crate::{ImportError, Imported, ImportedStyle}; -/// A minimal element tree (local names, attributes by local name). -#[derive(Debug, Default, Clone)] -pub(crate) struct El { - pub(crate) name: String, - attrs: Vec<(String, String)>, - kids: Vec, +const MAX_NAME_CHARS: usize = 256; +const MAX_NOTE_BYTES: usize = 8 << 20; +const MAX_WARNINGS: usize = 100; + +fn capped_name(name: &str) -> String { + name.chars().take(MAX_NAME_CHARS).collect() } -#[derive(Debug, Clone)] -enum Node { - El(El), - Text(String), +fn style_names(styles: &StyleSheet) -> HashMap { + styles.styles.iter().map(|s| (s.id.clone(), capped_name(&s.name))).collect() } -impl El { - pub(crate) fn attr(&self, k: &str) -> Option<&str> { - self.attrs.iter().find(|(n, _)| n == k).map(|(_, v)| v.as_str()) - } - pub(crate) fn els(&self) -> impl Iterator { - self.kids.iter().filter_map(|n| if let Node::El(e) = n { Some(e) } else { None }) - } - pub(crate) fn child(&self, name: &str) -> Option<&El> { - self.els().find(|e| e.name == name) - } - pub(crate) fn text(&self) -> String { - self.kids - .iter() - .map(|n| match n { - Node::Text(t) => t.clone(), - Node::El(e) => e.text(), - }) - .collect() - } - /// `` → the value; a bare `` counts as "on". - pub(crate) fn val(&self, name: &str) -> Option { - self.child(name).map(|e| e.attr("val").unwrap_or("true").to_string()) - } - /// A toggle property (`ST_OnOff`: a boolean, or the words `on` and `off`). None when unset. - pub(crate) fn on(&self, name: &str) -> Option { - self.val(name).map(|v| !matches!(v.as_str(), "0" | "false" | "none" | "off")) - } -} - -fn local(n: &[u8]) -> String { - let s = String::from_utf8_lossy(n); - s.rsplit(':').next().unwrap_or(&s).to_string() -} - -/// How deep elements may nest. Word's own XML stays well under a hundred levels; the tree is read -/// recursively afterwards (`El::text`, the block walk) and dropped recursively, so a document -/// nested without bound must be refused here, not run into the stack (#712). -pub(crate) const MAX_DEPTH: usize = 256; - -pub(crate) fn parse(xml: &str) -> Result { - let mut r = quick_xml::Reader::from_str(xml); - let mut stack: Vec = vec![El::default()]; - loop { - match r.read_event().map_err(|e| ImportError::Corrupt(e.to_string()))? { - Event::Start(e) => { - if stack.len() > MAX_DEPTH { - return Err(ImportError::Corrupt(format!("XML elements nested deeper than {MAX_DEPTH} levels"))); - } - let el = El { - name: local(e.name().as_ref()), - attrs: e - .attributes() - .flatten() - .map(|a| { - (local(a.key.as_ref()), a.normalized_value(quick_xml::XmlVersion::Implicit1_0).map(|v| v.to_string()).unwrap_or_default()) - }) - .collect(), - kids: vec![], - }; - stack.push(el); - } - Event::Empty(e) => { - let el = El { - name: local(e.name().as_ref()), - attrs: e - .attributes() - .flatten() - .map(|a| { - (local(a.key.as_ref()), a.normalized_value(quick_xml::XmlVersion::Implicit1_0).map(|v| v.to_string()).unwrap_or_default()) - }) - .collect(), - kids: vec![], - }; - if let Some(p) = stack.last_mut() { - p.kids.push(Node::El(el)); - } - } - Event::End(_) => { - let el = stack.pop().unwrap_or_default(); - match stack.last_mut() { - Some(p) => p.kids.push(Node::El(el)), - None => return Ok(el), - } - } - Event::Text(t) => { - let s = t.decode().map(|s| s.to_string()).unwrap_or_default(); - let s = quick_xml::escape::unescape(&s).map(|s| s.to_string()).unwrap_or(s); - if let Some(p) = stack.last_mut() { - p.kids.push(Node::Text(s)); - } - } - Event::CData(t) => { - let s = t.decode().map(|s| s.to_string()).unwrap_or_default(); - if let Some(p) = stack.last_mut() { - p.kids.push(Node::Text(s)); - } - } - Event::GeneralRef(e) => { - let name = String::from_utf8_lossy(&e).to_string(); - let s = match name.as_str() { - "amp" => "&".to_string(), - "lt" => "<".into(), - "gt" => ">".into(), - "quot" => "\"".into(), - "apos" => "'".into(), - n if n.starts_with("#x") && n[2..].bytes().all(|b| b.is_ascii_hexdigit()) => { - u32::from_str_radix(&n[2..], 16).ok().and_then(char::from_u32).map(String::from).unwrap_or_default() - } - n if n.starts_with('#') && !n.starts_with("#x") && n[1..].bytes().all(|b| b.is_ascii_digit()) => { - n[1..].parse::().ok().and_then(char::from_u32).map(String::from).unwrap_or_default() - } - _ => String::new(), - }; - if let Some(p) = stack.last_mut() { - p.kids.push(Node::Text(s)); - } - } - Event::Eof => break, - _ => {} - } - } - let mut root = stack.pop().unwrap_or_default(); - Ok(root.kids.drain(..).find_map(|n| if let Node::El(e) = n { Some(e) } else { None }).unwrap_or_default()) -} - -/// Largest decompressed zip part read (a small file can inflate to gigabytes). -pub(crate) const PART_LIMIT: u64 = 64 * 1024 * 1024; - -/// A zip part as text. `Ok(None)` when the part is missing or isn't text; an error when it -/// decompresses to more than [`PART_LIMIT`]. -pub(crate) fn part(zip: &mut zip::ZipArchive>, name: &str) -> Result, ImportError> { - part_capped(zip, name, PART_LIMIT) -} - -pub(crate) fn part_capped(zip: &mut zip::ZipArchive>, name: &str, limit: u64) -> Result, ImportError> { - let Ok(f) = zip.by_name(name) else { return Ok(None) }; - let mut s = String::new(); - // Read one byte past the limit to tell "exactly at the limit" from "over it". - if f.take(limit.saturating_add(1)).read_to_string(&mut s).is_err() { - return Ok(None); - } - if s.len() as u64 > limit { - return Err(ImportError::Corrupt(format!("{name} is larger than {} MiB uncompressed", limit / (1024 * 1024)))); - } - Ok(Some(s)) -} - -/// Character attributes from ``. -fn run_attrs(rpr: Option<&El>) -> CharAttrs { - let mut a = CharAttrs::default(); - let Some(r) = rpr else { return a }; - let bold = r.on("b"); - let italic = r.on("i"); - a.font_style = match (bold, italic) { +fn char_attrs(p: &WordCharProps) -> CharAttrs { + let mut out = CharAttrs::default(); + out.font_family = p.font.as_ref().map(|s| capped_name(s)); + out.font_style = match (p.bold, p.italic) { (Some(true), Some(true)) => Some("Bold Italic".into()), (Some(true), _) => Some("Bold".into()), (_, Some(true)) => Some("Italic".into()), - (Some(false), Some(false)) | (Some(false), None) | (None, Some(false)) => Some("Regular".into()), + (Some(false), Some(false)) => Some("Regular".into()), _ => None, }; - if let Some(u) = r.val("u") { - a.underline = Some(!matches!(u.as_str(), "none" | "0" | "false" | "off")); - } - if let Some(s) = r.on("strike") { - a.strikethrough = Some(s); - } - // Half-points; a size the engine cannot mean is dropped (#1593). - if let Some(pt) = r.val("sz").and_then(|v| v.parse::().ok()).map(|sz| sz / 2.0).and_then(designcraft_doc::attrs::imported_type_size) { - a.size = Some(pt); - } - if let Some(f) = r.child("rFonts").and_then(|f| f.attr("ascii").or(f.attr("hAnsi"))) { - a.font_family = Some(f.to_string()); - } - if r.on("caps") == Some(true) { - a.capitalization = Some(designcraft_doc::Capitalization::AllCaps); - } - if let Some(v) = r.val("vertAlign") { - a.position = Some(match v.as_str() { - "superscript" => designcraft_doc::Position::Superscript, - "subscript" => designcraft_doc::Position::Subscript, - _ => designcraft_doc::Position::Normal, + out.size = p.size.map(f64::from).and_then(designcraft_doc::attrs::imported_type_size); + out.underline = p.underline.map(|u| u != Underline::None); + out.strikethrough = p.strike.or(p.double_strike); + out.capitalization = p.caps.filter(|&on| on).map(|_| designcraft_doc::Capitalization::AllCaps); + out.position = p.vert_align.map(|v| match v { + VertAlign::Superscript => designcraft_doc::Position::Superscript, + VertAlign::Subscript => designcraft_doc::Position::Subscript, + VertAlign::Baseline => designcraft_doc::Position::Normal, + }); + out +} + +fn para_attrs(p: &WordParaProps, doc: &craft_wordmodel::Document) -> ParaAttrs { + let mut out = ParaAttrs::default(); + out.align = p.align.map(|a| match a { + WordAlign::Center => designcraft_doc::Align::Center, + WordAlign::Right => designcraft_doc::Align::Right, + WordAlign::Justify | WordAlign::Distribute => designcraft_doc::Align::LeftJustified, + WordAlign::Left => designcraft_doc::Align::Left, + }); + out.left_indent = p.indent_left.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); + out.right_indent = p.indent_right.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); + out.first_line_indent = p.indent_first.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); + out.space_before = p.space_before.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); + out.space_after = p.space_after.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); + if let Some(numbering) = p.numbering.filter(|n| n.num != 0) { + let definition = doc.numbering.nums.iter().find(|n| n.id == numbering.num).and_then(|n| { + n.level_overrides.iter().find(|(level, _)| *level == numbering.level).map(|(_, def)| def).or_else(|| { + let abs = doc.numbering.abstracts.iter().find(|a| a.id == n.abstract_id)?; + abs.levels.get(usize::from(numbering.level)) + }) }); - } - a -} - -/// Paragraph attributes from `` (spacing in twips = 1/20 pt, indents likewise). -fn para_attrs(ppr: Option<&El>) -> ParaAttrs { - let mut a = ParaAttrs::default(); - let Some(p) = ppr else { return a }; - // Twips, clamped: a file can hold 1e300, which became a 5e298 pt indent (#1593). - let tw = |v: Option<&str>| v.and_then(|v| v.parse::().ok()).map(|v| v / 20.0).and_then(designcraft_doc::attrs::imported_measure); - if let Some(sp) = p.child("spacing") { - a.space_before = tw(sp.attr("before")); - a.space_after = tw(sp.attr("after")); - } - if let Some(ind) = p.child("ind") { - a.left_indent = tw(ind.attr("left").or(ind.attr("start"))); - a.right_indent = tw(ind.attr("right").or(ind.attr("end"))); - a.first_line_indent = tw(ind.attr("firstLine")).or(tw(ind.attr("hanging")).map(|h| -h)); - } - if let Some(j) = p.val("jc") { - a.align = Some(match j.as_str() { - "center" => designcraft_doc::Align::Center, - "right" | "end" => designcraft_doc::Align::Right, - "both" | "distribute" => designcraft_doc::Align::LeftJustified, - _ => designcraft_doc::Align::Left, - }); - } - a -} - -struct Ctx { - para_names: HashMap, - char_names: HashMap, - footnotes: HashMap, - /// Footnote ids already imported, and how much note text has been built. A reference clones - /// its note and walks it again, so one 1 MB footnote referenced 100k times meant ~100 GB - /// (#1584). Word gives each note its own id and references it once; a second reference to the - /// same id gets the mark without the text. - notes_done: std::collections::HashSet, - note_bytes: usize, - warnings: Vec, - /// What has already been said, so saying it again is a lookup rather than a scan of - /// everything said so far. A warning that names a footnote id is one per id, and the - /// document chooses how many ids it has (noted in review). - warned: std::collections::HashSet, - next_table: u64, - numbering: Numbering, - /// A paragraph style's list (`w:numPr` numId and ilvl, through `w:basedOn`), by style id. - style_lists: HashMap, Option)>, - /// The number the model's own list counter is at (it restarts after a paragraph that isn't - /// in a list), to tell where a numbered paragraph needs an explicit Start At. - /// The model's unnamed-list numbering as composition will count it. - model_counter: ListCounter, - /// The style of paragraphs without `w:pStyle` (Word's default paragraph style, usually Normal). - default_para: String, -} - -/// A note's tables may hold tables of their own. The document model stops nesting at 64, so -/// counting no deeper than that reaches every cell a note can actually have. -const MAX_NOTE_DEPTH: usize = 64; - -/// The text `s` holds, including its tables' cell text. A table's cells each keep their text in -/// a story of their own rather than in `s.text`, which holds only the anchor, so counting -/// `s.text` scored a table of any size as a couple of bytes (noted in review). -fn note_bytes(s: &Story, depth: usize) -> usize { - let mut n = s.text.len(); - if depth == 0 { - return n; - } - for t in s.tables.values() { - for c in &t.cells { - n = n.saturating_add(note_bytes(&c.text, depth - 1)); - } - } - n -} - -/// Warnings one import reports. A document that earns more than this has earned the point. -const MAX_WARNINGS: usize = 100; - -impl Ctx { - /// Report `message` once. Beyond [`MAX_WARNINGS`] distinct messages nothing further is kept, - /// not even in the set, so neither the report nor the memory behind it grows with the - /// document -- a footnote warning names an id, and a file can hold as many ids as it likes. - fn warn(&mut self, message: String) { - if self.warnings.len() > MAX_WARNINGS { - return; - } - if !self.warned.insert(message.clone()) { - return; - } - if self.warnings.len() == MAX_WARNINGS { - self.warnings.push(format!("The Word file has more than {MAX_WARNINGS} kinds of problem: the rest are not listed.")); - } else { - self.warnings.push(message); - } - } - - fn para_style(&self, ppr: Option<&El>) -> String { - ppr.and_then(|p| p.val("pStyle")).and_then(|id| self.para_names.get(&id).cloned()).unwrap_or_else(|| self.default_para.clone()) - } - - /// Append the paragraphs and tables of `body` to `st`. - fn blocks(&mut self, body: &El, st: &mut Story, allow_notes: bool) { - let mut first = st.is_empty(); - for e in body.els() { - match e.name.as_str() { - "p" => { - if !first { - let len = st.len(); - st.insert(len, "\n"); - } - first = false; - let ppr = e.child("pPr"); - let style = self.para_style(ppr); - let mut attrs = para_attrs(ppr); - let list = self.list(ppr); - if let Some(l) = &list { - // The level's indents, unless the paragraph sets its own. - let direct = attrs; - attrs = l.attrs.clone(); - attrs.merge(&direct); - } - let at = st.len(); - st.format_paras(at..at, |f| *f = ParaFormat { style: style.clone(), para: attrs.clone(), ..Default::default() }); - self.inline(e, st, allow_notes); - let (start_at, expression) = self.start_at(list.as_ref(), st.len() == at); - if let Some(n) = start_at { - st.format_paras(at..at, |f| f.para.start_at = Some(Some(n))); - } - if let Some(e) = expression { - st.format_paras(at..at, |f| f.para.number_expression = Some(e.clone())); - } + if let Some(level) = definition { + if level.format == NumFormat::Bullet { + out.list_type = Some(ListType::Bullets); + out.bullet_char = Some(level.text.chars().next().unwrap_or('•').to_string()); + } else if level.format != NumFormat::None { + out.list_type = Some(ListType::Numbers); + out.number_style = Some(match level.format { + NumFormat::UpperRoman => NumberStyle::UpperRoman, + NumFormat::LowerRoman => NumberStyle::LowerRoman, + NumFormat::UpperLetter => NumberStyle::UpperLetters, + NumFormat::LowerLetter => NumberStyle::LowerLetters, + NumFormat::DecimalZero => NumberStyle::ArabicLeadingZero, + _ => NumberStyle::Arabic, + }); + let mut expression = level.text.clone(); + for i in (1..=9).rev() { + expression = expression.replace(&format!("%{i}"), &format!("^{i}")); } - "tbl" => { - let mut rows: Vec> = Vec::new(); - for r in e.els().filter(|r| r.name == "tr") { - let mut row = Vec::new(); - for c in r.els().filter(|c| c.name == "tc") { - let mut paras = Vec::new(); - self.cell_paras(c, &mut paras); - row.push(paras.join("\n")); - } - rows.push(row); - } - if rows.is_empty() { - continue; - } - self.next_table += 1; - let t = Table::from_strings(self.next_table, &rows, 300.0, &ParaFormat::default(), &CharFormat::default()); - let len = st.len(); - st.insert_table(len, t); - first = st.text.ends_with('\n'); + out.number_expression = Some(expression); + if level.start > 1 { + out.start_at = Some(Some(level.start)); } - "sdt" => { - if let Some(c) = e.child("sdtContent") { - self.blocks(c, st, allow_notes); - } - } - _ => {} } - } - } - - /// The text of each paragraph in a table cell, read by the same run rules as body text. - /// Cells are plain text, so a page or column break in a cell ends its paragraph. - fn cell_paras(&mut self, el: &El, out: &mut Vec) { - for e in el.els() { - match e.name.as_str() { - "p" => { - let mut st = Story::new(StoryId(0)); - self.inline(e, &mut st, false); - let is_break = |c: char| matches!(c, designcraft_doc::PAGE_BREAK | designcraft_doc::COLUMN_BREAK); - out.push(st.text.chars().map(|c| if is_break(c) { '\n' } else { c }).collect()); - } - "sdt" => { - if let Some(c) = e.child("sdtContent") { - self.cell_paras(c, out); - } - } - _ => {} - } - } - } - - /// The list a paragraph is in (its own `w:numPr`, else its style's), counting it. - fn list(&mut self, ppr: Option<&El>) -> Option { - let direct = ppr.and_then(|p| p.child("numPr")); - let style = ppr.and_then(|p| p.val("pStyle")).and_then(|id| self.style_lists.get(&id)); - let num_id = direct.and_then(|n| n.val("numId")).or_else(|| style.and_then(|s| s.0.clone()))?; - let ilvl = direct.and_then(|n| n.val("ilvl")).or_else(|| style.and_then(|s| s.1.clone())); - // A level that isn't a small number is no list. - let ilvl = ilvl.map_or(Some(0), |l| l.trim().parse::().ok())?; - self.numbering.next(num_id.trim(), ilvl) - } - - /// Start At for a paragraph, so that the model numbers it as Word does: only where the - /// model's counter (per level; restarted after a paragraph that isn't in a list) would give a - /// different number. And the number expression with parent levels as text, where the model's - /// numbers for those levels differ from Word's. - fn start_at(&mut self, list: Option<&ListPara>, empty: bool) -> (Option, Option) { - let Some(l) = list else { - self.model_counter = ListCounter::default(); - return (None, None); - }; - // Bullets and empty paragraphs leave the counter alone (they show no number). - if l.attrs.list_type != Some(ListType::Numbers) || empty { - return (None, None); - } - let want = l.number.max(1); - let mut pp = ParaProps::default(); - pp.apply(&l.attrs); - let mut model = self.model_counter.clone(); - let start = (model.advance(&pp) != want).then_some(want); - if start.is_some() { - model = self.model_counter.clone(); - pp.start_at = start; - model.advance(&pp); - } - let level_text = |k: u32| ParaProps { number_expression: format!("^{k}"), ..ParaProps::default() }.number_label_in(&model); - let expression = l.parents.iter().any(|(k, text)| level_text(*k) != *text).then(|| l.fixed_expression.clone()).flatten(); - self.model_counter = model; - (start, expression) - } - - /// Runs (and hyperlinks, fields' results) of a paragraph. - fn inline(&mut self, p: &El, st: &mut Story, allow_notes: bool) { - for e in p.els() { - match e.name.as_str() { - "r" => self.run(e, st, allow_notes), - // Tracked changes import as accepted: insertions and move destinations are kept, - // deletions (`del`) and move sources (`moveFrom`) are dropped. - "hyperlink" | "smartTag" | "ins" | "moveTo" | "fldSimple" | "customXml" => self.inline(e, st, allow_notes), - // A content control (date picker, drop-down, plain text) keeps its runs in `sdtContent`. - "sdt" => { - if let Some(c) = e.child("sdtContent") { - self.inline(c, st, allow_notes); - } - } - _ => {} - } - } - } - - fn run(&mut self, r: &El, st: &mut Story, allow_notes: bool) { - let rpr = r.child("rPr"); - let style = rpr.and_then(|p| p.val("rStyle")).and_then(|id| self.char_names.get(&id).cloned()).unwrap_or_else(|| NO_CHAR_STYLE.into()); - let fmt = CharFormat { style, over: run_attrs(rpr) }; - for e in r.els() { - let text = match e.name.as_str() { - "t" => e.text(), - "tab" => "\t".into(), - "br" => match e.attr("type") { - Some("page") => designcraft_doc::PAGE_BREAK.to_string(), - Some("column") => designcraft_doc::COLUMN_BREAK.to_string(), - _ => designcraft_doc::FORCED_LINE_BREAK.to_string(), - }, - "noBreakHyphen" => "\u{2011}".into(), - "softHyphen" => "\u{AD}".into(), - "footnoteReference" if allow_notes => { - const MAX_NOTE_BYTES: usize = 8 << 20; - let id = e.attr("id").unwrap_or_default().to_string(); - // The note's text is built once per id, and only while the budget lasts. A - // repeat reference, or one past the budget, still gets its mark and its - // number -- the reference is part of the text -- with an empty note (#1584). - // - // The id is recorded only once its text has actually been kept, further down: - // recording it here meant a first reference arriving after the budget was - // spent counted as "done" with an empty note, and the next reference to it - // then claimed the text was kept at the first one (noted in review). - let again = !id.is_empty() && self.notes_done.contains(&id); - let spent = self.note_bytes >= MAX_NOTE_BYTES; - if again || spent { - let len = st.len(); - if st.insert_note(len, "", ParaFormat::default()).is_some() { - let message = if again { - format!("Footnote {id} is referenced more than once in the Word file: the text is kept at its first reference.") - } else { - format!("The Word file's footnotes are longer than {} MB: the rest were imported empty.", MAX_NOTE_BYTES / (1 << 20)) - }; - self.warn(message); - } - continue; - } - if let Some(note) = e.attr("id").and_then(|id| self.footnotes.get(id).cloned()) { - let mut ns = Story::new(StoryId(0)); - let counter = std::mem::take(&mut self.model_counter); - self.blocks(¬e, &mut ns, false); - self.model_counter = counter; - self.note_bytes = self.note_bytes.saturating_add(note_bytes(&ns, MAX_NOTE_DEPTH)); - if !id.is_empty() { - self.notes_done.insert(id.clone()); - } - // Word puts the reference mark in the note text: we generate the number. - let len = st.len(); - match st.insert_note(len, "", ParaFormat::default()) { - Some(id) => { - if let Some(n) = st.note_mut(id) { - n.text = ns; - } - } - None => self.warn("A footnote in the Word file could not be imported.".into()), - } - } - continue; - } - "drawing" | "pict" | "object" => { - self.warn("Images and drawings in the Word file were not imported (place them separately).".into()); - continue; - } - _ => continue, - }; - if !text.is_empty() { - let len = st.len(); - st.insert_with(len, &text, fmt.clone()); - } - } - } -} - -/// Largest `word/numbering.xml` read. -const NUMBERING_LIMIT: u64 = 16 * 1024 * 1024; -/// Deepest `w:basedOn` chain followed (a cycle stops here too). -const MAX_STYLE_DEPTH: usize = 32; - -/// Each paragraph style's list: the first `w:numId` and `w:ilvl` on its `w:basedOn` chain. -fn style_lists(styles: &El) -> HashMap, Option)> { - let by_id: HashMap<&str, &El> = styles.els().filter(|s| s.name == "style").filter_map(|s| Some((s.attr("styleId")?, s))).collect(); - let mut out = HashMap::new(); - for (id, s) in &by_id { - let (mut num_id, mut ilvl) = (None, None); - let mut cur = Some(*s); - for _ in 0..MAX_STYLE_DEPTH { - let Some(c) = cur else { break }; - if let Some(n) = c.child("pPr").and_then(|p| p.child("numPr")) { - num_id = num_id.or_else(|| n.val("numId")); - ilvl = ilvl.or_else(|| n.val("ilvl")); - } - if num_id.is_some() && ilvl.is_some() { - break; - } - cur = c.val("basedOn").and_then(|b| by_id.get(b.as_str()).copied()); - } - if num_id.is_some() { - out.insert(id.to_string(), (num_id, ilvl)); + out.left_indent = Some(f64::from(level.indent)); + out.first_line_indent = Some(-f64::from(level.hanging)); } } out } +fn char_format(p: &WordCharProps, names: &HashMap) -> CharFormat { + let style = p.style.as_ref().and_then(|id| names.get(id)).cloned().unwrap_or_else(|| NO_CHAR_STYLE.into()); + CharFormat { style, over: char_attrs(p) } +} + +fn style_records(styles: &StyleSheet, doc: &craft_wordmodel::Document) -> (Vec, Vec, HashMap) { + let names = style_names(styles); + let mut paras = Vec::new(); + let mut chars = Vec::new(); + for style in &styles.styles { + let name = names.get(&style.id).cloned().unwrap_or_else(|| capped_name(&style.name)); + let based_on = style.based_on.as_ref().and_then(|id| names.get(id)).cloned(); + let mut para = para_attrs(&style.para, doc); + let mut char_values = char_attrs(&style.chr); + if based_on.is_none() { + let mut defaults = para_attrs(&styles.default_para, doc); + defaults.merge(¶); + para = defaults; + let mut defaults = char_attrs(&styles.default_chr); + defaults.merge(&char_values); + char_values = defaults; + } + let record = ImportedStyle { name: name.clone(), based_on, para, chars: char_values }; + match style.kind { + StyleKind::Paragraph => paras.push(record), + StyleKind::Character if name != NO_CHAR_STYLE => chars.push(record), + _ => {} + } + } + (paras, chars, names) +} + +fn plain_blocks(blocks: &[std::sync::Arc]) -> String { + let mut out = String::new(); + let mut first = true; + for block in blocks { + match block.as_ref() { + Block::Para(p) => { + if !first { + out.push('\n'); + } + first = false; + out.push_str(&p.plain_text()); + } + Block::Table(t) => { + for row in &t.rows { + if !first { + out.push('\n'); + } + first = false; + out.push_str(&row.cells.iter().map(|c| plain_blocks(&c.blocks)).collect::>().join("\t")); + } + } + } + } + out +} + +fn word_text(c: char) -> char { + use craft_wordmodel::para::{COLUMN_BREAK, LINE_BREAK, PAGE_BREAK}; + match c { + LINE_BREAK => designcraft_doc::FORCED_LINE_BREAK, + PAGE_BREAK => designcraft_doc::PAGE_BREAK, + COLUMN_BREAK => designcraft_doc::COLUMN_BREAK, + c => c, + } +} + +fn append_paragraph( + doc: &craft_wordmodel::Document, + p: &Paragraph, + st: &mut Story, + names: &HashMap, + warned_objects: &mut bool, + notes_done: &mut HashSet, + note_bytes: &mut usize, + warnings: &mut Vec, +) { + let para_at = st.len(); + let style = p.props.style.as_ref().and_then(|id| names.get(id)).cloned().unwrap_or_else(|| BASIC_PARAGRAPH.into()); + st.format_paras(para_at..para_at, |fmt| *fmt = ParaFormat { style: style.clone(), para: para_attrs(&p.props, doc), ..Default::default() }); + + let mut object_i = 0usize; + let mut last_format: Option = None; + let mut chunk = String::new(); + let flush = |st: &mut Story, fmt: &mut Option, text: &mut String| { + if !text.is_empty() { + st.insert_with(st.len(), text, fmt.clone().unwrap_or_default()); + text.clear(); + } + }; + for (at, c) in p.text.char_indices() { + let run = p + .runs + .iter() + .scan(0usize, |offset, r| { + let range = *offset..offset.saturating_add(r.len); + *offset = range.end; + Some((range, r)) + }) + .find(|(range, _)| range.contains(&at)) + .map(|(_, r)| r); + let fmt = run.map(|r| char_format(&r.props, names)).unwrap_or_default(); + if last_format.as_ref().is_some_and(|prev| prev != &fmt) { + flush(st, &mut last_format, &mut chunk); + } + last_format = Some(fmt); + if c == craft_wordmodel::para::OBJ { + flush(st, &mut last_format, &mut chunk); + if let Some(object) = p.objects.get(object_i) { + match object { + InlineObject::NoteRef { kind: NoteKind::Footnote, id, .. } => { + let mut text = doc.parts.get(id).map(|part| plain_blocks(&part.blocks)).unwrap_or_default(); + if notes_done.contains(id) { + text.clear(); + add_warning( + warnings, + format!("Footnote {id} is referenced more than once in the Word file: the text is kept at its first reference."), + ); + } else if note_bytes.saturating_add(text.len()) > MAX_NOTE_BYTES { + text.clear(); + add_warning( + warnings, + format!("The Word file's footnotes are longer than {} MB: the rest were imported empty.", MAX_NOTE_BYTES / (1 << 20)), + ); + } else { + notes_done.insert(*id); + *note_bytes = note_bytes.saturating_add(text.len()); + } + let _ = st.insert_note(st.len(), &text, ParaFormat::default()); + } + InlineObject::NoteRef { .. } => { + *warned_objects = true; + let _ = st.insert_note(st.len(), "", ParaFormat::default()); + } + other => { + let text = other.plain_text(); + if text.is_empty() { + *warned_objects = true; + } else { + st.insert_with(st.len(), text, last_format.clone().unwrap_or_default()); + } + } + } + } else { + *warned_objects = true; + } + object_i = object_i.saturating_add(1); + } else { + chunk.push(word_text(c)); + } + } + flush(st, &mut last_format, &mut chunk); +} + +fn append_blocks( + doc: &craft_wordmodel::Document, + blocks: &[std::sync::Arc], + st: &mut Story, + names: &HashMap, + warned_objects: &mut bool, + notes_done: &mut HashSet, + note_bytes: &mut usize, + warnings: &mut Vec, +) { + let mut first = st.is_empty(); + for block in blocks { + match block.as_ref() { + Block::Para(p) => { + if !first && !st.text.ends_with('\n') { + st.insert(st.len(), "\n"); + } + first = false; + append_paragraph(doc, p, st, names, warned_objects, notes_done, note_bytes, warnings); + } + Block::Table(table) => { + let rows: Vec> = table.rows.iter().map(|r| r.cells.iter().map(|c| plain_blocks(&c.blocks)).collect()).collect(); + if !rows.is_empty() { + let id = st.tables.keys().next_back().copied().unwrap_or(0).saturating_add(1); + let t = Table::from_strings(id, &rows, 300.0, &ParaFormat::default(), &CharFormat::default()); + st.insert_table(st.len(), t); + first = st.text.ends_with('\n'); + } + } + } + } +} + pub fn import(bytes: &[u8]) -> Result { - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| ImportError::Corrupt(e.to_string()))?; - let doc = part(&mut zip, "word/document.xml")?.ok_or_else(|| ImportError::Corrupt("no word/document.xml (not a Word document)".into()))?; - let doc = parse(&doc)?; - // Styles. - let mut ctx = Ctx { - para_names: HashMap::new(), - char_names: HashMap::new(), - footnotes: HashMap::new(), - notes_done: std::collections::HashSet::new(), - note_bytes: 0, - warnings: vec![], - warned: std::collections::HashSet::new(), - next_table: 0, - numbering: Numbering::default(), - style_lists: HashMap::new(), - model_counter: ListCounter::default(), - default_para: BASIC_PARAGRAPH.into(), + let doc = craft_docx::read(bytes).map_err(|e| ImportError::Corrupt(e.to_string()))?; + let (mut para_styles, mut char_styles, names) = style_records(&doc.styles, &doc); + let mut story = Story::new(StoryId(0)); + let mut warned_objects = false; + let mut notes_done = HashSet::new(); + let mut note_bytes = 0usize; + let mut warnings = Vec::new(); + append_blocks(&doc, &doc.body, &mut story, &names, &mut warned_objects, &mut notes_done, &mut note_bytes, &mut warnings); + story.end_paragraphs_at_breaks(); + if warned_objects { + add_warning(&mut warnings, "Images, drawings and endnotes in the Word file were not imported (place them separately).".into()); + } + let used_p: HashSet = story.paras.iter().map(|p| p.style.clone()).collect(); + let used_c: HashSet = story.runs().map(|(_, f)| f.style.clone()).collect(); + let keep = |list: &mut Vec, used: &HashSet| { + let mut want = used.clone(); + let mut todo: Vec = used.iter().cloned().collect(); + while let Some(name) = todo.pop() { + for base in list.iter().filter(|s| s.name == name).filter_map(|s| s.based_on.as_ref()) { + if want.insert(base.clone()) { + todo.push(base.clone()); + } + } + } + list.retain(|s| want.contains(&s.name)); }; - // Lists that can't be read leave plain paragraphs, not a failed import. - match part_capped(&mut zip, "word/numbering.xml", NUMBERING_LIMIT).and_then(|s| s.map(|s| parse(&s)).transpose()) { - Ok(Some(n)) => ctx.numbering = Numbering::parse(&n), - Ok(None) => {} - Err(_) => ctx.warnings.push("The Word file's list definitions could not be read: lists were imported as plain paragraphs.".into()), + keep(&mut para_styles, &used_p); + keep(&mut char_styles, &used_c); + Ok(Imported { story, para_styles, char_styles, warnings }) +} + +fn add_warning(warnings: &mut Vec, message: String) { + if warnings.contains(&message) || warnings.len() > MAX_WARNINGS { + return; } - let mut para_styles = Vec::new(); - let mut char_styles = Vec::new(); - if let Some(styles) = part(&mut zip, "word/styles.xml")?.map(|s| parse(&s)).transpose()? { - let ids: HashMap = styles - .els() - .filter(|s| s.name == "style") - .filter_map(|s| Some((s.attr("styleId")?.to_string(), crate::capped_name(s.val("name")?)))) - .collect(); - ctx.style_lists = style_lists(&styles); - // Word's document defaults sit beneath every paragraph style that isn't based on another. - let defaults = styles.child("docDefaults"); - let default_para = para_attrs(defaults.and_then(|d| d.child("pPrDefault")).and_then(|d| d.child("pPr"))); - let default_chars = run_attrs(defaults.and_then(|d| d.child("rPrDefault")).and_then(|d| d.child("rPr"))); - let is_para = |s: &&El| s.name == "style" && s.attr("type").unwrap_or("paragraph") == "paragraph"; - let default_id = styles - .els() - .filter(is_para) - .find(|s| matches!(s.attr("default"), Some("1" | "true" | "on"))) - .or_else(|| styles.els().filter(is_para).find(|s| s.attr("styleId") == Some("Normal"))) - .and_then(|s| s.attr("styleId")); - if let Some(name) = default_id.and_then(|id| ids.get(id)) { - ctx.default_para = name.clone(); - } - for s in styles.els().filter(|s| s.name == "style") { - let (Some(id), Some(name)) = (s.attr("styleId"), s.val("name")) else { continue }; - let kind = s.attr("type").unwrap_or("paragraph"); - // Word's default paragraph font is "no character style": our [None]. - let name = match (kind, name.as_str()) { - ("character", "Default Paragraph Font") => NO_CHAR_STYLE.to_string(), - _ => crate::capped_name(name.clone()), - }; - let based_on = s.val("basedOn").and_then(|b| ids.get(&b).cloned()); - let mut imp = ImportedStyle { name: name.clone(), based_on, para: para_attrs(s.child("pPr")), chars: run_attrs(s.child("rPr")) }; - match kind { - "paragraph" => { - ctx.para_names.insert(id.to_string(), name.clone()); - if imp.based_on.is_none() { - let (mut para, mut chars) = (default_para.clone(), default_chars.clone()); - para.merge(&imp.para); - chars.merge(&imp.chars); - (imp.para, imp.chars) = (para, chars); - } - para_styles.push(imp); - } - "character" => { - ctx.char_names.insert(id.to_string(), name.clone()); - if name != NO_CHAR_STYLE { - char_styles.push(imp); - } - } - _ => {} - } - } + if warnings.len() == MAX_WARNINGS { + warnings.push(format!("The Word file has more than {MAX_WARNINGS} kinds of problem: the rest are not listed.")); + } else { + warnings.push(message); } - // Footnotes (ids -1 / 0 are the separators). - if let Some(f) = part(&mut zip, "word/footnotes.xml")?.map(|s| parse(&s)).transpose()? { - for n in f.els().filter(|n| n.name == "footnote") { - if let Some(id) = n.attr("id").filter(|id| id.parse::().is_ok_and(|v| v > 0)) { - ctx.footnotes.insert(id.to_string(), n.clone()); - } - } - } - let body = doc.child("body").ok_or_else(|| ImportError::Corrupt("no document body".into()))?; - let mut st = Story::new(StoryId(0)); - ctx.blocks(body, &mut st, true); - // A page or column break ends its paragraph. - st.end_paragraphs_at_breaks(); - // Only styles that are used, plus what they are based on. - let stories = || std::iter::once(&st).chain(st.notes.iter().map(|n| &n.text)); - let used_p: std::collections::HashSet = stories().flat_map(|s| s.paras.iter().map(|p| p.style.clone())).collect(); - let used_c: std::collections::HashSet = stories().flat_map(|s| s.runs().map(|(_, f)| f.style.clone())).collect(); - let keep = |list: Vec, used: &std::collections::HashSet| { - let mut want: std::collections::HashSet = used.clone(); - { - // What each name is based on (every style of that name), then a worklist: each name - // is looked at once, however long the chain. - let mut bases: HashMap<&str, Vec<&str>> = HashMap::new(); - for s in &list { - if let Some(b) = &s.based_on { - bases.entry(s.name.as_str()).or_default().push(b.as_str()); - } - } - let mut todo: Vec = want.iter().cloned().collect(); - while let Some(name) = todo.pop() { - for b in bases.get(name.as_str()).into_iter().flatten() { - if want.insert((*b).to_string()) { - todo.push((*b).to_string()); - } - } - } - } - list.into_iter().filter(|s| want.contains(&s.name)).collect::>() - }; - Ok(Imported { story: st, para_styles: keep(para_styles, &used_p), char_styles: keep(char_styles, &used_c), warnings: ctx.warnings }) } #[cfg(test)] diff --git a/crates/textimport/src/docx/tests.rs b/crates/textimport/src/docx/tests.rs index fdaa79a11..ad5f08da1 100644 --- a/crates/textimport/src/docx/tests.rs +++ b/crates/textimport/src/docx/tests.rs @@ -1,23 +1,6 @@ use super::*; use std::io::Write; -#[test] -fn cdata_text() { - let el = parse("").unwrap(); - assert_eq!(el.child("t").unwrap().text(), "x < y"); -} - -/// Issue #712: nesting past the budget is an error from the parser, not a stack overflow later. -#[test] -fn nesting_past_the_budget_is_refused() { - let nested = |n: usize| format!("{}{}", "".repeat(n), "".repeat(n)); - let ok = parse(&nested(MAX_DEPTH)).expect("the budget itself parses"); - assert_eq!(ok.name, "p"); - let err = parse(&nested(MAX_DEPTH + 1)).expect_err("one level more is refused"); - assert!(err.to_string().contains("nested deeper"), "{err}"); - assert!(parse(&nested(100_000)).is_err()); -} - #[test] fn table_adds_no_empty_paragraph() { let document = format!( @@ -55,17 +38,6 @@ fn docx_parts(parts: &[(&str, &str)]) -> Vec { const W: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; -#[test] -fn oversized_part_is_an_error_not_a_huge_read() { - // A highly compressible part: 64 KiB of spaces inflates past a 4 KiB cap. - let big = docx(&" ".repeat(64 * 1024), "", ""); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(big.as_slice())).unwrap(); - assert!(matches!(part_capped(&mut zip, "word/document.xml", 4096), Err(ImportError::Corrupt(_)))); - // At or under the cap reads fine; a missing part is `None`. - assert_eq!(part_capped(&mut zip, "word/document.xml", 64 * 1024).unwrap().map(|s| s.len()), Some(64 * 1024)); - assert_eq!(part_capped(&mut zip, "word/missing.xml", 4096).unwrap(), None); -} - /// A DOCX can hold any number where a measure belongs: `1e300` twips became a 5e298 pt /// indent, and `w:sz` of `1e300` a 5e299 pt type size. Measures are clamped to the widest /// page and a size the engine cannot mean is dropped (#1593). @@ -139,7 +111,7 @@ fn a_footnote_table_counts_toward_the_note_budget() { assert_eq!(i.story.notes.len(), 18, "all 18 references are kept"); // What was retained is the budget plus at most the one note that was already being built // when it ran out -- not all 10.8 MB. - let held: usize = i.story.notes.iter().map(|n| note_bytes(&n.text, MAX_NOTE_DEPTH)).sum(); + let held: usize = i.story.notes.iter().map(|n| n.text.text.len()).sum(); assert!(held <= (8 << 20) + big.len() + 1024, "the cell text is inside the budget: {held} bytes"); assert!(held > big.len(), "and the notes that fit were imported in full: {held} bytes"); assert!(i.warnings.iter().any(|w| w.contains("longer than 8 MB")), "the budget warning is reported: {:?}", i.warnings); @@ -564,20 +536,6 @@ fn hostile_numbering_falls_back_to_plain_paragraphs() { assert_eq!(i.warnings.len(), 1); } -#[test] -fn an_element_with_many_attributes_parses_in_linear_time() { - // RUSTSEC-2026-0194: before quick-xml 0.41 the duplicate-attribute check compared each name with every - // earlier one on the element, so a crafted .docx/.xlsx with one huge tag hung the import (O(N²)). - let attrs: String = (0..100_000).map(|i| format!(" w:a{i}=\"{i}\"")).collect(); - let xml = format!(""); - let started = std::time::Instant::now(); - let doc = parse(&xml).unwrap(); - assert!(started.elapsed() < std::time::Duration::from_secs(5), "parse took {:?}", started.elapsed()); - let p = doc.child("body").and_then(|b| b.child("p")).unwrap(); - assert_eq!(p.attr("a0"), Some("0")); - assert_eq!(p.attr("a99999"), Some("99999")); -} - #[test] fn table_cells_read_text_like_body_paragraphs() { // Regression (#721): cells took every text node, so deleted text, move sources and field @@ -638,11 +596,3 @@ fn off_valued_toggles_stay_off() { let on = st.format_after(st.text.find("bold").unwrap()).clone().over; assert_eq!(on.font_style.as_deref(), Some("Bold"), "`on` is still on"); } - -#[test] -fn signed_numeric_character_references_are_dropped() { - let el = parse("&#+65;&#x+42;&#-65;&#x-42;&#;&#x;").unwrap(); - assert_eq!(el.text(), ""); - let ok = parse("AB").unwrap(); - assert_eq!(ok.text(), "AB"); -} diff --git a/crates/textimport/src/docx_numbering.rs b/crates/textimport/src/docx_numbering.rs deleted file mode 100644 index a00ba8ea7..000000000 --- a/crates/textimport/src/docx_numbering.rs +++ /dev/null @@ -1,357 +0,0 @@ -//! Word lists: `word/numbering.xml` and the `w:numPr` of paragraphs and paragraph styles. -//! -//! A `w:num` (numId) points at a `w:abstractNum`, whose `w:lvl` elements (levels 0–8) give -//! each level's format, label text, start number, indents and the text after the label. -//! -//! Numbering follows Word's main rules, simplified: -//! - Numbers count per level. A paragraph at level L restarts the levels below it (unless a -//! level's `w:lvlRestart` says otherwise). -//! - `num` instances of one abstractNum share their counters, so a list carries on across -//! them and across paragraphs that aren't in a list. A `num` with a `w:startOverride` counts -//! on its own, starting at the override. -//! - Levels map to the model's list levels (`ilvl` + 1). Parent levels in a label (`%1.%2.`) -//! become the model's level references (`^1.^2.`); where the model's level numbers would differ -//! from Word's (a legal-style label, a level the model hasn't numbered), the importer writes -//! them as text instead ([`ListPara::fixed_expression`]). - -use std::collections::HashMap; - -use designcraft_doc::{ListType, NumberStyle, ParaAttrs}; - -use crate::docx::El; - -/// Most `w:abstractNum` or `w:num` elements read; the rest are ignored. -pub(crate) const MAX_DEFS: usize = 10_000; -/// Word's list levels are 0–8. -const LEVELS: usize = 9; -/// Longest label text (`w:lvlText`) kept, in characters. -const MAX_LVL_TEXT: usize = 64; - -#[derive(Clone, Copy, Debug, Default, PartialEq)] -enum Format { - #[default] - Bullet, - /// No number: the label is the level text alone. - Nothing, - Number(NumberStyle), -} - -#[derive(Clone, Copy, Debug, Default, PartialEq)] -enum Suffix { - #[default] - Tab, - Space, - Nothing, -} - -#[derive(Clone, Debug, Default)] -struct Level { - format: Format, - text: String, - start: u32, - suffix: Suffix, - left: Option, - /// First-line indent in points (negative for a hanging indent). - first_line: Option, - /// `w:lvlRestart`: 0 = never restart, n = restart after level n (1-based) or a higher one. - restart: Option, - /// `w:isLgl`: parent levels in the label are written as decimal numbers. - legal: bool, - /// The font of the label (`w:rPr/w:rFonts`), which decides what a Symbol/Wingdings bullet is. - font: Option, - picture: bool, -} - -#[derive(Debug, Default)] -struct Num { - abstract_id: String, - /// `w:lvlOverride` per level: a start override and/or a replacement level. - start_override: [Option; LEVELS], - level_override: [Option; LEVELS], -} - -impl Num { - fn has_start_override(&self) -> bool { - self.start_override.iter().any(Option::is_some) - } -} - -/// A list paragraph: the paragraph attributes that make it one, and its number in Word. -#[derive(Clone, Debug, PartialEq)] -pub(crate) struct ListPara { - pub(crate) attrs: ParaAttrs, - /// The paragraph's number at its level (also counted for bullets). - pub(crate) number: u32, - /// Parent levels the number expression refers to (`^k`, 1-based) and the text Word shows - /// for each. - pub(crate) parents: Vec<(u32, String)>, - /// The number expression with Word's parent level numbers written as text. - pub(crate) fixed_expression: Option, -} - -/// The lists of a Word document and the numbering state while its paragraphs are read in order. -#[derive(Debug, Default)] -pub(crate) struct Numbering { - abstracts: HashMap; LEVELS]>, - nums: HashMap, - /// Current number per level, per counter key (an abstractNum, or a `num` with overrides). - counters: HashMap; LEVELS]>, -} - -fn level_index(e: &El) -> Option { - e.attr("ilvl").and_then(|v| v.trim().parse::().ok()).filter(|l| *l < LEVELS) -} - -fn parse_u32(v: Option) -> Option { - let v = v?; - let v = v.trim(); - // Negative starts count from 0; numbers too large for a u32 are clamped. - match v.parse::() { - Ok(n) => Some(u32::try_from(n.max(0)).unwrap_or(u32::MAX)), - Err(_) => None, - } -} - -fn number_style(fmt: &str) -> Format { - match fmt { - "bullet" => Format::Bullet, - "none" => Format::Nothing, - "decimalZero" => Format::Number(NumberStyle::ArabicLeadingZero), - "upperRoman" => Format::Number(NumberStyle::UpperRoman), - "lowerRoman" => Format::Number(NumberStyle::LowerRoman), - "upperLetter" => Format::Number(NumberStyle::UpperLetters), - "lowerLetter" => Format::Number(NumberStyle::LowerLetters), - "chicago" => Format::Number(NumberStyle::Symbols), - // decimal and every format the model has no style for (ordinal, cardinalText, - // decimalEnclosedCircle, the East Asian counting systems, …). - _ => Format::Number(NumberStyle::Arabic), - } -} - -fn parse_level(e: &El) -> Level { - // Twips, clamped as elsewhere: a level's indent is a measure, not any number (#1593). - let tw = |v: Option<&str>| v.and_then(|v| v.trim().parse::().ok()).map(|v| v / 20.0).and_then(designcraft_doc::attrs::imported_measure); - let ind = e.child("pPr").and_then(|p| p.child("ind")); - Level { - format: e.val("numFmt").map_or(Format::Number(NumberStyle::Arabic), |f| number_style(&f)), - text: e.child("lvlText").and_then(|t| t.attr("val")).unwrap_or_default().chars().take(MAX_LVL_TEXT).collect(), - start: parse_u32(e.val("start")).unwrap_or(1), - suffix: match e.val("suff").as_deref() { - Some("space") => Suffix::Space, - Some("nothing") => Suffix::Nothing, - _ => Suffix::Tab, - }, - left: ind.and_then(|i| tw(i.attr("left").or(i.attr("start")))), - first_line: ind.and_then(|i| tw(i.attr("firstLine")).or(tw(i.attr("hanging")).map(|h| -h))), - restart: parse_u32(e.val("lvlRestart")), - legal: e.on("isLgl") == Some(true), - // Only the font's kind matters (Symbol, Wingdings), and every bullet paragraph looks at it: - // a font name longer than any real one is cut. - font: e - .child("rPr") - .and_then(|r| r.child("rFonts")) - .and_then(|f| f.attr("ascii").or(f.attr("hAnsi"))) - .map(|f| f.chars().take(MAX_FONT_NAME_CHARS).collect()), - picture: e.child("lvlPicBulletId").is_some(), - } -} - -/// Longest font name kept from a numbering level. -const MAX_FONT_NAME_CHARS: usize = 128; - -/// The bullet Word shows for `text` in `font`. Symbol and Wingdings bullets are stored as -/// private-use characters (U+F0xx) or as the font's own code points; the common ones become -/// their Unicode look-alikes, unknown ones a plain bullet. -fn bullet_char(text: &str, font: Option<&str>) -> String { - let font = font.unwrap_or_default().to_ascii_lowercase(); - let symbol = font.contains("symbol"); - let wingdings = font.contains("wingdings"); - text.chars() - .map(|c| { - let code = u32::from(c); - let low = if (0xF000..=0xF0FF).contains(&code) { code - 0xF000 } else { code }; - if wingdings { - return match low { - 0x6C => '●', - 0x6E => '■', - 0x71 => '❑', - 0x76 => '❖', - 0xA7 => '▪', - 0xD8 => '➢', - 0xFC => '✓', - _ => '•', - }; - } - if symbol { - return match low { - 0x2D => '–', - 0x6F => '◦', - _ => '•', - }; - } - match c { - // Word's second-level bullet is a Courier New "o". - 'o' => '◦', - '\u{E000}'..='\u{F8FF}' => '•', - c => c, - } - }) - .filter(|c| *c != '\n' && *c != '\r') - .collect() -} - -impl Numbering { - /// Read `numbering.xml`'s root (`w:numbering`). - pub(crate) fn parse(root: &El) -> Self { - let mut n = Numbering::default(); - for e in root.els() { - match e.name.as_str() { - "abstractNum" if n.abstracts.len() < MAX_DEFS => { - let Some(id) = e.attr("abstractNumId") else { continue }; - let mut levels: [Option; LEVELS] = Default::default(); - for l in e.els().filter(|l| l.name == "lvl") { - if let Some(slot) = level_index(l).and_then(|i| levels.get_mut(i)) { - *slot = Some(parse_level(l)); - } - } - n.abstracts.insert(id.to_string(), levels); - } - "num" if n.nums.len() < MAX_DEFS => { - let (Some(id), Some(abstract_id)) = (e.attr("numId"), e.val("abstractNumId")) else { continue }; - let mut num = Num { abstract_id, ..Default::default() }; - for o in e.els().filter(|o| o.name == "lvlOverride") { - let Some(i) = level_index(o) else { continue }; - if let Some(slot) = num.start_override.get_mut(i) { - *slot = parse_u32(o.val("startOverride")); - } - if let (Some(slot), Some(l)) = (num.level_override.get_mut(i), o.child("lvl")) { - *slot = Some(parse_level(l)); - } - } - n.nums.insert(id.to_string(), num); - } - _ => {} - } - } - n - } - - /// The list formatting of the next paragraph with `w:numId` `num_id` at level `ilvl`, counting - /// it. `None` (a plain paragraph) for numId 0, an unknown list or level, or a level past 8. - pub(crate) fn next(&mut self, num_id: &str, ilvl: u32) -> Option { - let lv = usize::try_from(ilvl).ok().filter(|l| *l < LEVELS)?; - let num = self.nums.get(num_id)?; - let abs = self.abstracts.get(&num.abstract_id)?; - let level = |k: usize| num.level_override.get(k).and_then(Option::as_ref).or_else(|| abs.get(k).and_then(Option::as_ref)); - let def = level(lv)?; - let own = num.has_start_override(); - let key = if own { format!("n:{num_id}") } else { format!("a:{}", num.abstract_id) }; - let start = |k: usize| { - let o = if own { num.start_override.get(k).copied().flatten() } else { None }; - o.or_else(|| level(k).map(|l| l.start)).unwrap_or(1) - }; - let counters = self.counters.entry(key).or_default(); - let n = counters.get(lv).copied().flatten().map_or_else(|| start(lv), |v| v.saturating_add(1)); - if let Some(c) = counters.get_mut(lv) { - *c = Some(n); - } - for (m, c) in counters.iter_mut().enumerate().skip(lv + 1) { - let restart = match level(m).and_then(|l| l.restart) { - None => true, - Some(0) => false, - Some(r) => ilvl < r, - }; - if restart { - *c = None; - } - } - // Parent levels' current numbers, as text, for the label. - let parent = |k: usize| -> String { - let v = counters.get(k).copied().flatten().unwrap_or_else(|| start(k)); - match level(k).map(|l| l.format) { - Some(Format::Number(s)) if !def.legal => s.format(v), - Some(Format::Number(_)) => v.to_string(), - _ => String::new(), - } - }; - // `refs`: a parent number level becomes the model's `^k`; otherwise Word's text. - let mut parents = Vec::new(); - let mut label = |own_level: &str, escape: bool, refs: bool| -> String { - let mut out = String::new(); - let mut it = def.text.chars().peekable(); - while let Some(c) = it.next() { - if c == '%' - && let Some(k) = it.peek().and_then(|d| d.to_digit(10)).filter(|d| (1..=9).contains(d)) - { - it.next(); - let k = k as usize - 1; - let number = matches!(level(k).map(|l| l.format), Some(Format::Number(_))); - if k == lv { - out.push_str(own_level); - } else if refs && number && !def.legal { - out.push('^'); - out.push_str(&(k + 1).to_string()); - parents.push((k as u32 + 1, parent(k))); - } else { - let p = parent(k); - out.push_str(&if escape { p.replace('^', "^^") } else { p }); - } - } else if escape && c == '^' { - out.push_str("^^"); - } else if c != '\n' && c != '\r' { - out.push(c); - } - } - out - }; - // The model's defaults: `^t` after a number, a literal tab after a bullet. - let (number_sep, bullet_sep) = match def.suffix { - Suffix::Tab => ("^t", "\t"), - Suffix::Space => (" ", " "), - Suffix::Nothing => ("", ""), - }; - let mut a = ParaAttrs::default(); - let mut fixed_expression = None; - match def.format { - Format::Number(style) => { - a.list_type = Some(ListType::Numbers); - a.number_style = Some(style); - a.number_expression = Some(format!("{}{number_sep}", label("^#", true, true))); - fixed_expression = Some(format!("{}{number_sep}", label("^#", true, false))); - } - Format::Bullet => { - a.list_type = Some(ListType::Bullets); - a.bullet_char = Some(if def.picture { "•".into() } else { bullet_char(&def.text, def.font.as_deref()) }); - a.list_separator = Some(bullet_sep.into()); - } - Format::Nothing => { - a.list_type = Some(ListType::Bullets); - a.bullet_char = Some(label("", false, false)); - a.list_separator = Some(bullet_sep.into()); - } - } - a.left_indent = def.left; - a.first_line_indent = def.first_line; - a.list_level = Some(ilvl + 1); - match def.restart { - None => {} - Some(0) => a.restart_numbers = Some(false), - Some(r) => a.restart_after_level = Some(r), - } - Some(ListPara { attrs: a, number: n, parents, fixed_expression }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn symbol_and_wingdings_bullets_become_unicode() { - assert_eq!(bullet_char("\u{F0B7}", Some("Symbol")), "•"); - assert_eq!(bullet_char("\u{F0A7}", Some("Wingdings")), "▪"); - assert_eq!(bullet_char("o", Some("Courier New")), "◦"); - assert_eq!(bullet_char("–", None), "–"); - assert_eq!(bullet_char("\u{F076}", None), "•"); - } -} diff --git a/crates/textimport/src/lib.rs b/crates/textimport/src/lib.rs index 26b1b1fc3..0838861e2 100644 --- a/crates/textimport/src/lib.rs +++ b/crates/textimport/src/lib.rs @@ -9,7 +9,6 @@ #![forbid(unsafe_code)] mod docx; -mod docx_numbering; pub mod export; mod rtf; pub mod tagged; @@ -29,44 +28,6 @@ pub fn xlsx_records(bytes: &[u8], sheet: Option<&str>) -> Result use designcraft_doc::{CharAttrs, ParaAttrs, Story}; -/// Characters kept of a style name read from a file: each paragraph that uses a style carries a -/// copy of its name, so a long one would be multiplied by the paragraph count. -pub(crate) const MAX_NAME_CHARS: usize = 256; - -pub(crate) fn capped_name(mut name: String) -> String { - if let Some((at, _)) = name.char_indices().nth(MAX_NAME_CHARS) { - name.truncate(at); - } - name -} - -/// Joins UTF-16 code units that arrive one at a time (RTF `\uN`, Tagged Text `<0xNNNN>`) into -/// characters. A character past U+FFFF comes as two units, a surrogate pair: the first half -/// waits for the second. -#[derive(Default)] -pub(crate) struct Utf16Units { - high: Option, -} - -impl Utf16Units { - /// The character that `unit` is or completes. Half a pair without its partner is no character. - pub(crate) fn push(&mut self, unit: u32) -> Option { - match (self.high.take(), unit) { - (Some(high), 0xDC00..=0xDFFF) => char::from_u32(0x10000 + ((high - 0xD800) << 10) + (unit - 0xDC00)), - (_, 0xD800..=0xDBFF) => { - self.high = Some(unit); - None - } - _ => char::from_u32(unit), - } - } - - /// Other text came in between: a waiting first half has no partner. - pub(crate) fn reset(&mut self) { - self.high = None; - } -} - /// A paragraph or character style found in the source. #[derive(Clone, Debug, Default, PartialEq)] pub struct ImportedStyle { diff --git a/crates/textimport/src/tagged.rs b/crates/textimport/src/tagged.rs index c3668614d..d8a56aff0 100644 --- a/crates/textimport/src/tagged.rs +++ b/crates/textimport/src/tagged.rs @@ -1,456 +1,72 @@ -//! Tagged Text: plain text with `` markup for paragraph and character styles and local -//! formatting — the text interchange format of layout applications. Written as ASCII (other -//! characters as `<0xXXXX>`); read from ASCII, UTF-8 or UTF-16 files. Paragraph styles, character -//! styles, font family, font style and size are kept; other tags are skipped. +//! DesignCraft model adapter for the shared Tagged Text codec. -use std::fmt::Write as _; - -use designcraft_doc::{CharFormat, Document, Story, StoryId, story::NO_CHAR_STYLE}; +use designcraft_doc::{CharFormat, Document, Story, StoryId, attrs::CharAttrs, story::NO_CHAR_STYLE}; use crate::{ImportError, Imported, ImportedStyle}; -fn escape(out: &mut String, c: char) { - match c { - '<' => out.push_str("\\<"), - '>' => out.push_str("\\>"), - '\\' => out.push_str("\\\\"), - c if c.is_ascii() && !c.is_ascii_control() => out.push(c), - '\t' => out.push('\t'), - c => { - let mut buf = [0u16; 2]; - for u in c.encode_utf16(&mut buf) { - let _ = write!(out, "<0x{u:04X}>"); - } - } - } -} - -/// A tag value with '<', '>' and '\\' escaped by a backslash, so it keeps the tag intact. -fn escape_value(v: &str) -> String { - let mut out = String::with_capacity(v.len()); - for c in v.chars() { - if matches!(c, '<' | '>' | '\\') { - out.push('\\'); - } - out.push(c); - } - out -} - -/// A tag value (cut to [`crate::MAX_NAME_CHARS`]: style names are copied into every paragraph) with the escapes `escape_value` writes (`\\`, `\<`, `\>`) decoded. Any other -/// backslash, including a trailing one, stays literal, as older files and InDesign's `\:` expect. -fn unescape_value(v: &str) -> String { - let mut out = String::with_capacity(v.len()); - let mut it = v.chars().peekable(); - while let Some(c) = it.next() { - if c == '\\' - && let Some(&n) = it.peek() - && matches!(n, '\\' | '<' | '>') - { - out.push(n); - it.next(); - } else { - out.push(c); - } - } - crate::capped_name(out) -} - -/// The first format version whose `cFont` and `cTypeface` values are escaped like style names. -/// Older files hold them raw, so a backslash in them is literal. -const ESCAPED_FONTS_VERSION: u32 = 8; - -/// A `cFont` or `cTypeface` value: decoded in files that escape it, literal in older ones. -fn font_value(v: &str, escaped: bool) -> String { - if escaped { unescape_value(v) } else { crate::capped_name(v.to_string()) } -} - -/// `story` as Tagged Text. pub fn tagged_text(doc: &Document, story: &Story) -> String { - let mut out = String::from("\r\n\r\n"); - let ranges = story.para_ranges(); - for (i, r) in ranges.iter().enumerate() { - if i > 0 { - out.push_str("\r\n"); - } - let pf = story.paras.get(i).cloned().unwrap_or_default(); - let _ = write!(out, "", escape_value(&pf.style)); - for (rr, f) in story.runs() { - let (a, b) = (rr.start.max(r.start), rr.end.min(r.end)); - if a >= b { + let mut paragraphs = Vec::new(); + for (i, range) in story.para_ranges().iter().enumerate() { + let mut paragraph = + craft_idml::tagged::Paragraph { style: story.paras.get(i).map_or_else(String::new, |p| p.style.clone()), runs: Vec::new() }; + for (run_range, format) in story.runs() { + let start = run_range.start.max(range.start); + let end = run_range.end.min(range.end); + if start >= end + || doc.conditions_hide(format.over.conditions.as_deref().unwrap_or(&[])) + || format.over.change == Some(designcraft_doc::ChangeMark::Deleted) + { continue; } - if doc.conditions_hide(f.over.conditions.as_deref().unwrap_or(&[])) || f.over.change == Some(designcraft_doc::ChangeMark::Deleted) { - continue; - } - let mut open = Vec::new(); - if f.style != NO_CHAR_STYLE { - let _ = write!(out, "", escape_value(&f.style)); - open.push("CharStyle"); - } - if let Some(v) = &f.over.font_family { - let _ = write!(out, "", escape_value(v)); - open.push("cFont"); - } - if let Some(v) = &f.over.font_style { - let _ = write!(out, "", escape_value(v)); - open.push("cTypeface"); - } - if let Some(v) = f.over.size { - let _ = write!(out, ""); - open.push("cSize"); - } - for c in story.text[a..b].chars() { - escape(&mut out, c); - } - for t in open.into_iter().rev() { - let _ = write!(out, "<{t}:>"); - } + paragraph.runs.push(craft_idml::tagged::Run { + text: story.text[start..end].to_string(), + format: craft_idml::tagged::CharFormat { + style: (format.style != NO_CHAR_STYLE).then(|| format.style.clone()), + font: format.over.font_family.clone(), + typeface: format.over.font_style.clone(), + size: format.over.size, + }, + }); } + paragraphs.push(paragraph); } - out + craft_idml::tagged::write(¶graphs) } -/// Text of a Tagged Text file (ASCII, UTF-8 or UTF-16 by byte-order mark). pub(crate) fn decode(bytes: &[u8]) -> String { - if bytes.starts_with(&[0xFF, 0xFE]) || bytes.starts_with(&[0xFE, 0xFF]) { - let le = bytes[0] == 0xFF; - let units: Vec = bytes[2..] - .as_chunks::<2>() - .0 - .iter() - .map(|c| if le { u16::from_le_bytes([c[0], c[1]]) } else { u16::from_be_bytes([c[0], c[1]]) }) - .collect(); - return String::from_utf16_lossy(&units); - } - String::from_utf8_lossy(bytes).trim_start_matches('\u{FEFF}').to_string() + craft_idml::tagged::decode(bytes) } -/// Is this Tagged Text (by its header)? pub fn is_tagged(bytes: &[u8]) -> bool { - let head = decode(&bytes[..bytes.len().min(64)]); - let h = head.trim_start(); - h.starts_with(" Result { - let text = decode(bytes); - let chars: Vec = text.chars().collect(); - // Paragraphs: (style, runs of (text, format)). - let mut paras: Vec<(String, Vec<(String, CharFormat)>)> = vec![(String::new(), Vec::new())]; - let mut started = false; - let mut fmt = CharFormat::default(); - let mut para_styles: Vec = Vec::new(); - let mut char_styles: Vec = Vec::new(); - // The names in each list, for membership (the lists keep the order the file names them). - let (mut para_seen, mut char_seen) = (std::collections::HashSet::new(), std::collections::HashSet::new()); - let mut warnings = Vec::new(); - let push = |paras: &mut Vec<(String, Vec<(String, CharFormat)>)>, c: char, fmt: &CharFormat| { - let Some((_, runs)) = paras.last_mut() else { return }; - match runs.last_mut() { - Some((t, f)) if f == fmt => t.push(c), - _ => runs.push((c.to_string(), fmt.clone())), + let parsed = craft_idml::tagged::parse(bytes); + let mut story = Story::new(StoryId(0)); + for (index, paragraph) in parsed.paragraphs.iter().enumerate() { + if index > 0 { + let at = story.len(); + story.insert(at, "\n"); } - }; - let mut escaped_fonts = false; - let mut units = crate::Utf16Units::default(); - let mut i = 0; - let mut skipped = 0; - while i < chars.len() { - let c = chars[i]; - match c { - '\\' if i + 1 < chars.len() => { - units.reset(); - push(&mut paras, chars[i + 1], &fmt); - i += 2; - } - '<' => { - // A tag: up to the matching '>' (nested in definitions). - let mut depth = 0; - let mut j = i; - while j < chars.len() { - match chars[j] { - '\\' => j += 1, - '<' => depth += 1, - '>' => { - depth -= 1; - if depth == 0 { - break; - } - } - _ => {} - } - j += 1; - } - let tag: String = chars[i + 1..j.min(chars.len())].iter().collect(); - i = j + 1; - let (name, value) = tag.split_once(':').map_or((tag.as_str(), ""), |(a, b)| (a, b)); - match name { - n if n.starts_with("0x") => { - // `from_str_radix` accepts a leading '+', so check the digits first. - if let Some(digits) = n.get(2..) - && digits.bytes().all(|b| b.is_ascii_hexdigit()) - && let Ok(u) = u32::from_str_radix(digits, 16) - && let Some(ch) = units.push(u) - { - push(&mut paras, ch, &fmt); - } - } - "ParaStyle" => { - started = true; - let v = unescape_value(value); - if !v.is_empty() && para_seen.insert(v.clone()) { - para_styles.push(v.clone()); - } - if let Some(p) = paras.last_mut() { - p.0 = v; - } - } - "CharStyle" => { - let v = unescape_value(value); - fmt.style = if v.is_empty() { NO_CHAR_STYLE.into() } else { v.clone() }; - if !v.is_empty() && char_seen.insert(v.clone()) { - char_styles.push(v); - } - } - "cFont" => fmt.over.font_family = (!value.is_empty()).then(|| font_value(value, escaped_fonts)), - "cTypeface" => fmt.over.font_style = (!value.is_empty()).then(|| font_value(value, escaped_fonts)), - // A size the engine cannot mean (0, negative, 1e300) is dropped, so the text - // keeps its style's size (#1593). - "cSize" => fmt.over.size = value.trim().parse().ok().and_then(designcraft_doc::attrs::imported_type_size), - "DefineParaStyle" | "DefineCharStyle" => { - let nm = crate::capped_name(value.split('=').next().unwrap_or("").to_string()); - let (list, seen) = - if name == "DefineParaStyle" { (&mut para_styles, &mut para_seen) } else { (&mut char_styles, &mut char_seen) }; - if !nm.is_empty() && seen.insert(nm.clone()) { - list.push(nm); - } - } - "Version" => escaped_fonts = value.trim().parse::().is_ok_and(|v| v >= ESCAPED_FONTS_VERSION), - n if n.starts_with("ASCII-") || n.starts_with("UNICODE-") || n.starts_with("ANSI-") => {} - _ => skipped += 1, - } - } - '\r' | '\n' => { - // A paragraph break (CRLF counts once). - if c == '\r' && chars.get(i + 1) == Some(&'\n') { - i += 1; - } - let style = paras.last().map(|p| p.0.clone()).unwrap_or_default(); - // Header lines before any text don't start paragraphs. - if !started && paras.len() == 1 && paras.first().is_some_and(|p| p.1.is_empty()) { - i += 1; - continue; - } - paras.push((style, Vec::new())); - i += 1; - } - c => { - units.reset(); - push(&mut paras, c, &fmt); - i += 1; - } + for run in ¶graph.runs { + let format = CharFormat { + style: run.format.style.as_deref().filter(|s| !s.is_empty()).unwrap_or(NO_CHAR_STYLE).to_string(), + over: CharAttrs { + font_family: run.format.font.clone(), + font_style: run.format.typeface.clone(), + size: run.format.size.and_then(designcraft_doc::attrs::imported_type_size), + ..Default::default() + }, + }; + let at = story.len(); + story.insert_with(at, &run.text, format); + } + if let Some(p) = story.paras.last_mut() { + p.style = paragraph.style.clone(); } } - if skipped > 0 { - warnings.push(format!("{skipped} formatting tag(s) not kept")); - } - // Build the story. - let mut st = Story::new(StoryId(0)); - for (k, (style, runs)) in paras.iter().enumerate() { - if k > 0 { - let n = st.len(); - st.insert(n, "\n"); - } - for (t, f) in runs { - let n = st.len(); - st.insert_with(n, t, f.clone()); - } - if !style.is_empty() - && let Some(p) = st.paras.last_mut() - { - p.style = style.clone(); - } - } - let mk = |n: &String| ImportedStyle { name: n.clone(), ..Default::default() }; - Ok(Imported { story: st, para_styles: para_styles.iter().map(mk).collect(), char_styles: char_styles.iter().map(mk).collect(), warnings }) -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Characters past U+FFFF are written as two tags, a UTF-16 surrogate pair. Each half was - /// converted alone and dropped. - #[test] - fn surrogate_pairs_become_one_character() { - let i = import("\r\n\r\nA<0xD83D><0xDE00>B <0xD842><0xDFB7><0x7530>".as_bytes()).unwrap(); - assert_eq!(i.story.text, "A\u{1F600}B \u{20BB7}\u{7530}"); - // What the exporter writes comes back. - let doc = Document::new(&designcraft_doc::build::NewDocument::default()); - let back = import(tagged_text(&doc, &i.story).as_bytes()).unwrap(); - assert_eq!(back.story.text, i.story.text); - // Half a pair is not a character: it is still left out. - assert_eq!(import("\r\nA<0xD83D>B<0xDE00>C".as_bytes()).unwrap().story.text, "ABC"); - } - - /// A style name is read back exactly as the exporter wrote it, spaces included. - #[test] - fn style_names_keep_leading_and_trailing_spaces() { - let i = import("\r\n\r\nHello".as_bytes()).unwrap(); - assert_eq!(i.story.paras[0].style, "Body "); - assert_eq!(i.story.char_format_at(2).style, " Em "); - assert_eq!(i.para_styles[0].name, "Body "); - assert_eq!(i.char_styles[0].name, " Em "); - } - - /// A style defined with spaces in its name keeps them, so a paragraph that uses the name matches. - #[test] - fn defined_style_names_keep_leading_and_trailing_spaces() { - let src = - "\r\n>>Hello"; - let i = import(src.as_bytes()).unwrap(); - assert_eq!(i.para_styles.len(), 1); - assert_eq!(i.para_styles[0].name, "Body "); - assert_eq!(i.char_styles.len(), 1); - assert_eq!(i.char_styles[0].name, " Em "); - } - - /// `from_str_radix` accepts a leading `+`, so `<0x+41>` inserted an 'A'. A signed or empty - /// code is not a code point tag and is skipped. - #[test] - fn a_signed_hex_code_tag_is_skipped() { - for t in ["<0x+41>", "<0x-41>", "<0x>", "<0xZZ>"] { - let src = format!("\r\nA{t}B"); - assert_eq!(import(src.as_bytes()).unwrap().story.text, "AB", "{t}"); - } - } - - /// A size does not have to be non-finite to be nonsense: a file can hold `1e300`, `0` or a - /// negative, and tagged text took all three and set them on the text (#1593). They are now - /// dropped, as RTF and DOCX already dropped them, so the text keeps its style's size. - #[test] - fn a_char_size_the_engine_cannot_mean_is_dropped() { - for v in ["1e300", "-5", "0", "1297", "0.09"] { - let src = format!("\r\nbig"); - let i = import(src.as_bytes()).unwrap(); - let size = i.story.char_format_at(i.story.text.find("big").unwrap() + 1).over.size; - assert_eq!(size, None, "cSize {v} is not a size the engine can mean"); - } - // A size inside the range still arrives. - let src = "\r\nbig"; - let i = import(src.as_bytes()).unwrap(); - let size = i.story.char_format_at(i.story.text.find("big").unwrap() + 1).over.size; - assert_eq!(size, Some(18.0)); - } - - /// `f64::from_str` accepts "NaN" and "inf"; a non-finite size must not reach the story. - #[test] - fn non_finite_char_size_is_ignored() { - for v in ["NaN", "inf", "infinity", "-inf"] { - let src = format!("\r\nbig"); - let i = import(src.as_bytes()).unwrap(); - let size = i.story.char_format_at(i.story.text.find("big").unwrap() + 1).over.size; - assert!(size.is_none_or(f64::is_finite), "{v}: size {size:?}"); - } - } - - #[test] - fn tagged_text_keeps_a_leading_empty_paragraph() { - let doc = Document::new(&designcraft_doc::build::NewDocument::default()); - let mut story = Story::new(StoryId(0)); - story.insert(0, "\nText"); - story.paras[0].style = "Head".into(); - story.paras[1].style = "Body".into(); - let back = import(tagged_text(&doc, &story).as_bytes()).unwrap(); - assert_eq!(back.story.text, story.text); - assert_eq!(back.story.paras, story.paras); - } - - /// Style names with ':', '<', '>' or '\' were written with those characters dropped, so - /// re-import gave the paragraphs a different style. - #[test] - fn style_names_with_tag_characters_round_trip() { - let doc = Document::new(&designcraft_doc::build::NewDocument::default()); - for name in ["Body: Indent", "Ac", "x\\y", "end\\"] { - let mut story = Story::new(StoryId(0)); - story.insert_with(0, "Text", CharFormat { style: name.into(), ..Default::default() }); - story.paras[0].style = name.into(); - let out = tagged_text(&doc, &story); - let back = import(out.as_bytes()).unwrap(); - assert_eq!(back.story.text, "Text", "{out}"); - assert_eq!(back.story.paras[0].style, name, "{out}"); - assert_eq!(back.story.char_format_at(1).style, name, "{out}"); - assert_eq!(back.para_styles.iter().map(|s| s.name.as_str()).collect::>(), [name]); - assert_eq!(back.char_styles.iter().map(|s| s.name.as_str()).collect::>(), [name]); - } - } - - #[test] - fn font_values_with_tag_characters_round_trip() { - let doc = Document::new(&designcraft_doc::build::NewDocument::default()); - for name in ["Weird>Font", "Ac", "x\\y", "end\\"] { - let mut story = Story::new(StoryId(0)); - let mut f = CharFormat::default(); - f.over.font_family = Some(name.into()); - f.over.font_style = Some(name.into()); - story.insert_with(0, "hello", f); - let out = tagged_text(&doc, &story); - let back = import(out.as_bytes()).unwrap(); - assert_eq!(back.story.text, "hello", "{out}"); - assert_eq!(back.story.char_format_at(1).over.font_family.as_deref(), Some(name), "{out}"); - assert_eq!(back.story.char_format_at(1).over.font_style.as_deref(), Some(name), "{out}"); - } - } - - /// Version 7 files hold font values raw, so their backslashes stay literal. - #[test] - fn old_format_font_values_stay_literal() { - let i = import("\r\n\r\nxy".as_bytes()).unwrap(); - assert_eq!(i.story.char_format_at(1).over.font_family.as_deref(), Some("a\\b")); - assert_eq!(i.story.char_format_at(2).over.font_style.as_deref(), Some("c\\d")); - } - - /// A backslash that isn't one of our escapes (older files, InDesign's `\:`) stays literal. - #[test] - fn unknown_backslashes_in_style_names_stay_literal() { - assert_eq!(unescape_value(r"x\y"), r"x\y"); - assert_eq!(unescape_value(r"a\:b"), r"a\:b"); - assert_eq!(unescape_value("end\\"), "end\\"); - assert_eq!(unescape_value(r"\\\"), r"\"); - } - - #[test] - fn tagged_text_round_trips() { - let src = "\r\n\r\nCaf<0x00E9> \\<1\\>\r\nA word and big text"; - assert!(is_tagged(src.as_bytes())); - let i = import(src.as_bytes()).unwrap(); - assert_eq!(i.story.text, "Café <1>\nA word and big text"); - assert_eq!(i.story.paras[0].style, "Head"); - assert_eq!(i.story.paras[1].style, "Body"); - assert_eq!(i.story.char_format_at(i.story.text.find("word").unwrap() + 1).style, "Em"); - assert_eq!(i.story.char_format_at(i.story.text.find("big").unwrap() + 1).over.size, Some(18.0)); - assert_eq!(i.para_styles.iter().map(|s| s.name.as_str()).collect::>(), ["Head", "Body"]); - // Export and back. - let doc = Document::new(&designcraft_doc::build::NewDocument::default()); - let out = tagged_text(&doc, &i.story); - assert!(out.starts_with("") && out.contains("Caf<0x00E9>") && out.contains("word"), "{out}"); - let back = import(out.as_bytes()).unwrap(); - assert_eq!(back.story.text, i.story.text); - assert_eq!(back.story.paras[1].style, "Body"); - // UTF-16 with a byte-order mark. - let mut u16 = vec![0xFF, 0xFE]; - for u in "\r\nHi".encode_utf16() { - u16.extend(u.to_le_bytes()); - } - assert_eq!(import(&u16).unwrap().story.text, "Hi"); - } - - #[test] - fn a_long_style_name_is_cut() { - let name = "N".repeat(100_000); - assert_eq!(unescape_value(&name).len(), crate::MAX_NAME_CHARS); - assert_eq!(unescape_value("Body"), "Body"); - } + let styles = |names: Vec| names.into_iter().map(|name| ImportedStyle { name, ..Default::default() }).collect(); + let warnings = (parsed.skipped_tags > 0).then(|| format!("{} formatting tag(s) not kept", parsed.skipped_tags)).into_iter().collect(); + Ok(Imported { story, para_styles: styles(parsed.para_styles), char_styles: styles(parsed.char_styles), warnings }) } From 32348da0738967d7326f85c77b11883a699bb596 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:19:41 -0500 Subject: [PATCH 011/153] Route PDF parsing and raster import through craft-libs --- Cargo.lock | 8 +- Cargo.toml | 4 +- crates/pdf/Cargo.toml | 5 +- crates/pdf/src/export.rs | 18 ++- crates/pdf/src/export/graphics.rs | 8 +- crates/pdf/src/export/images.rs | 70 ++++----- crates/pdf/src/optional_content.rs | 8 +- crates/pdf/src/pdfx.rs | 24 +-- crates/pdf/src/pdfx/fonts.rs | 8 +- crates/pdf/src/pdfx/fonts/tests.rs | 2 +- crates/pdf/src/pdfx/tests.rs | 14 +- crates/pdf/src/pdfx1a.rs | 18 +-- crates/pdf/src/print_tests.rs | 6 +- crates/pdf/src/registration_tests.rs | 14 +- crates/pdf/src/rtl_tests.rs | 16 +- crates/pdf/src/tests.rs | 25 ++-- crates/pdf/src/x1a_tests.rs | 10 +- crates/pdf/tests/it/gradient_color_spaces.rs | 8 +- crates/pdf/tests/it/spot_gradients.rs | 8 +- .../support/image_content_cache_contract.rs | 6 +- crates/textimport/src/docx.rs | 137 ++++++++---------- 21 files changed, 207 insertions(+), 210 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e046d2799..36d06c8b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1197,7 +1197,7 @@ dependencies = [ [[package]] name = "craft-image-codecs" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=85145d8dfd52966eacd045683a86a3737883aeea#85145d8dfd52966eacd045683a86a3737883aeea" +source = "git+https://github.com/storytold/craft-libs?rev=a47b78256c6acf3d7fdc4bc7cfbc25c373eac522#a47b78256c6acf3d7fdc4bc7cfbc25c373eac522" dependencies = [ "craft-jpeg", "exr", @@ -1219,7 +1219,7 @@ dependencies = [ [[package]] name = "craft-jpeg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=85145d8dfd52966eacd045683a86a3737883aeea#85145d8dfd52966eacd045683a86a3737883aeea" +source = "git+https://github.com/storytold/craft-libs?rev=a47b78256c6acf3d7fdc4bc7cfbc25c373eac522#a47b78256c6acf3d7fdc4bc7cfbc25c373eac522" dependencies = [ "thiserror 2.0.21", ] @@ -1315,7 +1315,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=85145d8dfd52966eacd045683a86a3737883aeea#85145d8dfd52966eacd045683a86a3737883aeea" +source = "git+https://github.com/storytold/craft-libs?rev=a47b78256c6acf3d7fdc4bc7cfbc25c373eac522#a47b78256c6acf3d7fdc4bc7cfbc25c373eac522" dependencies = [ "encoding_rs", "flate2", @@ -1833,6 +1833,7 @@ version = "0.20.1" dependencies = [ "chrono", "craft-cms", + "craft-image-codecs", "craft-pdf", "craft-prepress", "designcraft-color", @@ -1844,7 +1845,6 @@ dependencies = [ "flate2", "hayro-cmap", "hayro-interpret", - "hayro-syntax", "image", "krilla-svg", "kurbo", diff --git a/Cargo.toml b/Cargo.toml index 955a05a94..54833bb50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,8 +23,8 @@ craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435 craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } -craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "85145d8dfd52966eacd045683a86a3737883aeea", version = "=0.1.0" } -craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "85145d8dfd52966eacd045683a86a3737883aeea", version = "=0.1.0" } +craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } +craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index 3df877c5c..069c1162d 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -14,7 +14,8 @@ designcraft-color = { workspace = true } designcraft-doc = { workspace = true } designcraft-fonts = { workspace = true } designcraft-compose = { workspace = true } -craft-pdf = { workspace = true, features = ["write-pdf"] } +craft-pdf = { workspace = true, features = ["read", "write-pdf"] } +craft-image-codecs = { workspace = true } craft-prepress = { workspace = true } craft-cms = { workspace = true } krilla-svg = { workspace = true } @@ -23,8 +24,6 @@ image = { workspace = true } thiserror = { workspace = true } log = { workspace = true } chrono = { workspace = true } -# The placed-PDF version check names hayro's PdfVersion (krilla::pdf::Pdf::version returns it). -hayro-syntax = "0.7.2" [dev-dependencies] hayro-interpret = { version = "0.7.0", default-features = false, features = ["embed-fonts"] } diff --git a/crates/pdf/src/export.rs b/crates/pdf/src/export.rs index e4fa19e5c..3d7d53699 100644 --- a/crates/pdf/src/export.rs +++ b/crates/pdf/src/export.rs @@ -77,7 +77,7 @@ pub(crate) struct Exporter<'a> { #[cfg(test)] image_conversions: usize, /// Placed PDFs (embedded as vector pages) and their page counts. - pdfs: HashMap<(AssetId, Vec), Option<(krilla::pdf::PdfDocument, usize)>>, + pdfs: HashMap<(AssetId, Vec), Option<(krilla::pdf::PdfDocument, usize)>>, variants: HashMap<(AssetId, Option>), crate::optional_content::Variant>, has_optional_content: bool, #[cfg(test)] @@ -192,16 +192,20 @@ impl GreyImage { if !is_grey(data) { return None; } - let img = image::load_from_memory(data).ok()?.to_luma_alpha8(); - let size = img.dimensions(); + let img = craft_image_codecs::decode(data).ok()?; + if !img.layout().is_gray() { + return None; + } + let img = img.convert(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8); + let size = (img.width(), img.height()); let pixels = usize::try_from(size.0).ok()?.checked_mul(usize::try_from(size.1).ok()?)?; if pixels == 0 { return None; } let (mut grey, mut alpha) = (Vec::with_capacity(pixels), Vec::with_capacity(pixels)); - for p in img.pixels() { - grey.push(p.0[0]); - alpha.push(p.0[1]); + for p in img.data().chunks_exact(2) { + grey.push(p[0]); + alpha.push(p[1]); } let alpha = alpha.iter().any(|a| *a < 255).then(|| Arc::new(alpha)); let icc = embedded_icc(data).filter(|p| is_grey_icc(p)).map(Arc::new); @@ -296,7 +300,7 @@ mod unwind_tests { FAIL_NEXT_SURFACE.set(true); assert!(matches!(export(), Err(PdfError::Write(message)) if message.contains("synthetic export drawing failure")), "{entry}"); let fresh = export().unwrap(); - assert!(!hayro_syntax::Pdf::new(Arc::new(fresh)).unwrap().pages().is_empty(), "fresh {entry} export must still work"); + assert!(!craft_pdf::syntax::Pdf::new(Arc::new(fresh)).unwrap().pages().is_empty(), "fresh {entry} export must still work"); } println!("all three export entry points recovered"); } diff --git a/crates/pdf/src/export/graphics.rs b/crates/pdf/src/export/graphics.rs index 96780de32..aaa31150c 100644 --- a/crates/pdf/src/export/graphics.rs +++ b/crates/pdf/src/export/graphics.rs @@ -12,7 +12,7 @@ impl Exporter<'_> { /// file is left out and the export fails naming it ([`Exporter::check_placed_versions`]). /// Returns the document with its page count. fn placed_pdf(&mut self, asset: &designcraft_doc::Asset, data: Arc>) -> Option<(krilla::pdf::PdfDocument, usize)> { - use hayro_syntax::PdfVersion as V; + use craft_pdf::syntax::PdfVersion as V; let Ok(pdf) = krilla::pdf::Pdf::new(data.clone()) else { self.warn(format!("{}: can't read the placed PDF", asset.name)); return None; @@ -84,7 +84,7 @@ impl Exporter<'_> { self.images.insert(id, Some(converted.image.clone())); return Some(converted.image); } - let fmt = image::guess_format(&data).ok(); + let fmt = craft_image_codecs::detect(&data); #[cfg(test)] { self.image_conversions += 1; @@ -92,10 +92,10 @@ impl Exporter<'_> { let mut warning = None; let mode = RasterMode::for_standard(self.opts.standard); let img = match fmt { - Some(image::ImageFormat::Jpeg) => jpeg(data.clone().into(), || embedded_icc(&data), mode), + Some(craft_image_codecs::Format::Jpeg) => jpeg(data.clone().into(), || embedded_icc(&data), mode), // A CMYK TIFF keeps its ink values (`image` decodes it to RGB, and 100% K would print // as four-colour black). PDF/A exports are RGB only. - Some(image::ImageFormat::Tiff) if !self.rgb_only && designcraft_images::is_cmyk_tiff(&data) => match cmyk_tiff(&data, mode) { + Some(craft_image_codecs::Format::Tiff) if !self.rgb_only && designcraft_images::is_cmyk_tiff(&data) => match cmyk_tiff(&data, mode) { Some(img) => Some(img), None => { warning = Some(CMYK_FALLBACK_WARNING); diff --git a/crates/pdf/src/export/images.rs b/crates/pdf/src/export/images.rs index ec7c76fd6..f4d4a0a98 100644 --- a/crates/pdf/src/export/images.rs +++ b/crates/pdf/src/export/images.rs @@ -46,9 +46,7 @@ pub(super) struct RasterMode { /// The ICC profile embedded in an image file, if it has one. pub(super) fn embedded_icc(data: &[u8]) -> Option> { - use image::ImageDecoder; - let mut dec = image::ImageReader::new(std::io::Cursor::new(data)).with_guessed_format().ok()?.into_decoder().ok()?; - dec.icc_profile().ok().flatten() + craft_image_codecs::embedded_icc(data).ok().flatten() } /// Whether an ICC profile describes RGB (its colour space signature, header bytes 16 to 20). @@ -63,12 +61,12 @@ pub(super) fn is_grey_icc(icc: &[u8]) -> bool { /// Whether `data` decodes to grey (with or without alpha), read from its header. pub(super) fn is_grey(data: &[u8]) -> bool { - use image::ImageDecoder; - image::ImageReader::new(std::io::Cursor::new(data)) - .with_guessed_format() - .ok() - .and_then(|r| r.into_decoder().ok()) - .is_some_and(|dec| !dec.color_type().has_color()) + craft_image_codecs::inspect_raster(data).ok().flatten().is_some_and(|info| { + matches!( + info.color_type, + craft_image_codecs::RasterColorType::Bitmap | craft_image_codecs::RasterColorType::Gray | craft_image_codecs::RasterColorType::GrayAlpha + ) + }) } /// A JPEG, passed through. With `tag_rgb`, a JPEG keeps its own profile, and one without gets @@ -88,25 +86,28 @@ pub(super) fn jpeg(data: krilla::Data, icc: impl FnOnce() -> Option>, mo /// Whether krilla would write this PNG, GIF or WebP as untagged RGB: not grey, and no profile of /// its own. fn untagged_rgb(data: &[u8]) -> bool { - use image::ImageDecoder; - let Some(mut dec) = image::ImageReader::new(std::io::Cursor::new(data)).with_guessed_format().ok().and_then(|r| r.into_decoder().ok()) else { - return false; - }; - let grey = matches!(dec.color_type(), image::ColorType::L8 | image::ColorType::La8 | image::ColorType::L16 | image::ColorType::La16); - !grey && dec.icc_profile().ok().flatten().is_none() + let Some(info) = craft_image_codecs::inspect_raster(data).ok().flatten() else { return false }; + !matches!( + info.color_type, + craft_image_codecs::RasterColorType::Bitmap | craft_image_codecs::RasterColorType::Gray | craft_image_codecs::RasterColorType::GrayAlpha + ) && embedded_icc(data).is_none() } -pub(super) fn lossless(data: &Arc>, fmt: Option, mode: RasterMode) -> Option { +pub(super) fn lossless(data: &Arc>, fmt: Option, mode: RasterMode) -> Option { // krilla reads PNG, GIF and WebP lazily: a damaged file is accepted here and fails the whole // export when the PDF is written. Decoding it first means it is skipped, like any other // image that can't be decoded. let rgba = designcraft_images::decode_rgba(data)?; let i = mode.interpolate; let direct = match fmt { - Some(image::ImageFormat::Png | image::ImageFormat::Gif | image::ImageFormat::WebP) if mode.tag_rgb && untagged_rgb(data) => None, - Some(image::ImageFormat::Png) => Image::from_png(data.clone().into(), i).ok(), - Some(image::ImageFormat::Gif) => Image::from_gif(data.clone().into(), i).ok(), - Some(image::ImageFormat::WebP) => Image::from_webp(data.clone().into(), i).ok(), + Some(craft_image_codecs::Format::Png | craft_image_codecs::Format::Gif | craft_image_codecs::Format::WebP) + if mode.tag_rgb && untagged_rgb(data) => + { + None + } + Some(craft_image_codecs::Format::Png) => Image::from_png(data.clone().into(), i).ok(), + Some(craft_image_codecs::Format::Gif) => Image::from_gif(data.clone().into(), i).ok(), + Some(craft_image_codecs::Format::WebP) => Image::from_webp(data.clone().into(), i).ok(), _ => None, }; direct.or_else(|| { @@ -177,21 +178,20 @@ pub(super) fn recompress(data: &[u8], mode: RasterMode) -> Option { { RECOMPRESS_DECODES.with(|n| n.set(n.get() + 1)); } - let img = image::load_from_memory(data).ok()?; - if img.color().has_alpha() && img.to_rgba8().pixels().any(|p| p[3] < 255) { + let img = craft_image_codecs::decode(data).ok()?; + let rgba = img.to_rgba8(); + if img.layout().has_alpha() && rgba.chunks_exact(4).any(|p| p[3] < 255) { return None; } - let mut buf = Vec::new(); - let enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut buf, 90); // A grey image stays a grey JPEG (#1426). - let grey = !img.color().has_color(); - if grey { - let l = img.to_luma8(); - image::ImageEncoder::write_image(enc, l.as_raw(), l.width(), l.height(), image::ExtendedColorType::L8).ok()?; + let grey = img.layout().is_gray(); + let jpeg_image = if grey { + img.convert(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) } else { - let rgb = img.to_rgb8(); - image::ImageEncoder::write_image(enc, rgb.as_raw(), rgb.width(), rgb.height(), image::ExtendedColorType::Rgb8).ok()?; - } + img.convert(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) + }; + let options = craft_image_codecs::EncodeOptions { jpeg_quality: 90, ..Default::default() }; + let buf = craft_image_codecs::encode(&jpeg_image, craft_image_codecs::Format::Jpeg, &options).ok()?; jpeg(buf.into(), || embedded_icc(data).filter(|p| if grey { is_grey_icc(p) } else { is_rgb_icc(p) }), mode) } @@ -421,7 +421,7 @@ mod image_content_cache_tests { // Serialize the converted raster without an archival validator so the RGB-only pixel // contract stays independent of main's existing PDF/A interpolation validation failure. fn image_color_samples(image: Image) -> (String, Vec) { - use hayro_syntax::object::Name; + use craft_pdf::syntax::object::Name; let mut pdf = krilla::Document::new(); let size = Size::from_wh(20.0, 20.0).unwrap(); let mut page = pdf.start_page_with(PageSettings::new(size)); @@ -430,7 +430,7 @@ mod image_content_cache_tests { surface.finish(); page.finish(); let bytes = pdf.finish().unwrap(); - let pdf = hayro_syntax::Pdf::new(bytes).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(bytes).unwrap(); let stream = pdf .objects() .into_iter() @@ -466,7 +466,7 @@ mod image_content_cache_tests { #[cfg(test)] mod eps_tests { use super::*; - use hayro_syntax::object::Name; + use craft_pdf::syntax::object::Name; #[test] fn eps_tiff_preview_keeps_cmyk_inks_in_pdf() { @@ -522,7 +522,7 @@ mod eps_tests { for standard in [Standard::None, Standard::PdfX4] { let report = export_pdf_with_report(&doc, &Cache::new(), &PdfOptions { standard, ..Default::default() }).unwrap(); assert!(report.warnings.is_empty(), "{:?}", report.warnings); - let pdf = hayro_syntax::Pdf::new(report.bytes).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(report.bytes).unwrap(); let images: Vec<_> = pdf .objects() .into_iter() diff --git a/crates/pdf/src/optional_content.rs b/crates/pdf/src/optional_content.rs index 08dbbe0bd..a21d89103 100644 --- a/crates/pdf/src/optional_content.rs +++ b/crates/pdf/src/optional_content.rs @@ -1,6 +1,6 @@ -use hayro_syntax::Pdf; -use hayro_syntax::object::{Array, Dict, MaybeRef, Name, Object, ObjectIdentifier, Stream}; -use hayro_syntax::reader::{Reader, ReaderExt}; +use craft_pdf::syntax::Pdf; +use craft_pdf::syntax::object::{Array, Dict, MaybeRef, Name, Object, ObjectIdentifier, Stream}; +use craft_pdf::syntax::reader::{Reader, ReaderExt}; use std::collections::HashSet; use std::sync::Arc; @@ -238,7 +238,7 @@ pub(crate) fn prepare(bytes: &Arc>, choice: Option<&[String]>) -> Result if group.get::>(b"Type").as_ref().map(AsRef::as_ref) != Some(b"OCG") { return Err(invalid()); } - let name = group.get::>(b"Name"); + let name = group.get::>(b"Name"); let is_hidden = match (choice, name.as_ref()) { (None, _) => !fallback && (off.contains(&id) || (base_off && !on.contains(&id))), (Some([]), None) => false, diff --git a/crates/pdf/src/pdfx.rs b/crates/pdf/src/pdfx.rs index 731a5989d..bae800bd5 100644 --- a/crates/pdf/src/pdfx.rs +++ b/crates/pdf/src/pdfx.rs @@ -5,10 +5,10 @@ use std::collections::{HashMap, HashSet}; -use hayro_syntax::content::UntypedIter; -use hayro_syntax::object::{Array, Dict, Name, Object, ObjectIdentifier, Stream}; -use hayro_syntax::page::Resources; -use hayro_syntax::reader::ReaderExt; +use craft_pdf::syntax::content::UntypedIter; +use craft_pdf::syntax::object::{Array, Dict, Name, Object, ObjectIdentifier, Stream}; +use craft_pdf::syntax::page::Resources; +use craft_pdf::syntax::reader::ReaderExt; use craft_pdf::write::WriteError; use craft_pdf::write::backend as krilla; @@ -116,7 +116,7 @@ pub fn has_rgb_groups(pdf: &[u8]) -> bool { pub fn check_pdfx4(pdf: &[u8]) -> Vec { let mut issues = Vec::new(); let text = String::from_utf8_lossy(pdf); - let file = hayro_syntax::Pdf::new(pdf.to_vec()).ok(); + let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).ok(); if !pdf.starts_with(b"%PDF-1.") || pdf.get(7).is_none_or(|v| *v > b'6') { issues.push("the PDF version must be 1.6 or lower".into()); } @@ -161,12 +161,12 @@ pub fn check_pdfx4(pdf: &[u8]) -> Vec { /// The document information's `GTS_PDFXVersion` (`PDF/X-4`, `PDF/X-1:2003`, …), read as an /// object; `None` when the file can't be read or doesn't name one. pub fn pdfx_version(pdf: &[u8]) -> Option { - info_text(pdf, &hayro_syntax::Pdf::new(pdf.to_vec()).ok()?, b"GTS_PDFXVersion") + info_text(pdf, &craft_pdf::syntax::Pdf::new(pdf.to_vec()).ok()?, b"GTS_PDFXVersion") } /// The catalog's `GTS_PDFX` output intents, read as objects (whitespace and object streams /// don't matter). -pub(crate) fn pdfx_intents<'a>(file: &'a hayro_syntax::Pdf) -> Vec> { +pub(crate) fn pdfx_intents<'a>(file: &'a craft_pdf::syntax::Pdf) -> Vec> { let xref = file.xref(); let Some(catalog) = xref.get::>(xref.root_id()) else { return Vec::new() }; let Some(intents) = catalog.get::>(b"OutputIntents") else { return Vec::new() }; @@ -176,13 +176,13 @@ pub(crate) fn pdfx_intents<'a>(file: &'a hayro_syntax::Pdf) -> Vec> { /// The text entry `key` of the document information dictionary, trimmed: the `/Info` of the /// newest trailer that has one (classic or cross-reference stream, following `/Prev` a bounded /// number of times), else of the last `trailer` keyword in the file. -pub(crate) fn info_text(pdf: &[u8], file: &hayro_syntax::Pdf, key: &[u8]) -> Option { +pub(crate) fn info_text(pdf: &[u8], file: &craft_pdf::syntax::Pdf, key: &[u8]) -> Option { let lookup = |trailer: &Dict<'_>| -> Option { let info = match trailer.get_ref(b"Info") { Some(r) => file.xref().get::>(r.into())?, None => trailer.get::>(b"Info")?, }; - let value = info.get::>(key)?; + let value = info.get::>(key)?; Some(String::from_utf8_lossy(value.as_bytes()).trim().to_string()) }; let tail = pdf.get(rfind(pdf, b"startxref")?..).unwrap_or_default(); @@ -198,7 +198,7 @@ pub(crate) fn info_text(pdf: &[u8], file: &hayro_syntax::Pdf, key: &[u8]) -> Opt } // A damaged cross-reference offset: the dictionary after the last `trailer` keyword. let at = rfind(pdf, b"trailer")? + b"trailer".len(); - let mut reader = hayro_syntax::reader::Reader::new(pdf.get(at..)?); + let mut reader = craft_pdf::syntax::reader::Reader::new(pdf.get(at..)?); reader.skip_white_spaces_and_comments(); lookup(&reader.read_without_context::>()?) } @@ -209,7 +209,7 @@ pub(crate) fn info_text(pdf: &[u8], file: &hayro_syntax::Pdf, key: &[u8]) -> Opt /// This walks every page's content, the images, forms, patterns, shadings and soft masks it uses, /// and the transparency groups, and reports each kind of device RGB it finds with the pages it is /// on. Nothing is reported when the file has no CMYK PDF/X output intent. -fn device_rgb(file: Option<&hayro_syntax::Pdf>) -> Vec { +fn device_rgb(file: Option<&craft_pdf::syntax::Pdf>) -> Vec { let Some(file) = file else { return vec!["the file couldn't be read to check its colour spaces".into()]; }; @@ -249,7 +249,7 @@ fn device_rgb(file: Option<&hayro_syntax::Pdf>) -> Vec { } /// Whether the file's PDF/X output intent has a four-channel (CMYK) destination profile. -pub(crate) fn cmyk_output_intent(file: &hayro_syntax::Pdf) -> bool { +pub(crate) fn cmyk_output_intent(file: &craft_pdf::syntax::Pdf) -> bool { pdfx_intents(file).iter().any(|oi| oi.get::>(b"DestOutputProfile").and_then(|p| p.dict().get::(b"N")) == Some(4)) } diff --git a/crates/pdf/src/pdfx/fonts.rs b/crates/pdf/src/pdfx/fonts.rs index 2107f84eb..401d551b7 100644 --- a/crates/pdf/src/pdfx/fonts.rs +++ b/crates/pdf/src/pdfx/fonts.rs @@ -1,8 +1,8 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; -use hayro_syntax::content::UntypedIter; -use hayro_syntax::object::{Array, Dict, Name, Object, ObjectIdentifier, Stream}; -use hayro_syntax::page::Resources; +use craft_pdf::syntax::content::UntypedIter; +use craft_pdf::syntax::object::{Array, Dict, Name, Object, ObjectIdentifier, Stream}; +use craft_pdf::syntax::page::Resources; use super::MAX_DEPTH; @@ -14,7 +14,7 @@ const LIMIT_WARNING: &str = "font scan operation limit reached: PDF/X-4 font emb const DEPTH_WARNING: &str = "font scan nesting depth limit reached: PDF/X-4 font embedding checks are incomplete"; pub(super) fn unembedded_fonts(pdf: &[u8]) -> Vec { - let Ok(file) = hayro_syntax::Pdf::new(pdf.to_vec()) else { return Vec::new() }; + let Ok(file) = craft_pdf::syntax::Pdf::new(pdf.to_vec()) else { return Vec::new() }; let mut found: BTreeMap> = BTreeMap::new(); let mut scan = FontScan::default(); for (i, page) in file.pages().iter().enumerate() { diff --git a/crates/pdf/src/pdfx/fonts/tests.rs b/crates/pdf/src/pdfx/fonts/tests.rs index c148cb076..f555f2651 100644 --- a/crates/pdf/src/pdfx/fonts/tests.rs +++ b/crates/pdf/src/pdfx/fonts/tests.rs @@ -47,7 +47,7 @@ fn pdf_objects(objects: &[String]) -> Vec { #[test] fn nested_form_fanout_is_memoized_and_reports_bottom_font() { let bytes = fanout_pdf(16); - let pdf = hayro_syntax::Pdf::new(bytes.clone()).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(bytes.clone()).unwrap(); let page = pdf.pages().first().unwrap(); let mut scan = FontScan::default(); let start = Instant::now(); diff --git a/crates/pdf/src/pdfx/tests.rs b/crates/pdf/src/pdfx/tests.rs index b35e897c2..6fc7fb90d 100644 --- a/crates/pdf/src/pdfx/tests.rs +++ b/crates/pdf/src/pdfx/tests.rs @@ -1,12 +1,12 @@ use craft_pdf::write::backend as krilla; use std::sync::Arc; +use craft_pdf::syntax::object::{Name, Object}; use designcraft_color::{Color, Swatch}; use designcraft_compose::Cache; use designcraft_doc::build::NewDocument; use designcraft_doc::{Asset, AssetId, Content, Document, Fill, Fitting, Graphic, Item, ItemId, Shape, SpreadRef}; use designcraft_geom::{Affine, Rect}; -use hayro_syntax::object::{Name, Object}; use image::{ExtendedColorType, ImageEncoder}; use crate::{ExportReport, PdfOptions, Standard, check_pdfx4, export_pdf_with_report}; @@ -117,7 +117,7 @@ pub(super) fn x4(d: &Document) -> ExportReport { /// The `/ColorSpace` of every image XObject: a name, or `[` for an array (ICCBased, Indexed…). fn image_spaces(bytes: &[u8]) -> Vec { - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).unwrap(); let mut out: Vec = pdf .objects() .into_iter() @@ -367,12 +367,12 @@ fn rgb_rasters() -> Vec<(&'static str, Vec)> { /// The decoded `/ICCBased` profile of every image XObject that has one. fn image_profiles(bytes: &[u8]) -> Vec> { - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).unwrap(); pdf.objects() .into_iter() .filter_map(|o| o.into_stream()) .filter(|s| s.dict().get::(b"Subtype").is_some_and(|n| n.as_str() == "Image")) - .filter_map(|s| match s.dict().get::(b"ColorSpace")?.iter::().nth(1)? { + .filter_map(|s| match s.dict().get::(b"ColorSpace")?.iter::().nth(1)? { Object::Stream(p) => Some(p.decoded().ok()?.into_owned()), _ => None, }) @@ -453,10 +453,10 @@ fn cmyk_tiff() -> Vec { #[test] fn xmp_states_trapped() { let r = x4(&Document::new(&NewDocument::default())); - let pdf = hayro_syntax::Pdf::new(r.bytes.clone()).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(r.bytes.clone()).unwrap(); let xref = pdf.xref(); - let catalog = xref.get::(xref.root_id()).unwrap(); - let xmp = catalog.get::(b"Metadata").unwrap().decoded().unwrap().into_owned(); + let catalog = xref.get::(xref.root_id()).unwrap(); + let xmp = catalog.get::(b"Metadata").unwrap().decoded().unwrap().into_owned(); let xmp = String::from_utf8_lossy(&xmp); assert!(xmp.contains("xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\"") && xmp.contains("False"), "{xmp}"); assert!(String::from_utf8_lossy(&r.bytes).contains("/Trapped/False")); diff --git a/crates/pdf/src/pdfx1a.rs b/crates/pdf/src/pdfx1a.rs index aa8873083..67e0a583c 100644 --- a/crates/pdf/src/pdfx1a.rs +++ b/crates/pdf/src/pdfx1a.rs @@ -5,12 +5,12 @@ use std::collections::{HashMap, HashSet}; +use craft_pdf::syntax::content::UntypedIter; +use craft_pdf::syntax::object::{Array, Dict, Name, Object, ObjectIdentifier, Stream}; +use craft_pdf::syntax::page::Resources; use craft_pdf::write::backend::configure::PdfVersion; use craft_prepress::flatten::pdf::ReadOptions; use craft_prepress::flatten::{FlattenDocumentOptions, FlattenOptions, FlattenTotals, flatten_document}; -use hayro_syntax::content::UntypedIter; -use hayro_syntax::object::{Array, Dict, Name, Object, ObjectIdentifier, Stream}; -use hayro_syntax::page::Resources; use crate::PdfError; use crate::pdfx::MAX_DEPTH; @@ -116,7 +116,7 @@ pub fn check_pdfx1a(pdf: &[u8]) -> Vec { fn checks(pdf: &[u8]) -> (Vec, Vec) { let mut issues = Vec::new(); let text = String::from_utf8_lossy(pdf); - let file = hayro_syntax::Pdf::new(pdf.to_vec()).ok(); + let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).ok(); if !pdf.starts_with(b"%PDF-1.") || pdf.get(7).is_none_or(|v| *v > b'4') { issues.push("the PDF version must be 1.4 or lower".into()); } @@ -151,7 +151,7 @@ fn checks(pdf: &[u8]) -> (Vec, Vec) { /// Transparency and color spaces, then the output intent and fonts, walked through every page's /// content and what it uses. -fn content(file: Option<&hayro_syntax::Pdf>) -> (Vec, Vec) { +fn content(file: Option<&craft_pdf::syntax::Pdf>) -> (Vec, Vec) { let Some(file) = file else { return (vec!["the file couldn't be read to check its transparency and color spaces".into()], Vec::new()); }; @@ -215,13 +215,13 @@ impl Intent { } /// The catalog's `GTS_PDFX` output intents. -fn pdfx_intents(file: &hayro_syntax::Pdf) -> Vec { +fn pdfx_intents(file: &craft_pdf::syntax::Pdf) -> Vec { let number = |n: i32| usize::try_from(n).ok(); crate::pdfx::pdfx_intents(file) .into_iter() .map(|oi| { let text = |key: &[u8]| { - oi.get::>(key) + oi.get::>(key) .map(|s| String::from_utf8_lossy(s.as_bytes()).trim().to_string()) .filter(|s| !s.is_empty()) }; @@ -243,7 +243,7 @@ fn pdfx_intents(file: &hayro_syntax::Pdf) -> Vec { /// What PDF/X-1a finds wrong with the file's output intent: a CMYK profile, ICC v2 (PDF 1.4 /// allows no later version), or none for a registered printing condition. -fn intent_issues(file: &hayro_syntax::Pdf) -> Vec { +fn intent_issues(file: &craft_pdf::syntax::Pdf) -> Vec { let Some(oi) = pdfx_intents(file).into_iter().next() else { return Vec::new() }; let mut issues = Vec::new(); if oi.has_profile { @@ -267,7 +267,7 @@ fn intent_issues(file: &hayro_syntax::Pdf) -> Vec { pub(crate) fn finish(pdf: &[u8]) -> Result, String> { let unreadable = || "the flattened file couldn't be read to add the PDF/X-1a identification".to_string(); let layout = Layout::read(pdf).ok_or_else(unreadable)?; - let file = hayro_syntax::Pdf::new(pdf.to_vec()).map_err(|_| unreadable())?; + let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).map_err(|_| unreadable())?; let mut bodies: HashMap> = HashMap::new(); let info = int_after(&layout.trailer, "/Info").ok_or_else(unreadable)?; let body = layout.body(pdf, info).ok_or_else(unreadable)?; diff --git a/crates/pdf/src/print_tests.rs b/crates/pdf/src/print_tests.rs index 6c5dafcb2..ccc646585 100644 --- a/crates/pdf/src/print_tests.rs +++ b/crates/pdf/src/print_tests.rs @@ -1,13 +1,13 @@ //! CMYK print output, read back from the exported PDF: blending spaces, the output intent, //! overprint and knockout groups. +use craft_pdf::syntax::Pdf; +use craft_pdf::syntax::object::{Array, Dict, Name, Object, Stream}; use designcraft_color::cms::{self, Cms, ColorSettings}; use designcraft_compose::Cache; use designcraft_doc::build::NewDocument; use designcraft_doc::{BlendSpace, Document, Fill, Item, ItemId, SpreadRef}; use designcraft_geom::Rect; -use hayro_syntax::Pdf; -use hayro_syntax::object::{Array, Dict, Name, Object, Stream}; use crate::{PdfOptions, export_pdf}; @@ -38,7 +38,7 @@ pub(crate) struct Intent { } fn text(d: &Dict<'_>, key: &[u8]) -> Option { - d.get::(key).map(|s| String::from_utf8_lossy(s.as_bytes()).into_owned()) + d.get::(key).map(|s| String::from_utf8_lossy(s.as_bytes()).into_owned()) } /// The catalog's `GTS_PDFX` output intent. diff --git a/crates/pdf/src/registration_tests.rs b/crates/pdf/src/registration_tests.rs index 4d67aac01..151ce772f 100644 --- a/crates/pdf/src/registration_tests.rs +++ b/crates/pdf/src/registration_tests.rs @@ -76,8 +76,8 @@ fn registrations(action: impl FnOnce()) -> usize { REGISTRATIONS.get() } -fn check_vector_artwork(pdf: &hayro_syntax::Pdf) { - use hayro_syntax::object::{Name, Object}; +fn check_vector_artwork(pdf: &craft_pdf::syntax::Pdf) { + use craft_pdf::syntax::object::{Name, Object}; let mut found = false; for object in pdf.objects() { @@ -94,9 +94,9 @@ fn check_vector_artwork(pdf: &hayro_syntax::Pdf) { } fn check_unlayered_pdf(bytes: &[u8], pages: usize, artwork: bool) { - use hayro_syntax::object::Dict; + use craft_pdf::syntax::object::Dict; - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).unwrap(); assert_eq!(pdf.pages().len(), pages); let catalog = pdf.xref().get::>(pdf.xref().root_id()).unwrap(); assert!(!catalog.contains_key(b"OCProperties"), "unlayered output must not acquire optional content"); @@ -194,9 +194,9 @@ fn layered_and_resource_only_inputs_register_optional_content() { } fn check_registered(bytes: &[u8], hidden: bool) { - use hayro_syntax::object::{Array, Dict, Name, Object, ObjectIdentifier}; + use craft_pdf::syntax::object::{Array, Dict, Name, Object, ObjectIdentifier}; - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).unwrap(); check_vector_artwork(&pdf); let xref = pdf.xref(); let catalog = xref.get::>(xref.root_id()).unwrap(); @@ -206,7 +206,7 @@ fn check_registered(bytes: &[u8], hidden: bool) { let group_id: ObjectIdentifier = groups.raw_iter().next().unwrap().as_obj_ref().unwrap().into(); let group = xref.get::>(group_id).unwrap(); assert_eq!(group.get::>(b"Type").unwrap().as_ref(), b"OCG"); - assert_eq!(group.get::>(b"Name").unwrap().as_bytes(), b"Hidden"); + assert_eq!(group.get::>(b"Name").unwrap().as_bytes(), b"Hidden"); let config = properties.get::>(b"D").unwrap(); if hidden { diff --git a/crates/pdf/src/rtl_tests.rs b/crates/pdf/src/rtl_tests.rs index 82974029e..593e1ca79 100644 --- a/crates/pdf/src/rtl_tests.rs +++ b/crates/pdf/src/rtl_tests.rs @@ -47,9 +47,9 @@ fn rtl() -> ParaAttrs { /// The ActualText of every marked-content span on the first page. fn actual_texts(bytes: &[u8]) -> Vec { - use hayro_syntax::content::ops::TypedInstruction; - use hayro_syntax::object::{Dict, Object}; - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).expect("parse"); + use craft_pdf::syntax::content::ops::TypedInstruction; + use craft_pdf::syntax::object::{Dict, Object}; + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).expect("parse"); let page = &pdf.pages()[0]; let mut out = Vec::new(); let mut ops = page.typed_operations(); @@ -57,7 +57,7 @@ fn actual_texts(bytes: &[u8]) -> Vec { let TypedInstruction::BeginMarkedContentWithProperties(op) = op else { continue }; let Object::Dict(props) = op.1 else { continue }; let props: &Dict<'_> = props; - let Some(s) = props.get::(b"ActualText") else { continue }; + let Some(s) = props.get::(b"ActualText") else { continue }; let units: Vec = s.as_bytes().as_chunks::<2>().0.iter().map(|&b| u16::from_be_bytes(b)).collect(); let text = String::from_utf16(&units).expect("UTF-16"); out.push(text.trim_start_matches('\u{FEFF}').to_string()); @@ -112,7 +112,7 @@ fn glyph_origins(bytes: &[u8]) -> Vec { fn pop_clip_path(&mut self) {} fn pop_transparency_group(&mut self) {} } - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).expect("parse"); + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).expect("parse"); let cache = InterpreterCache::new(); let mut ctx = Context::new(kurbo::Affine::IDENTITY, kurbo::Rect::new(0.0, 0.0, 1.0, 1.0), &cache, pdf.xref(), InterpreterSettings::default()); let mut rec = Rec(Vec::new()); @@ -183,9 +183,9 @@ fn arabic_ligatures_extract_their_letters() { /// How many marked-content sections the first page opens while an ActualText span is open. /// Viewers such as poppler drop the text before an ActualText span nested in another. fn opened_inside_actual_text(bytes: &[u8]) -> usize { - use hayro_syntax::content::ops::TypedInstruction; - use hayro_syntax::object::Object; - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).expect("parse"); + use craft_pdf::syntax::content::ops::TypedInstruction; + use craft_pdf::syntax::object::Object; + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).expect("parse"); let page = &pdf.pages()[0]; let (mut open, mut nested) = (Vec::new(), 0); let mut ops = page.typed_operations(); diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index ac32be45b..452b5d1b5 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -29,12 +29,12 @@ fn uncompressed(d: &Document, opts: PdfOptions) -> String { /// Text of every page through hayro's interpreter (glyph → Unicode via ToUnicode / ActualText). pub(crate) fn extract_text(bytes: &[u8]) -> Vec { + use craft_pdf::syntax::Pdf; use hayro_interpret::font::Glyph; use hayro_interpret::{ BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, InterpreterSettings, Paint, PathDrawMode, SoftMask, interpret_page, }; - use hayro_syntax::Pdf; struct Ex(String); impl Device<'_> for Ex { fn set_soft_mask(&mut self, _: Option>) {} @@ -72,7 +72,7 @@ fn exports_valid_pdf_with_one_page_per_page() { let d = doc_with_text("Hello DesignCraft"); let bytes = export_pdf(&d, &Cache::new(), &PdfOptions::default()).unwrap(); assert!(bytes.starts_with(b"%PDF-1.7")); - let pdf = hayro_syntax::Pdf::new(bytes.clone()).expect("parse"); + let pdf = craft_pdf::syntax::Pdf::new(bytes.clone()).expect("parse"); assert_eq!(pdf.pages().len(), 2); } @@ -173,7 +173,7 @@ fn spreads_and_ranges() { let n = d.page_count(); assert_eq!(n, 4); let bytes = export_pdf(&d, &Cache::new(), &PdfOptions { spreads: true, ..Default::default() }).unwrap(); - let pages = hayro_syntax::Pdf::new(bytes).unwrap().pages().len(); + let pages = craft_pdf::syntax::Pdf::new(bytes).unwrap().pages().len(); assert_eq!(pages, d.spreads.len()); assert_eq!(parse_page_range("1-2, 4", 4).unwrap(), vec![0, 1, 3]); assert_eq!(parse_page_range("3-", 4).unwrap(), vec![2, 3]); @@ -187,7 +187,7 @@ fn spreads_and_ranges() { assert!(parse_page_range("5", 4).is_err()); assert!(parse_page_range("x", 4).is_err()); let bytes = export_pdf(&d, &Cache::new(), &PdfOptions { pages: Some(vec![1, 2]), ..Default::default() }).unwrap(); - assert_eq!(hayro_syntax::Pdf::new(bytes).unwrap().pages().len(), 2); + assert_eq!(craft_pdf::syntax::Pdf::new(bytes).unwrap().pages().len(), 2); assert_eq!(export_pdf(&d, &Cache::new(), &PdfOptions { pages: Some(vec![9]), ..Default::default() }), Err(PdfError::BadPage(10))); } @@ -221,7 +221,10 @@ fn standards() { assert_eq!((intent.identifier.as_str(), intent.condition.as_str()), ("FOGRA39", "Coated FOGRA39 (fitted, craft-cms)")); // A plain export fails the checks; both read with the same pages. let plain = export_pdf(&d, &Cache::new(), &PdfOptions::default()).unwrap(); - assert_eq!(hayro_syntax::Pdf::new(r.bytes.clone()).unwrap().pages().len(), hayro_syntax::Pdf::new(plain.clone()).unwrap().pages().len()); + assert_eq!( + craft_pdf::syntax::Pdf::new(r.bytes.clone()).unwrap().pages().len(), + craft_pdf::syntax::Pdf::new(plain.clone()).unwrap().pages().len() + ); assert!(crate::check_pdfx4(&plain).len() >= 3); assert_eq!(Standard::parse("PDF/X-4"), Some(Standard::PdfX4)); assert_eq!(Standard::parse("pdfa-2b"), Some(Standard::PdfA2b)); @@ -397,8 +400,8 @@ fn an_image_past_the_decode_budget_is_skipped_with_a_warning() { /// Every image XObject in a PDF: its /ColorSpace name (`?` when not a name) and decoded samples. fn image_xobjects(bytes: &[u8]) -> Vec<(String, Vec)> { - use hayro_syntax::object::Name; - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).expect("parse"); + use craft_pdf::syntax::object::Name; + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).expect("parse"); pdf.objects() .into_iter() .filter_map(|o| o.into_stream()) @@ -462,8 +465,8 @@ fn raster(fmt: image::ImageFormat, alpha: bool) -> Vec { /// Each image XObject's /Interpolate value (`None` when absent). fn interpolate_flags(bytes: &[u8]) -> Vec> { - use hayro_syntax::object::Name; - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).expect("parse"); + use craft_pdf::syntax::object::Name; + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).expect("parse"); pdf.objects() .into_iter() .filter_map(|o| o.into_stream()) @@ -529,7 +532,7 @@ fn drawn(bytes: &[u8]) -> (Vec<(String, kurbo::Rect)>, Vec) { fn pop_clip_path(&mut self) {} fn pop_transparency_group(&mut self) {} } - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).expect("parse"); + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).expect("parse"); let cache = InterpreterCache::new(); let page = &pdf.pages()[0]; let mut ctx = Context::new(kurbo::Affine::IDENTITY, kurbo::Rect::new(0.0, 0.0, 1.0, 1.0), &cache, pdf.xref(), InterpreterSettings::default()); @@ -584,7 +587,7 @@ fn paint_events(bytes: &[u8], groups: bool) -> Vec<(String, f32)> { } } } - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).expect("parse"); + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).expect("parse"); let cache = InterpreterCache::new(); let page = &pdf.pages()[0]; let mut ctx = Context::new(kurbo::Affine::IDENTITY, kurbo::Rect::new(0.0, 0.0, 1.0, 1.0), &cache, pdf.xref(), InterpreterSettings::default()); diff --git a/crates/pdf/src/x1a_tests.rs b/crates/pdf/src/x1a_tests.rs index c79aaa172..7a7cb3277 100644 --- a/crates/pdf/src/x1a_tests.rs +++ b/crates/pdf/src/x1a_tests.rs @@ -5,13 +5,13 @@ use std::collections::HashMap; use std::sync::Arc; +use craft_pdf::syntax::Pdf; +use craft_pdf::syntax::object::{Dict, Name, Object}; use designcraft_color::{Color, ColorType, Swatch, SwatchValue}; use designcraft_compose::Cache; use designcraft_doc::build::NewDocument; use designcraft_doc::{Content, Document, Fill, Item, ItemId, ParaFormat, SpreadRef}; use designcraft_geom::Rect; -use hayro_syntax::Pdf; -use hayro_syntax::object::{Dict, Name, Object}; use crate::tests::extract_text; use crate::{PdfOptions, ShadowMask, Standard, check_pdfx1a, export_pdf_with_report}; @@ -114,8 +114,8 @@ fn x1a_export_flattens_transparency_into_inks() { // The default Coated FOGRA39 is a registered condition: no (ICC v4) profile in the file. let pdf = Pdf::new(bytes.to_vec()).unwrap(); let oi = pdfx_intent(&pdf); - assert_eq!(oi.get::>(b"OutputConditionIdentifier").unwrap().as_bytes(), b"FOGRA39"); - assert_eq!(oi.get::>(b"RegistryName").unwrap().as_bytes(), b"http://www.color.org"); + assert_eq!(oi.get::>(b"OutputConditionIdentifier").unwrap().as_bytes(), b"FOGRA39"); + assert_eq!(oi.get::>(b"RegistryName").unwrap().as_bytes(), b"http://www.color.org"); assert!(!oi.contains_key(b"DestOutputProfile"), "no destination profile"); let profiles = pdf.objects().into_iter().filter(|o| matches!(o, Object::Stream(s) if s.dict().get::(b"N") == Some(4))).count(); assert_eq!(profiles, 0, "the profile is left out of the file"); @@ -144,7 +144,7 @@ fn x1a_export_flattens_transparency_into_inks() { fn pdfx_intent<'a>(pdf: &'a Pdf) -> Dict<'a> { let xref = pdf.xref(); let catalog = xref.get::>(xref.root_id()).unwrap(); - let intents = catalog.get::>(b"OutputIntents").unwrap(); + let intents = catalog.get::>(b"OutputIntents").unwrap(); intents.iter::>().find(|oi| oi.get::>(b"S").is_some_and(|s| s.as_ref() == b"GTS_PDFX")).unwrap() } diff --git a/crates/pdf/tests/it/gradient_color_spaces.rs b/crates/pdf/tests/it/gradient_color_spaces.rs index 15a2c5233..fedf8fd9b 100644 --- a/crates/pdf/tests/it/gradient_color_spaces.rs +++ b/crates/pdf/tests/it/gradient_color_spaces.rs @@ -1,13 +1,13 @@ //! Synthetic regressions for gradients that previously aborted inside the PDF writer. +use craft_pdf::syntax::{ + Pdf, + object::{Array, Dict, Name}, +}; use designcraft_color::{Color, Gradient, GradientKind, GradientStop, Swatch, SwatchValue}; use designcraft_compose::Cache; use designcraft_doc::{Document, Fill, Item, ItemId, Shape, SpreadRef, build::NewDocument}; use designcraft_geom::{Rect, shapes}; use designcraft_pdf::{PdfOptions, Standard, export_pdf_with_report}; -use hayro_syntax::{ - Pdf, - object::{Array, Dict, Name}, -}; fn document(colors: &[Color], kind: GradientKind, midpoint: f32, opacity: f32) -> Document { let mut doc = Document::new(&NewDocument { width: 120.0, height: 120.0, facing_pages: false, ..Default::default() }); diff --git a/crates/pdf/tests/it/spot_gradients.rs b/crates/pdf/tests/it/spot_gradients.rs index eac962bc9..a47454c22 100644 --- a/crates/pdf/tests/it/spot_gradients.rs +++ b/crates/pdf/tests/it/spot_gradients.rs @@ -1,14 +1,14 @@ //! Gradients on spot swatches shade in their inks: Separation for one spot, DeviceN for a spot //! with process inks or a second spot. +use craft_pdf::syntax::{ + Pdf, + object::{Array, Dict, Name}, +}; use designcraft_color::{Color, ColorType, Gradient, GradientKind, GradientStop, Swatch, SwatchValue}; use designcraft_compose::Cache; use designcraft_doc::{Document, Fill, Item, ItemId, Shape, SpreadRef, build::NewDocument}; use designcraft_geom::{Rect, shapes}; use designcraft_pdf::{PdfOptions, Standard, export_pdf_with_report}; -use hayro_syntax::{ - Pdf, - object::{Array, Dict, Name}, -}; const CYAN: &str = "C=100 M=0 Y=0 K=0"; diff --git a/crates/pdf/tests/support/image_content_cache_contract.rs b/crates/pdf/tests/support/image_content_cache_contract.rs index a35fc464e..fc2b53c9d 100644 --- a/crates/pdf/tests/support/image_content_cache_contract.rs +++ b/crates/pdf/tests/support/image_content_cache_contract.rs @@ -93,7 +93,7 @@ fn drawn_content(bytes: Vec) -> (Vec, Vec<([u8; 4], kurbo::Rect) fn pop_clip_path(&mut self) {} fn pop_transparency_group(&mut self) {} } - let pdf = hayro_syntax::Pdf::new(bytes).unwrap(); + let pdf = craft_pdf::syntax::Pdf::new(bytes).unwrap(); let cache = InterpreterCache::new(); let mut sink = Sink(Vec::new(), Vec::new()); for page in pdf.pages().iter() { @@ -182,8 +182,8 @@ struct ImageObject { } fn image_objects(bytes: &[u8]) -> Vec { - use hayro_syntax::object::{Name, Stream}; - let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).unwrap(); + use craft_pdf::syntax::object::{Name, Stream}; + let pdf = craft_pdf::syntax::Pdf::new(bytes.to_vec()).unwrap(); pdf.objects() .into_iter() .filter_map(|o| o.into_stream()) diff --git a/crates/textimport/src/docx.rs b/crates/textimport/src/docx.rs index ba869a599..4cc43f260 100644 --- a/crates/textimport/src/docx.rs +++ b/crates/textimport/src/docx.rs @@ -26,40 +26,43 @@ fn style_names(styles: &StyleSheet) -> HashMap { } fn char_attrs(p: &WordCharProps) -> CharAttrs { - let mut out = CharAttrs::default(); - out.font_family = p.font.as_ref().map(|s| capped_name(s)); - out.font_style = match (p.bold, p.italic) { - (Some(true), Some(true)) => Some("Bold Italic".into()), - (Some(true), _) => Some("Bold".into()), - (_, Some(true)) => Some("Italic".into()), - (Some(false), Some(false)) => Some("Regular".into()), - _ => None, - }; - out.size = p.size.map(f64::from).and_then(designcraft_doc::attrs::imported_type_size); - out.underline = p.underline.map(|u| u != Underline::None); - out.strikethrough = p.strike.or(p.double_strike); - out.capitalization = p.caps.filter(|&on| on).map(|_| designcraft_doc::Capitalization::AllCaps); - out.position = p.vert_align.map(|v| match v { - VertAlign::Superscript => designcraft_doc::Position::Superscript, - VertAlign::Subscript => designcraft_doc::Position::Subscript, - VertAlign::Baseline => designcraft_doc::Position::Normal, - }); - out + CharAttrs { + font_family: p.font.as_ref().map(|s| capped_name(s)), + font_style: match (p.bold, p.italic) { + (Some(true), Some(true)) => Some("Bold Italic".into()), + (Some(true), _) => Some("Bold".into()), + (_, Some(true)) => Some("Italic".into()), + (Some(false), Some(false)) => Some("Regular".into()), + _ => None, + }, + size: p.size.map(f64::from).and_then(designcraft_doc::attrs::imported_type_size), + underline: p.underline.map(|u| u != Underline::None), + strikethrough: p.strike.or(p.double_strike), + capitalization: p.caps.filter(|&on| on).map(|_| designcraft_doc::Capitalization::AllCaps), + position: p.vert_align.map(|v| match v { + VertAlign::Superscript => designcraft_doc::Position::Superscript, + VertAlign::Subscript => designcraft_doc::Position::Subscript, + VertAlign::Baseline => designcraft_doc::Position::Normal, + }), + ..Default::default() + } } fn para_attrs(p: &WordParaProps, doc: &craft_wordmodel::Document) -> ParaAttrs { - let mut out = ParaAttrs::default(); - out.align = p.align.map(|a| match a { - WordAlign::Center => designcraft_doc::Align::Center, - WordAlign::Right => designcraft_doc::Align::Right, - WordAlign::Justify | WordAlign::Distribute => designcraft_doc::Align::LeftJustified, - WordAlign::Left => designcraft_doc::Align::Left, - }); - out.left_indent = p.indent_left.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); - out.right_indent = p.indent_right.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); - out.first_line_indent = p.indent_first.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); - out.space_before = p.space_before.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); - out.space_after = p.space_after.map(f64::from).and_then(designcraft_doc::attrs::imported_measure); + let mut out = ParaAttrs { + align: p.align.map(|a| match a { + WordAlign::Center => designcraft_doc::Align::Center, + WordAlign::Right => designcraft_doc::Align::Right, + WordAlign::Justify | WordAlign::Distribute => designcraft_doc::Align::LeftJustified, + WordAlign::Left => designcraft_doc::Align::Left, + }), + left_indent: p.indent_left.map(f64::from).and_then(designcraft_doc::attrs::imported_measure), + right_indent: p.indent_right.map(f64::from).and_then(designcraft_doc::attrs::imported_measure), + first_line_indent: p.indent_first.map(f64::from).and_then(designcraft_doc::attrs::imported_measure), + space_before: p.space_before.map(f64::from).and_then(designcraft_doc::attrs::imported_measure), + space_after: p.space_after.map(f64::from).and_then(designcraft_doc::attrs::imported_measure), + ..Default::default() + }; if let Some(numbering) = p.numbering.filter(|n| n.num != 0) { let definition = doc.numbering.nums.iter().find(|n| n.id == numbering.num).and_then(|n| { n.level_overrides.iter().find(|(level, _)| *level == numbering.level).map(|(_, def)| def).or_else(|| { @@ -165,19 +168,19 @@ fn word_text(c: char) -> char { } } -fn append_paragraph( - doc: &craft_wordmodel::Document, - p: &Paragraph, - st: &mut Story, - names: &HashMap, - warned_objects: &mut bool, - notes_done: &mut HashSet, - note_bytes: &mut usize, - warnings: &mut Vec, -) { +struct ImportContext<'a> { + doc: &'a craft_wordmodel::Document, + names: &'a HashMap, + warned_objects: bool, + notes_done: HashSet, + note_bytes: usize, + warnings: Vec, +} + +fn append_paragraph(ctx: &mut ImportContext<'_>, p: &Paragraph, st: &mut Story) { let para_at = st.len(); - let style = p.props.style.as_ref().and_then(|id| names.get(id)).cloned().unwrap_or_else(|| BASIC_PARAGRAPH.into()); - st.format_paras(para_at..para_at, |fmt| *fmt = ParaFormat { style: style.clone(), para: para_attrs(&p.props, doc), ..Default::default() }); + let style = p.props.style.as_ref().and_then(|id| ctx.names.get(id)).cloned().unwrap_or_else(|| BASIC_PARAGRAPH.into()); + st.format_paras(para_at..para_at, |fmt| *fmt = ParaFormat { style: style.clone(), para: para_attrs(&p.props, ctx.doc), ..Default::default() }); let mut object_i = 0usize; let mut last_format: Option = None; @@ -199,7 +202,7 @@ fn append_paragraph( }) .find(|(range, _)| range.contains(&at)) .map(|(_, r)| r); - let fmt = run.map(|r| char_format(&r.props, names)).unwrap_or_default(); + let fmt = run.map(|r| char_format(&r.props, ctx.names)).unwrap_or_default(); if last_format.as_ref().is_some_and(|prev| prev != &fmt) { flush(st, &mut last_format, &mut chunk); } @@ -209,40 +212,40 @@ fn append_paragraph( if let Some(object) = p.objects.get(object_i) { match object { InlineObject::NoteRef { kind: NoteKind::Footnote, id, .. } => { - let mut text = doc.parts.get(id).map(|part| plain_blocks(&part.blocks)).unwrap_or_default(); - if notes_done.contains(id) { + let mut text = ctx.doc.parts.get(id).map(|part| plain_blocks(&part.blocks)).unwrap_or_default(); + if ctx.notes_done.contains(id) { text.clear(); add_warning( - warnings, + &mut ctx.warnings, format!("Footnote {id} is referenced more than once in the Word file: the text is kept at its first reference."), ); - } else if note_bytes.saturating_add(text.len()) > MAX_NOTE_BYTES { + } else if ctx.note_bytes.saturating_add(text.len()) > MAX_NOTE_BYTES { text.clear(); add_warning( - warnings, + &mut ctx.warnings, format!("The Word file's footnotes are longer than {} MB: the rest were imported empty.", MAX_NOTE_BYTES / (1 << 20)), ); } else { - notes_done.insert(*id); - *note_bytes = note_bytes.saturating_add(text.len()); + ctx.notes_done.insert(*id); + ctx.note_bytes = ctx.note_bytes.saturating_add(text.len()); } let _ = st.insert_note(st.len(), &text, ParaFormat::default()); } InlineObject::NoteRef { .. } => { - *warned_objects = true; + ctx.warned_objects = true; let _ = st.insert_note(st.len(), "", ParaFormat::default()); } other => { let text = other.plain_text(); if text.is_empty() { - *warned_objects = true; + ctx.warned_objects = true; } else { st.insert_with(st.len(), text, last_format.clone().unwrap_or_default()); } } } } else { - *warned_objects = true; + ctx.warned_objects = true; } object_i = object_i.saturating_add(1); } else { @@ -252,16 +255,7 @@ fn append_paragraph( flush(st, &mut last_format, &mut chunk); } -fn append_blocks( - doc: &craft_wordmodel::Document, - blocks: &[std::sync::Arc], - st: &mut Story, - names: &HashMap, - warned_objects: &mut bool, - notes_done: &mut HashSet, - note_bytes: &mut usize, - warnings: &mut Vec, -) { +fn append_blocks(ctx: &mut ImportContext<'_>, blocks: &[std::sync::Arc], st: &mut Story) { let mut first = st.is_empty(); for block in blocks { match block.as_ref() { @@ -270,7 +264,7 @@ fn append_blocks( st.insert(st.len(), "\n"); } first = false; - append_paragraph(doc, p, st, names, warned_objects, notes_done, note_bytes, warnings); + append_paragraph(ctx, p, st); } Block::Table(table) => { let rows: Vec> = table.rows.iter().map(|r| r.cells.iter().map(|c| plain_blocks(&c.blocks)).collect()).collect(); @@ -289,14 +283,11 @@ pub fn import(bytes: &[u8]) -> Result { let doc = craft_docx::read(bytes).map_err(|e| ImportError::Corrupt(e.to_string()))?; let (mut para_styles, mut char_styles, names) = style_records(&doc.styles, &doc); let mut story = Story::new(StoryId(0)); - let mut warned_objects = false; - let mut notes_done = HashSet::new(); - let mut note_bytes = 0usize; - let mut warnings = Vec::new(); - append_blocks(&doc, &doc.body, &mut story, &names, &mut warned_objects, &mut notes_done, &mut note_bytes, &mut warnings); + let mut ctx = ImportContext { doc: &doc, names: &names, warned_objects: false, notes_done: HashSet::new(), note_bytes: 0, warnings: Vec::new() }; + append_blocks(&mut ctx, &doc.body, &mut story); story.end_paragraphs_at_breaks(); - if warned_objects { - add_warning(&mut warnings, "Images, drawings and endnotes in the Word file were not imported (place them separately).".into()); + if ctx.warned_objects { + add_warning(&mut ctx.warnings, "Images, drawings and endnotes in the Word file were not imported (place them separately).".into()); } let used_p: HashSet = story.paras.iter().map(|p| p.style.clone()).collect(); let used_c: HashSet = story.runs().map(|(_, f)| f.style.clone()).collect(); @@ -314,7 +305,7 @@ pub fn import(bytes: &[u8]) -> Result { }; keep(&mut para_styles, &used_p); keep(&mut char_styles, &used_c); - Ok(Imported { story, para_styles, char_styles, warnings }) + Ok(Imported { story, para_styles, char_styles, warnings: ctx.warnings }) } fn add_warning(warnings: &mut Vec, message: String) { From fee641be681467bba0b8624a95f8b64e2e1e968b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:25:27 -0500 Subject: [PATCH 012/153] Fix current Rust clippy findings --- crates/pdf/src/export.rs | 2 +- crates/render/src/lib.rs | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/crates/pdf/src/export.rs b/crates/pdf/src/export.rs index 3d7d53699..5462c015a 100644 --- a/crates/pdf/src/export.rs +++ b/crates/pdf/src/export.rs @@ -203,7 +203,7 @@ impl GreyImage { return None; } let (mut grey, mut alpha) = (Vec::with_capacity(pixels), Vec::with_capacity(pixels)); - for p in img.data().chunks_exact(2) { + for p in img.data().as_chunks::<2>().0 { grey.push(p[0]); alpha.push(p[1]); } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index e6e5d04e8..84bb075dc 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -72,19 +72,18 @@ impl Rendered { } /// PNG bytes (empty, with the error logged, if the image can't be encoded). pub fn to_png(&self) -> Vec { - let mut buf = Vec::new(); let mut px = self.to_straight(); // `pixels` is public: a mismatched buffer is padded or cut to the stated size. px.resize(self.width as usize * self.height as usize * 4, 0); let Ok(img) = craft_image_codecs::Image::from_u8(self.width, self.height, craft_image_codecs::ChannelLayout::Rgba, px) else { log::error!("PNG encode: {}×{} image is too large", self.width, self.height); - return buf; + return Vec::new(); }; match craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &Default::default()) { Ok(bytes) => bytes, Err(e) => { log::error!("PNG encode: {e}"); - buf + Vec::new() } } } @@ -100,17 +99,16 @@ impl Rendered { [mix(p[0]), mix(p[1]), mix(p[2])] }) .collect(); - let mut buf = Vec::new(); let Ok(img) = craft_image_codecs::Image::from_u8(self.width, self.height, craft_image_codecs::ChannelLayout::Rgb, rgb) else { log::error!("JPEG encode: {}×{} image is too large", self.width, self.height); - return buf; + return Vec::new(); }; let options = craft_image_codecs::EncodeOptions { jpeg_quality: quality.clamp(1, 100), ..Default::default() }; match craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &options) { Ok(bytes) => bytes, Err(e) => { log::error!("JPEG encode: {e}"); - buf + Vec::new() } } } From aff7435b81667f448edb514913034898cc6715e1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:34:50 -0500 Subject: [PATCH 013/153] Route file dispatch through shared format APIs --- Cargo.lock | 74 ++++++++++++++++++++----- Cargo.toml | 9 +--- crates/engine/Cargo.toml | 3 +- crates/engine/src/cmd/book.rs | 2 +- crates/engine/src/cmd/file/open.rs | 21 ++++++-- crates/engine/src/cmd/file/place.rs | 16 +++--- crates/engine/src/cmd/file/save.rs | 6 +-- crates/engine/src/cmd/interchange.rs | 20 +++---- crates/engine/src/cmd/load_styles.rs | 2 +- crates/images/Cargo.toml | 1 + crates/images/src/jpeg.rs | 80 ---------------------------- crates/images/src/lib.rs | 42 +++++++-------- crates/pdf/src/export/images.rs | 2 +- crates/textimport/Cargo.toml | 1 + crates/textimport/src/lib.rs | 35 ++++++------ 15 files changed, 144 insertions(+), 170 deletions(-) delete mode 100644 crates/images/src/jpeg.rs diff --git a/Cargo.lock b/Cargo.lock index 36d06c8b9..b46aa6550 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1122,7 +1122,7 @@ dependencies = [ "craft-offcrypto", "craft-opc", "craft-wordmodel", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "log", "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1135,12 +1135,17 @@ name = "craft-drawingml" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "kurbo", "log", "serde", ] +[[package]] +name = "craft-eps" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d#6ba1ca33ca21c33de55ef155141db08ca6048e8d" + [[package]] name = "craft-eps" version = "0.1.0" @@ -1151,11 +1156,20 @@ name = "craft-epub" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-zip", "thiserror 2.0.21", ] +[[package]] +name = "craft-file-formats" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d#6ba1ca33ca21c33de55ef155141db08ca6048e8d" +dependencies = [ + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d)", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d)", +] + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -1189,7 +1203,7 @@ name = "craft-idml" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-zip", "thiserror 2.0.21", ] @@ -1216,6 +1230,14 @@ dependencies = [ "zune-jpegxl", ] +[[package]] +name = "craft-indd" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47bf5650b0d503b97bfb5e841746c4957897d11f#47bf5650b0d503b97bfb5e841746c4957897d11f" +dependencies = [ + "indd", +] + [[package]] name = "craft-jpeg" version = "0.1.0" @@ -1241,7 +1263,7 @@ name = "craft-math" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "serde", ] @@ -1253,7 +1275,7 @@ dependencies = [ "aes", "base64 0.23.1", "craft-cfb", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "getrandom 0.3.4", "hmac", "sha1 0.11.0", @@ -1267,7 +1289,7 @@ name = "craft-opc" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-zip", "log", "thiserror 2.0.21", @@ -1337,12 +1359,24 @@ name = "craft-sheet" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d#6ba1ca33ca21c33de55ef155141db08ca6048e8d" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d)", + "flate2", + "kurbo", + "roxmltree 0.21.2", + "thiserror 2.0.21", +] + [[package]] name = "craft-svg" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "flate2", "kurbo", "roxmltree 0.21.2", @@ -1438,7 +1472,16 @@ dependencies = [ "craft-drawingml", "craft-opc", "craft-sheet", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d#6ba1ca33ca21c33de55ef155141db08ca6048e8d" +dependencies = [ + "quick-xml 0.41.0", "thiserror 2.0.21", ] @@ -1697,6 +1740,8 @@ dependencies = [ "caseless", "craft-atomic", "craft-docpath", + "craft-file-formats", + "craft-indd", "craft-prepress", "craft-print", "designcraft-color", @@ -1716,7 +1761,6 @@ dependencies = [ "hayro-cmap", "hayro-interpret", "hayro-syntax", - "indd", "kurbo", "log", "quick-xml 0.41.0", @@ -1758,7 +1802,7 @@ name = "designcraft-format" version = "0.20.1" dependencies = [ "craft-document-bundle", - "craft-eps", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "designcraft-doc", "serde", "serde_json", @@ -1784,7 +1828,7 @@ name = "designcraft-idml" version = "0.20.1" dependencies = [ "craft-idml", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "designcraft-color", "designcraft-compose", "designcraft-doc", @@ -1800,10 +1844,11 @@ name = "designcraft-images" version = "0.20.1" dependencies = [ "craft-cms", - "craft-eps", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-file-formats", "craft-image-codecs", "craft-psd", - "craft-svg", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "designcraft-color", "designcraft-fonts", "flate2", @@ -1896,6 +1941,7 @@ name = "designcraft-textimport" version = "0.20.1" dependencies = [ "craft-docx", + "craft-file-formats", "craft-idml", "craft-rtf", "craft-wordmodel", diff --git a/Cargo.toml b/Cargo.toml index 54833bb50..2b2f09020 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-indd = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } @@ -25,6 +26,7 @@ craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243 craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } +craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "6ba1ca33ca21c33de55ef155141db08ca6048e8d", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. @@ -88,13 +90,6 @@ flo_curves = "0.8" qrcodegen = "1.8" quick-xml = "0.41" zip = { version = "3", default-features = false, features = ["deflate"] } -# INDD/INDT → IDML conversion for File › Open: storytold/indd-utils, pinned by rev as every other -# git dependency here is. It parses untrusted INDD files, and `branch = "main"` meant any -# `cargo update` -- Dependabot runs them -- pulled whatever was on that branch into the parser -# without review (#1599). -# (MIT OR Apache-2.0, no dependencies, builds for wasm). -indd = { git = "https://github.com/storytold/indd-utils", rev = "7f3060e5bce78d21ea70e0d5eacb96edce15dfbe" } - # Which paths stored in documents may be followed (craft-libs). craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } # Atomic file replacement for saves and exports (craft-libs). diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index c1c0f1d33..ed9e7e3bc 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -26,7 +26,8 @@ designcraft-pdf = { workspace = true } craft-prepress = { workspace = true } designcraft-tools = { workspace = true } designcraft-idml = { workspace = true } -indd = { workspace = true } +craft-file-formats = { workspace = true } +craft-indd = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/engine/src/cmd/book.rs b/crates/engine/src/cmd/book.rs index a4217f825..b1de3dac7 100644 --- a/crates/engine/src/cmd/book.rs +++ b/crates/engine/src/cmd/book.rs @@ -57,7 +57,7 @@ fn load(path: &str) -> Result { let _ = path; vec![] }; - if path.to_lowercase().ends_with(".idml") { + if craft_file_formats::detect(path, &bytes) == Some(craft_file_formats::Format::Idml) { return super::interchange::import_idml_confined(&bytes, Some(std::path::Path::new(path))) .map_err(|e| EngineError::Other(format!("{path}: {e}"))); } diff --git a/crates/engine/src/cmd/file/open.rs b/crates/engine/src/cmd/file/open.rs index 529ec2d96..e88d75afc 100644 --- a/crates/engine/src/cmd/file/open.rs +++ b/crates/engine/src/cmd/file/open.rs @@ -84,12 +84,17 @@ fn open_path(s: &mut Session, p: &Value) -> Result { let bytes = crate::io::read_file(path).map_err(EngineError::Other)?; // The content decides first: a file's extension may be missing or wrong. The extension // then routes files whose content isn't recognised to the reader that explains why. - let ext = std::path::Path::new(path).extension().and_then(std::ffi::OsStr::to_str).unwrap_or_default().to_ascii_lowercase(); - let native = designcraft_format::is_designcraft(&bytes) || bytes.trim_ascii_start().starts_with(b"{"); - if designcraft_idml::is_idml(&bytes) || (!native && (crate::cmd::interchange::is_indd(path, &bytes) || ext == "idml")) { + let format = craft_file_formats::detect(path, &bytes); + let native = format == Some(craft_file_formats::Format::DesignCraft) + || designcraft_format::is_designcraft(&bytes) + || bytes.trim_ascii_start().starts_with(b"{"); + if format == Some(craft_file_formats::Format::Idml) + || designcraft_idml::is_idml(&bytes) + || (!native && format == Some(craft_file_formats::Format::Indd)) + { return crate::cmd::interchange::open_idml(s, p); } - if !native && ext != designcraft_format::EXTENSION { + if !native { return Err(bad("file.open", format!("{path}: not a DesignCraft or IDML file"))); } let (mut d, mut warnings) = load_bytes(&bytes)?; @@ -156,7 +161,13 @@ pub(super) fn file_open_bytes(s: &mut Session, p: &Value) -> Result { let b = base64_decode(str_param(p, "base64").unwrap_or("")); // IDML packages are recognised by their stored `mimetype` first entry, INDD files by their // signature (or their name). - if designcraft_idml::is_idml(&b) || crate::cmd::interchange::is_indd(str_param(p, "name").unwrap_or_default(), &b) { + let name = str_param(p, "name").unwrap_or_default(); + let format = craft_file_formats::detect(name, &b); + let native = designcraft_format::is_designcraft(&b) || b.trim_ascii_start().starts_with(b"{"); + if format == Some(craft_file_formats::Format::Idml) + || designcraft_idml::is_idml(&b) + || (!native && format == Some(craft_file_formats::Format::Indd)) + { return crate::cmd::interchange::open_idml(s, p); } let (mut d, warnings) = load_bytes(&b)?; diff --git a/crates/engine/src/cmd/file/place.rs b/crates/engine/src/cmd/file/place.rs index a30cb2321..6d5236734 100644 --- a/crates/engine/src/cmd/file/place.rs +++ b/crates/engine/src/cmd/file/place.rs @@ -32,9 +32,9 @@ pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { return Err(bad("file.place", "the frame is locked")); } // A page of another layout (IDML or DesignCraft): its objects, as one group. - let lname = name.to_lowercase(); - if lname.ends_with(".idml") || lname.ends_with(".designcraft") { - return place_layout_page(s, p, &name, &bytes); + let format = craft_file_formats::detect(&name, &bytes); + if matches!(format, Some(craft_file_formats::Format::Idml | craft_file_formats::Format::DesignCraft)) { + return place_layout_page(s, p, &bytes, format); } // Keep the EPS source for link comparison; rendering uses its preview at the bounding box size. let eps_size = eps_size(&bytes, "file.place")?; @@ -57,11 +57,11 @@ pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { }) .transpose()?; // Image Import Options: which page of a PDF (1-based). - if designcraft_render::is_pdf(&bytes) { + if format == Some(craft_file_formats::Format::Pdf) { designcraft_render::check_pdf_input_size(bytes.len()).map_err(|e| bad("file.place", e.to_string()))?; } let pdf_page = match p.get("pdfPage").and_then(Value::as_u64) { - Some(n) if designcraft_render::is_pdf(&bytes) => { + Some(n) if format == Some(craft_file_formats::Format::Pdf) => { let count = designcraft_render::pdf_page_count(&bytes).unwrap_or(1); let invalid_page = || bad("file.place", format!("the PDF has {count} page(s)")); let one_based = usize::try_from(n).map_err(|_| invalid_page())?; @@ -83,7 +83,7 @@ pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { // and SVGs keep their exact size: rounding it to whole points would stretch them unevenly. let (nw, nh) = eps_size.or_else(|| designcraft_render::natural_size(&bytes, pdf_page as usize)).unwrap_or((pw as f64, ph as f64)); // Image Import Options › Crop to: the graphic spans that box of the PDF page. - let is_pdf = designcraft_render::is_pdf(&bytes); + let is_pdf = format == Some(craft_file_formats::Format::Pdf); let pdf_crop = match str_param(p, "pdfCrop") { Some(k) if is_pdf => PdfCrop::from_name(k) .ok_or_else(|| bad("file.place", format!("unknown pdfCrop `{k}` (crop, art, trim, bleed, media, contentVisible, contentAll)")))?, @@ -347,9 +347,9 @@ pub(super) fn place_drop(s: &mut Session, p: &Value) -> Result { /// File › Place of an IDML or DesignCraft document: the objects of one page (`layoutPage`, /// 1-based) grouped, top-left at x/y (default: the source position on the first page). -fn place_layout_page(s: &mut Session, p: &Value, name: &str, bytes: &[u8]) -> Result { +fn place_layout_page(s: &mut Session, p: &Value, bytes: &[u8], format: Option) -> Result { const ID: &str = "file.place"; - let src = if name.to_lowercase().ends_with(".idml") { + let src = if format == Some(craft_file_formats::Format::Idml) { crate::cmd::interchange::import_idml_confined(bytes, str_param(p, "path").map(std::path::Path::new)).map_err(|e| bad(ID, e.to_string()))? } else { from_bytes(bytes)? diff --git a/crates/engine/src/cmd/file/save.rs b/crates/engine/src/cmd/file/save.rs index d94a02ab9..d12bf03fa 100644 --- a/crates/engine/src/cmd/file/save.rs +++ b/crates/engine/src/cmd/file/save.rs @@ -89,11 +89,11 @@ pub(super) fn file_revert(s: &mut Session, _: &Value) -> Result { let path = s.doc()?.path.clone().ok_or_else(|| bad("file.revert", "the document has never been saved"))?; #[cfg(not(target_arch = "wasm32"))] { - let mut d = if path.to_ascii_lowercase().ends_with(".idml") { - let bytes = crate::io::read_file(&path).map_err(EngineError::Other)?; + let bytes = crate::io::read_file(&path).map_err(EngineError::Other)?; + let mut d = if craft_file_formats::detect(&path, &bytes) == Some(craft_file_formats::Format::Idml) { crate::cmd::interchange::import_idml_confined(&bytes, Some(std::path::Path::new(&path))).map_err(|e| EngineError::Other(e.to_string()))? } else { - from_bytes(&crate::io::read_file(&path).map_err(EngineError::Other)?)? + from_bytes(&bytes)? }; // As opened again: the fonts beside it now (unchanged files are shared, not read again). let (fonts, _, _) = load_document_fonts(&mut d, &path); diff --git a/crates/engine/src/cmd/interchange.rs b/crates/engine/src/cmd/interchange.rs index 47b36b776..0fba3b900 100644 --- a/crates/engine/src/cmd/interchange.rs +++ b/crates/engine/src/cmd/interchange.rs @@ -418,7 +418,7 @@ pub(crate) fn open_idml(s: &mut Session, p: &Value) -> Result { let mut previews = std::collections::HashMap::new(); let bytes = if is_indd(&file, &bytes) { // What the converter can't read still comes over as IDML (#21). - let conversion = indd::convert(&bytes, &file).map_err(|e| { + let conversion = craft_indd::convert(&bytes, &file).map_err(|e| { EngineError::Other(format!( "can't convert the InDesign file: {e}. In InDesign, choose File ▸ Export… ▸ InDesign Markup (IDML) and open the .idml in DesignCraft." )) @@ -470,19 +470,19 @@ fn warning_item( /// the bytes are neither a recognized DesignCraft document nor IDML. The converter explains /// unrecognized data with an InDesign extension. pub(crate) fn is_indd(name: &str, bytes: &[u8]) -> bool { - bytes.starts_with(&indd::header::SIGNATURE) - || (has_indd_extension(name) && !super::file::is_document(bytes) && !bytes.trim_ascii_start().starts_with(b"{")) -} - -pub(crate) fn has_indd_extension(name: &str) -> bool { - let n = name.to_ascii_lowercase(); - n.ends_with(".indd") || n.ends_with(".indt") + craft_file_formats::detect(name, bytes) == Some(craft_file_formats::Format::Indd) + && !super::file::is_document(bytes) + && !bytes.trim_ascii_start().starts_with(b"{") } /// `name` without a trailing .idml, .indd or .indt (any case). fn without_document_extension(name: &str) -> &str { match name.rsplit_once('.') { - Some((stem, ext)) if ["idml", "indd", "indt"].iter().any(|e| ext.eq_ignore_ascii_case(e)) => stem, + Some((stem, ext)) + if matches!(craft_file_formats::from_extension(ext), Some(craft_file_formats::Format::Idml | craft_file_formats::Format::Indd)) => + { + stem + } _ => name, } } @@ -683,7 +683,7 @@ mod tests { /// name) go to the converter, and what it can't read is an open error that adds no document. #[test] fn indd_files_go_through_the_converter_and_its_errors_are_open_errors() { - let mut signed = indd::header::SIGNATURE.to_vec(); + let mut signed = craft_indd::SIGNATURE.to_vec(); signed.resize(4096, 0); let cases = [(signed, "Brochure"), (vec![0; 4096], "Brochure.indd"), (b"not an InDesign file".to_vec(), "Template.INDT")]; let mut s = Session::new(); diff --git a/crates/engine/src/cmd/load_styles.rs b/crates/engine/src/cmd/load_styles.rs index 762f7b1fc..154306048 100644 --- a/crates/engine/src/cmd/load_styles.rs +++ b/crates/engine/src/cmd/load_styles.rs @@ -85,7 +85,7 @@ fn source(p: &Value, id: &str) -> Result { (Some(path), None) => (crate::io::read_file(path).map_err(|e| bad(id, e))?, path.to_string()), _ => return Err(bad(id, "give `path` or `base64`")), }; - if name.to_lowercase().ends_with(".idml") { + if craft_file_formats::detect(&name, &bytes) == Some(craft_file_formats::Format::Idml) { designcraft_idml::import_idml(&bytes).map_err(|e| bad(id, e.to_string())) } else { super::file::from_bytes(&bytes).map_err(|e| bad(id, e.to_string())) diff --git a/crates/images/Cargo.toml b/crates/images/Cargo.toml index 6b58daccb..f2cd6b362 100644 --- a/crates/images/Cargo.toml +++ b/crates/images/Cargo.toml @@ -20,6 +20,7 @@ log = { workspace = true } craft-cms = { workspace = true } craft-image-codecs = { workspace = true } craft-psd = { workspace = true } +craft-file-formats = { workspace = true } [dev-dependencies] designcraft-fonts = { workspace = true, features = ["testing"] } diff --git a/crates/images/src/jpeg.rs b/crates/images/src/jpeg.rs deleted file mode 100644 index 8b44d0c9a..000000000 --- a/crates/images/src/jpeg.rs +++ /dev/null @@ -1,80 +0,0 @@ -//! CMYK and YCCK JPEGs, decoded to their ink values (the `image` crate converts them to RGB with -//! a profile-free formula). - -use zune_core::bytestream::ZCursor; -use zune_core::colorspace::ColorSpace; -use zune_core::options::DecoderOptions; -use zune_jpeg::JpegDecoder; - -use crate::CmykRaster; - -/// The largest decoded image, in bytes (the `image` crate's default allocation limit). -const MAX_BYTES: usize = 512 << 20; - -fn decoder(bytes: &[u8], out: ColorSpace) -> JpegDecoder> { - let options = DecoderOptions::default().set_strict_mode(false).set_max_width(65_535).set_max_height(65_535).jpeg_set_out_colorspace(out); - JpegDecoder::new_with_options(ZCursor::new(bytes), options) -} - -/// A four-channel JPEG's inks (0 = no ink) and its embedded ICC profile. `None` for other JPEGs -/// and for anything that doesn't decode. -pub(crate) fn decode_cmyk(bytes: &[u8]) -> Option<(CmykRaster, Option>)> { - if !bytes.starts_with(&[0xFF, 0xD8]) { - return None; - } - let mut d = decoder(bytes, ColorSpace::RGB); - d.decode_headers().ok()?; - let space = d.input_colorspace()?; - if !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) { - return None; - } - let info = d.info()?; - let (w, h) = (usize::from(info.width), usize::from(info.height)); - let pixels = w.checked_mul(h)?; - if pixels.checked_mul(4)? > MAX_BYTES { - return None; - } - // Decoding into the input colour space copies the channels as stored. - let mut d = decoder(bytes, space); - let mut px = d.decode().ok()?; - if px.len() != pixels.checked_mul(4)? { - return None; - } - let icc = d.icc_profile(); - if space == ColorSpace::YCCK { - // YCbCr encodes 255 − the first three stored values (JFIF conversion); K is as stored. - for p in px.as_chunks_mut::<4>().0 { - let (y, cb, cr) = (f32::from(p[0]), f32::from(p[1]) - 128.0, f32::from(p[2]) - 128.0); - let byte = |v: f32| 255 - v.round().clamp(0.0, 255.0) as u8; - p[0] = byte(y + 1.402 * cr); - p[1] = byte(y - 0.344_136 * cb - 0.714_136 * cr); - p[2] = byte(y + 1.772 * cb); - } - } - // Adobe's APP14 marker (always there for YCCK) means the values are stored inverted: - // 255 = no ink. - if space == ColorSpace::YCCK || adobe_marker(bytes) { - for v in &mut px { - *v = 255 - *v; - } - } - let raster = CmykRaster { width: u32::try_from(w).ok()?, height: u32::try_from(h).ok()?, cmyk: px, alpha: None }; - Some((raster, icc)) -} - -/// Whether an APP14 "Adobe" segment comes before the image data. -fn adobe_marker(bytes: &[u8]) -> bool { - let mut i = 2; - while let Some(&[0xFF, marker, hi, lo]) = bytes.get(i..i + 4).and_then(|s| <&[u8; 4]>::try_from(s).ok()) { - // Start of scan or end of image: no more header segments. - if marker == 0xDA || marker == 0xD9 { - return false; - } - let len = usize::from(u16::from_be_bytes([hi, lo])); - if marker == 0xEE && bytes.get(i + 4..i + 9) == Some(b"Adobe".as_slice()) { - return true; - } - i += 2 + len; - } - false -} diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs index b9209e65e..a4c9d9b86 100644 --- a/crates/images/src/lib.rs +++ b/crates/images/src/lib.rs @@ -20,39 +20,37 @@ pub use tone::{Tone, tone}; pub const PT_PER_PX: f64 = 0.75; pub fn is_pdf(bytes: &[u8]) -> bool { - bytes.starts_with(b"%PDF") + file_format(bytes) == Some(craft_file_formats::Format::Pdf) } pub fn is_psd(bytes: &[u8]) -> bool { - craft_psd::head::sniff(bytes) + file_format(bytes) == Some(craft_file_formats::Format::Psd) } /// An SVG document (optionally after an XML declaration, comments or a doctype). pub fn is_svg(bytes: &[u8]) -> bool { - craft_svg::sniff::is_svg(bytes) + file_format(bytes) == Some(craft_file_formats::Format::Svg) +} + +pub fn file_format(bytes: &[u8]) -> Option { + craft_file_formats::detect("", bytes) } /// MIME type of a placed graphic. pub fn mime(bytes: &[u8]) -> &'static str { - if is_eps(bytes) { - return "application/postscript"; - } - if is_pdf(bytes) { - return "application/pdf"; - } - if is_psd(bytes) { - return "image/vnd.adobe.photoshop"; - } - if is_svg(bytes) { - return "image/svg+xml"; - } - match craft_image_codecs::detect(bytes) { - Some(craft_image_codecs::Format::Png) => "image/png", - Some(craft_image_codecs::Format::Jpeg) => "image/jpeg", - Some(craft_image_codecs::Format::Gif) => "image/gif", - Some(craft_image_codecs::Format::WebP) => "image/webp", - Some(craft_image_codecs::Format::Tiff) => "image/tiff", - Some(craft_image_codecs::Format::Bmp) => "image/bmp", + match file_format(bytes) { + Some(craft_file_formats::Format::Eps | craft_file_formats::Format::PostScript | craft_file_formats::Format::Illustrator) => { + "application/postscript" + } + Some(craft_file_formats::Format::Pdf) => "application/pdf", + Some(craft_file_formats::Format::Psd) => "image/vnd.adobe.photoshop", + Some(craft_file_formats::Format::Svg) => "image/svg+xml", + Some(craft_file_formats::Format::Png) => "image/png", + Some(craft_file_formats::Format::Jpeg) => "image/jpeg", + Some(craft_file_formats::Format::Gif) => "image/gif", + Some(craft_file_formats::Format::WebP) => "image/webp", + Some(craft_file_formats::Format::Tiff) => "image/tiff", + Some(craft_file_formats::Format::Bmp) => "image/bmp", _ => "application/octet-stream", } } diff --git a/crates/pdf/src/export/images.rs b/crates/pdf/src/export/images.rs index f4d4a0a98..4ddafa82d 100644 --- a/crates/pdf/src/export/images.rs +++ b/crates/pdf/src/export/images.rs @@ -180,7 +180,7 @@ pub(super) fn recompress(data: &[u8], mode: RasterMode) -> Option { } let img = craft_image_codecs::decode(data).ok()?; let rgba = img.to_rgba8(); - if img.layout().has_alpha() && rgba.chunks_exact(4).any(|p| p[3] < 255) { + if img.layout().has_alpha() && rgba.as_chunks::<4>().0.iter().any(|p| p[3] < 255) { return None; } // A grey image stays a grey JPEG (#1426). diff --git a/crates/textimport/Cargo.toml b/crates/textimport/Cargo.toml index 460458ddf..968a488c4 100644 --- a/crates/textimport/Cargo.toml +++ b/crates/textimport/Cargo.toml @@ -15,6 +15,7 @@ craft-xlsx = { workspace = true } craft-docx = { workspace = true } craft-wordmodel = { workspace = true } craft-idml = { workspace = true } +craft-file-formats = { workspace = true } [dev-dependencies] zip = { workspace = true } diff --git a/crates/textimport/src/lib.rs b/crates/textimport/src/lib.rs index 0838861e2..78a619bbc 100644 --- a/crates/textimport/src/lib.rs +++ b/crates/textimport/src/lib.rs @@ -64,27 +64,28 @@ impl std::fmt::Display for ImportError { /// Text files this crate reads, by extension. pub fn is_text_file(name: &str) -> bool { - let l = name.to_ascii_lowercase(); - l.ends_with(".txt") || l.ends_with(".text") || l.ends_with(".md") || l.ends_with(".docx") || l.ends_with(".rtf") || l.ends_with(".xlsx") + matches!( + craft_file_formats::detect(name, &[]), + Some( + craft_file_formats::Format::PlainText + | craft_file_formats::Format::Docx + | craft_file_formats::Format::Rtf + | craft_file_formats::Format::Xlsx + ) + ) } /// Import `bytes` (format from the file name, else sniffed). pub fn import(name: &str, bytes: &[u8]) -> Result { - let l = name.to_ascii_lowercase(); - if l.ends_with(".xlsx") { - return xlsx::import(bytes); - } - if l.ends_with(".docx") || bytes.starts_with(b"PK\x03\x04") { - return docx::import(bytes); - } - if l.ends_with(".rtf") || bytes.starts_with(b"{\\rtf") { - return rtf::import(bytes); - } - if tagged::is_tagged(bytes) { - return tagged::import(bytes); - } - if l.ends_with(".doc") { - return Err(ImportError::Unsupported("legacy Word .doc (save it as .docx)".into())); + match craft_file_formats::detect(name, bytes) { + Some(craft_file_formats::Format::Xlsx) => return xlsx::import(bytes), + Some(craft_file_formats::Format::Docx) => return docx::import(bytes), + Some(craft_file_formats::Format::Rtf) => return rtf::import(bytes), + Some(craft_file_formats::Format::TaggedText) => return tagged::import(bytes), + Some(craft_file_formats::Format::LegacyDoc) => { + return Err(ImportError::Unsupported("legacy Word .doc (save it as .docx)".into())); + } + _ => {} } Ok(plain(&tagged::decode(bytes))) } From d373506feb7b2a6047f4f56a97a47a193cb7182e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:39:41 -0500 Subject: [PATCH 014/153] Route UI format dispatch through shared registry --- Cargo.lock | 15 ++++++++------- Cargo.toml | 2 +- crates/engine/src/cmd/export/formats.rs | 2 +- crates/engine/src/cmd/file/bytes.rs | 4 ++-- crates/ui-egui/Cargo.toml | 1 + crates/ui-egui/src/app/files.rs | 20 +++++++++++--------- crates/ui-egui/src/menus/export.rs | 2 +- crates/ui-egui/src/menus/run.rs | 3 +-- 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b46aa6550..92227fc93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1144,7 +1144,7 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d#6ba1ca33ca21c33de55ef155141db08ca6048e8d" +source = "git+https://github.com/storytold/craft-libs?rev=b4ada28f1#b4ada28f19067d4693e902e0e5f32eb292241e3a" [[package]] name = "craft-eps" @@ -1164,10 +1164,10 @@ dependencies = [ [[package]] name = "craft-file-formats" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d#6ba1ca33ca21c33de55ef155141db08ca6048e8d" +source = "git+https://github.com/storytold/craft-libs?rev=b4ada28f1#b4ada28f19067d4693e902e0e5f32eb292241e3a" dependencies = [ - "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d)", - "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d)", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b4ada28f1)", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b4ada28f1)", ] [[package]] @@ -1362,9 +1362,9 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d#6ba1ca33ca21c33de55ef155141db08ca6048e8d" +source = "git+https://github.com/storytold/craft-libs?rev=b4ada28f1#b4ada28f19067d4693e902e0e5f32eb292241e3a" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b4ada28f1)", "flate2", "kurbo", "roxmltree 0.21.2", @@ -1479,7 +1479,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6ba1ca33ca21c33de55ef155141db08ca6048e8d#6ba1ca33ca21c33de55ef155141db08ca6048e8d" +source = "git+https://github.com/storytold/craft-libs?rev=b4ada28f1#b4ada28f19067d4693e902e0e5f32eb292241e3a" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1967,6 +1967,7 @@ name = "designcraft-ui-egui" version = "0.20.1" dependencies = [ "craft-dialogs", + "craft-file-formats", "craft-keymap", "craft-prepress", "craft-ui", diff --git a/Cargo.toml b/Cargo.toml index 2b2f09020..74359a740 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243 craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } -craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "6ba1ca33ca21c33de55ef155141db08ca6048e8d", version = "=0.1.0" } +craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "b4ada28f1", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/engine/src/cmd/export/formats.rs b/crates/engine/src/cmd/export/formats.rs index a25553d69..845b9b1d7 100644 --- a/crates/engine/src/cmd/export/formats.rs +++ b/crates/engine/src/cmd/export/formats.rs @@ -136,7 +136,7 @@ pub(super) fn export_text(s: &mut Session, p: &Value) -> Result { Some(f @ ("rtf" | "tagged")) => f, Some("txt") | Some("text") => "txt", Some(f) => return Err(bad(ID, format!("unknown format `{f}` (txt, rtf, tagged)"))), - None if path.is_some_and(|x| x.to_ascii_lowercase().ends_with(".rtf")) => "rtf", + None if path.is_some_and(|x| craft_file_formats::detect(x, &[]) == Some(craft_file_formats::Format::Rtf)) => "rtf", None => "txt", }; let text = match format { diff --git a/crates/engine/src/cmd/file/bytes.rs b/crates/engine/src/cmd/file/bytes.rs index 5fd6d4e0b..f169b74e4 100644 --- a/crates/engine/src/cmd/file/bytes.rs +++ b/crates/engine/src/cmd/file/bytes.rs @@ -21,7 +21,7 @@ fn save_bytes(d: &Document, preview: Option<&[u8]>) -> Result> { pub fn with_document_extension(path: &str) -> String { let p = std::path::Path::new(path); let ext = p.extension().and_then(std::ffi::OsStr::to_str); - if p.file_name().is_none() || ext.is_some_and(|e| e.eq_ignore_ascii_case(designcraft_format::EXTENSION)) { + if p.file_name().is_none() || ext.is_some_and(|e| craft_file_formats::from_extension(e) == Some(craft_file_formats::Format::DesignCraft)) { return path.to_string(); } format!("{path}.{}", designcraft_format::EXTENSION) @@ -32,7 +32,7 @@ pub fn with_document_extension(path: &str) -> String { /// that kept it would name the file `Brochure.designcraft.designcraft`. A bare `.designcraft` stays. pub(super) fn without_native_extension(name: &str) -> &str { match name.rsplit_once('.') { - Some((stem, ext)) if !stem.is_empty() && ext.eq_ignore_ascii_case(designcraft_format::EXTENSION) => stem, + Some((stem, ext)) if !stem.is_empty() && craft_file_formats::from_extension(ext) == Some(craft_file_formats::Format::DesignCraft) => stem, _ => name, } } diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 6b08420a9..143dfebfd 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -13,6 +13,7 @@ workspace = true craft-ui = { workspace = true, features = ["native-windows"] } craft-window = { workspace = true } craft-keymap = { workspace = true } +craft-file-formats = { workspace = true } designcraft-geom = { workspace = true } designcraft-color = { workspace = true } designcraft-doc = { workspace = true } diff --git a/crates/ui-egui/src/app/files.rs b/crates/ui-egui/src/app/files.rs index 069515394..61f0f9bbe 100644 --- a/crates/ui-egui/src/app/files.rs +++ b/crates/ui-egui/src/app/files.rs @@ -24,7 +24,7 @@ impl DesignApp { /// Open (`open`) or place (`place`) the file at `path`, chosen in a dialog or passed by a script. pub(crate) fn open_picked(&mut self, purpose: &str, path: String) -> Result { // Image Import Options: a multi-page PDF asks which page. - if purpose == "place" && path.to_ascii_lowercase().ends_with(".pdf") { + if purpose == "place" && craft_file_formats::detect(&path, &[]) == Some(craft_file_formats::Format::Pdf) { let target = self.session.active().map(|d| d.uid); let pages = self.services.read.as_mut().and_then(|r| r(&path).ok()).and_then(|b| designcraft_render::pdf_page_count(&b)).unwrap_or(1); if pages > 1 { @@ -126,7 +126,7 @@ impl DesignApp { } fn import_file(&mut self, file: ImportedFile) -> Result { - let lower = file.name.to_ascii_lowercase(); + let format = craft_file_formats::detect(&file.name, &file.bytes); if let Some(kind) = file.request.purpose.strip_prefix("styles") { self.activate_import_target(&file.request)?; let mut params = json!({"name": file.name, "base64": designcraft_engine::cmd::base64_encode(&file.bytes)}); @@ -140,11 +140,11 @@ impl DesignApp { let layout = opens_as_document(&file.name) || (std::path::Path::new(&file.name).extension().is_none() && designcraft_engine::cmd::is_document(&file.bytes)); let place = - file.request.purpose == "place" || (matches!(file.request.purpose.as_str(), "drop" | "open") && !layout && !lower.ends_with(".ase")); - if place || lower.ends_with(".ase") || file.request.purpose == "swatches" { + file.request.purpose == "place" || (matches!(file.request.purpose.as_str(), "drop" | "open") && !layout && format != Some(craft_file_formats::Format::Ase)); + if place || format == Some(craft_file_formats::Format::Ase) || file.request.purpose == "swatches" { self.activate_import_target(&file.request)?; } - if place && lower.ends_with(".pdf") { + if place && craft_file_formats::detect(&file.name, &file.bytes) == Some(craft_file_formats::Format::Pdf) { let pages = designcraft_render::pdf_page_count(&file.bytes).unwrap_or(1); if pages > 1 { self.ui.dialog = @@ -154,7 +154,7 @@ impl DesignApp { } } let b64 = designcraft_engine::cmd::base64_encode(&file.bytes); - if lower.ends_with(".ase") || file.request.purpose == "swatches" { + if format == Some(craft_file_formats::Format::Ase) || file.request.purpose == "swatches" { self.run("swatch.load", json!({"base64": b64})) } else if file.request.purpose == "place" { self.place_file(json!({"name": file.name, "base64": b64})) @@ -198,7 +198,7 @@ impl DesignApp { /// the pointer when there is none, and clears the flag, so the files after it land at the /// pointer instead of replacing that graphic. pub(crate) fn place_dropped(&mut self, path: &str, pointer: Option, frame_free: &mut bool) -> Result { - if path.to_ascii_lowercase().ends_with(".ase") { + if craft_file_formats::detect(path, &[]) == Some(craft_file_formats::Format::Ase) { return self.run("swatch.load", json!({"path": path})); } let document = opens_as_document(path) @@ -318,8 +318,10 @@ impl DesignApp { /// Files that open as documents (when dropped or picked) rather than being placed: DesignCraft, /// IDML, and InDesign documents and templates (converted to IDML on open). pub fn opens_as_document(name: &str) -> bool { - let n = name.to_ascii_lowercase(); - [".designcraft", ".idml", ".indd", ".indt"].iter().any(|ext| n.ends_with(ext)) + matches!( + craft_file_formats::detect(name, &[]), + Some(craft_file_formats::Format::DesignCraft | craft_file_formats::Format::Idml | craft_file_formats::Format::Indd) + ) } #[cfg(test)] diff --git a/crates/ui-egui/src/menus/export.rs b/crates/ui-egui/src/menus/export.rs index 2c1946d4f..a80a55fc0 100644 --- a/crates/ui-egui/src/menus/export.rs +++ b/crates/ui-egui/src/menus/export.rs @@ -92,7 +92,7 @@ pub(super) fn export_bytes(app: &mut DesignApp, p: &Value, ext: &str, cmd: &str) _ => json!({}), }; if cmd == "file.exportText" { - let txt = path.to_ascii_lowercase().ends_with(".txt"); + let txt = craft_file_formats::detect(&path, &[]) == Some(craft_file_formats::Format::PlainText); params["format"] = json!(if txt { "txt" } else { "rtf" }); } let r = app.run(cmd, params)?; diff --git a/crates/ui-egui/src/menus/run.rs b/crates/ui-egui/src/menus/run.rs index 731efb570..054bd8060 100644 --- a/crates/ui-egui/src/menus/run.rs +++ b/crates/ui-egui/src/menus/run.rs @@ -142,8 +142,7 @@ fn run_ui_part1(app: &mut DesignApp, id: &str, p: &Value) -> Option { // Word/RTF Import Options: style mapping and conflicts before the text lands. let Some(path) = p.get("path").and_then(Value::as_str) else { return Some(rerun_with_path(app, "place", id, p, "path")) }; - let l = path.to_lowercase(); - if !(l.ends_with(".docx") || l.ends_with(".rtf")) { + if !matches!(craft_file_formats::detect(path, &[]), Some(craft_file_formats::Format::Docx | craft_file_formats::Format::Rtf)) { return Some(app.place_file(json!({"path": path}))); } let info = match app.run("place.styles", json!({"path": path})) { From 44fd4aec60010932c40b0c75a450e88f8b8f8da4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:42:41 -0500 Subject: [PATCH 015/153] Route CLI format dispatch through shared registry --- Cargo.lock | 2 ++ apps/designcraft-cli/Cargo.toml | 1 + apps/designcraft-cli/src/main.rs | 6 +++--- crates/engine/src/cmd/book.rs | 2 +- crates/mcp/Cargo.toml | 1 + crates/mcp/src/headless.rs | 4 ++-- 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 92227fc93..891437aa0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1675,6 +1675,7 @@ dependencies = [ name = "designcraft-cli" version = "0.20.1" dependencies = [ + "craft-file-formats", "designcraft-color", "designcraft-compose", "designcraft-doc", @@ -1863,6 +1864,7 @@ dependencies = [ name = "designcraft-mcp" version = "0.20.1" dependencies = [ + "craft-file-formats", "designcraft-engine", "designcraft-geom", "designcraft-render", diff --git a/apps/designcraft-cli/Cargo.toml b/apps/designcraft-cli/Cargo.toml index ab2464147..edebc5e29 100644 --- a/apps/designcraft-cli/Cargo.toml +++ b/apps/designcraft-cli/Cargo.toml @@ -22,5 +22,6 @@ designcraft-compose = { workspace = true } designcraft-format = { workspace = true } designcraft-pdf = { workspace = true } designcraft-mcp = { workspace = true } +craft-file-formats = { workspace = true } image = { workspace = true } serde_json = { workspace = true } diff --git a/apps/designcraft-cli/src/main.rs b/apps/designcraft-cli/src/main.rs index 79997e0ae..23817bbe7 100644 --- a/apps/designcraft-cli/src/main.rs +++ b/apps/designcraft-cli/src/main.rs @@ -597,8 +597,8 @@ fn export(s: &mut Session, out: &str, page: usize, scale: f64) -> Result<(), Str let img = r .render_page(&st.doc, &s.cache, page, scale, true, &designcraft_render::RenderOptions { printing_only: true, ..Default::default() }) .ok_or("no such page")?; - let lower = out.to_lowercase(); - let bytes = if lower.ends_with(".jpg") || lower.ends_with(".jpeg") { img.to_jpeg(90) } else { img.to_png() }; + let jpeg = craft_file_formats::detect(out, &[]) == Some(craft_file_formats::Format::Jpeg); + let bytes = if jpeg { img.to_jpeg(90) } else { img.to_png() }; designcraft_engine::atomic::write_atomic(std::path::Path::new(out), &bytes).map_err(|e| format!("{out}: {e}"))?; eprintln!("wrote {out} ({}×{}, {:.1} ms, {} glyphs)", img.width, img.height, t.elapsed().as_secs_f64() * 1000.0, r.stats.glyphs); Ok(()) @@ -645,7 +645,7 @@ fn script(args: &[String]) -> Result<(), String> { match a.as_str() { "--in" => { let p = val()?; - let cmd = if p.to_lowercase().ends_with(".idml") { "file.openIdml" } else { "file.open" }; + let cmd = if craft_file_formats::detect(&p, &[]) == Some(craft_file_formats::Format::Idml) { "file.openIdml" } else { "file.open" }; setup.push((cmd.into(), json!({"path": p}))); } "--sample" => setup.push(("file.newSample".into(), json!({}))), diff --git a/crates/engine/src/cmd/book.rs b/crates/engine/src/cmd/book.rs index b1de3dac7..2b171d388 100644 --- a/crates/engine/src/cmd/book.rs +++ b/crates/engine/src/cmd/book.rs @@ -130,7 +130,7 @@ impl ChapterBatch { fn writable_chapter(s: &Session, path: &str, command: &'static str) -> Result<()> { use std::path::Path; - if Path::new(path).extension().and_then(std::ffi::OsStr::to_str).is_some_and(|ext| ext.eq_ignore_ascii_case("idml")) { + if Path::new(path).extension().and_then(std::ffi::OsStr::to_str).is_some_and(|ext| craft_file_formats::from_extension(ext) == Some(craft_file_formats::Format::Idml)) { return Err(bad(command, format!("cannot change imported IDML chapter {path}; convert it to a native DesignCraft document first"))); } let target = crate::io::canonicalize(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?; diff --git a/crates/mcp/Cargo.toml b/crates/mcp/Cargo.toml index fa863aa94..da6434f82 100644 --- a/crates/mcp/Cargo.toml +++ b/crates/mcp/Cargo.toml @@ -12,6 +12,7 @@ workspace = true designcraft-engine = { workspace = true } designcraft-render = { workspace = true } designcraft-tools = { workspace = true } +craft-file-formats = { workspace = true } designcraft-geom = { workspace = true } serde_json = { workspace = true } log = { workspace = true } diff --git a/crates/mcp/src/headless.rs b/crates/mcp/src/headless.rs index a81aba154..8023e41da 100644 --- a/crates/mcp/src/headless.rs +++ b/crates/mcp/src/headless.rs @@ -259,8 +259,8 @@ impl Headless { .renderer .render_page(&st.doc, &self.session.cache, page, scale, false, &RenderOptions { printing_only: true, ..Default::default() }) .ok_or_else(|| format!("no page {page}"))?; - let lower = path.to_ascii_lowercase(); - let bytes = if lower.ends_with(".jpg") || lower.ends_with(".jpeg") { img.to_jpeg(90) } else { img.to_png() }; + let jpeg = craft_file_formats::detect(path, &[]) == Some(craft_file_formats::Format::Jpeg); + let bytes = if jpeg { img.to_jpeg(90) } else { img.to_png() }; designcraft_engine::io::write_file(path, &bytes)?; Ok(json!({"path": path, "width": img.width, "height": img.height})) } From 3028b5328cca7f539bfec925b2d85561a37f0cfa Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:45:29 -0500 Subject: [PATCH 016/153] Format shared registry dispatch --- crates/engine/src/cmd/book.rs | 6 +++++- crates/ui-egui/src/app/files.rs | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/book.rs b/crates/engine/src/cmd/book.rs index 2b171d388..7a3995093 100644 --- a/crates/engine/src/cmd/book.rs +++ b/crates/engine/src/cmd/book.rs @@ -130,7 +130,11 @@ impl ChapterBatch { fn writable_chapter(s: &Session, path: &str, command: &'static str) -> Result<()> { use std::path::Path; - if Path::new(path).extension().and_then(std::ffi::OsStr::to_str).is_some_and(|ext| craft_file_formats::from_extension(ext) == Some(craft_file_formats::Format::Idml)) { + if Path::new(path) + .extension() + .and_then(std::ffi::OsStr::to_str) + .is_some_and(|ext| craft_file_formats::from_extension(ext) == Some(craft_file_formats::Format::Idml)) + { return Err(bad(command, format!("cannot change imported IDML chapter {path}; convert it to a native DesignCraft document first"))); } let target = crate::io::canonicalize(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?; diff --git a/crates/ui-egui/src/app/files.rs b/crates/ui-egui/src/app/files.rs index 61f0f9bbe..79353dcf6 100644 --- a/crates/ui-egui/src/app/files.rs +++ b/crates/ui-egui/src/app/files.rs @@ -139,8 +139,8 @@ impl DesignApp { } let layout = opens_as_document(&file.name) || (std::path::Path::new(&file.name).extension().is_none() && designcraft_engine::cmd::is_document(&file.bytes)); - let place = - file.request.purpose == "place" || (matches!(file.request.purpose.as_str(), "drop" | "open") && !layout && format != Some(craft_file_formats::Format::Ase)); + let place = file.request.purpose == "place" + || (matches!(file.request.purpose.as_str(), "drop" | "open") && !layout && format != Some(craft_file_formats::Format::Ase)); if place || format == Some(craft_file_formats::Format::Ase) || file.request.purpose == "swatches" { self.activate_import_target(&file.request)?; } From bfef83bfd1cb4fd501ed3e3bbb5f9ea0f1652e3d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:46:50 -0500 Subject: [PATCH 017/153] Use shared format probes for document opens --- Cargo.lock | 58 ++++++++++++++++++++-------- Cargo.toml | 2 +- crates/engine/src/cmd/file/bytes.rs | 5 ++- crates/engine/src/cmd/file/open.rs | 16 ++------ crates/engine/src/cmd/interchange.rs | 4 +- crates/ui-egui/src/app/files.rs | 5 ++- 6 files changed, 55 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 891437aa0..7610f1a70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1104,7 +1104,7 @@ name = "craft-document-bundle" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-zip", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "serde_json", "thiserror 2.0.21", "zip 9.1.1", @@ -1144,12 +1144,12 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b4ada28f1#b4ada28f19067d4693e902e0e5f32eb292241e3a" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" [[package]] name = "craft-epub" @@ -1157,17 +1157,21 @@ version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", - "craft-zip", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "thiserror 2.0.21", ] [[package]] name = "craft-file-formats" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b4ada28f1#b4ada28f19067d4693e902e0e5f32eb292241e3a" +source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" dependencies = [ - "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b4ada28f1)", - "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b4ada28f1)", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", + "serde", + "serde_json", ] [[package]] @@ -1204,7 +1208,17 @@ version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", - "craft-zip", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-idml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", "thiserror 2.0.21", ] @@ -1290,7 +1304,7 @@ version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", - "craft-zip", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "log", "thiserror 2.0.21", ] @@ -1362,9 +1376,9 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b4ada28f1#b4ada28f19067d4693e902e0e5f32eb292241e3a" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b4ada28f1)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "flate2", "kurbo", "roxmltree 0.21.2", @@ -1374,9 +1388,9 @@ dependencies = [ [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", "flate2", "kurbo", "roxmltree 0.21.2", @@ -1479,7 +1493,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b4ada28f1#b4ada28f19067d4693e902e0e5f32eb292241e3a" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1488,7 +1502,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1504,6 +1518,16 @@ dependencies = [ "zip 9.1.1", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + [[package]] name = "crc32fast" version = "1.5.3" @@ -1828,7 +1852,7 @@ dependencies = [ name = "designcraft-idml" version = "0.20.1" dependencies = [ - "craft-idml", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "designcraft-color", "designcraft-compose", @@ -1944,7 +1968,7 @@ version = "0.20.1" dependencies = [ "craft-docx", "craft-file-formats", - "craft-idml", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-rtf", "craft-wordmodel", "craft-xlsx", diff --git a/Cargo.toml b/Cargo.toml index 74359a740..bc7d95694 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe243 craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } -craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "b4ada28f1", version = "=0.1.0" } +craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "fccc0082711b8ae4e19824dd20339a8473d47309", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/engine/src/cmd/file/bytes.rs b/crates/engine/src/cmd/file/bytes.rs index f169b74e4..c5468d016 100644 --- a/crates/engine/src/cmd/file/bytes.rs +++ b/crates/engine/src/cmd/file/bytes.rs @@ -41,7 +41,10 @@ pub(super) fn without_native_extension(name: &str) -> &str { /// The first [`DOCUMENT_SNIFF_BYTES`] of a file are enough for packages that start with their /// `mimetype` entry, as both formats write them. pub fn is_document(bytes: &[u8]) -> bool { - designcraft_idml::is_idml(bytes) || designcraft_format::starts_designcraft(bytes) || designcraft_format::is_designcraft(bytes) + matches!( + craft_file_formats::detect("", bytes), + Some(craft_file_formats::Format::DesignCraft | craft_file_formats::Format::DesignCraftJson | craft_file_formats::Format::Idml) + ) } /// How much of a file [`is_document`] needs to recognise a package by its `mimetype` entry. diff --git a/crates/engine/src/cmd/file/open.rs b/crates/engine/src/cmd/file/open.rs index e88d75afc..e41a44135 100644 --- a/crates/engine/src/cmd/file/open.rs +++ b/crates/engine/src/cmd/file/open.rs @@ -85,13 +85,8 @@ fn open_path(s: &mut Session, p: &Value) -> Result { // The content decides first: a file's extension may be missing or wrong. The extension // then routes files whose content isn't recognised to the reader that explains why. let format = craft_file_formats::detect(path, &bytes); - let native = format == Some(craft_file_formats::Format::DesignCraft) - || designcraft_format::is_designcraft(&bytes) - || bytes.trim_ascii_start().starts_with(b"{"); - if format == Some(craft_file_formats::Format::Idml) - || designcraft_idml::is_idml(&bytes) - || (!native && format == Some(craft_file_formats::Format::Indd)) - { + let native = matches!(format, Some(craft_file_formats::Format::DesignCraft | craft_file_formats::Format::DesignCraftJson)); + if format == Some(craft_file_formats::Format::Idml) || (!native && format == Some(craft_file_formats::Format::Indd)) { return crate::cmd::interchange::open_idml(s, p); } if !native { @@ -163,11 +158,8 @@ pub(super) fn file_open_bytes(s: &mut Session, p: &Value) -> Result { // signature (or their name). let name = str_param(p, "name").unwrap_or_default(); let format = craft_file_formats::detect(name, &b); - let native = designcraft_format::is_designcraft(&b) || b.trim_ascii_start().starts_with(b"{"); - if format == Some(craft_file_formats::Format::Idml) - || designcraft_idml::is_idml(&b) - || (!native && format == Some(craft_file_formats::Format::Indd)) - { + let native = matches!(format, Some(craft_file_formats::Format::DesignCraft | craft_file_formats::Format::DesignCraftJson)); + if format == Some(craft_file_formats::Format::Idml) || (!native && format == Some(craft_file_formats::Format::Indd)) { return crate::cmd::interchange::open_idml(s, p); } let (mut d, warnings) = load_bytes(&b)?; diff --git a/crates/engine/src/cmd/interchange.rs b/crates/engine/src/cmd/interchange.rs index 0fba3b900..eefb46541 100644 --- a/crates/engine/src/cmd/interchange.rs +++ b/crates/engine/src/cmd/interchange.rs @@ -470,9 +470,7 @@ fn warning_item( /// the bytes are neither a recognized DesignCraft document nor IDML. The converter explains /// unrecognized data with an InDesign extension. pub(crate) fn is_indd(name: &str, bytes: &[u8]) -> bool { - craft_file_formats::detect(name, bytes) == Some(craft_file_formats::Format::Indd) - && !super::file::is_document(bytes) - && !bytes.trim_ascii_start().starts_with(b"{") + craft_file_formats::detect(name, bytes) == Some(craft_file_formats::Format::Indd) && !super::file::is_document(bytes) } /// `name` without a trailing .idml, .indd or .indt (any case). diff --git a/crates/ui-egui/src/app/files.rs b/crates/ui-egui/src/app/files.rs index 79353dcf6..3967c949e 100644 --- a/crates/ui-egui/src/app/files.rs +++ b/crates/ui-egui/src/app/files.rs @@ -138,7 +138,10 @@ impl DesignApp { return self.run("style.load", params); } let layout = opens_as_document(&file.name) - || (std::path::Path::new(&file.name).extension().is_none() && designcraft_engine::cmd::is_document(&file.bytes)); + || matches!( + format, + Some(craft_file_formats::Format::DesignCraft | craft_file_formats::Format::DesignCraftJson | craft_file_formats::Format::Idml) + ); let place = file.request.purpose == "place" || (matches!(file.request.purpose.as_str(), "drop" | "open") && !layout && format != Some(craft_file_formats::Format::Ase)); if place || format == Some(craft_file_formats::Format::Ase) || file.request.purpose == "swatches" { From 29e019895219486f7594aa8f4b8b640182a41f14 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:50:15 -0500 Subject: [PATCH 018/153] Delegate delimited text parsing to craft-csv --- Cargo.lock | 9 + Cargo.toml | 1 + crates/engine/Cargo.toml | 1 + crates/engine/src/cmd/datamerge/parse.rs | 250 +++++------------------ 4 files changed, 66 insertions(+), 195 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7610f1a70..3abdb8bc0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1081,6 +1081,14 @@ name = "craft-codepage" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +[[package]] +name = "craft-csv" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47bf5650b0d503b97bfb5e841746c4957897d11f#47bf5650b0d503b97bfb5e841746c4957897d11f" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-dialogs" version = "0.1.0" @@ -1764,6 +1772,7 @@ version = "0.20.1" dependencies = [ "caseless", "craft-atomic", + "craft-csv", "craft-docpath", "craft-file-formats", "craft-indd", diff --git a/Cargo.toml b/Cargo.toml index bc7d95694..159929912 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435 craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "fccc0082711b8ae4e19824dd20339a8473d47309", version = "=0.1.0" } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index ed9e7e3bc..48facb938 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -27,6 +27,7 @@ craft-prepress = { workspace = true } designcraft-tools = { workspace = true } designcraft-idml = { workspace = true } craft-file-formats = { workspace = true } +craft-csv = { workspace = true } craft-indd = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/engine/src/cmd/datamerge/parse.rs b/crates/engine/src/cmd/datamerge/parse.rs index dfc49e842..16ce57b0f 100644 --- a/crates/engine/src/cmd/datamerge/parse.rs +++ b/crates/engine/src/cmd/datamerge/parse.rs @@ -353,36 +353,20 @@ pub fn status_of(path: &Path, previous: Option) -> SourceStatus { } } -fn decode_utf8(bytes: &[u8], name: &str) -> Result { - let bytes = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes); - let mut text = String::from_utf8(bytes.to_vec()).map_err(|_| format!("{name} is not UTF-8"))?; - if text.starts_with('\u{FEFF}') { - text.replace_range(..'\u{FEFF}'.len_utf8(), ""); - } - Ok(text) +fn decode_utf8(bytes: &[u8], _name: &str) -> Result { + Ok(craft_csv::decode_text(bytes)) } fn sniff(name: &str, text: &str) -> Delimiter { - let ext = extension(name); - if ext == "tsv" || ext == "tab" { - return Delimiter::Tab; + match extension(name).as_str() { + "tsv" | "tab" => return Delimiter::Tab, + "csv" => return Delimiter::Comma, + _ => {} } - if ext == "csv" { - return Delimiter::Comma; - } - let tab = header_delimiter_candidate(text, '\t'); - let semicolon = header_delimiter_candidate(text, ';'); - let comma = header_delimiter_candidate(text, ','); - // Prefer formats with coherent quoted-field boundaries. If none separates fields, - // retain the legacy permissive choice; this is detection, not record validation. - let prefer_coherent = [&tab, &semicolon, &comma].iter().any(|c| c.found && c.coherent); - let eligible = |c: &HeaderDelimiterCandidate| c.found && (!prefer_coherent || c.coherent); - if eligible(&tab) { - Delimiter::Tab - } else if eligible(&semicolon) && !eligible(&comma) { - Delimiter::Semicolon - } else { - Delimiter::Comma + match craft_csv::sniff_delimiter(text, '"') { + b'\t' => Delimiter::Tab, + b';' => Delimiter::Semicolon, + _ => Delimiter::Comma, } } @@ -390,68 +374,6 @@ pub fn extension(name: &str) -> String { Path::new(name).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("").to_ascii_lowercase() } -struct HeaderDelimiterCandidate { - found: bool, - coherent: bool, -} - -/// A candidate separator outside quotes in its first nonempty logical record. -/// Only that candidate starts a new field. Quotes in an unquoted field stay literal; -/// Non-whitespace text after a quoted field closes lowers coherence, not validity. -fn header_delimiter_candidate(text: &str, delimiter: char) -> HeaderDelimiterCandidate { - let mut found = false; - let mut coherent = true; - let mut quoted = false; - let mut after_quote = false; - let mut field_start = true; - let mut nonempty = false; - let mut chars = text.chars().peekable(); - while let Some(c) = chars.next() { - if quoted { - if c == '"' { - if chars.peek() == Some(&'"') { - chars.next(); - nonempty = true; - } else { - quoted = false; - after_quote = true; - } - } else if !c.is_whitespace() { - nonempty = true; - } - continue; - } - match c { - '"' if field_start => { - quoted = true; - field_start = false; - } - separator if separator == delimiter => { - found = true; - field_start = true; - after_quote = false; - } - '\r' | '\n' => { - if nonempty { - break; - } - found = false; - coherent = true; - field_start = true; - after_quote = false; - } - _ => { - // Whitespace may follow a closing quote, but does not start a new field - // or allow a later non-whitespace suffix to regain coherence. - coherent &= !after_quote || c.is_whitespace(); - nonempty |= !c.is_whitespace(); - field_start = false; - } - } - } - HeaderDelimiterCandidate { found, coherent: coherent && !quoted } -} - /// Header cell: trim, drop one leading apostrophe, then `@` is image and `#` is qr. fn classify_header(raw: &str) -> Result<(String, DataFieldKind), String> { let trimmed = raw.trim(); @@ -511,121 +433,59 @@ fn table_from_raw(raw: &[RawRow]) -> Result { Ok(Table { fields, rows, warnings, delimiter: Delimiter::Comma }) } -/// Split `s` on `delim`. Quoted fields keep commas, doubled quotes, and newlines. -/// More than 200 header columns, or the 100,001st non-empty data row, is an error and -/// the oversized row is not kept. +/// Parse delimited text with the shared CSV grammar, then apply DesignCraft's table limits and row policy. fn parse_delimited(s: &str, delim: char) -> Result, String> { - let mut rows: Vec = Vec::new(); - let mut row: Vec = Vec::new(); - let mut field = String::new(); - let mut quoted = false; - let mut field_start = true; + let mut rows = Vec::new(); let mut line = 1usize; - let mut row_line = 1usize; - let mut data_rows = 0usize; - let mut header_width: Option = None; - let mut discarding = false; - let mut chars = s.chars().peekable(); - - let finish = |row: &mut Vec, - field: &mut String, - rows: &mut Vec, - data_rows: &mut usize, - header_width: &mut Option, - row_line: usize, - discarding: &mut bool| - -> Result<(), String> { - if !*discarding { - row.push(std::mem::take(field)); - } else { - field.clear(); - } - *discarding = false; - let empty = row.iter().all(|c| c.trim().is_empty()); - if empty { - row.clear(); - return Ok(()); - } - match *header_width { - None => { - if row.len() > DATA_MERGE_MAX_COLS { - return Err(format!("the file has {} columns; the limit is {DATA_MERGE_MAX_COLS}", row.len())); - } - *header_width = Some(row.len()); - rows.push(RawRow { line: row_line, cells: std::mem::take(row) }); - } - Some(width) => { - *data_rows = data_rows.saturating_add(1); - if *data_rows > DATA_MERGE_MAX_ROWS { - return Err(format!("the file has more than {DATA_MERGE_MAX_ROWS} data rows")); - } - if row.len() > width.saturating_add(1) { - row.truncate(width.saturating_add(1)); - } - rows.push(RawRow { line: row_line, cells: std::mem::take(row) }); - } - } - Ok(()) - }; - - while let Some(c) = chars.next() { - if c == '\n' || (c == '\r' && chars.peek() != Some(&'\n')) { - line = line.saturating_add(1); - } - if quoted { - match c { - '"' if chars.peek() == Some(&'"') => { - if !discarding { - field.push('"'); + let mut error = None; + craft_csv::parse_records(s, delim, '"', DATA_MERGE_MAX_COLS.saturating_add(1), |cells| { + let row_line = line; + let inside_newlines = cells + .iter() + .map(|cell| { + let mut count = 0usize; + let mut previous_cr = false; + for c in cell.chars() { + if c == '\r' { + count = count.saturating_add(1); + previous_cr = true; + } else if c == '\n' { + if !previous_cr { + count = count.saturating_add(1); + } + previous_cr = false; + } else { + previous_cr = false; } - chars.next(); } - '"' => quoted = false, - _ if !discarding => field.push(c), - _ => {} - } - continue; + count + }) + .sum::(); + line = line.saturating_add(inside_newlines).saturating_add(1); + if cells.iter().all(|cell| cell.trim().is_empty()) { + return true; } - if c == delim { - field_start = true; - if discarding { - field.clear(); - continue; - } - row.push(std::mem::take(&mut field)); - if header_width.is_none() && row.len() > DATA_MERGE_MAX_COLS { - return Err(format!("the file has {} columns; the limit is {DATA_MERGE_MAX_COLS}", row.len())); - } - if header_width.is_some_and(|w| row.len() > w.saturating_add(1)) { - row.pop(); - discarding = true; - } - continue; + if rows.is_empty() && cells.len() > DATA_MERGE_MAX_COLS { + error = Some(format!("the file has {} columns; the limit is {DATA_MERGE_MAX_COLS}", cells.len())); + return false; } - match c { - '"' if field_start => { - quoted = true; - field_start = false; - } - '\r' | '\n' => { - if c == '\r' && chars.peek() == Some(&'\n') { - chars.next(); - line = line.saturating_add(1); - } - finish(&mut row, &mut field, &mut rows, &mut data_rows, &mut header_width, row_line, &mut discarding)?; - row_line = line; - field_start = true; - } - _ => { - field_start = false; - if !discarding { - field.push(c); - } - } + if !rows.is_empty() && rows.len() > DATA_MERGE_MAX_ROWS { + error = Some(format!("the file has more than {DATA_MERGE_MAX_ROWS} data rows")); + return false; } + rows.push(RawRow { line: row_line, cells }); + true + }); + if let Some(error) = error { + return Err(error); } - if !field.is_empty() || !row.is_empty() || discarding { - finish(&mut row, &mut field, &mut rows, &mut data_rows, &mut header_width, row_line, &mut discarding)?; + let Some(width) = rows.first().map(|row| row.cells.len()) else { + return Ok(rows); + }; + for row in rows.iter_mut().skip(1) { + if row.cells.len() > width.saturating_add(1) { + row.cells.truncate(width.saturating_add(1)); + } } Ok(rows) } From a23b40e29aa6df1e9ff5506f82f34b0cfa4f2d47 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:52:51 -0500 Subject: [PATCH 019/153] Use shared CSV and TSV format mappings --- Cargo.lock | 42 ++++++++++++------------ Cargo.toml | 2 +- crates/engine/src/cmd/datamerge/parse.rs | 6 ++-- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3abdb8bc0..940657fd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1152,12 +1152,12 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" [[package]] name = "craft-epub" @@ -1172,12 +1172,12 @@ dependencies = [ [[package]] name = "craft-file-formats" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" +source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" dependencies = [ - "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", - "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", - "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", "serde", "serde_json", ] @@ -1213,20 +1213,20 @@ dependencies = [ [[package]] name = "craft-idml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", "thiserror 2.0.21", ] [[package]] name = "craft-idml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "thiserror 2.0.21", ] @@ -1384,9 +1384,9 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", "flate2", "kurbo", "roxmltree 0.21.2", @@ -1396,9 +1396,9 @@ dependencies = [ [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "flate2", "kurbo", "roxmltree 0.21.2", @@ -1501,7 +1501,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1510,7 +1510,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1519,7 +1519,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" dependencies = [ "flate2", "thiserror 2.0.21", @@ -1529,7 +1529,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=fccc0082711b8ae4e19824dd20339a8473d47309#fccc0082711b8ae4e19824dd20339a8473d47309" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "flate2", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index 159929912..606797d72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24 craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } -craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "fccc0082711b8ae4e19824dd20339a8473d47309", version = "=0.1.0" } +craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "71337f6772b57a61e0f171c941f695822eea19a5", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/engine/src/cmd/datamerge/parse.rs b/crates/engine/src/cmd/datamerge/parse.rs index 16ce57b0f..77dfb669a 100644 --- a/crates/engine/src/cmd/datamerge/parse.rs +++ b/crates/engine/src/cmd/datamerge/parse.rs @@ -358,9 +358,9 @@ fn decode_utf8(bytes: &[u8], _name: &str) -> Result { } fn sniff(name: &str, text: &str) -> Delimiter { - match extension(name).as_str() { - "tsv" | "tab" => return Delimiter::Tab, - "csv" => return Delimiter::Comma, + match craft_file_formats::detect(name, &[]) { + Some(craft_file_formats::Format::Tsv) => return Delimiter::Tab, + Some(craft_file_formats::Format::Csv) => return Delimiter::Comma, _ => {} } match craft_csv::sniff_delimiter(text, '"') { From 08a65d45a88d0b5b709dfcee3078f578c29e4b76 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:56:22 -0500 Subject: [PATCH 020/153] Route spreadsheet extensions through shared registry --- Cargo.lock | 26 ++++++++++++------------ Cargo.toml | 2 +- crates/engine/src/cmd/datamerge.rs | 9 ++++---- crates/engine/src/cmd/datamerge/parse.rs | 4 ---- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 940657fd2..6cd7ecf1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1152,7 +1152,7 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" [[package]] name = "craft-eps" @@ -1172,12 +1172,12 @@ dependencies = [ [[package]] name = "craft-file-formats" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" dependencies = [ - "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", - "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", - "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", "serde", "serde_json", ] @@ -1213,10 +1213,10 @@ dependencies = [ [[package]] name = "craft-idml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", "thiserror 2.0.21", ] @@ -1384,9 +1384,9 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", "flate2", "kurbo", "roxmltree 0.21.2", @@ -1501,7 +1501,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1519,7 +1519,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=71337f6772b57a61e0f171c941f695822eea19a5#71337f6772b57a61e0f171c941f695822eea19a5" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" dependencies = [ "flate2", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index 606797d72..ca146fb5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24 craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } -craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "71337f6772b57a61e0f171c941f695822eea19a5", version = "=0.1.0" } +craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/engine/src/cmd/datamerge.rs b/crates/engine/src/cmd/datamerge.rs index 7cb332fd3..93ceaf8cb 100644 --- a/crates/engine/src/cmd/datamerge.rs +++ b/crates/engine/src/cmd/datamerge.rs @@ -19,7 +19,7 @@ use serde_json::{Value, json}; use super::{CommandSpec, bad, cmd, has_doc, id_param, ok, str_param}; use crate::{DocState, Result, Session}; use fill::{FillReport, ImageCache, Maps, MissingImage, Sources, copy_item, fill_row, identity_map}; -use parse::{Table, TileInput, extension, relative_between, select_records, status_of, table_from_bytes, table_from_grid, table_from_objects, tile}; +use parse::{Table, TileInput, relative_between, select_records, status_of, table_from_bytes, table_from_grid, table_from_objects, tile}; #[cfg(not(target_arch = "wasm32"))] pub(crate) use parse::fingerprint_from; @@ -763,11 +763,10 @@ fn take_file(p: &Value) -> std::result::Result<(Vec, String, Option) } fn parse_file(bytes: &[u8], name: &str, delimiter: Option, sheet: Option<&str>) -> std::result::Result { - let ext = extension(name); - if ext == "xls" || ext == "xlsm" { - return Err(format!("{ext} workbooks are not supported")); + if craft_file_formats::detect(name, &[]) == Some(craft_file_formats::Format::LegacyExcel) { + return Err("legacy Excel .xls/.xlsm workbooks are not supported".into()); } - if ext == "xlsx" { + if craft_file_formats::detect(name, bytes) == Some(craft_file_formats::Format::Xlsx) { let grid = designcraft_textimport::xlsx_records(bytes, sheet).map_err(|e| e.to_string())?; return table_from_grid(grid, Delimiter::Comma); } diff --git a/crates/engine/src/cmd/datamerge/parse.rs b/crates/engine/src/cmd/datamerge/parse.rs index 77dfb669a..4a71223f0 100644 --- a/crates/engine/src/cmd/datamerge/parse.rs +++ b/crates/engine/src/cmd/datamerge/parse.rs @@ -370,10 +370,6 @@ fn sniff(name: &str, text: &str) -> Delimiter { } } -pub fn extension(name: &str) -> String { - Path::new(name).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("").to_ascii_lowercase() -} - /// Header cell: trim, drop one leading apostrophe, then `@` is image and `#` is qr. fn classify_header(raw: &str) -> Result<(String, DataFieldKind), String> { let trimmed = raw.trim(); From b6edf038090e520db1d0db07f10db703cf678845 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:57:32 -0500 Subject: [PATCH 021/153] Delegate PPTX ZIP packaging to craft-opc --- Cargo.lock | 16 ++++++++++++++-- Cargo.toml | 1 + crates/pptx/Cargo.toml | 3 ++- crates/pptx/src/package.rs | 28 +++++----------------------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6cd7ecf1b..4489cddd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1128,7 +1128,7 @@ dependencies = [ "craft-drawingml", "craft-math", "craft-offcrypto", - "craft-opc", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-wordmodel", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "log", @@ -1306,6 +1306,17 @@ dependencies = [ "zeroize", ] +[[package]] +name = "craft-opc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "log", + "thiserror 2.0.21", +] + [[package]] name = "craft-opc" version = "0.1.0" @@ -1492,7 +1503,7 @@ version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ "craft-drawingml", - "craft-opc", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-sheet", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "thiserror 2.0.21", @@ -1938,6 +1949,7 @@ name = "designcraft-pptx" version = "0.20.1" dependencies = [ "chrono", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", "designcraft-color", "designcraft-compose", "designcraft-doc", diff --git a/Cargo.toml b/Cargo.toml index ca146fb5e..9dbba9581 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0 craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } +craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/pptx/Cargo.toml b/crates/pptx/Cargo.toml index afc90c69c..c81e72d48 100644 --- a/crates/pptx/Cargo.toml +++ b/crates/pptx/Cargo.toml @@ -18,9 +18,10 @@ designcraft-images = { workspace = true } skrifa = { workspace = true } image = { workspace = true } thiserror = { workspace = true } -zip = { workspace = true } +craft-opc = { workspace = true } chrono = { workspace = true } [dev-dependencies] +zip = { workspace = true } designcraft-fonts = { workspace = true, features = ["testing"] } quick-xml = { workspace = true } diff --git a/crates/pptx/src/package.rs b/crates/pptx/src/package.rs index 7e9e123e0..5ff1fa160 100644 --- a/crates/pptx/src/package.rs +++ b/crates/pptx/src/package.rs @@ -1,11 +1,9 @@ //! The PresentationML package: one blank master and layout, a theme, the slides and their media. -use std::fmt::Write as _; -use std::io::Write as _; - use crate::fonts::EmbeddedFont; use crate::xml::{emu_len, esc}; use crate::{PptxError, Result}; +use std::fmt::Write as _; pub const NS: &str = r#"xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main""#; const DECL: &str = "\n"; @@ -241,26 +239,10 @@ pub fn write(p: &Package) -> Result> { ), ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let deflate = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); - let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); - let zerr = |e: zip::result::ZipError| PptxError::Write(e.to_string()); - let ioerr = |e: std::io::Error| PptxError::Write(e.to_string()); - for (name, bytes) in &parts { - zw.start_file(name.as_str(), deflate).map_err(zerr)?; - zw.write_all(bytes).map_err(ioerr)?; - } - for (name, bytes) in &font_parts { - zw.start_file(name.as_str(), deflate).map_err(zerr)?; - zw.write_all(bytes).map_err(ioerr)?; - } - for m in p.media { - // Compressed formats gain nothing from deflate. - let opts = if m.name.ends_with(".svg") { deflate } else { stored }; - zw.start_file(format!("ppt/media/{}", m.name), opts).map_err(zerr)?; - zw.write_all(&m.bytes).map_err(ioerr)?; - } - Ok(zw.finish().map_err(zerr)?.into_inner()) + let mut entries = parts; + entries.extend(font_parts.into_iter().map(|(name, bytes)| (name, bytes.to_vec()))); + entries.extend(p.media.iter().map(|m| (format!("ppt/media/{}", m.name), m.bytes.clone()))); + craft_opc::zip_entries(&entries).map_err(|e| PptxError::Write(e.to_string())) } fn rel_kind(k: &str) -> &'static str { From 32c239caccc59ff8819f2717b141d7f7f84a5a71 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:59:00 -0500 Subject: [PATCH 022/153] Use shared image codecs for CLI previews --- Cargo.lock | 1 + apps/designcraft-cli/Cargo.toml | 1 + apps/designcraft-cli/src/main.rs | 21 +++++++++++---------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4489cddd5..8fdfd50b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1719,6 +1719,7 @@ name = "designcraft-cli" version = "0.20.1" dependencies = [ "craft-file-formats", + "craft-image-codecs", "designcraft-color", "designcraft-compose", "designcraft-doc", diff --git a/apps/designcraft-cli/Cargo.toml b/apps/designcraft-cli/Cargo.toml index edebc5e29..d214b9d84 100644 --- a/apps/designcraft-cli/Cargo.toml +++ b/apps/designcraft-cli/Cargo.toml @@ -23,5 +23,6 @@ designcraft-format = { workspace = true } designcraft-pdf = { workspace = true } designcraft-mcp = { workspace = true } craft-file-formats = { workspace = true } +craft-image-codecs = { workspace = true } image = { workspace = true } serde_json = { workspace = true } diff --git a/apps/designcraft-cli/src/main.rs b/apps/designcraft-cli/src/main.rs index 23817bbe7..1e39e906e 100644 --- a/apps/designcraft-cli/src/main.rs +++ b/apps/designcraft-cli/src/main.rs @@ -319,16 +319,17 @@ fn names_the_same_file(input: &std::path::Path, out: &std::path::Path) -> bool { /// as it is; it is decoded anyway, so a broken one is an error rather than an unreadable thumbnail. fn fit_png(png: Vec, size: Option) -> Result, String> { // The preview comes from the file: it may claim any size. - let mut limits = image::Limits::default(); - limits.max_image_width = Some(4096); - limits.max_image_height = Some(4096); - let mut reader = image::ImageReader::with_format(std::io::Cursor::new(&png), image::ImageFormat::Png); - reader.limits(limits); - let img = reader.decode().map_err(|e| e.to_string())?; - let Some(size) = size.filter(|s| img.width().max(img.height()) > *s) else { return Ok(png) }; - let mut out = std::io::Cursor::new(Vec::new()); - img.resize(size, size, image::imageops::FilterType::Triangle).write_to(&mut out, image::ImageFormat::Png).map_err(|e| e.to_string())?; - Ok(out.into_inner()) + let info = craft_image_codecs::inspect_raster(&png).map_err(|e| e.to_string())?.ok_or("invalid PNG preview")?; + if info.width > 4096 || info.height > 4096 { + return Err("preview is larger than 4096 pixels".into()); + } + let decoded = craft_image_codecs::decode(&png).map_err(|e| e.to_string())?; + let Some(size) = size.filter(|s| decoded.width().max(decoded.height()) > *s) else { return Ok(png) }; + let rgba = image::RgbaImage::from_raw(decoded.width(), decoded.height(), decoded.to_rgba8()).ok_or("invalid PNG pixel data")?; + let resized = image::DynamicImage::ImageRgba8(rgba).resize(size, size, image::imageops::FilterType::Triangle).to_rgba8(); + let encoded = craft_image_codecs::Image::from_u8(resized.width(), resized.height(), craft_image_codecs::ChannelLayout::Rgba, resized.into_raw()) + .map_err(|e| e.to_string())?; + craft_image_codecs::encode(&encoded, craft_image_codecs::Format::Png, &Default::default()).map_err(|e| e.to_string()) } /// `mcp` (headless, in-process engine) or `mcp --connect PORT|HOST:PORT` (drive a running app From 3eaba67f5853a2f268fef77b72f2e901bdc43fda Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:05:13 -0500 Subject: [PATCH 023/153] Fix Clippy range loop in path orientation --- crates/engine/src/cmd/paths.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/paths.rs b/crates/engine/src/cmd/paths.rs index 7800b3189..b3f8d3300 100644 --- a/crates/engine/src/cmd/paths.rs +++ b/crates/engine/src/cmd/paths.rs @@ -79,11 +79,13 @@ fn spread_xf(d: &Document, id: ItemId) -> Option<(SpreadRef, Affine)> { /// non-zero rule they become holes. fn orient_holes(path: &mut PathData) { let beziers: Vec = path.subpaths.iter().map(|s| PathData::single(s.clone()).to_bezpath()).collect(); - let n = path.subpaths.len(); - for i in 0..n { - let Some(p) = path.subpaths[i].anchors.first().map(|a| a.p) else { continue }; - let depth = (0..n).filter(|&j| j != i && designcraft_geom::hit::fill_contains(&beziers[j], designcraft_geom::FillRule::NonZero, p)).count(); - let sp = &mut path.subpaths[i]; + for (i, sp) in path.subpaths.iter_mut().enumerate() { + let Some(p) = sp.anchors.first().map(|a| a.p) else { continue }; + let depth = beziers + .iter() + .enumerate() + .filter(|(j, bezier)| *j != i && designcraft_geom::hit::fill_contains(bezier, designcraft_geom::FillRule::NonZero, p)) + .count(); let cw = sp.area() > 0.0; if cw == (depth % 2 == 1) { sp.reverse(); From 916749b2d0329cc90b881bdb35873061fc516bf6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:13:35 -0500 Subject: [PATCH 024/153] Delegate XML parsing and PPTX writing to craft-libs --- Cargo.lock | 32 +++++++++++++- Cargo.toml | 1 + crates/engine/Cargo.toml | 1 + crates/engine/src/cmd/xml.rs | 81 +++++++++++++----------------------- crates/pptx/Cargo.toml | 1 + crates/pptx/src/lib.rs | 8 +++- 6 files changed, 69 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8fdfd50b1..772bf2185 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1125,7 +1125,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 dependencies = [ "base64 0.23.1", "chrono", - "craft-drawingml", + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-math", "craft-offcrypto", "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", @@ -1138,6 +1138,17 @@ dependencies = [ "zeroize", ] +[[package]] +name = "craft-drawingml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "kurbo", + "log", + "serde", +] + [[package]] name = "craft-drawingml" version = "0.1.0" @@ -1341,6 +1352,21 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-pptx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +dependencies = [ + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "kurbo", + "log", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-prepress" version = "0.1.0" @@ -1502,7 +1528,7 @@ name = "craft-xlsx" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-drawingml", + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "craft-sheet", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", @@ -1790,6 +1816,7 @@ dependencies = [ "craft-indd", "craft-prepress", "craft-print", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "designcraft-color", "designcraft-compose", "designcraft-doc", @@ -1951,6 +1978,7 @@ version = "0.20.1" dependencies = [ "chrono", "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-pptx", "designcraft-color", "designcraft-compose", "designcraft-doc", diff --git a/Cargo.toml b/Cargo.toml index 9dbba9581..f8a7a7c0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } +craft-pptx = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 48facb938..57d196a62 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -34,6 +34,7 @@ serde_json = { workspace = true } thiserror = { workspace = true } log = { workspace = true } quick-xml = { workspace = true } +craft-xml = { workspace = true } caseless = { workspace = true } unicode-segmentation = { workspace = true } diff --git a/crates/engine/src/cmd/xml.rs b/crates/engine/src/cmd/xml.rs index 81b57b5b3..71b71c804 100644 --- a/crates/engine/src/cmd/xml.rs +++ b/crates/engine/src/cmd/xml.rs @@ -131,61 +131,38 @@ struct Element { } fn parse(xml: &str) -> std::result::Result, String> { - use quick_xml::events::Event; - let mut r = quick_xml::Reader::from_str(xml); - let mut depth = 0usize; - let mut out: Vec = Vec::new(); - let mut child: Option<(String, String)> = None; - loop { - match r.read_event().map_err(|e| e.to_string())? { - Event::Start(e) => { - depth += 1; - let name = String::from_utf8_lossy(e.local_name().as_ref()).to_string(); - match depth { - 2 => out.push(Element { name, text: String::new(), children: vec![] }), - 3 => child = Some((name, String::new())), - _ => {} - } + fn descendant_text(element: &craft_xml::El, into: &mut String) { + for node in &element.children { + match node { + craft_xml::Node::Text(text) | craft_xml::Node::CData(text) => into.push_str(text), + craft_xml::Node::El(child) => descendant_text(child, into), + craft_xml::Node::Pi { .. } | craft_xml::Node::Comment(_) => {} } - Event::Empty(e) if depth == 1 => { - out.push(Element { name: String::from_utf8_lossy(e.local_name().as_ref()).to_string(), text: String::new(), children: vec![] }); - } - ev @ (Event::Text(_) | Event::GeneralRef(_) | Event::CData(_)) => { - let s = match ev { - Event::Text(t) => t.decode().map_err(|e| e.to_string())?.to_string(), - Event::CData(t) => String::from_utf8_lossy(&t).to_string(), - Event::GeneralRef(g) => match g.resolve_char_ref().map_err(|e| e.to_string())? { - Some(c) => c.to_string(), - None => match g.decode().map_err(|e| e.to_string())?.as_ref() { - "amp" => "&".into(), - "lt" => "<".into(), - "gt" => ">".into(), - "quot" => "\"".into(), - "apos" => "'".into(), - _ => String::new(), - }, - }, - _ => String::new(), - }; - match (depth, child.as_mut(), out.last_mut()) { - (3.., Some(c), _) => c.1.push_str(&s), - (2, _, Some(el)) => el.text.push_str(&s), - _ => {} - } - } - Event::End(_) => { - if depth == 3 - && let (Some(c), Some(el)) = (child.take(), out.last_mut()) - { - el.children.push(c); - } - depth = depth.saturating_sub(1); - } - Event::Eof => break, - _ => {} } } - Ok(out) + + let mut options = craft_xml::Options::local(); + options.strict = true; + let root = options.parse(xml.as_bytes()).map_err(|e| e.to_string())?; + root.elements() + .map(|element| { + let mut children = Vec::new(); + for child in element.elements() { + let mut text = String::new(); + descendant_text(child, &mut text); + children.push((child.name.clone(), text)); + } + let mut text = String::new(); + for node in &element.children { + match node { + craft_xml::Node::Text(value) | craft_xml::Node::CData(value) => text.push_str(value), + craft_xml::Node::Pi { .. } | craft_xml::Node::Comment(_) => {} + craft_xml::Node::El(_) => {} + } + } + Ok(Element { name: element.name.clone(), text, children }) + }) + .collect() } pub fn specs() -> Vec { diff --git a/crates/pptx/Cargo.toml b/crates/pptx/Cargo.toml index c81e72d48..be8b19a4f 100644 --- a/crates/pptx/Cargo.toml +++ b/crates/pptx/Cargo.toml @@ -19,6 +19,7 @@ skrifa = { workspace = true } image = { workspace = true } thiserror = { workspace = true } craft-opc = { workspace = true } +craft-pptx = { workspace = true } chrono = { workspace = true } [dev-dependencies] diff --git a/crates/pptx/src/lib.rs b/crates/pptx/src/lib.rs index e05d05117..6cdd3e600 100644 --- a/crates/pptx/src/lib.rs +++ b/crates/pptx/src/lib.rs @@ -117,7 +117,11 @@ pub fn export_pptx(doc: &Document, cache: &Cache, opts: &PptxOptions) -> Result< Vec::new() }; let title = opts.title.clone().unwrap_or_else(|| doc.title.clone()); - let bytes = package::write(&package::Package { + // Build a compatibility package from DesignCraft's composed-page mapping, then hand the + // document model and final OOXML serialization to the shared PresentationML implementation. + // This keeps the existing geometry, text, table and media mapping intact while the mapping + // moves from DrawingML fragments to craft_pptx model values. + let mapped = package::write(&package::Package { size, slides: &slides, media: &ex.media, @@ -126,5 +130,7 @@ pub fn export_pptx(doc: &Document, cache: &Cache, opts: &PptxOptions) -> Result< author: opts.author.as_deref().unwrap_or(""), created: opts.created, })?; + let presentation = craft_pptx::import(&mapped, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; + let bytes = craft_pptx::export(&presentation, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; Ok(ExportReport { bytes, slides: slides.len(), warnings: ex.warnings }) } From 2a6918255e01c4785aa4af5e43375e3edc189a4b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:18:53 -0500 Subject: [PATCH 025/153] Map simple PPTX shapes to shared presentation models --- crates/pptx/src/lib.rs | 16 ++++- crates/pptx/src/package.rs | 2 + crates/pptx/src/slide.rs | 126 +++++++++++++++++++++++++++++++++++-- 3 files changed, 138 insertions(+), 6 deletions(-) diff --git a/crates/pptx/src/lib.rs b/crates/pptx/src/lib.rs index 6cdd3e600..04b11ac58 100644 --- a/crates/pptx/src/lib.rs +++ b/crates/pptx/src/lib.rs @@ -80,6 +80,16 @@ pub enum PptxError { pub type Result = std::result::Result; +fn apply_native_shapes(shapes: &mut [craft_pptx::Shape], native: &[craft_pptx::Shape]) { + for shape in shapes { + if let Some(mapped) = native.iter().find(|mapped| mapped.id.0 == shape.id.0) { + *shape = mapped.clone(); + } else if let craft_pptx::ShapeKind::Group { children, .. } = &mut shape.kind { + apply_native_shapes(children, native); + } + } +} + /// Export `doc` as a PowerPoint presentation. pub fn export_pptx(doc: &Document, cache: &Cache, opts: &PptxOptions) -> Result { let count = doc.page_count(); @@ -130,7 +140,11 @@ pub fn export_pptx(doc: &Document, cache: &Cache, opts: &PptxOptions) -> Result< author: opts.author.as_deref().unwrap_or(""), created: opts.created, })?; - let presentation = craft_pptx::import(&mapped, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; + let mut presentation = craft_pptx::import(&mapped, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; + for (source, slide) in slides.iter().zip(&mut presentation.slides) { + let slide = std::sync::Arc::make_mut(slide); + apply_native_shapes(&mut slide.shapes, &source.native_shapes); + } let bytes = craft_pptx::export(&presentation, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; Ok(ExportReport { bytes, slides: slides.len(), warnings: ex.warnings }) } diff --git a/crates/pptx/src/package.rs b/crates/pptx/src/package.rs index 5ff1fa160..3b4b7774b 100644 --- a/crates/pptx/src/package.rs +++ b/crates/pptx/src/package.rs @@ -27,6 +27,8 @@ pub struct Slide { pub shapes: String, pub rels: Vec, pub name: String, + /// Shapes that have a direct DesignCraft-to-Presentation model mapping. + pub native_shapes: Vec, } /// A media part (`ppt/media/`). diff --git a/crates/pptx/src/slide.rs b/crates/pptx/src/slide.rs index ec278c6c6..06ee06b82 100644 --- a/crates/pptx/src/slide.rs +++ b/crates/pptx/src/slide.rs @@ -105,6 +105,7 @@ pub fn box_of(inner: Rect, m: Affine) -> Option<(XfrmBox, Decomposed)> { pub struct Geometry { pub xfrm: XfrmBox, pub xml: String, + pub model: craft_pptx::Geom, } fn custom_geometry(bp: &BezPath, w: f64, h: f64, filled: bool) -> String { @@ -171,23 +172,31 @@ pub fn geometry(it: &Item, m: Affine) -> Geometry { let inner = bp.bounding_box(); if let Some((xfrm, dm)) = box_of(inner, m) { if it.corners.is_none() && is_rect(&bp, inner) { - return Geometry { xfrm, xml: preset("rect") }; + return Geometry { xfrm, xml: preset("rect"), model: craft_pptx::Geom::preset("rect") }; } if it.shape == Shape::Oval && it.corners.is_none() && closed { - return Geometry { xfrm, xml: preset("ellipse") }; + return Geometry { xfrm, xml: preset("ellipse"), model: craft_pptx::Geom::preset("ellipse") }; } // The path in the box's own space: scaled (not flipped: the box flips), from its corner. let (kx, ky) = (dm.sx.abs(), dm.sy.abs()); let mut local = bp.clone(); local.apply_affine(Affine::scale_non_uniform(kx, ky) * Affine::translate(-inner.origin().to_vec2())); - return Geometry { xfrm, xml: custom_geometry(&local, xfrm.rect.width(), xfrm.rect.height(), closed) }; + return Geometry { + xfrm, + xml: custom_geometry(&local, xfrm.rect.width(), xfrm.rect.height(), closed), + model: craft_pptx::Geom::Custom { paths: vec![craft_pptx::CustomPath::from_bez(&local, xfrm.rect.width(), xfrm.rect.height(), closed)] }, + }; } // Sheared: the transformed path in slide space. let mut pb = bp; pb.apply_affine(m); let b = pb.bounding_box(); pb.apply_affine(Affine::translate(-b.origin().to_vec2())); - Geometry { xfrm: XfrmBox::plain(b), xml: custom_geometry(&pb, b.width(), b.height(), closed) } + Geometry { + xfrm: XfrmBox::plain(b), + xml: custom_geometry(&pb, b.width(), b.height(), closed), + model: craft_pptx::Geom::Custom { paths: vec![craft_pptx::CustomPath::from_bez(&pb, b.width(), b.height(), closed)] }, + } } pub struct Exporter<'a> { @@ -196,6 +205,8 @@ pub struct Exporter<'a> { pub opts: &'a PptxOptions, pub warnings: Vec, pub media: Vec, + /// Native PresentationML mappings for model elements the exporter can express directly. + pub native_shapes: Vec, /// Asset → (picture media name, SVG media name). media_of: HashMap)>>, pub fonts: Fonts, @@ -220,6 +231,7 @@ impl<'a> Exporter<'a> { opts, warnings: Vec::new(), media: Vec::new(), + native_shapes: Vec::new(), media_of: HashMap::new(), fonts: Fonts::default(), out: String::new(), @@ -252,6 +264,7 @@ impl<'a> Exporter<'a> { let page = sp.pages.get(pi).ok_or(PptxError::BadPage(abs + 1))?; let bounds = page.bounds(); self.out.clear(); + self.native_shapes.clear(); self.next_id = 2; self.rels.clear(); self.rel_of.clear(); @@ -295,6 +308,7 @@ impl<'a> Exporter<'a> { shapes: std::mem::take(&mut self.out), rels: std::mem::take(&mut self.rels), name: page_name, + native_shapes: std::mem::take(&mut self.native_shapes), }) } @@ -390,6 +404,11 @@ impl<'a> Exporter<'a> { return; } let id = self.id(); + if text.is_none() + && let Some(shape) = self.native_shape(it, &g, id, opacity) + { + self.native_shapes.push(shape); + } let txbox = if text.is_some() && it.fill.is_none() && it.stroke.is_none() { " txBox=\"1\"" } else { "" }; let _ = write!( self.out, @@ -402,6 +421,103 @@ impl<'a> Exporter<'a> { ); } + /// A shape whose complete appearance fits the shared PresentationML model. + fn native_shape(&self, it: &Item, geometry: &Geometry, id: u32, opacity: f32) -> Option { + if it.effects != designcraft_doc::Effects::default() || it.fill.overprint || it.stroke.overprint || it.stroke.gap_overprint { + return None; + } + let fill = if it.fill.is_none() || !it.path.is_closed() { + craft_pptx::Fill::None + } else { + if resolve_gradient(&self.doc.swatches, &it.fill.swatch).is_some() { + return None; + } + match self.doc.resolve_color(&it.fill.swatch, it.fill.tint) { + Some(color) => { + let [r, g, b, a] = color.to_rgba8(opacity); + craft_pptx::Fill::solid(craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a))) + } + None => craft_pptx::Fill::None, + } + }; + let line = if it.stroke.is_none() { + craft_pptx::Line::none() + } else { + let stroke = &it.stroke; + let supported_dash = matches!(&stroke.kind, StrokeType::Solid | StrokeType::Dotted | StrokeType::Dashed { .. }); + if stroke.start != Arrowhead::None + || stroke.end != Arrowhead::None + || stroke.gradient_angle.is_some() + || stroke.gradient_vector.is_some() + || !supported_dash + { + return None; + } + if resolve_gradient(&self.doc.swatches, &stroke.swatch).is_some() { + return None; + } + let color = self.doc.resolve_color(&stroke.swatch, stroke.tint)?; + let [r, g, b, a] = color.to_rgba8(opacity); + let mut line = craft_pptx::Line::solid(craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a)), stroke.weight); + line.cap = Some(match stroke.cap { + Cap::Butt => craft_pptx::style::LineCap::Flat, + Cap::Round => craft_pptx::style::LineCap::Round, + Cap::Projecting => craft_pptx::style::LineCap::Square, + }); + line.join = Some(match stroke.join { + Join::Miter => craft_pptx::style::LineJoin::Miter, + Join::Round => craft_pptx::style::LineJoin::Round, + Join::Bevel => craft_pptx::style::LineJoin::Bevel, + }); + line.align = Some(match stroke.align { + StrokeAlign::Inside => craft_pptx::style::LineAlign::Inside, + StrokeAlign::Center | StrokeAlign::Outside => craft_pptx::style::LineAlign::Center, + }); + if stroke.join == Join::Miter && (stroke.miter_limit - 4.0).abs() > 0.001 { + return None; + } + match &stroke.kind { + StrokeType::Solid => {} + StrokeType::Dotted => line.dash = Some(craft_pptx::style::Dash::Preset { name: "sysDot".into() }), + StrokeType::Dashed { pattern } => { + if pattern.iter().any(|length| *length > 0.0) { + let mut pattern = pattern.clone(); + if pattern.len() % 2 == 1 { + pattern.extend(pattern.clone()); + } + let pairs = pattern + .chunks(2) + .filter_map(|pair| pair.first().zip(pair.get(1))) + .map(|(dash, gap)| (*dash / stroke.weight, *gap / stroke.weight)) + .collect(); + line.dash = Some(craft_pptx::style::Dash::Custom { pattern: pairs }); + } + } + _ => return None, + } + line + }; + let xfrm = craft_pptx::Xfrm { + x: geometry.xfrm.rect.x0, + y: geometry.xfrm.rect.y0, + w: geometry.xfrm.rect.width(), + h: geometry.xfrm.rect.height(), + rot: geometry.xfrm.rot, + flip_h: geometry.xfrm.flip_h, + flip_v: geometry.xfrm.flip_v, + }; + Some(craft_pptx::Shape { + id: craft_pptx::ShapeId(id), + name: name_of(it, id), + xfrm: Some(xfrm), + geom: geometry.model.clone(), + fill: Some(fill), + line: Some(line), + descr: it.alt_text.clone(), + ..Default::default() + }) + } + /// An sRGB colour element for a swatch and tint, `None` for [None] and unknown swatches. pub fn color(&self, swatch: &str, tint: f32, alpha: f32) -> Option { if let Some(g) = resolve_gradient(&self.doc.swatches, swatch) { @@ -652,7 +768,7 @@ impl<'a> Exporter<'a> { let gi = Rect::new(0.0, 0.0, g.size.0, g.size.1); let gm = m * g.xf; let xfrm = box_of(gi, gm).map(|(x, _)| x).unwrap_or_else(|| XfrmBox::plain(gm.transform_rect_bbox(gi))); - (Geometry { xfrm, xml: preset("rect") }, String::new()) + (Geometry { xfrm, xml: preset("rect"), model: craft_pptx::Geom::preset("rect") }, String::new()) }; let rid = self.rel(format!("../media/{name}")); let svg_ext = match svg { From 304f93dda84319aefa0ae5bae426bffbdbcd9d44 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:24:12 -0500 Subject: [PATCH 026/153] Map composed PPTX text to shared presentation models --- crates/pptx/src/slide.rs | 13 ++- crates/pptx/src/text.rs | 171 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 180 insertions(+), 4 deletions(-) diff --git a/crates/pptx/src/slide.rs b/crates/pptx/src/slide.rs index 06ee06b82..c9f864523 100644 --- a/crates/pptx/src/slide.rs +++ b/crates/pptx/src/slide.rs @@ -395,6 +395,15 @@ impl<'a> Exporter<'a> { /// A shape with the item's geometry, fill, stroke and effects; `text` is a ``. pub fn shape(&mut self, it: &Item, xf: Affine, opacity: f32, text: Option<&str>) { + self.shape_inner(it, xf, opacity, text, None); + } + + /// A text-bearing shape with both its compatibility fragment and shared model representation. + pub fn shape_with_text_model(&mut self, it: &Item, xf: Affine, opacity: f32, text: &str, model: craft_pptx::TextBody) { + self.shape_inner(it, xf, opacity, Some(text), Some(model)); + } + + fn shape_inner(&mut self, it: &Item, xf: Affine, opacity: f32, text: Option<&str>, model_text: Option) { let m = self.to_slide * xf; let g = geometry(it, m); let fill = self.fill(&it.fill, it.path.is_closed(), opacity); @@ -404,9 +413,11 @@ impl<'a> Exporter<'a> { return; } let id = self.id(); - if text.is_none() + if (text.is_none() || model_text.is_some()) && let Some(shape) = self.native_shape(it, &g, id, opacity) { + let mut shape = shape; + shape.text = model_text; self.native_shapes.push(shape); } let txbox = if text.is_some() && it.fill.is_none() && it.stroke.is_none() { " txBox=\"1\"" } else { "" }; diff --git a/crates/pptx/src/text.rs b/crates/pptx/src/text.rs index 005e651a5..a03991729 100644 --- a/crates/pptx/src/text.rs +++ b/crates/pptx/src/text.rs @@ -11,6 +11,7 @@ use std::fmt::Write as _; +use designcraft_color::swatch::resolve_gradient; use designcraft_compose::{ComposedStory, FrameText, Line, PlacedGlyph, RunStyle}; use designcraft_doc::{Align, Item, TextFrame}; use designcraft_geom::{Affine, Rect}; @@ -127,7 +128,8 @@ pub fn frame(ex: &mut Exporter, it: &Item, tfr: &TextFrame, xf: Affine, opacity: let insets = [r.x0 - inner.x0, r.y0 - inner.y0, inner.x1 - r.x1, inner.y1 - r.y1]; if insets.iter().all(|v| *v >= -0.01) { let body = body(ex, seg, r, k, opacity, insets); - ex.shape(it, xf, opacity, Some(&body)); + let model = model_body(ex, seg, k, opacity, insets); + ex.shape_with_text_model(it, xf, opacity, &body, model); return; } } @@ -212,11 +214,31 @@ fn text_box(ex: &mut Exporter, seg: &Segment, m: Affine, k: f64, opacity: f32, n let r = seg_rect(seg); let Some((xfrm, _)) = box_of(r, m) else { return }; let body = body(ex, seg, r, k, opacity, [0.0; 4]); - write_box(ex, xfrm, &body, name); + let model = model_body(ex, seg, k, opacity, [0.0; 4]); + write_box(ex, xfrm, &body, model, name); } -fn write_box(ex: &mut Exporter, xfrm: XfrmBox, body: &str, name: &str) { +fn write_box(ex: &mut Exporter, xfrm: XfrmBox, body: &str, model: craft_pptx::TextBody, name: &str) { let id = ex.id(); + ex.native_shapes.push(craft_pptx::Shape { + id: craft_pptx::ShapeId(id), + name: format!("{name} {id}"), + xfrm: Some(craft_pptx::Xfrm { + x: xfrm.rect.x0, + y: xfrm.rect.y0, + w: xfrm.rect.width(), + h: xfrm.rect.height(), + rot: xfrm.rot, + flip_h: xfrm.flip_h, + flip_v: xfrm.flip_v, + }), + geom: craft_pptx::Geom::preset("rect"), + fill: Some(craft_pptx::Fill::None), + line: Some(craft_pptx::Line::none()), + text_box: true, + text: Some(model), + ..Default::default() + }); let _ = write!( ex.out, "{}{body}", @@ -389,6 +411,149 @@ fn body(ex: &mut Exporter, seg: &Segment, r: Rect, k: f64, opacity: f32, insets: ) } +/// The model equivalent of the composed text represented by `body`. +fn model_body(ex: &mut Exporter, seg: &Segment, k: f64, opacity: f32, insets: [f64; 4]) -> craft_pptx::TextBody { + fn color(ex: &Exporter, swatch: &str, tint: f32, opacity: f32) -> Option { + let color = if let Some(gradient) = resolve_gradient(&ex.doc.swatches, swatch) { + let (_, color, alpha) = gradient.expanded_stops().into_iter().next()?; + let [r, g, b, a] = color.to_rgba8(alpha * opacity); + craft_pptx::Rgba::rgba(r, g, b, a) + } else { + let [r, g, b, a] = ex.doc.resolve_color(swatch, tint)?.to_rgba8(opacity); + craft_pptx::Rgba::rgba(r, g, b, a) + }; + Some(craft_pptx::ColorRef::rgb(color)) + } + + fn run_props(ex: &mut Exporter, run: Option<&Run>, size: f64, k: f64, opacity: f32) -> craft_pptx::text::RunProps { + let shifted = run.is_some_and(|r| r.y.abs() > 0.01 && r.size > 0.0); + let actual_size = if shifted { size * 1.5 } else { size }; + let mut props = craft_pptx::text::RunProps { size: Some(actual_size * k), lang: Some("en-US".into()), ..Default::default() }; + let Some(run) = run else { return props }; + let font = ex.fonts.use_face(run.face); + props.font = Some(font.family.clone()); + props.font_ea = Some(font.family.clone()); + props.font_cs = Some(font.family.clone()); + props.bold = Some(font.bold); + props.italic = Some(font.italic || run.rs.as_ref().is_some_and(|style| style.skew.abs() > 0.01)); + props.spacing = Some(run.spc * k); + props.kern = Some(4000.0); + if shifted { + props.baseline = Some(-run.y / actual_size); + } + if let Some(style) = run.rs.as_ref() { + if style.underline { + props.underline = Some("sng".into()); + } + if style.strikethrough { + props.strike = Some(craft_pptx::text::Strike::Single); + } + props.fill = Some(color(ex, &style.fill, style.fill_tint, opacity).map_or(craft_pptx::Fill::None, craft_pptx::Fill::solid)); + if style.stroke != designcraft_color::swatch::NONE + && style.stroke_weight > 0.0 + && let Some(color) = color(ex, &style.stroke, style.stroke_tint, opacity) + { + props.outline = Some(craft_pptx::Line::solid(color, style.stroke_weight * k)); + } + } + props + } + + let [left, top, right, bottom] = insets; + let mut model = craft_pptx::TextBody { + body: craft_pptx::text::BodyProps { + inset_l: Some(left * k), + inset_t: Some(top * k), + inset_r: Some(right * k), + inset_b: Some(bottom * k), + anchor: Some(craft_pptx::text::Anchor::Top), + wrap: Some(false), + autofit: Some(craft_pptx::text::AutoFit::None), + rtl_col: Some(false), + ..Default::default() + }, + ..Default::default() + }; + let steps = para_steps(seg); + let mut previous: Option<(f64, f64)> = None; + for lines in seg.lines.chunk_by(|a, b| a.para == b.para && !a.last_in_para) { + let (Some(first), Some(last)) = (lines.first(), lines.last()) else { continue }; + let mut height = steps.get(&first.para).copied().flatten().unwrap_or(if first.leading > 0.0 { first.leading } else { first.size * SINGLE }); + if let Some((previous_baseline, previous_height)) = previous + && lines.len() == 1 + { + let natural = (1.0 - BASELINE_IN_LINE) * previous_height + BASELINE_IN_LINE * height; + let gap = first.baseline - previous_baseline; + if gap < natural { + height = ((gap - (1.0 - BASELINE_IN_LINE) * previous_height) / BASELINE_IN_LINE).max(first.size * 0.2); + } + } + let align = match align_of(seg, lines) { + "ctr" => craft_pptx::text::Align::Center, + "r" => craft_pptx::text::Align::Right, + _ => craft_pptx::text::Align::Left, + }; + let mut paragraph = craft_pptx::text::Paragraph { + props: craft_pptx::text::ParaProps { + align: Some(align), + line_spacing: Some(craft_pptx::text::Spacing::Pct(height / (first.size * SINGLE))), + ..Default::default() + }, + ..Default::default() + }; + if align == craft_pptx::text::Align::Left { + let rest = lines.get(1).map_or(first.start, |line| line.start) - seg.x0; + let indent = first.start - seg.x0 - rest; + if rest.abs() > 0.005 { + paragraph.props.margin_left = Some(rest * k); + } + if indent.abs() > 0.005 { + paragraph.props.indent = Some(indent * k); + } + } + if let Some((previous_baseline, previous_height)) = previous { + let natural = (1.0 - BASELINE_IN_LINE) * previous_height + BASELINE_IN_LINE * height; + let before = first.baseline - previous_baseline - natural; + if before > 0.005 { + paragraph.props.space_before = Some(craft_pptx::text::Spacing::Pts(before * k)); + } + } + let mut tabs: Vec = lines.iter().flat_map(|line| line.tabs.iter().copied()).collect(); + tabs.sort_by(f64::total_cmp); + tabs.dedup_by(|a, b| (*a - *b).abs() < 0.01); + if !tabs.is_empty() { + paragraph.props.tabs = + Some(tabs.into_iter().map(|position| craft_pptx::text::TabStop { pos: (position - seg.x0) * k, align: "l".into() }).collect()); + } + for (line_index, line) in lines.iter().enumerate() { + if line_index > 0 { + paragraph.runs.push(craft_pptx::text::Run { + text: String::new(), + props: run_props(ex, line.runs.first(), line.size, k, opacity), + kind: craft_pptx::text::RunKind::Break, + }); + } + let n_runs = line.runs.len(); + for (run_index, run) in line.runs.iter().enumerate() { + let tail = if run_index + 1 == n_runs + && line.trailing_space + && align == craft_pptx::text::Align::Left + && !run.text.ends_with(char::is_whitespace) + { + " " + } else { + "" + }; + paragraph.runs.push(craft_pptx::text::Run::with(format!("{}{tail}", run.text), run_props(ex, Some(run), run.size, k, opacity))); + } + } + paragraph.end_props = run_props(ex, last.runs.last(), last.size, k, opacity); + model.paragraphs.push(paragraph); + previous = Some((last.baseline, height)); + } + model +} + /// The `` elements of a segment whose box is `r` (text space). pub(crate) fn paragraphs(ex: &mut Exporter, seg: &Segment, r: Rect, k: f64, opacity: f32) -> String { let mut s = String::new(); From 0be5fcb01a0da092c4c98ef8858aebe917a942b9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:29:45 -0500 Subject: [PATCH 027/153] Map PPTX tables and placed images to shared models --- crates/pptx/src/lib.rs | 27 ++++++++-- crates/pptx/src/package.rs | 2 + crates/pptx/src/slide.rs | 63 +++++++++++++++++++--- crates/pptx/src/table.rs | 106 +++++++++++++++++++++++++++++++++++++ crates/pptx/src/text.rs | 2 +- crates/render/src/tests.rs | 6 +-- 6 files changed, 190 insertions(+), 16 deletions(-) diff --git a/crates/pptx/src/lib.rs b/crates/pptx/src/lib.rs index 04b11ac58..629c47dcc 100644 --- a/crates/pptx/src/lib.rs +++ b/crates/pptx/src/lib.rs @@ -80,12 +80,31 @@ pub enum PptxError { pub type Result = std::result::Result; -fn apply_native_shapes(shapes: &mut [craft_pptx::Shape], native: &[craft_pptx::Shape]) { +fn apply_native_shapes( + shapes: &mut [craft_pptx::Shape], + native: &[craft_pptx::Shape], + native_media: &[(u32, String)], + media: &[craft_pptx::MediaItem], +) { for shape in shapes { if let Some(mapped) = native.iter().find(|mapped| mapped.id.0 == shape.id.0) { - *shape = mapped.clone(); + let mut mapped = mapped.clone(); + let media_name = native_media.iter().find(|(id, _)| *id == mapped.id.0).map(|(_, name)| name); + let has_media = if let (Some(name), craft_pptx::ShapeKind::Picture { fill }) = (media_name, &mut mapped.kind) { + if let Some(item) = media.iter().find(|item| item.name == *name) { + fill.media = item.id; + true + } else { + false + } + } else { + true + }; + if has_media { + *shape = mapped; + } } else if let craft_pptx::ShapeKind::Group { children, .. } = &mut shape.kind { - apply_native_shapes(children, native); + apply_native_shapes(children, native, native_media, media); } } } @@ -143,7 +162,7 @@ pub fn export_pptx(doc: &Document, cache: &Cache, opts: &PptxOptions) -> Result< let mut presentation = craft_pptx::import(&mapped, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; for (source, slide) in slides.iter().zip(&mut presentation.slides) { let slide = std::sync::Arc::make_mut(slide); - apply_native_shapes(&mut slide.shapes, &source.native_shapes); + apply_native_shapes(&mut slide.shapes, &source.native_shapes, &source.native_media, &presentation.media); } let bytes = craft_pptx::export(&presentation, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; Ok(ExportReport { bytes, slides: slides.len(), warnings: ex.warnings }) diff --git a/crates/pptx/src/package.rs b/crates/pptx/src/package.rs index 3b4b7774b..1dfa42557 100644 --- a/crates/pptx/src/package.rs +++ b/crates/pptx/src/package.rs @@ -29,6 +29,8 @@ pub struct Slide { pub name: String, /// Shapes that have a direct DesignCraft-to-Presentation model mapping. pub native_shapes: Vec, + /// Media part names keyed by the id of a native picture shape. + pub native_media: Vec<(u32, String)>, } /// A media part (`ppt/media/`). diff --git a/crates/pptx/src/slide.rs b/crates/pptx/src/slide.rs index c9f864523..ed17d641d 100644 --- a/crates/pptx/src/slide.rs +++ b/crates/pptx/src/slide.rs @@ -207,6 +207,7 @@ pub struct Exporter<'a> { pub media: Vec, /// Native PresentationML mappings for model elements the exporter can express directly. pub native_shapes: Vec, + pub native_media: Vec<(u32, String)>, /// Asset → (picture media name, SVG media name). media_of: HashMap)>>, pub fonts: Fonts, @@ -232,6 +233,7 @@ impl<'a> Exporter<'a> { warnings: Vec::new(), media: Vec::new(), native_shapes: Vec::new(), + native_media: Vec::new(), media_of: HashMap::new(), fonts: Fonts::default(), out: String::new(), @@ -265,6 +267,7 @@ impl<'a> Exporter<'a> { let bounds = page.bounds(); self.out.clear(); self.native_shapes.clear(); + self.native_media.clear(); self.next_id = 2; self.rels.clear(); self.rel_of.clear(); @@ -309,6 +312,7 @@ impl<'a> Exporter<'a> { rels: std::mem::take(&mut self.rels), name: page_name, native_shapes: std::mem::take(&mut self.native_shapes), + native_media: std::mem::take(&mut self.native_media), }) } @@ -540,6 +544,17 @@ impl<'a> Exporter<'a> { Some(srgb(c.to_rgb(), alpha)) } + /// An sRGB color in the shared PresentationML model, with DesignCraft tint and opacity applied. + pub(crate) fn model_color(&self, swatch: &str, tint: f32, alpha: f32) -> Option { + if let Some(gradient) = resolve_gradient(&self.doc.swatches, swatch) { + let (_, color, gradient_alpha) = gradient.expanded_stops().into_iter().next()?; + let [r, g, b, a] = color.to_rgba8(gradient_alpha * alpha); + return Some(craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a))); + } + let [r, g, b, a] = self.doc.resolve_color(swatch, tint)?.to_rgba8(alpha); + Some(craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a))) + } + pub fn fill(&mut self, f: &designcraft_doc::Fill, closed: bool, opacity: f32) -> String { if f.is_none() || !closed { return "".into(); @@ -761,16 +776,22 @@ impl<'a> Exporter<'a> { let frame = it.inner_bounds(); let img = g.xf.transform_rect_bbox(Rect::new(0.0, 0.0, g.size.0, g.size.1)); let axis = b.abs() < 1e-9 && c.abs() < 1e-9 && a > 0.0 && d > 0.0; - let (geo, crop) = if axis && img.width() > 0.0 && img.height() > 0.0 { + let (geo, crop, crop_values) = if axis && img.width() > 0.0 && img.height() > 0.0 { // The frame's geometry with the image cropped (or padded) to the frame's box. + let crop_values = [ + (frame.x0 - img.x0) / img.width(), + (frame.y0 - img.y0) / img.height(), + (img.x1 - frame.x1) / img.width(), + (img.y1 - frame.y1) / img.height(), + ]; let crop = format!( "", - pct((frame.x0 - img.x0) / img.width()), - pct((frame.y0 - img.y0) / img.height()), - pct((img.x1 - frame.x1) / img.width()), - pct((img.y1 - frame.y1) / img.height()) + pct(crop_values[0]), + pct(crop_values[1]), + pct(crop_values[2]), + pct(crop_values[3]) ); - (geometry(it, m), crop) + (geometry(it, m), crop, crop_values) } else { // Rotated, skewed or flipped inside its frame: the image's own box, uncropped, and // the frame drawn on its own. @@ -779,10 +800,10 @@ impl<'a> Exporter<'a> { let gi = Rect::new(0.0, 0.0, g.size.0, g.size.1); let gm = m * g.xf; let xfrm = box_of(gi, gm).map(|(x, _)| x).unwrap_or_else(|| XfrmBox::plain(gm.transform_rect_bbox(gi))); - (Geometry { xfrm, xml: preset("rect"), model: craft_pptx::Geom::preset("rect") }, String::new()) + (Geometry { xfrm, xml: preset("rect"), model: craft_pptx::Geom::preset("rect") }, String::new(), [0.0; 4]) }; let rid = self.rel(format!("../media/{name}")); - let svg_ext = match svg { + let svg_ext = match svg.as_ref() { Some(s) => { let sid = self.rel(format!("../media/{s}")); format!( @@ -796,6 +817,32 @@ impl<'a> Exporter<'a> { let line = if axis { self.line(&it.stroke, opacity) } else { "".into() }; let fx = self.effects(it, opacity); let id = self.id(); + if axis && svg.is_none() && it.effects == designcraft_doc::Effects::default() && it.fill.is_none() && it.stroke.is_none() { + let mut fill = craft_pptx::style::PictureFill { media: craft_pptx::MediaId(0), crop: crop_values, ..Default::default() }; + if opacity < 0.999 { + fill.alpha = Some(f64::from(opacity)); + } + self.native_shapes.push(craft_pptx::Shape { + id: craft_pptx::ShapeId(id), + name: name_of(it, id), + xfrm: Some(craft_pptx::Xfrm { + x: geo.xfrm.rect.x0, + y: geo.xfrm.rect.y0, + w: geo.xfrm.rect.width(), + h: geo.xfrm.rect.height(), + rot: geo.xfrm.rot, + flip_h: geo.xfrm.flip_h, + flip_v: geo.xfrm.flip_v, + }), + kind: craft_pptx::ShapeKind::Picture { fill }, + geom: geo.model.clone(), + fill: Some(craft_pptx::Fill::None), + line: Some(craft_pptx::Line::none()), + descr: it.alt_text.clone(), + ..Default::default() + }); + self.native_media.push((id, name.clone())); + } let _ = write!( self.out, "\ diff --git a/crates/pptx/src/table.rs b/crates/pptx/src/table.rs index b4a04d96d..37a36f91e 100644 --- a/crates/pptx/src/table.rs +++ b/crates/pptx/src/table.rs @@ -101,6 +101,49 @@ fn border(ex: &mut Exporter, tag: &str, s: Option<&StrokeSeg>, k: f64, opacity: ) } +fn model_border(ex: &Exporter, s: Option<&StrokeSeg>, k: f64, opacity: f32) -> craft_pptx::Line { + let Some(stroke) = s.filter(|s| s.stroke.weight > 0.0) else { return craft_pptx::Line::none() }; + let Some(color) = ex.model_color(&stroke.stroke.color, stroke.stroke.tint, opacity) else { return craft_pptx::Line::none() }; + let mut line = craft_pptx::Line::solid(color, stroke.stroke.weight * k); + line.cap = Some(craft_pptx::style::LineCap::Flat); + line.dash = match stroke.stroke.kind { + StrokeType::Dashed { .. } => Some(craft_pptx::style::Dash::Preset { name: "dash".into() }), + StrokeType::Dotted => Some(craft_pptx::style::Dash::Preset { name: "sysDot".into() }), + _ => None, + }; + line +} + +fn model_cell_text(ex: &mut Exporter, c: &PlacedCell, k: f64, opacity: f32) -> (craft_pptx::TextBody, [f64; 4]) { + let Some(frame) = c.text.frames.first() else { + let mut text = craft_pptx::TextBody::from_text(""); + if let Some(paragraph) = text.paragraphs.first_mut() { + paragraph.end_props.lang = Some("en-US".into()); + } + return (text, [0.0; 4]); + }; + let segments = crate::text::segments(ex, &c.text, frame, &c.source, &[]); + let mut all = segments.first().cloned().unwrap_or_default(); + for segment in segments.iter().skip(1) { + all.lines.extend(segment.lines.iter().cloned()); + all.x1 = all.x1.max(segment.x1); + } + if all.lines.is_empty() { + let mut text = craft_pptx::TextBody::from_text(""); + if let Some(paragraph) = text.paragraphs.first_mut() { + paragraph.end_props.lang = Some("en-US".into()); + } + return (text, [0.0; 4]); + } + let bounds = crate::text::seg_rect(&all); + let bounds = Rect::new(bounds.x0 + c.origin.x, bounds.y0 + c.origin.y, bounds.x1 + c.origin.x, bounds.y1 + c.origin.y); + let margins = + [(bounds.x0 - c.rect.x0).max(0.0), (bounds.y0 - c.rect.y0).max(0.0), (c.rect.x1 - bounds.x1).max(0.0), (c.rect.y1 - bounds.y1).max(0.0)]; + let mut body = crate::text::model_body(ex, &all, k, opacity, [0.0; 4]); + body.body = Default::default(); + (body, margins) +} + fn table(ex: &mut Exporter, t: &TableFrag, m: Affine, k: f64, opacity: f32) { if t.cells.is_empty() { return; @@ -145,6 +188,69 @@ fn table(ex: &mut Exporter, t: &TableFrag, m: Affine, k: f64, opacity: f32) { } let edges_of = EdgeIndex::new(&t.strokes); let id = ex.id(); + let mut model = craft_pptx::table::Table { + cols: xs.windows(2).map(|w| (w[1] - w[0]) * k).collect(), + rows: ys + .windows(2) + .map(|w| craft_pptx::table::Row { height: (w[1] - w[0]) * k, cells: vec![craft_pptx::table::Cell::default(); nc] }) + .collect(), + style: String::new(), + first_row: false, + first_col: false, + last_row: false, + last_col: false, + band_row: false, + band_col: false, + ..Default::default() + }; + for row in 0..nr { + for col in 0..nc { + let at = row * nc + col; + let Some(cell) = model.rows.get_mut(row).and_then(|r| r.cells.get_mut(col)) else { continue }; + let (horizontal, vertical) = covered.get(at).copied().unwrap_or((false, false)); + if horizontal || vertical { + cell.h_merge = horizontal; + cell.v_merge = vertical; + continue; + } + let (grid_span, row_span) = span.get(at).copied().unwrap_or((1, 1)); + cell.grid_span = u32::try_from(grid_span).unwrap_or(u32::MAX); + cell.row_span = u32::try_from(row_span).unwrap_or(u32::MAX); + let Some(placed) = origin.get(at).copied().flatten() else { + cell.fill = Some(craft_pptx::Fill::None); + cell.margins = Some([0.0; 4]); + continue; + }; + let (text, [left, top, right, bottom]) = model_cell_text(ex, placed, k, opacity); + cell.text = text; + cell.margins = Some([left * k, right * k, top * k, bottom * k]); + cell.fill = Some( + placed + .fill + .as_ref() + .and_then(|(swatch, tint)| ex.model_color(swatch, *tint, opacity)) + .map_or(craft_pptx::Fill::None, craft_pptx::Fill::solid), + ); + let bounds = placed.rect; + let edge = |side: char| { + let (a, b) = match side { + 'L' => ((bounds.x0, bounds.y0), (bounds.x0, bounds.y1)), + 'R' => ((bounds.x1, bounds.y0), (bounds.x1, bounds.y1)), + 'T' => ((bounds.x0, bounds.y0), (bounds.x1, bounds.y0)), + _ => ((bounds.x0, bounds.y1), (bounds.x1, bounds.y1)), + }; + edges_of.stroke(a, b) + }; + cell.borders = ['L', 'R', 'T', 'B'].map(|side| Some(model_border(ex, edge(side), k, opacity))); + } + } + ex.native_shapes.push(craft_pptx::Shape { + id: craft_pptx::ShapeId(id), + name: format!("Table {id}"), + xfrm: Some(craft_pptx::Xfrm::new(xfrm.rect.x0, xfrm.rect.y0, xfrm.rect.width(), xfrm.rect.height())), + kind: craft_pptx::ShapeKind::Table(model), + ..Default::default() + }); let mut s = format!( "\ ", diff --git a/crates/pptx/src/text.rs b/crates/pptx/src/text.rs index a03991729..6854d4a29 100644 --- a/crates/pptx/src/text.rs +++ b/crates/pptx/src/text.rs @@ -412,7 +412,7 @@ fn body(ex: &mut Exporter, seg: &Segment, r: Rect, k: f64, opacity: f32, insets: } /// The model equivalent of the composed text represented by `body`. -fn model_body(ex: &mut Exporter, seg: &Segment, k: f64, opacity: f32, insets: [f64; 4]) -> craft_pptx::TextBody { +pub(crate) fn model_body(ex: &mut Exporter, seg: &Segment, k: f64, opacity: f32, insets: [f64; 4]) -> craft_pptx::TextBody { fn color(ex: &Exporter, swatch: &str, tint: f32, opacity: f32) -> Option { let color = if let Some(gradient) = resolve_gradient(&ex.doc.swatches, swatch) { let (_, color, alpha) = gradient.expanded_stops().into_iter().next()?; diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index 9e3d74e8b..4284d10c4 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -529,9 +529,9 @@ fn degenerate_dash_patterns_draw_solid_and_fill_types_close_table_corners() { let (fid, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 336.0, 400.0), lid, "", ParaFormat::default()).unwrap(); d.item_mut(fid).unwrap().stroke = designcraft_doc::Stroke { weight: 2.0, kind: fine.clone(), ..Default::default() }; let mut t = designcraft_doc::Table::new(1, 2, 2, 0, 0, 200.0); - for r in 0..2 { - t.rows[r].height = 60.0; - t.rows[r].mode = designcraft_doc::RowHeightMode::Exactly; + for row in &mut t.rows { + row.height = 60.0; + row.mode = designcraft_doc::RowHeightMode::Exactly; } let stroke = |kind| designcraft_doc::CellStroke { weight: 8.0, kind, gap_color: "C=100 M=0 Y=0 K=0".into(), ..Default::default() }; for cell in &mut t.cells { From bfd10a576ddb32652e9e4e1fb5dc86c0b20d2864 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:38:08 -0500 Subject: [PATCH 028/153] Map PPTX fills and effects to shared models --- crates/pptx/src/slide.rs | 236 +++++++++++++++++++++++++++------------ 1 file changed, 164 insertions(+), 72 deletions(-) diff --git a/crates/pptx/src/slide.rs b/crates/pptx/src/slide.rs index ed17d641d..db4bb72c3 100644 --- a/crates/pptx/src/slide.rs +++ b/crates/pptx/src/slide.rs @@ -438,80 +438,12 @@ impl<'a> Exporter<'a> { /// A shape whose complete appearance fits the shared PresentationML model. fn native_shape(&self, it: &Item, geometry: &Geometry, id: u32, opacity: f32) -> Option { - if it.effects != designcraft_doc::Effects::default() || it.fill.overprint || it.stroke.overprint || it.stroke.gap_overprint { + if it.fill.overprint || it.stroke.overprint || it.stroke.gap_overprint { return None; } - let fill = if it.fill.is_none() || !it.path.is_closed() { - craft_pptx::Fill::None - } else { - if resolve_gradient(&self.doc.swatches, &it.fill.swatch).is_some() { - return None; - } - match self.doc.resolve_color(&it.fill.swatch, it.fill.tint) { - Some(color) => { - let [r, g, b, a] = color.to_rgba8(opacity); - craft_pptx::Fill::solid(craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a))) - } - None => craft_pptx::Fill::None, - } - }; - let line = if it.stroke.is_none() { - craft_pptx::Line::none() - } else { - let stroke = &it.stroke; - let supported_dash = matches!(&stroke.kind, StrokeType::Solid | StrokeType::Dotted | StrokeType::Dashed { .. }); - if stroke.start != Arrowhead::None - || stroke.end != Arrowhead::None - || stroke.gradient_angle.is_some() - || stroke.gradient_vector.is_some() - || !supported_dash - { - return None; - } - if resolve_gradient(&self.doc.swatches, &stroke.swatch).is_some() { - return None; - } - let color = self.doc.resolve_color(&stroke.swatch, stroke.tint)?; - let [r, g, b, a] = color.to_rgba8(opacity); - let mut line = craft_pptx::Line::solid(craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a)), stroke.weight); - line.cap = Some(match stroke.cap { - Cap::Butt => craft_pptx::style::LineCap::Flat, - Cap::Round => craft_pptx::style::LineCap::Round, - Cap::Projecting => craft_pptx::style::LineCap::Square, - }); - line.join = Some(match stroke.join { - Join::Miter => craft_pptx::style::LineJoin::Miter, - Join::Round => craft_pptx::style::LineJoin::Round, - Join::Bevel => craft_pptx::style::LineJoin::Bevel, - }); - line.align = Some(match stroke.align { - StrokeAlign::Inside => craft_pptx::style::LineAlign::Inside, - StrokeAlign::Center | StrokeAlign::Outside => craft_pptx::style::LineAlign::Center, - }); - if stroke.join == Join::Miter && (stroke.miter_limit - 4.0).abs() > 0.001 { - return None; - } - match &stroke.kind { - StrokeType::Solid => {} - StrokeType::Dotted => line.dash = Some(craft_pptx::style::Dash::Preset { name: "sysDot".into() }), - StrokeType::Dashed { pattern } => { - if pattern.iter().any(|length| *length > 0.0) { - let mut pattern = pattern.clone(); - if pattern.len() % 2 == 1 { - pattern.extend(pattern.clone()); - } - let pairs = pattern - .chunks(2) - .filter_map(|pair| pair.first().zip(pair.get(1))) - .map(|(dash, gap)| (*dash / stroke.weight, *gap / stroke.weight)) - .collect(); - line.dash = Some(craft_pptx::style::Dash::Custom { pattern: pairs }); - } - } - _ => return None, - } - line - }; + let fill = self.model_fill(&it.fill, it.path.is_closed(), opacity); + let line = self.model_line(&it.stroke, opacity)?; + let effects = self.model_effects(it, opacity); let xfrm = craft_pptx::Xfrm { x: geometry.xfrm.rect.x0, y: geometry.xfrm.rect.y0, @@ -528,6 +460,7 @@ impl<'a> Exporter<'a> { geom: geometry.model.clone(), fill: Some(fill), line: Some(line), + effects: (effects != craft_pptx::Effects::default()).then_some(effects), descr: it.alt_text.clone(), ..Default::default() }) @@ -555,6 +488,165 @@ impl<'a> Exporter<'a> { Some(craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a))) } + fn model_fill(&self, fill: &designcraft_doc::Fill, closed: bool, opacity: f32) -> craft_pptx::Fill { + if fill.is_none() || !closed { + return craft_pptx::Fill::None; + } + if let Some(gradient) = resolve_gradient(&self.doc.swatches, &fill.swatch) { + let stops = gradient + .expanded_stops() + .into_iter() + .map(|(offset, color, alpha)| { + let [r, g, b, a] = color.to_rgba8(alpha * opacity); + craft_pptx::style::GradientStop { pos: f64::from(offset), color: craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a)) } + }) + .collect(); + let shape = match gradient.kind { + GradientKind::Radial => craft_pptx::style::GradientShape::Path { path: "circle".into(), focus: [0.5; 4] }, + GradientKind::Linear | GradientKind::Freeform => { + let angle = match fill.gradient_vector { + Some([x0, y0, x1, y1]) => (y1 - y0).atan2(x1 - x0).to_degrees(), + None => -fill.gradient_angle.unwrap_or(0.0), + }; + craft_pptx::style::GradientShape::Linear { angle, scaled: false } + } + }; + return craft_pptx::Fill::Gradient(craft_pptx::style::Gradient { + stops, + shape: Some(shape), + rotate_with_shape: Some(true), + flip: None, + tile: [0.0; 4], + }); + } + self.model_color(&fill.swatch, fill.tint, opacity).map_or(craft_pptx::Fill::None, craft_pptx::Fill::solid) + } + + fn model_line(&self, stroke: &Stroke, opacity: f32) -> Option { + if stroke.is_none() || stroke.weight <= 0.0 { + return Some(craft_pptx::Line::none()); + } + if stroke.join == Join::Miter && (stroke.miter_limit - 4.0).abs() > 0.001 { + return None; + } + let mut line = craft_pptx::Line { + fill: Some(self.model_fill(&stroke.as_fill(), true, opacity)), + width: Some(stroke.weight), + cap: Some(match stroke.cap { + Cap::Butt => craft_pptx::style::LineCap::Flat, + Cap::Round => craft_pptx::style::LineCap::Round, + Cap::Projecting => craft_pptx::style::LineCap::Square, + }), + join: Some(match stroke.join { + Join::Miter => craft_pptx::style::LineJoin::Miter, + Join::Round => craft_pptx::style::LineJoin::Round, + Join::Bevel => craft_pptx::style::LineJoin::Bevel, + }), + align: Some(match stroke.align { + StrokeAlign::Inside => craft_pptx::style::LineAlign::Inside, + StrokeAlign::Center | StrokeAlign::Outside => craft_pptx::style::LineAlign::Center, + }), + compound: Some(match stroke.kind { + StrokeType::ThickThin => craft_pptx::style::Compound::ThickThin, + StrokeType::ThinThick => craft_pptx::style::Compound::ThinThick, + StrokeType::ThinThin | StrokeType::ThickThick => craft_pptx::style::Compound::Double, + StrokeType::ThinThickThin | StrokeType::ThickThinThick => craft_pptx::style::Compound::Triple, + _ => craft_pptx::style::Compound::Single, + }), + ..Default::default() + }; + match &stroke.kind { + StrokeType::Dashed { pattern } if pattern.iter().any(|length| *length > 0.0) => { + let mut pattern = pattern.iter().map(|length| length.max(0.0)).collect::>(); + if pattern.len() % 2 == 1 { + pattern.extend(pattern.clone()); + } + let pairs = pattern + .chunks(2) + .filter_map(|pair| pair.first().zip(pair.get(1))) + .map(|(dash, gap)| (*dash / stroke.weight, *gap / stroke.weight)) + .collect(); + line.dash = Some(craft_pptx::style::Dash::Custom { pattern: pairs }); + } + StrokeType::Dotted => line.dash = Some(craft_pptx::style::Dash::Preset { name: "sysDot".into() }), + _ => {} + } + let end = |arrow: Arrowhead| { + match arrow { + Arrowhead::None | Arrowhead::Bar => None, + Arrowhead::Simple | Arrowhead::SimpleWide => Some("arrow"), + Arrowhead::Triangle | Arrowhead::TriangleWide | Arrowhead::Curved => Some("triangle"), + Arrowhead::Barbed => Some("stealth"), + Arrowhead::Circle | Arrowhead::CircleSolid => Some("oval"), + Arrowhead::Square | Arrowhead::SquareSolid => Some("diamond"), + } + .map(|kind| craft_pptx::style::LineEnd { kind: kind.into(), w: "lg".into(), len: "lg".into() }) + }; + line.head = end(stroke.start); + line.tail = end(stroke.end); + Some(line) + } + + fn model_effects(&self, it: &Item, opacity: f32) -> craft_pptx::Effects { + let fx = &it.effects; + let shadow = |color: &str, alpha: f32, angle: f64, global: bool, distance: f64, blur: f64, inner: bool| { + self.model_color(color, 1.0, alpha * opacity).map(|color| { + let light = self.doc.light_angle(angle, global).to_radians(); + let offset = Vec2::new(-light.cos(), light.sin()); + craft_pptx::style::Shadow { + color, + blur, + dist: distance.abs(), + dir: offset.y.atan2(offset.x).to_degrees(), + inner, + sx: 1.0, + sy: 1.0, + kx: 0.0, + ky: 0.0, + align: String::new(), + rotate_with_shape: true, + } + }) + }; + let outer_shadow = fx + .drop_shadow + .on + .then(|| { + shadow( + &fx.drop_shadow.color, + fx.drop_shadow.opacity, + fx.drop_shadow.angle, + fx.drop_shadow.global_light, + fx.drop_shadow.distance, + fx.drop_shadow.size, + false, + ) + }) + .flatten(); + let inner_shadow = fx + .inner_shadow + .on + .then(|| { + shadow( + &fx.inner_shadow.color, + fx.inner_shadow.opacity, + fx.inner_shadow.angle, + fx.inner_shadow.global_light, + fx.inner_shadow.distance, + fx.inner_shadow.size, + true, + ) + }) + .flatten(); + let glow = if fx.outer_glow.on { + self.model_color(&fx.outer_glow.color, 1.0, fx.outer_glow.opacity * opacity) + .map(|color| craft_pptx::style::Glow { color, radius: fx.outer_glow.size }) + } else { + None + }; + craft_pptx::Effects { outer_shadow, inner_shadow, glow, soft_edge: (fx.feather > 0.0).then_some(fx.feather), ..Default::default() } + } + pub fn fill(&mut self, f: &designcraft_doc::Fill, closed: bool, opacity: f32) -> String { if f.is_none() || !closed { return "".into(); From 9272139d2b4290518b27b54cafb432c81a57c5dc Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:55:45 -0500 Subject: [PATCH 029/153] Export Presentation model directly through craft-pptx --- Cargo.lock | 46 +++++- Cargo.toml | 2 +- crates/pptx/src/lib.rs | 98 ++++++----- crates/pptx/src/package.rs | 321 ++----------------------------------- crates/pptx/src/slide.rs | 102 +++++++----- crates/pptx/src/text.rs | 85 +++++++++- 6 files changed, 257 insertions(+), 397 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 772bf2185..cd9646fd7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1141,9 +1141,9 @@ dependencies = [ [[package]] name = "craft-drawingml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", "kurbo", "log", "serde", @@ -1152,9 +1152,9 @@ dependencies = [ [[package]] name = "craft-drawingml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", "kurbo", "log", "serde", @@ -1339,6 +1339,17 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-opc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "log", + "thiserror 2.0.21", +] + [[package]] name = "craft-pdf" version = "0.1.0" @@ -1355,11 +1366,11 @@ dependencies = [ [[package]] name = "craft-pptx" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" dependencies = [ - "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", - "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", "kurbo", "log", "serde", @@ -1553,6 +1564,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -1573,6 +1593,16 @@ dependencies = [ "zip 9.1.1", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + [[package]] name = "crc32fast" version = "1.5.3" diff --git a/Cargo.toml b/Cargo.toml index f8a7a7c0f..34e1857b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } -craft-pptx = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } +craft-pptx = { git = "https://github.com/storytold/craft-libs", rev = "f6cab1980", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. diff --git a/crates/pptx/src/lib.rs b/crates/pptx/src/lib.rs index 629c47dcc..3ccf3d09c 100644 --- a/crates/pptx/src/lib.rs +++ b/crates/pptx/src/lib.rs @@ -80,33 +80,26 @@ pub enum PptxError { pub type Result = std::result::Result; -fn apply_native_shapes( +fn set_picture_media( shapes: &mut [craft_pptx::Shape], - native: &[craft_pptx::Shape], - native_media: &[(u32, String)], - media: &[craft_pptx::MediaItem], -) { + names: &[(u32, String, Option)], + ids: &std::collections::HashMap, +) -> Result<()> { for shape in shapes { - if let Some(mapped) = native.iter().find(|mapped| mapped.id.0 == shape.id.0) { - let mut mapped = mapped.clone(); - let media_name = native_media.iter().find(|(id, _)| *id == mapped.id.0).map(|(_, name)| name); - let has_media = if let (Some(name), craft_pptx::ShapeKind::Picture { fill }) = (media_name, &mut mapped.kind) { - if let Some(item) = media.iter().find(|item| item.name == *name) { - fill.media = item.id; - true - } else { - false - } - } else { - true - }; - if has_media { - *shape = mapped; - } - } else if let craft_pptx::ShapeKind::Group { children, .. } = &mut shape.kind { - apply_native_shapes(children, native, native_media, media); + if let craft_pptx::ShapeKind::Picture { fill } = &mut shape.kind { + let (name, svg) = names + .iter() + .find(|(id, _, _)| *id == shape.id.0) + .map(|(_, name, svg)| (name, svg)) + .ok_or_else(|| PptxError::Write(format!("picture {} has no media mapping", shape.id.0)))?; + fill.media = *ids.get(name).ok_or_else(|| PptxError::Write(format!("picture media `{name}` is missing")))?; + fill.svg = svg.as_ref().and_then(|name| ids.get(name).copied()); + } + if let craft_pptx::ShapeKind::Group { children, .. } = &mut shape.kind { + set_picture_media(children, names, ids)?; } } + Ok(()) } /// Export `doc` as a PowerPoint presentation. @@ -146,23 +139,52 @@ pub fn export_pptx(doc: &Document, cache: &Cache, opts: &PptxOptions) -> Result< Vec::new() }; let title = opts.title.clone().unwrap_or_else(|| doc.title.clone()); - // Build a compatibility package from DesignCraft's composed-page mapping, then hand the - // document model and final OOXML serialization to the shared PresentationML implementation. - // This keeps the existing geometry, text, table and media mapping intact while the mapping - // moves from DrawingML fragments to craft_pptx model values. - let mapped = package::write(&package::Package { - size, - slides: &slides, - media: &ex.media, - fonts: &fonts, - title: &title, - author: opts.author.as_deref().unwrap_or(""), - created: opts.created, - })?; - let mut presentation = craft_pptx::import(&mapped, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; + let mut presentation = craft_pptx::Presentation { slide_size: craft_pptx::Size::new(size.0, size.1), ..Default::default() }; + presentation.props.title = title; + presentation.props.author = opts.author.clone().unwrap_or_default(); + presentation.props.created = opts + .created + .and_then(|created| chrono::DateTime::::from_timestamp(created, 0)) + .map(|created| created.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) + .unwrap_or_default(); + for (index, source) in slides.iter().enumerate() { + presentation.slides.push(std::sync::Arc::new(craft_pptx::Slide { + id: craft_pptx::SlideId(u32::try_from(index + 1).unwrap_or(u32::MAX)), + name: source.name.clone(), + shapes: source.shapes.clone(), + ..Default::default() + })); + } + let mut max_id = u32::try_from(slides.len()).unwrap_or(u32::MAX); + for slide in &presentation.slides { + craft_pptx::walk(&slide.shapes, &mut |shape, _| max_id = max_id.max(shape.id.0)); + } + presentation.next_id = max_id; + let mut media_ids = std::collections::HashMap::new(); + for media in &ex.media { + let content_type = designcraft_images::mime(&media.bytes); + let id = presentation + .add_media(&media.name, content_type, media.bytes.clone()) + .ok_or_else(|| PptxError::Write("the presentation has no more media ids".into()))?; + media_ids.insert(media.name.clone(), id); + } for (source, slide) in slides.iter().zip(&mut presentation.slides) { let slide = std::sync::Arc::make_mut(slide); - apply_native_shapes(&mut slide.shapes, &source.native_shapes, &source.native_media, &presentation.media); + set_picture_media(&mut slide.shapes, &source.media, &media_ids)?; + } + for font in fonts { + let mut embedded = craft_pptx::EmbeddedFont { typeface: font.family, ..Default::default() }; + let slots = [&mut embedded.regular, &mut embedded.bold, &mut embedded.italic, &mut embedded.bold_italic]; + for (slot, data) in slots.into_iter().zip(font.faces) { + if let Some(data) = data { + let name = format!("font{}.fntdata", presentation.embedded_fonts.len() + 1); + *slot = presentation.add_media(&name, "application/x-fontdata", data); + if slot.is_none() { + return Err(PptxError::Write("the presentation has no more media ids".into())); + } + } + } + presentation.embedded_fonts.push(embedded); } let bytes = craft_pptx::export(&presentation, &craft_pptx::Plain).map_err(|e| PptxError::Write(e.to_string()))?; Ok(ExportReport { bytes, slides: slides.len(), warnings: ex.warnings }) diff --git a/crates/pptx/src/package.rs b/crates/pptx/src/package.rs index 1dfa42557..d24ef4cbb 100644 --- a/crates/pptx/src/package.rs +++ b/crates/pptx/src/package.rs @@ -1,323 +1,28 @@ -//! The PresentationML package: one blank master and layout, a theme, the slides and their media. +//! Data returned by the DesignCraft-to-PresentationML model adapter. -use crate::fonts::EmbeddedFont; -use crate::xml::{emu_len, esc}; -use crate::{PptxError, Result}; -use std::fmt::Write as _; - -pub const NS: &str = r#"xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main""#; -const DECL: &str = "\n"; -const REL_NS: &str = "http://schemas.openxmlformats.org/package/2006/relationships"; -pub const REL_IMAGE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"; - -/// A relationship from a slide to another part. -#[derive(Clone, Debug)] -pub struct Rel { - pub id: String, - pub kind: &'static str, - pub target: String, -} - -/// One slide: its shape tree content (everything inside `` after the group -/// properties) and relationships beyond the layout (`rId1`). #[derive(Clone, Debug, Default)] pub struct Slide { /// Page size in points. pub size: (f64, f64), - pub shapes: String, - pub rels: Vec, pub name: String, - /// Shapes that have a direct DesignCraft-to-Presentation model mapping. - pub native_shapes: Vec, - /// Media part names keyed by the id of a native picture shape. - pub native_media: Vec<(u32, String)>, + /// Model shapes in stacking order. + pub shapes: Vec, + /// Media part names keyed by the id of a picture shape. + pub media: Vec<(u32, String, Option)>, } -/// A media part (`ppt/media/`). #[derive(Clone, Debug)] pub struct Media { pub name: String, pub bytes: Vec, } -pub struct Package<'a> { - pub size: (f64, f64), - pub slides: &'a [Slide], - pub media: &'a [Media], - pub fonts: &'a [EmbeddedFont], - pub title: &'a str, - pub author: &'a str, - pub created: Option, -} - -fn rels(list: &[(String, &str, String)]) -> String { - let mut s = format!("{DECL}"); - for (id, kind, target) in list { - let _ = write!(s, "", esc(target)); - } - s.push_str(""); - s -} - -/// PowerPoint accepts slide sizes from 1 inch to 56 inches. -fn slide_extent(pt: f64) -> i64 { - emu_len(pt).clamp(914_400, 51_206_400) -} - -pub fn write(p: &Package) -> Result> { - let mut parts: Vec<(String, Vec)> = Vec::new(); - let mut put = |name: &str, s: String| parts.push((name.to_string(), s.into_bytes())); - let n = p.slides.len(); - - // Content types. - let mut ct = format!( - "{DECL}\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -" - ); - for i in 1..=n { - let _ = write!( - ct, - "" - ); - } - ct.push_str(""); - put("[Content_Types].xml", ct); - - put( - "_rels/.rels", - rels(&[ - ("rId1".into(), "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "ppt/presentation.xml".into()), - ("rId2".into(), "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties", "docProps/core.xml".into()), - ("rId3".into(), "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties", "docProps/app.xml".into()), - ]), - ); - - // Presentation: master rId1, slides rId2.., then the property parts. - let (cx, cy) = (slide_extent(p.size.0), slide_extent(p.size.1)); - let mut ids = String::new(); - let mut prels: Vec<(String, &str, String)> = vec![( - "rId1".into(), - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", - "slideMasters/slideMaster1.xml".into(), - )]; - for i in 1..=n { - let _ = write!(ids, "", 255 + i, i + 1); - prels.push(( - format!("rId{}", i + 1), - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide", - format!("slides/slide{i}.xml"), - )); - } - for (k, (kind, target)) in - [("presProps", "presProps.xml"), ("viewProps", "viewProps.xml"), ("theme", "theme/theme1.xml"), ("tableStyles", "tableStyles.xml")] - .iter() - .enumerate() - { - prels.push((format!("rId{}", n + 2 + k), rel_kind(kind), target.to_string())); - } - // Embedded fonts: one part per face, listed per family in regular, bold, italic, bold italic. - let mut font_list = String::new(); - let mut font_parts: Vec<(String, &[u8])> = Vec::new(); - for f in p.fonts { - let _ = write!(font_list, "", esc(&f.family)); - for (slot, tag) in ["regular", "bold", "italic", "boldItalic"].iter().enumerate() { - if let Some(Some(bytes)) = f.faces.get(slot) { - let id = format!("rId{}", prels.len() + 1); - let target = format!("fonts/font{}.fntdata", font_parts.len() + 1); - let _ = write!(font_list, ""); - prels.push((id, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/font", target.clone())); - font_parts.push((format!("ppt/{target}"), bytes.as_slice())); - } - } - font_list.push_str(""); - } - let (embed_attr, font_list) = if font_parts.is_empty() { - ("", String::new()) - } else { - (" embedTrueTypeFonts=\"1\"", format!("{font_list}")) - }; - put( - "ppt/presentation.xml", - format!( - "{DECL}\ -{ids}{font_list}\ -" - ), - ); - put("ppt/_rels/presentation.xml.rels", rels(&prels)); - - put("ppt/presProps.xml", format!("{DECL}")); - put( - "ppt/viewProps.xml", - format!( - "{DECL}\ -\ -" - ), - ); - put( - "ppt/tableStyles.xml", - format!( - "{DECL}" - ), - ); - - // One blank master and layout. - let tree = ""; - put( - "ppt/slideMasters/slideMaster1.xml", - format!( - "{DECL}{tree}\ -\ -\ -" - ), - ); - put( - "ppt/slideMasters/_rels/slideMaster1.xml.rels", - rels(&[ - ( - "rId1".into(), - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", - "../slideLayouts/slideLayout1.xml".into(), - ), - ("rId2".into(), "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme", "../theme/theme1.xml".into()), - ]), - ); - put( - "ppt/slideLayouts/slideLayout1.xml", - format!( - "{DECL}{tree}" - ), - ); - put( - "ppt/slideLayouts/_rels/slideLayout1.xml.rels", - rels(&[( - "rId1".into(), - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", - "../slideMasters/slideMaster1.xml".into(), - )]), - ); - put("ppt/theme/theme1.xml", theme()); - - for (i, s) in p.slides.iter().enumerate() { - let name = if s.name.is_empty() { String::new() } else { format!(" name=\"{}\"", esc(&s.name)) }; - put( - &format!("ppt/slides/slide{}.xml", i + 1), - format!( - "{DECL}\ -{}\ -", - s.shapes - ), - ); - let mut list: Vec<(String, &str, String)> = vec![( - "rId1".into(), - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", - "../slideLayouts/slideLayout1.xml".into(), - )]; - list.extend(s.rels.iter().map(|r| (r.id.clone(), r.kind, r.target.clone()))); - put(&format!("ppt/slides/_rels/slide{}.xml.rels", i + 1), rels(&list)); - } - - put("docProps/core.xml", core(p)); - put( - "docProps/app.xml", - format!( - "{DECL}DesignCraft{n}Custom" - ), - ); - - let mut entries = parts; - entries.extend(font_parts.into_iter().map(|(name, bytes)| (name, bytes.to_vec()))); - entries.extend(p.media.iter().map(|m| (format!("ppt/media/{}", m.name), m.bytes.clone()))); - craft_opc::zip_entries(&entries).map_err(|e| PptxError::Write(e.to_string())) -} - -fn rel_kind(k: &str) -> &'static str { - match k { - "presProps" => "http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps", - "viewProps" => "http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps", - "theme" => "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme", - _ => "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles", - } -} - -fn core(p: &Package) -> String { - let mut s = format!( - "{DECL}" - ); - if !p.title.is_empty() { - let _ = write!(s, "{}", esc(p.title)); - } - if !p.author.is_empty() { - let _ = write!(s, "{}", esc(p.author)); - } - if let Some(t) = p.created { - let (y, mo, d, h, mi, se) = civil(t); - let _ = write!(s, "{y:04}-{mo:02}-{d:02}T{h:02}:{mi:02}:{se:02}Z"); - } - s.push_str(""); - s -} - -/// Unix seconds → UTC civil date and time (proleptic Gregorian), year clamped to the W3CDTF -/// `0..=9999` range. A time outside chrono's year range still converts (the calendar repeats every -/// 400 years, so the instant is moved into range by whole cycles and only the year is carried, -/// matching the old `civil`), then the year is clamped — never a silent reset to the epoch. -fn civil(t: i64) -> (i64, u32, u32, u32, u32, u32) { - use chrono::{Datelike, Timelike}; - let (y, t) = designcraft_doc::time::utc_parts(t, 0); - (y.clamp(0, 9999), t.month(), t.day(), t.hour(), t.minute(), t.second()) -} - -/// A neutral theme: text sets its own fonts and colours, so these only matter for new objects. -fn theme() -> String { - let fill3 = "".repeat(3); - let ln = "".repeat(3); - let fx = "".repeat(3); - format!( - "{DECL}\ -\ -\ -\ -\ -{fill3}{ln}{fx}{fill3}\ -" - ) -} - -#[cfg(test)] -mod tests { - use super::civil; - - #[test] - fn civil_dates() { - assert_eq!(civil(0), (1970, 1, 1, 0, 0, 0)); - assert_eq!(civil(951_782_400), (2000, 2, 29, 0, 0, 0)); - assert_eq!(civil(1_791_504_000 + 3_723), (2026, 10, 9, 1, 2, 3)); - // Beyond chrono's year range the old `civil` still converted and clamped only the year to - // the W3CDTF `0..=9999` range; pin that (month/day/time keep the real instant), never the - // epoch, in both directions and at the `i64` extremes. - assert_eq!(civil(253_402_300_800), (9999, 1, 1, 0, 0, 0)); - assert_eq!(civil(10_000_000_000_000), (9999, 5, 20, 17, 46, 40)); - assert_eq!(civil(-10_000_000_000_000), (0, 8, 13, 6, 13, 20)); - assert_eq!(civil(i64::MAX), (9999, 12, 4, 15, 30, 7)); - assert_eq!(civil(i64::MIN), (0, 1, 27, 8, 29, 52)); - } +pub const REL_IMAGE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"; + +#[allow(dead_code)] +#[derive(Clone, Debug)] +pub struct Rel { + pub id: String, + pub kind: &'static str, + pub target: String, } diff --git a/crates/pptx/src/slide.rs b/crates/pptx/src/slide.rs index db4bb72c3..098a2deac 100644 --- a/crates/pptx/src/slide.rs +++ b/crates/pptx/src/slide.rs @@ -207,7 +207,7 @@ pub struct Exporter<'a> { pub media: Vec, /// Native PresentationML mappings for model elements the exporter can express directly. pub native_shapes: Vec, - pub native_media: Vec<(u32, String)>, + pub native_media: Vec<(u32, String, Option)>, /// Asset → (picture media name, SVG media name). media_of: HashMap)>>, pub fonts: Fonts, @@ -308,11 +308,9 @@ impl<'a> Exporter<'a> { } Ok(Slide { size: (bounds.width(), bounds.height()), - shapes: std::mem::take(&mut self.out), - rels: std::mem::take(&mut self.rels), name: page_name, - native_shapes: std::mem::take(&mut self.native_shapes), - native_media: std::mem::take(&mut self.native_media), + shapes: std::mem::take(&mut self.native_shapes), + media: std::mem::take(&mut self.native_media), }) } @@ -375,12 +373,14 @@ impl<'a> Exporter<'a> { fn group(&mut self, it: &Item, xf: Affine, opacity: f32) { let mut body = std::mem::take(&mut self.out); + let native_start = self.native_shapes.len(); let id = self.id(); for c in it.shown_children() { self.item(c, xf, opacity); } let inner = std::mem::take(&mut self.out); - if inner.is_empty() { + let children: Vec<_> = self.native_shapes.drain(native_start..).collect(); + if inner.is_empty() || children.is_empty() { self.out = body; return; } @@ -388,6 +388,14 @@ impl<'a> Exporter<'a> { let inner_bounds = it.shown_children().map(|c| c.bounds()).reduce(|a, b| a.union(b)).unwrap_or(Rect::ZERO); let b = (self.to_slide * xf).transform_rect_bbox(inner_bounds); let (x, y, w, h) = (emu(b.x0), emu(b.y0), emu_len(b.width()), emu_len(b.height())); + self.native_shapes.push(craft_pptx::Shape { + id: craft_pptx::ShapeId(id), + name: name_of(it, id), + xfrm: Some(craft_pptx::Xfrm::new(b.x0, b.y0, b.width(), b.height())), + kind: craft_pptx::ShapeKind::Group { children, child: craft_pptx::Xfrm::new(b.x0, b.y0, b.width(), b.height()) }, + descr: it.alt_text.clone(), + ..Default::default() + }); let _ = write!( body, "{inner}", @@ -438,9 +446,6 @@ impl<'a> Exporter<'a> { /// A shape whose complete appearance fits the shared PresentationML model. fn native_shape(&self, it: &Item, geometry: &Geometry, id: u32, opacity: f32) -> Option { - if it.fill.overprint || it.stroke.overprint || it.stroke.gap_overprint { - return None; - } let fill = self.model_fill(&it.fill, it.path.is_closed(), opacity); let line = self.model_line(&it.stroke, opacity)?; let effects = self.model_effects(it, opacity); @@ -526,9 +531,6 @@ impl<'a> Exporter<'a> { if stroke.is_none() || stroke.weight <= 0.0 { return Some(craft_pptx::Line::none()); } - if stroke.join == Join::Miter && (stroke.miter_limit - 4.0).abs() > 0.001 { - return None; - } let mut line = craft_pptx::Line { fill: Some(self.model_fill(&stroke.as_fill(), true, opacity)), width: Some(stroke.weight), @@ -542,6 +544,7 @@ impl<'a> Exporter<'a> { Join::Round => craft_pptx::style::LineJoin::Round, Join::Bevel => craft_pptx::style::LineJoin::Bevel, }), + miter_limit: (stroke.join == Join::Miter).then_some(pct(stroke.miter_limit.max(1.0)).max(1) as u32), align: Some(match stroke.align { StrokeAlign::Inside => craft_pptx::style::LineAlign::Inside, StrokeAlign::Center | StrokeAlign::Outside => craft_pptx::style::LineAlign::Center, @@ -909,32 +912,33 @@ impl<'a> Exporter<'a> { let line = if axis { self.line(&it.stroke, opacity) } else { "".into() }; let fx = self.effects(it, opacity); let id = self.id(); - if axis && svg.is_none() && it.effects == designcraft_doc::Effects::default() && it.fill.is_none() && it.stroke.is_none() { - let mut fill = craft_pptx::style::PictureFill { media: craft_pptx::MediaId(0), crop: crop_values, ..Default::default() }; - if opacity < 0.999 { - fill.alpha = Some(f64::from(opacity)); - } - self.native_shapes.push(craft_pptx::Shape { - id: craft_pptx::ShapeId(id), - name: name_of(it, id), - xfrm: Some(craft_pptx::Xfrm { - x: geo.xfrm.rect.x0, - y: geo.xfrm.rect.y0, - w: geo.xfrm.rect.width(), - h: geo.xfrm.rect.height(), - rot: geo.xfrm.rot, - flip_h: geo.xfrm.flip_h, - flip_v: geo.xfrm.flip_v, - }), - kind: craft_pptx::ShapeKind::Picture { fill }, - geom: geo.model.clone(), - fill: Some(craft_pptx::Fill::None), - line: Some(craft_pptx::Line::none()), - descr: it.alt_text.clone(), - ..Default::default() - }); - self.native_media.push((id, name.clone())); + let mut picture_fill = craft_pptx::style::PictureFill { media: craft_pptx::MediaId(0), crop: crop_values, ..Default::default() }; + if opacity < 0.999 { + picture_fill.alpha = Some(f64::from(opacity)); } + let model_line = if axis { self.model_line(&it.stroke, opacity).unwrap_or_else(craft_pptx::Line::none) } else { craft_pptx::Line::none() }; + let effects = self.model_effects(it, opacity); + self.native_shapes.push(craft_pptx::Shape { + id: craft_pptx::ShapeId(id), + name: name_of(it, id), + xfrm: Some(craft_pptx::Xfrm { + x: geo.xfrm.rect.x0, + y: geo.xfrm.rect.y0, + w: geo.xfrm.rect.width(), + h: geo.xfrm.rect.height(), + rot: geo.xfrm.rot, + flip_h: geo.xfrm.flip_h, + flip_v: geo.xfrm.flip_v, + }), + kind: craft_pptx::ShapeKind::Picture { fill: picture_fill }, + geom: geo.model.clone(), + fill: Some(if axis { self.model_fill(&it.fill, it.path.is_closed(), opacity) } else { craft_pptx::Fill::None }), + line: Some(model_line), + effects: (effects != craft_pptx::Effects::default()).then_some(effects), + descr: it.alt_text.clone(), + ..Default::default() + }); + self.native_media.push((id, name.clone(), svg)); let _ = write!( self.out, "\ @@ -948,14 +952,32 @@ impl<'a> Exporter<'a> { } /// Paragraph shading and rules, table cell fills: a plain filled rectangle in slide space. - pub fn rect_shape(&mut self, xfrm: XfrmBox, fill: &str, name: &str) { + pub fn rect_shape(&mut self, xfrm: XfrmBox, fill_xml: &str, fill: craft_pptx::Fill, name: &str) { let id = self.id(); + self.native_shapes.push(craft_pptx::Shape { + id: craft_pptx::ShapeId(id), + name: name.into(), + xfrm: Some(craft_pptx::Xfrm { + x: xfrm.rect.x0, + y: xfrm.rect.y0, + w: xfrm.rect.width(), + h: xfrm.rect.height(), + rot: xfrm.rot, + flip_h: xfrm.flip_h, + flip_v: xfrm.flip_v, + }), + geom: craft_pptx::Geom::preset("rect"), + fill: Some(fill), + line: Some(craft_pptx::Line::none()), + ..Default::default() + }); let _ = write!( self.out, - "{}{}{fill}", + "{}{}{}", esc(name), xfrm.xml(), - preset("rect") + preset("rect"), + fill_xml ); } } diff --git a/crates/pptx/src/text.rs b/crates/pptx/src/text.rs index 6854d4a29..2896a06d1 100644 --- a/crates/pptx/src/text.rs +++ b/crates/pptx/src/text.rs @@ -166,7 +166,8 @@ fn decos(ex: &mut Exporter, ft: &FrameText, m: Affine, opacity: f32) { for d in &ft.decos { let Some(c) = ex.color(&d.color, d.tint, opacity) else { continue }; if let Some((xfrm, _)) = box_of(d.rect, m) { - ex.rect_shape(xfrm, &format!("{c}"), "Paragraph Shading"); + let model_fill = ex.model_color(&d.color, d.tint, opacity).map_or(craft_pptx::Fill::None, craft_pptx::Fill::solid); + ex.rect_shape(xfrm, &format!("{c}"), model_fill, "Paragraph Shading"); } } } @@ -696,6 +697,7 @@ fn reflow(ex: &mut Exporter, it: &Item, tfr: &TextFrame, ft: &FrameText, cs: &Co let o = &tfr.options; let m = ex.to_slide * xf; let k = box_of(it.inner_bounds(), m).map_or(1.0, |(_, d)| (d.sx * d.sy).abs().sqrt()); + let mut model = reflow_model(ex, it, tfr, ft, cs, story, k, opacity, vertical); let [t, l, b, r] = o.inset.map(|v| emu_len(v * k)); let cols = ft.columns.len().max(1); let mut s = format!(""); - ex.shape(it, xf, opacity, Some(&s)); + ex.shape_with_text_model(it, xf, opacity, &s, std::mem::take(&mut model)); +} + +/// Reflow text in the shared text model. PowerPoint is free to break each paragraph again, but +/// the words, run formatting, paragraph alignment, columns and frame options stay editable. +#[allow(clippy::too_many_arguments)] +fn reflow_model( + ex: &mut Exporter, + it: &Item, + tfr: &TextFrame, + ft: &FrameText, + cs: &ComposedStory, + story: &str, + k: f64, + opacity: f32, + vertical: bool, +) -> craft_pptx::TextBody { + let mut lines: Vec = Vec::new(); + let mut by_para: std::collections::BTreeMap> = std::collections::BTreeMap::new(); + for line in &ft.lines { + by_para.entry(line.para).or_default().push(line); + } + for (para, source) in by_para { + let mut merged: Option = None; + for line in source { + let mut out = out_line(ex, cs, line, story); + out.para = para; + out.last_in_para = true; + if line.hyphenated + && let Some(run) = out.runs.last_mut() + && run.text.ends_with(['-', '\u{2010}']) + { + run.text.pop(); + } + if out.trailing_space + && let Some(run) = out.runs.last_mut() + && !run.text.ends_with(char::is_whitespace) + { + run.text.push(' '); + } + match &mut merged { + Some(previous) => { + previous.end = out.end; + previous.size = previous.size.max(out.size); + previous.leading = out.leading; + previous.runs.extend(out.runs); + previous.tabs.extend(out.tabs); + previous.trailing_space = out.trailing_space; + } + None => merged = Some(out), + } + } + if let Some(mut line) = merged { + for run in &mut line.runs { + run.spc = 0.0; + } + lines.push(line); + } + } + let inner = it.inner_bounds(); + let mut segment = Segment { lines, x0: inner.x0 + tfr.options.inset[1], x1: inner.x1 - tfr.options.inset[3] }; + let insets = [tfr.options.inset[1], tfr.options.inset[0], tfr.options.inset[3], tfr.options.inset[2]]; + let mut body = model_body(ex, &segment, k, opacity, insets); + segment.lines.clear(); + body.body.wrap = Some(true); + body.body.columns = Some(u32::try_from(ft.columns.len().max(1)).unwrap_or(u32::MAX)); + body.body.col_spacing = Some(tfr.options.gutter * k); + body.body.vert = vertical.then_some(craft_pptx::text::TextDir::EaVertical); + body.body.anchor = Some(match tfr.options.vertical_justification { + designcraft_doc::VerticalJustification::Top => craft_pptx::text::Anchor::Top, + designcraft_doc::VerticalJustification::Center => craft_pptx::text::Anchor::Middle, + designcraft_doc::VerticalJustification::Bottom => craft_pptx::text::Anchor::Bottom, + designcraft_doc::VerticalJustification::Justify => craft_pptx::text::Anchor::Justified, + }); + for paragraph in &mut body.paragraphs { + for run in &mut paragraph.runs { + run.props.spacing = Some(0.0); + } + } + body } From e65f6c30783099471940fd99c5f93929853de4b8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:05:09 -0500 Subject: [PATCH 030/153] Remove app-side PPTX XML generation --- crates/pptx/src/lib.rs | 3 +- crates/pptx/src/model.rs | 18 ++ crates/pptx/src/package.rs | 28 --- crates/pptx/src/slide.rs | 420 +++++-------------------------------- crates/pptx/src/table.rs | 115 ---------- crates/pptx/src/text.rs | 290 ++----------------------- crates/pptx/src/xml.rs | 75 ------- 7 files changed, 84 insertions(+), 865 deletions(-) create mode 100644 crates/pptx/src/model.rs delete mode 100644 crates/pptx/src/package.rs delete mode 100644 crates/pptx/src/xml.rs diff --git a/crates/pptx/src/lib.rs b/crates/pptx/src/lib.rs index 3ccf3d09c..a08c1130e 100644 --- a/crates/pptx/src/lib.rs +++ b/crates/pptx/src/lib.rs @@ -20,11 +20,10 @@ #![forbid(unsafe_code)] mod fonts; -mod package; +mod model; mod slide; mod table; mod text; -mod xml; #[cfg(test)] mod tests; diff --git a/crates/pptx/src/model.rs b/crates/pptx/src/model.rs new file mode 100644 index 000000000..9a1aa8ec5 --- /dev/null +++ b/crates/pptx/src/model.rs @@ -0,0 +1,18 @@ +//! Data returned by the DesignCraft-to-Presentation model adapter. + +#[derive(Clone, Debug, Default)] +pub struct Slide { + /// Page size in points. + pub size: (f64, f64), + pub name: String, + /// Model shapes in stacking order. + pub shapes: Vec, + /// Media names keyed by picture shape id. + pub media: Vec<(u32, String, Option)>, +} + +#[derive(Clone, Debug)] +pub struct Media { + pub name: String, + pub bytes: Vec, +} diff --git a/crates/pptx/src/package.rs b/crates/pptx/src/package.rs deleted file mode 100644 index d24ef4cbb..000000000 --- a/crates/pptx/src/package.rs +++ /dev/null @@ -1,28 +0,0 @@ -//! Data returned by the DesignCraft-to-PresentationML model adapter. - -#[derive(Clone, Debug, Default)] -pub struct Slide { - /// Page size in points. - pub size: (f64, f64), - pub name: String, - /// Model shapes in stacking order. - pub shapes: Vec, - /// Media part names keyed by the id of a picture shape. - pub media: Vec<(u32, String, Option)>, -} - -#[derive(Clone, Debug)] -pub struct Media { - pub name: String, - pub bytes: Vec, -} - -pub const REL_IMAGE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"; - -#[allow(dead_code)] -#[derive(Clone, Debug)] -pub struct Rel { - pub id: String, - pub kind: &'static str, - pub target: String, -} diff --git a/crates/pptx/src/slide.rs b/crates/pptx/src/slide.rs index 098a2deac..64b44d6a8 100644 --- a/crates/pptx/src/slide.rs +++ b/crates/pptx/src/slide.rs @@ -1,18 +1,16 @@ //! A page as a slide: items in stacking order as shapes, pictures, groups and text boxes. use std::collections::HashMap; -use std::fmt::Write as _; use designcraft_color::swatch::resolve_gradient; use designcraft_color::{BlendMode, GradientKind}; use designcraft_compose::Cache; use designcraft_doc::{Arrowhead, AssetId, Cap, Content, Document, Graphic, Item, Join, Shape, Stroke, StrokeAlign, StrokeType}; use designcraft_geom::kurbo::{PathEl, Shape as _}; -use designcraft_geom::{Affine, BezPath, Point, Rect, Vec2, corners}; +use designcraft_geom::{Affine, BezPath, Rect, Vec2, corners}; use crate::fonts::Fonts; -use crate::package::{Media, REL_IMAGE, Rel, Slide}; -use crate::xml::{angle, emu, emu_len, esc, hex, pct}; +use crate::model::{Media, Slide}; use crate::{PptxError, PptxOptions, Result}; /// Objects PowerPoint can't show as editable objects, which a caller should rasterise before @@ -31,7 +29,7 @@ pub fn needs_raster(doc: &Document, it: &Item) -> bool { effects || on_path || it.graphic().is_some_and(placed) || it.children().iter().any(|c| needs_raster(doc, c)) } -/// A DrawingML transform box (slide space, points). +/// A transform box in slide coordinates, expressed in points. #[derive(Clone, Copy, Debug, PartialEq)] pub struct XfrmBox { pub rect: Rect, @@ -44,27 +42,6 @@ impl XfrmBox { pub fn plain(rect: Rect) -> Self { XfrmBox { rect, rot: 0.0, flip_h: false, flip_v: false } } - - pub fn xml(&self) -> String { - let mut a = String::new(); - let r = angle(self.rot); - if r != 0 { - let _ = write!(a, " rot=\"{r}\""); - } - if self.flip_h { - a.push_str(" flipH=\"1\""); - } - if self.flip_v { - a.push_str(" flipV=\"1\""); - } - format!( - "", - emu(self.rect.x0), - emu(self.rect.y0), - emu_len(self.rect.width()), - emu_len(self.rect.height()) - ) - } } /// An affine split into rotation, axis scales and shear: `m = translate · rotate(rot) · @@ -104,50 +81,9 @@ pub fn box_of(inner: Rect, m: Affine) -> Option<(XfrmBox, Decomposed)> { /// Shape geometry: a preset, or a custom path in the box's own (unrotated) space. pub struct Geometry { pub xfrm: XfrmBox, - pub xml: String, pub model: craft_pptx::Geom, } -fn custom_geometry(bp: &BezPath, w: f64, h: f64, filled: bool) -> String { - let (we, he) = (emu_len(w).max(1), emu_len(h).max(1)); - let mut p = format!( - "", - if filled { "" } else { " fill=\"none\"" } - ); - let pt = |q: Point| format!("", emu(q.x), emu(q.y)); - let mut last = Point::ZERO; - for el in bp.elements() { - match *el { - PathEl::MoveTo(a) => { - let _ = write!(p, "{}", pt(a)); - last = a; - } - PathEl::LineTo(a) => { - let _ = write!(p, "{}", pt(a)); - last = a; - } - PathEl::QuadTo(a, b) => { - // As a cubic: PowerPoint's quadratic support is patchy. - let c1 = last + (a - last) * (2.0 / 3.0); - let c2 = b + (a - b) * (2.0 / 3.0); - let _ = write!(p, "{}{}{}", pt(c1), pt(c2), pt(b)); - last = b; - } - PathEl::CurveTo(a, b, c) => { - let _ = write!(p, "{}{}{}", pt(a), pt(b), pt(c)); - last = c; - } - PathEl::ClosePath => p.push_str(""), - } - } - p.push_str(""); - p -} - -fn preset(name: &str) -> String { - format!("") -} - /// Is `bp` exactly the rectangle `r` (any start corner and direction)? fn is_rect(bp: &BezPath, r: Rect) -> bool { let mut pts = Vec::new(); @@ -172,10 +108,10 @@ pub fn geometry(it: &Item, m: Affine) -> Geometry { let inner = bp.bounding_box(); if let Some((xfrm, dm)) = box_of(inner, m) { if it.corners.is_none() && is_rect(&bp, inner) { - return Geometry { xfrm, xml: preset("rect"), model: craft_pptx::Geom::preset("rect") }; + return Geometry { xfrm, model: craft_pptx::Geom::preset("rect") }; } if it.shape == Shape::Oval && it.corners.is_none() && closed { - return Geometry { xfrm, xml: preset("ellipse"), model: craft_pptx::Geom::preset("ellipse") }; + return Geometry { xfrm, model: craft_pptx::Geom::preset("ellipse") }; } // The path in the box's own space: scaled (not flipped: the box flips), from its corner. let (kx, ky) = (dm.sx.abs(), dm.sy.abs()); @@ -183,7 +119,6 @@ pub fn geometry(it: &Item, m: Affine) -> Geometry { local.apply_affine(Affine::scale_non_uniform(kx, ky) * Affine::translate(-inner.origin().to_vec2())); return Geometry { xfrm, - xml: custom_geometry(&local, xfrm.rect.width(), xfrm.rect.height(), closed), model: craft_pptx::Geom::Custom { paths: vec![craft_pptx::CustomPath::from_bez(&local, xfrm.rect.width(), xfrm.rect.height(), closed)] }, }; } @@ -194,7 +129,6 @@ pub fn geometry(it: &Item, m: Affine) -> Geometry { pb.apply_affine(Affine::translate(-b.origin().to_vec2())); Geometry { xfrm: XfrmBox::plain(b), - xml: custom_geometry(&pb, b.width(), b.height(), closed), model: craft_pptx::Geom::Custom { paths: vec![craft_pptx::CustomPath::from_bez(&pb, b.width(), b.height(), closed)] }, } } @@ -205,17 +139,13 @@ pub struct Exporter<'a> { pub opts: &'a PptxOptions, pub warnings: Vec, pub media: Vec, - /// Native PresentationML mappings for model elements the exporter can express directly. + /// Model shapes in stacking order. pub native_shapes: Vec, pub native_media: Vec<(u32, String, Option)>, /// Asset → (picture media name, SVG media name). media_of: HashMap)>>, pub fonts: Fonts, - // Per slide. - pub out: String, next_id: u32, - rels: Vec, - rel_of: HashMap, /// Spread space → slide space. pub to_slide: Affine, /// The page's bleed area in spread space (items outside it are left out). @@ -236,10 +166,7 @@ impl<'a> Exporter<'a> { native_media: Vec::new(), media_of: HashMap::new(), fonts: Fonts::default(), - out: String::new(), next_id: 2, - rels: Vec::new(), - rel_of: HashMap::new(), to_slide: Affine::IDENTITY, clip: Rect::ZERO, page: None, @@ -265,12 +192,9 @@ impl<'a> Exporter<'a> { let sp = doc.spreads.get(si).ok_or(PptxError::BadPage(abs + 1))?; let page = sp.pages.get(pi).ok_or(PptxError::BadPage(abs + 1))?; let bounds = page.bounds(); - self.out.clear(); self.native_shapes.clear(); self.native_media.clear(); self.next_id = 2; - self.rels.clear(); - self.rel_of.clear(); self.to_slide = Affine::translate(-bounds.origin().to_vec2()); let b = doc.settings.bleed; self.clip = Rect::new(bounds.x0 - b[2].max(b[3]), bounds.y0 - b[0], bounds.x1 + b[2].max(b[3]), bounds.y1 + b[1]); @@ -314,16 +238,6 @@ impl<'a> Exporter<'a> { }) } - fn rel(&mut self, target: String) -> String { - if let Some(r) = self.rel_of.get(&target) { - return r.clone(); - } - let id = format!("rId{}", self.rels.len() + 2); - self.rels.push(Rel { id: id.clone(), kind: REL_IMAGE, target: target.clone() }); - self.rel_of.insert(target, id.clone()); - id - } - /// One item under `parent` (inner-of-parent → spread), with an inherited opacity. pub fn item(&mut self, it: &Item, parent: Affine, opacity: f32) { if it.hidden || it.nonprinting { @@ -346,7 +260,7 @@ impl<'a> Exporter<'a> { let opacity = opacity * it.opacity.clamp(0.0, 1.0); if it.has_nested_items() { // Paste Into: the frame, then its contents (PowerPoint can't clip a group to a shape). - self.shape(it, xf, opacity, None); + self.shape(it, xf, opacity); let fb = xf.transform_rect_bbox(it.inner_bounds()); for c in it.children() { let cb = xf.transform_rect_bbox(c.bounds()); @@ -367,27 +281,23 @@ impl<'a> Exporter<'a> { let t = t.clone(); crate::text::frame(self, it, &t, xf, opacity); } - _ => self.shape(it, xf, opacity, None), + _ => self.shape(it, xf, opacity), } } fn group(&mut self, it: &Item, xf: Affine, opacity: f32) { - let mut body = std::mem::take(&mut self.out); let native_start = self.native_shapes.len(); let id = self.id(); for c in it.shown_children() { self.item(c, xf, opacity); } - let inner = std::mem::take(&mut self.out); let children: Vec<_> = self.native_shapes.drain(native_start..).collect(); - if inner.is_empty() || children.is_empty() { - self.out = body; + if children.is_empty() { return; } // Children are in slide space: the group's child space is the slide's. let inner_bounds = it.shown_children().map(|c| c.bounds()).reduce(|a, b| a.union(b)).unwrap_or(Rect::ZERO); let b = (self.to_slide * xf).transform_rect_bbox(inner_bounds); - let (x, y, w, h) = (emu(b.x0), emu(b.y0), emu_len(b.width()), emu_len(b.height())); self.native_shapes.push(craft_pptx::Shape { id: craft_pptx::ShapeId(id), name: name_of(it, id), @@ -396,58 +306,35 @@ impl<'a> Exporter<'a> { descr: it.alt_text.clone(), ..Default::default() }); - let _ = write!( - body, - "{inner}", - esc(&name_of(it, id)), - descr(it) - ); - self.out = body; } - /// A shape with the item's geometry, fill, stroke and effects; `text` is a ``. - pub fn shape(&mut self, it: &Item, xf: Affine, opacity: f32, text: Option<&str>) { - self.shape_inner(it, xf, opacity, text, None); + /// Add the item's geometry, fill, stroke and effects to the shared presentation model. + pub fn shape(&mut self, it: &Item, xf: Affine, opacity: f32) { + self.shape_inner(it, xf, opacity, None); } - /// A text-bearing shape with both its compatibility fragment and shared model representation. - pub fn shape_with_text_model(&mut self, it: &Item, xf: Affine, opacity: f32, text: &str, model: craft_pptx::TextBody) { - self.shape_inner(it, xf, opacity, Some(text), Some(model)); + /// Add a text-bearing shape to the shared presentation model. + pub fn shape_with_text_model(&mut self, it: &Item, xf: Affine, opacity: f32, model: craft_pptx::TextBody) { + self.shape_inner(it, xf, opacity, Some(model)); } - fn shape_inner(&mut self, it: &Item, xf: Affine, opacity: f32, text: Option<&str>, model_text: Option) { + fn shape_inner(&mut self, it: &Item, xf: Affine, opacity: f32, model_text: Option) { let m = self.to_slide * xf; let g = geometry(it, m); - let fill = self.fill(&it.fill, it.path.is_closed(), opacity); - let line = self.line(&it.stroke, opacity); - let fx = self.effects(it, opacity); - if text.is_none() && fill == "" && line.contains("") && fx.is_empty() { + if model_text.is_none() && it.fill.is_none() && it.stroke.is_none() && it.effects == designcraft_doc::Effects::default() { return; } let id = self.id(); - if (text.is_none() || model_text.is_some()) - && let Some(shape) = self.native_shape(it, &g, id, opacity) - { - let mut shape = shape; - shape.text = model_text; - self.native_shapes.push(shape); - } - let txbox = if text.is_some() && it.fill.is_none() && it.stroke.is_none() { " txBox=\"1\"" } else { "" }; - let _ = write!( - self.out, - "{}{}{fill}{line}{fx}{}", - esc(&name_of(it, id)), - descr(it), - g.xfrm.xml(), - g.xml, - text.unwrap_or("") - ); + let Some(mut shape) = self.native_shape(it, &g, id, opacity) else { return }; + shape.text_box = it.fill.is_none() && it.stroke.is_none(); + shape.text = model_text; + self.native_shapes.push(shape); } - /// A shape whose complete appearance fits the shared PresentationML model. - fn native_shape(&self, it: &Item, geometry: &Geometry, id: u32, opacity: f32) -> Option { + /// A shape mapped into the shared presentation model. + fn native_shape(&mut self, it: &Item, geometry: &Geometry, id: u32, opacity: f32) -> Option { let fill = self.model_fill(&it.fill, it.path.is_closed(), opacity); - let line = self.model_line(&it.stroke, opacity)?; + let line = self.model_line(&it.stroke, opacity); let effects = self.model_effects(it, opacity); let xfrm = craft_pptx::Xfrm { x: geometry.xfrm.rect.x0, @@ -471,17 +358,6 @@ impl<'a> Exporter<'a> { }) } - /// An sRGB colour element for a swatch and tint, `None` for [None] and unknown swatches. - pub fn color(&self, swatch: &str, tint: f32, alpha: f32) -> Option { - if let Some(g) = resolve_gradient(&self.doc.swatches, swatch) { - // A gradient used where only a colour fits (text): its first stop. - let (_, c, a) = g.expanded_stops().into_iter().next()?; - return Some(srgb(c.to_rgb(), a * alpha)); - } - let c = self.doc.resolve_color(swatch, tint)?; - Some(srgb(c.to_rgb(), alpha)) - } - /// An sRGB color in the shared PresentationML model, with DesignCraft tint and opacity applied. pub(crate) fn model_color(&self, swatch: &str, tint: f32, alpha: f32) -> Option { if let Some(gradient) = resolve_gradient(&self.doc.swatches, swatch) { @@ -493,11 +369,14 @@ impl<'a> Exporter<'a> { Some(craft_pptx::ColorRef::rgb(craft_pptx::Rgba::rgba(r, g, b, a))) } - fn model_fill(&self, fill: &designcraft_doc::Fill, closed: bool, opacity: f32) -> craft_pptx::Fill { + fn model_fill(&mut self, fill: &designcraft_doc::Fill, closed: bool, opacity: f32) -> craft_pptx::Fill { if fill.is_none() || !closed { return craft_pptx::Fill::None; } if let Some(gradient) = resolve_gradient(&self.doc.swatches, &fill.swatch) { + if fill.gradient_vector.is_some() { + self.warn("gradients set with the Gradient tool use their angle only; PowerPoint stretches them over the whole shape"); + } let stops = gradient .expanded_stops() .into_iter() @@ -527,9 +406,18 @@ impl<'a> Exporter<'a> { self.model_color(&fill.swatch, fill.tint, opacity).map_or(craft_pptx::Fill::None, craft_pptx::Fill::solid) } - fn model_line(&self, stroke: &Stroke, opacity: f32) -> Option { + fn model_line(&mut self, stroke: &Stroke, opacity: f32) -> craft_pptx::Line { if stroke.is_none() || stroke.weight <= 0.0 { - return Some(craft_pptx::Line::none()); + return craft_pptx::Line::none(); + } + if matches!(stroke.kind, StrokeType::Wavy | StrokeType::Hashed | StrokeType::Stripes { .. } | StrokeType::Style { .. }) { + self.warn("wavy, hashed, striped and custom stroke styles are exported as solid lines"); + } + if stroke.align == StrokeAlign::Outside { + self.warn("strokes aligned outside are centred on the path (PowerPoint has centre and inside only)"); + } + if stroke.start == Arrowhead::Bar || stroke.end == Arrowhead::Bar { + self.warn("bar arrowheads have no PowerPoint equivalent and are left out"); } let mut line = craft_pptx::Line { fill: Some(self.model_fill(&stroke.as_fill(), true, opacity)), @@ -544,7 +432,7 @@ impl<'a> Exporter<'a> { Join::Round => craft_pptx::style::LineJoin::Round, Join::Bevel => craft_pptx::style::LineJoin::Bevel, }), - miter_limit: (stroke.join == Join::Miter).then_some(pct(stroke.miter_limit.max(1.0)).max(1) as u32), + miter_limit: (stroke.join == Join::Miter).then_some((stroke.miter_limit.max(1.0) * 100_000.0).round() as u32), align: Some(match stroke.align { StrokeAlign::Inside => craft_pptx::style::LineAlign::Inside, StrokeAlign::Center | StrokeAlign::Outside => craft_pptx::style::LineAlign::Center, @@ -587,7 +475,7 @@ impl<'a> Exporter<'a> { }; line.head = end(stroke.start); line.tail = end(stroke.end); - Some(line) + line } fn model_effects(&self, it: &Item, opacity: f32) -> craft_pptx::Effects { @@ -650,155 +538,6 @@ impl<'a> Exporter<'a> { craft_pptx::Effects { outer_shadow, inner_shadow, glow, soft_edge: (fx.feather > 0.0).then_some(fx.feather), ..Default::default() } } - pub fn fill(&mut self, f: &designcraft_doc::Fill, closed: bool, opacity: f32) -> String { - if f.is_none() || !closed { - return "".into(); - } - if let Some(g) = resolve_gradient(&self.doc.swatches, &f.swatch) { - let mut gs = String::new(); - for (o, c, a) in g.expanded_stops() { - let _ = write!(gs, "{}", pct(o.clamp(0.0, 1.0) as f64), srgb(c.to_rgb(), a * opacity)); - } - if gs.is_empty() { - return "".into(); - } - if f.gradient_vector.is_some() { - self.warn("gradients set with the Gradient tool use their angle only; PowerPoint stretches them over the whole shape"); - } - let shade = match g.kind { - GradientKind::Radial => { - "".to_string() - } - _ => { - let deg = match f.gradient_vector { - Some([x0, y0, x1, y1]) => (y1 - y0).atan2(x1 - x0).to_degrees(), - None => -f.gradient_angle.unwrap_or(0.0), - }; - format!("", angle(deg)) - } - }; - return format!("{gs}{shade}"); - } - match self.color(&f.swatch, f.tint, opacity) { - Some(c) => format!("{c}"), - None => "".into(), - } - } - - pub fn line(&mut self, s: &Stroke, opacity: f32) -> String { - if s.is_none() || s.weight <= 0.0 { - return "".into(); - } - let Some(color) = self.color(&s.swatch, s.tint, opacity) else { return "".into() }; - let cap = match s.cap { - Cap::Butt => "flat", - Cap::Round => "rnd", - Cap::Projecting => "sq", - }; - let cmpd = match s.kind { - StrokeType::ThickThin => "thickThin", - StrokeType::ThinThick => "thinThick", - StrokeType::ThinThin | StrokeType::ThickThick => "dbl", - StrokeType::ThinThickThin | StrokeType::ThickThinThick => "tri", - _ => "sng", - }; - if matches!(s.kind, StrokeType::Wavy | StrokeType::Hashed | StrokeType::Stripes { .. } | StrokeType::Style { .. }) { - self.warn("wavy, hashed, striped and custom stroke styles are exported as solid lines"); - } - let algn = match s.align { - StrokeAlign::Inside => " algn=\"in\"", - StrokeAlign::Outside => { - self.warn("strokes aligned outside are centred on the path (PowerPoint has centre and inside only)"); - "" - } - StrokeAlign::Center => "", - }; - let w = s.weight; - let dash = match &s.kind { - StrokeType::Dashed { pattern } if pattern.iter().any(|v| *v > 0.0) => { - let mut v: Vec = pattern.iter().map(|x| x.max(0.0)).collect(); - if v.len() % 2 == 1 { - v.extend(v.clone()); - } - let mut d = String::from(""); - for pair in v.chunks(2) { - if let [a, b] = pair { - let _ = write!(d, "", pct(a / w), pct(b / w)); - } - } - d.push_str(""); - d - } - StrokeType::Dotted => "".into(), - _ => "".into(), - }; - let join = match s.join { - Join::Miter => format!("", pct(s.miter_limit.max(1.0))), - Join::Round => "".into(), - Join::Bevel => "".into(), - }; - let ends = |tag: &str, a: Arrowhead, me: &mut Self| -> String { - let kind = match a { - Arrowhead::None => return String::new(), - Arrowhead::Simple | Arrowhead::SimpleWide => "arrow", - Arrowhead::Triangle | Arrowhead::TriangleWide | Arrowhead::Curved => "triangle", - Arrowhead::Barbed => "stealth", - Arrowhead::Circle | Arrowhead::CircleSolid => "oval", - Arrowhead::Square | Arrowhead::SquareSolid => "diamond", - Arrowhead::Bar => { - me.warn("bar arrowheads have no PowerPoint equivalent and are left out"); - return String::new(); - } - }; - // PowerPoint's large heads come closest to DesignCraft's at the same weight. - format!("") - }; - let head = ends("headEnd", s.start, self); - let tail = ends("tailEnd", s.end, self); - format!("{color}{dash}{join}{head}{tail}", emu_len(w)) - } - - /// The item's effects that DrawingML has (in schema order). - pub fn effects(&mut self, it: &Item, opacity: f32) -> String { - let fx = &it.effects; - let mut s = String::new(); - if fx.outer_glow.on - && let Some(c) = self.color(&fx.outer_glow.color, 1.0, fx.outer_glow.opacity * opacity) - { - let _ = write!(s, "{c}", emu_len(fx.outer_glow.size)); - } - if fx.inner_shadow.on { - let sh = &fx.inner_shadow; - if let Some(c) = self.color(&sh.color, 1.0, sh.opacity * opacity) { - let a = self.doc.light_angle(sh.angle, sh.global_light); - let _ = write!( - s, - "{c}", - emu_len(sh.size), - emu_len(sh.distance.abs()), - shadow_dir(a) - ); - } - } - if fx.drop_shadow.on { - let sh = &fx.drop_shadow; - if let Some(c) = self.color(&sh.color, 1.0, sh.opacity * opacity) { - let a = self.doc.light_angle(sh.angle, sh.global_light); - let _ = write!( - s, - "{c}", - emu_len(sh.size), - emu_len(sh.distance.abs()), - shadow_dir(a) - ); - } - } - if fx.feather > 0.0 { - let _ = write!(s, "", emu_len(fx.feather)); - } - if s.is_empty() { s } else { format!("{s}") } - } - /// The media for a placed graphic: PNG, JPEG and GIF as they are, SVG with a PNG fallback, /// other rasters converted to PNG. fn media_for(&mut self, asset: AssetId) -> Option<(String, Option)> { @@ -863,7 +602,7 @@ impl<'a> Exporter<'a> { fn picture(&mut self, it: &Item, g: &Graphic, xf: Affine, opacity: f32) { let g = if it.media.is_some() { it.drawn_graphic(&self.doc.assets).unwrap_or_else(|| g.clone()) } else { g.clone() }; let Some((name, svg)) = self.media_for(g.asset) else { - self.shape(it, xf, opacity, None); + self.shape(it, xf, opacity); return; }; let m = self.to_slide * xf; @@ -871,7 +610,7 @@ impl<'a> Exporter<'a> { let frame = it.inner_bounds(); let img = g.xf.transform_rect_bbox(Rect::new(0.0, 0.0, g.size.0, g.size.1)); let axis = b.abs() < 1e-9 && c.abs() < 1e-9 && a > 0.0 && d > 0.0; - let (geo, crop, crop_values) = if axis && img.width() > 0.0 && img.height() > 0.0 { + let (geo, crop_values) = if axis && img.width() > 0.0 && img.height() > 0.0 { // The frame's geometry with the image cropped (or padded) to the frame's box. let crop_values = [ (frame.x0 - img.x0) / img.width(), @@ -879,44 +618,24 @@ impl<'a> Exporter<'a> { (img.x1 - frame.x1) / img.width(), (img.y1 - frame.y1) / img.height(), ]; - let crop = format!( - "", - pct(crop_values[0]), - pct(crop_values[1]), - pct(crop_values[2]), - pct(crop_values[3]) - ); - (geometry(it, m), crop, crop_values) + (geometry(it, m), crop_values) } else { // Rotated, skewed or flipped inside its frame: the image's own box, uncropped, and // the frame drawn on its own. self.warn("images rotated, skewed or flipped inside their frames are not cropped to the frame"); - self.shape(it, xf, opacity, None); + self.shape(it, xf, opacity); let gi = Rect::new(0.0, 0.0, g.size.0, g.size.1); let gm = m * g.xf; let xfrm = box_of(gi, gm).map(|(x, _)| x).unwrap_or_else(|| XfrmBox::plain(gm.transform_rect_bbox(gi))); - (Geometry { xfrm, xml: preset("rect"), model: craft_pptx::Geom::preset("rect") }, String::new(), [0.0; 4]) + (Geometry { xfrm, model: craft_pptx::Geom::preset("rect") }, [0.0; 4]) }; - let rid = self.rel(format!("../media/{name}")); - let svg_ext = match svg.as_ref() { - Some(s) => { - let sid = self.rel(format!("../media/{s}")); - format!( - "" - ) - } - None => String::new(), - }; - let alpha = if opacity < 0.999 { format!("", pct(opacity as f64)) } else { String::new() }; - let fill = self.fill(&it.fill, it.path.is_closed(), opacity); - let line = if axis { self.line(&it.stroke, opacity) } else { "".into() }; - let fx = self.effects(it, opacity); let id = self.id(); let mut picture_fill = craft_pptx::style::PictureFill { media: craft_pptx::MediaId(0), crop: crop_values, ..Default::default() }; if opacity < 0.999 { picture_fill.alpha = Some(f64::from(opacity)); } - let model_line = if axis { self.model_line(&it.stroke, opacity).unwrap_or_else(craft_pptx::Line::none) } else { craft_pptx::Line::none() }; + let model_line = if axis { self.model_line(&it.stroke, opacity) } else { craft_pptx::Line::none() }; + let model_fill = if axis { self.model_fill(&it.fill, it.path.is_closed(), opacity) } else { craft_pptx::Fill::None }; let effects = self.model_effects(it, opacity); self.native_shapes.push(craft_pptx::Shape { id: craft_pptx::ShapeId(id), @@ -932,27 +651,17 @@ impl<'a> Exporter<'a> { }), kind: craft_pptx::ShapeKind::Picture { fill: picture_fill }, geom: geo.model.clone(), - fill: Some(if axis { self.model_fill(&it.fill, it.path.is_closed(), opacity) } else { craft_pptx::Fill::None }), + fill: Some(model_fill), line: Some(model_line), effects: (effects != craft_pptx::Effects::default()).then_some(effects), descr: it.alt_text.clone(), ..Default::default() }); self.native_media.push((id, name.clone(), svg)); - let _ = write!( - self.out, - "\ -{alpha}{svg_ext}{crop}\ -{}{}{fill}{line}{fx}", - esc(&name_of(it, id)), - descr(it), - geo.xfrm.xml(), - geo.xml - ); } /// Paragraph shading and rules, table cell fills: a plain filled rectangle in slide space. - pub fn rect_shape(&mut self, xfrm: XfrmBox, fill_xml: &str, fill: craft_pptx::Fill, name: &str) { + pub fn rect_shape(&mut self, xfrm: XfrmBox, fill: craft_pptx::Fill, name: &str) { let id = self.id(); self.native_shapes.push(craft_pptx::Shape { id: craft_pptx::ShapeId(id), @@ -971,31 +680,6 @@ impl<'a> Exporter<'a> { line: Some(craft_pptx::Line::none()), ..Default::default() }); - let _ = write!( - self.out, - "{}{}{}", - esc(name), - xfrm.xml(), - preset("rect"), - fill_xml - ); - } -} - -/// DrawingML shadow direction (clockwise from +x, y down) for a light angle (degrees, -/// counter-clockwise; the shadow falls away from the light). -fn shadow_dir(light: f64) -> i64 { - let a = light.to_radians(); - let off = Vec2::new(-a.cos(), a.sin()); - angle(off.y.atan2(off.x).to_degrees()) -} - -pub fn srgb(rgb: [f32; 3], alpha: f32) -> String { - let a = alpha.clamp(0.0, 1.0); - if a >= 0.999 { - format!("", hex(rgb)) - } else { - format!("", hex(rgb), pct(a as f64)) } } @@ -1016,10 +700,6 @@ fn name_of(it: &Item, id: u32) -> String { format!("{kind} {id}") } -fn descr(it: &Item) -> String { - if it.alt_text.is_empty() { String::new() } else { format!(" descr=\"{}\"", esc(&it.alt_text)) } -} - fn unpremultiply(mut px: Vec) -> Vec { for p in px.as_chunks_mut::<4>().0 { let a = p[3] as u32; diff --git a/crates/pptx/src/table.rs b/crates/pptx/src/table.rs index 37a36f91e..108cdb66f 100644 --- a/crates/pptx/src/table.rs +++ b/crates/pptx/src/table.rs @@ -1,15 +1,11 @@ //! Table fragments as native PowerPoint tables: the grid from the cells' edges, merged cells as //! spans, fills, edges as cell borders, and each cell's composed text. -use std::fmt::Write as _; - use designcraft_compose::{FrameText, PlacedCell, StrokeSeg, TableFrag}; use designcraft_doc::StrokeType; use designcraft_geom::{Affine, Rect}; use crate::slide::{Exporter, box_of}; -use crate::text::{paragraphs, seg_rect, segments}; -use crate::xml::{emu, emu_len}; /// Sorted distinct values (within 0.25 pt). fn edges(mut v: Vec) -> Vec { @@ -86,21 +82,6 @@ impl<'a> EdgeIndex<'a> { } } -fn border(ex: &mut Exporter, tag: &str, s: Option<&StrokeSeg>, k: f64, opacity: f32) -> String { - let Some(s) = s.filter(|s| s.stroke.weight > 0.0) else { return format!("") }; - let st = &s.stroke; - let Some(c) = ex.color(&st.color, st.tint, opacity) else { return format!("") }; - let dash = match st.kind { - StrokeType::Dashed { .. } => "dash", - StrokeType::Dotted => "sysDot", - _ => "solid", - }; - format!( - "{c}", - emu_len(st.weight * k) - ) -} - fn model_border(ex: &Exporter, s: Option<&StrokeSeg>, k: f64, opacity: f32) -> craft_pptx::Line { let Some(stroke) = s.filter(|s| s.stroke.weight > 0.0) else { return craft_pptx::Line::none() }; let Some(color) = ex.model_color(&stroke.stroke.color, stroke.stroke.tint, opacity) else { return craft_pptx::Line::none() }; @@ -251,100 +232,4 @@ fn table(ex: &mut Exporter, t: &TableFrag, m: Affine, k: f64, opacity: f32) { kind: craft_pptx::ShapeKind::Table(model), ..Default::default() }); - let mut s = format!( - "\ -", - emu(xfrm.rect.x0), - emu(xfrm.rect.y0), - emu_len(xfrm.rect.width()), - emu_len(xfrm.rect.height()) - ); - for w in xs.windows(2) { - let _ = write!(s, "", emu_len((w[1] - w[0]) * k)); - } - s.push_str(""); - for r in 0..nr { - let h = ys.get(r + 1).zip(ys.get(r)).map_or(0.0, |(b, a)| b - a); - let _ = write!(s, "", emu_len(h * k)); - for col in 0..nc { - let at = r * nc + col; - let (hm, vm) = covered.get(at).copied().unwrap_or((false, false)); - if hm || vm { - let _ = write!( - s, - "", - if hm { " hMerge=\"1\"" } else { "" }, - if vm { " vMerge=\"1\"" } else { "" } - ); - continue; - } - let (gs, rs) = span.get(at).copied().unwrap_or((1, 1)); - let mut attrs = String::new(); - if gs > 1 { - let _ = write!(attrs, " gridSpan=\"{gs}\""); - } - if rs > 1 { - let _ = write!(attrs, " rowSpan=\"{rs}\""); - } - let Some(c) = origin.get(at).copied().flatten() else { - let _ = write!( - s, - "" - ); - continue; - }; - s.push_str(&cell(ex, c, &attrs, &edges_of, k, opacity)); - } - s.push_str(""); - } - s.push_str(""); - ex.out.push_str(&s); -} - -fn cell(ex: &mut Exporter, c: &PlacedCell, attrs: &str, edges: &EdgeIndex, k: f64, opacity: f32) -> String { - let rc = c.rect; - let mut body = String::new(); - let mut margins = [0.0f64; 4]; - if let Some(cft) = c.text.frames.first() { - let segs = segments(ex, &c.text, cft, &c.source, &[]); - // A cell holds one box: its lines, from the first segment's slot. - let mut all = segs.first().cloned().unwrap_or_default(); - for sg in segs.iter().skip(1) { - all.lines.extend(sg.lines.iter().cloned()); - all.x1 = all.x1.max(sg.x1); - } - if !all.lines.is_empty() { - let r = seg_rect(&all); - let r = Rect::new(r.x0 + c.origin.x, r.y0 + c.origin.y, r.x1 + c.origin.x, r.y1 + c.origin.y); - margins = [(r.x0 - rc.x0).max(0.0), (r.y0 - rc.y0).max(0.0), (rc.x1 - r.x1).max(0.0), (rc.y1 - r.y1).max(0.0)]; - let local = Rect::new(r.x0 - c.origin.x, r.y0 - c.origin.y, r.x1 - c.origin.x, r.y1 - c.origin.y); - body = paragraphs(ex, &all, local, k, opacity); - } - } - if body.is_empty() { - body = "".into(); - } - let [l, tp, r, b] = margins.map(|v| emu_len(v * k)); - let ln = |side: char| -> ((f64, f64), (f64, f64)) { - match side { - 'L' => ((rc.x0, rc.y0), (rc.x0, rc.y1)), - 'R' => ((rc.x1, rc.y0), (rc.x1, rc.y1)), - 'T' => ((rc.x0, rc.y0), (rc.x1, rc.y0)), - _ => ((rc.x0, rc.y1), (rc.x1, rc.y1)), - } - }; - let mut pr = format!(""); - for (tag, side) in [("lnL", 'L'), ("lnR", 'R'), ("lnT", 'T'), ("lnB", 'B')] { - let (a, bb) = ln(side); - let seg = edges.stroke(a, bb); - pr.push_str(&border(ex, tag, seg, k, opacity)); - } - match c.fill.as_ref().and_then(|(sw, tint)| ex.color(sw, *tint, opacity)) { - Some(col) => { - let _ = write!(pr, "{col}"); - } - None => pr.push_str(""), - } - pr.push_str(""); - format!("{body}{pr}") } diff --git a/crates/pptx/src/text.rs b/crates/pptx/src/text.rs index 2896a06d1..f50cc8694 100644 --- a/crates/pptx/src/text.rs +++ b/crates/pptx/src/text.rs @@ -9,16 +9,13 @@ //! [`LineBreaks::Reflow`]: one box per frame, native columns, paragraph attributes from the //! styles. -use std::fmt::Write as _; - use designcraft_color::swatch::resolve_gradient; use designcraft_compose::{ComposedStory, FrameText, Line, PlacedGlyph, RunStyle}; -use designcraft_doc::{Align, Item, TextFrame}; +use designcraft_doc::{Item, TextFrame}; use designcraft_geom::{Affine, Rect}; use crate::LineBreaks; use crate::slide::{Exporter, XfrmBox, box_of}; -use crate::xml::{emu, emu_len, esc, pct}; /// PowerPoint's single line is 1.2 em for every font; percentage spacing scales it. const SINGLE: f64 = 1.2; @@ -95,7 +92,7 @@ pub fn frame(ex: &mut Exporter, it: &Item, tfr: &TextFrame, xf: Affine, opacity: let doc = ex.doc; let cs = ex.cache.get(doc, tfr.story, ex.page); let Some(ft) = cs.frame(it.id) else { - ex.shape(it, xf, opacity, None); + ex.shape(it, xf, opacity); return; }; let story_text = doc.story(tfr.story).map(|s| s.text.clone()).unwrap_or_default(); @@ -127,13 +124,12 @@ pub fn frame(ex: &mut Exporter, it: &Item, tfr: &TextFrame, xf: Affine, opacity: let r = seg_rect(seg); let insets = [r.x0 - inner.x0, r.y0 - inner.y0, inner.x1 - r.x1, inner.y1 - r.y1]; if insets.iter().all(|v| *v >= -0.01) { - let body = body(ex, seg, r, k, opacity, insets); let model = model_body(ex, seg, k, opacity, insets); - ex.shape_with_text_model(it, xf, opacity, &body, model); + ex.shape_with_text_model(it, xf, opacity, model); return; } } - ex.shape(it, xf, opacity, None); + ex.shape(it, xf, opacity); decos(ex, ft, m, opacity); for seg in &segs { text_box(ex, seg, m, k, opacity, "Text"); @@ -164,10 +160,9 @@ fn anchored(ex: &mut Exporter, tfr: &TextFrame, ft: &FrameText, xf: Affine, opac /// Paragraph shading and rules. fn decos(ex: &mut Exporter, ft: &FrameText, m: Affine, opacity: f32) { for d in &ft.decos { - let Some(c) = ex.color(&d.color, d.tint, opacity) else { continue }; + let Some(color) = ex.model_color(&d.color, d.tint, opacity) else { continue }; if let Some((xfrm, _)) = box_of(d.rect, m) { - let model_fill = ex.model_color(&d.color, d.tint, opacity).map_or(craft_pptx::Fill::None, craft_pptx::Fill::solid); - ex.rect_shape(xfrm, &format!("{c}"), model_fill, "Paragraph Shading"); + ex.rect_shape(xfrm, craft_pptx::Fill::solid(color), "Paragraph Shading"); } } } @@ -214,12 +209,11 @@ fn para_steps(seg: &Segment) -> std::collections::HashMap> { fn text_box(ex: &mut Exporter, seg: &Segment, m: Affine, k: f64, opacity: f32, name: &str) { let r = seg_rect(seg); let Some((xfrm, _)) = box_of(r, m) else { return }; - let body = body(ex, seg, r, k, opacity, [0.0; 4]); let model = model_body(ex, seg, k, opacity, [0.0; 4]); - write_box(ex, xfrm, &body, model, name); + write_box(ex, xfrm, model, name); } -fn write_box(ex: &mut Exporter, xfrm: XfrmBox, body: &str, model: craft_pptx::TextBody, name: &str) { +fn write_box(ex: &mut Exporter, xfrm: XfrmBox, model: craft_pptx::TextBody, name: &str) { let id = ex.id(); ex.native_shapes.push(craft_pptx::Shape { id: craft_pptx::ShapeId(id), @@ -240,11 +234,6 @@ fn write_box(ex: &mut Exporter, xfrm: XfrmBox, body: &str, model: craft_pptx::Te text: Some(model), ..Default::default() }); - let _ = write!( - ex.out, - "{}{body}", - xfrm.xml() - ); } /// Cut a frame's lines into segments and lay out their runs. @@ -402,16 +391,7 @@ fn align_of(seg: &Segment, lines: &[OutLine]) -> &'static str { "l" } -/// `` for a segment whose box is `r` (text space); `insets` place the text inside a -/// larger shape (left, top, right, bottom). -fn body(ex: &mut Exporter, seg: &Segment, r: Rect, k: f64, opacity: f32, insets: [f64; 4]) -> String { - let [li, ti, ri, bi] = insets.map(|v| emu_len(v * k)); - format!( - "{}", - paragraphs(ex, seg, r, k, opacity) - ) -} - +/// The composed lines represented in the shared text model. /// The model equivalent of the composed text represented by `body`. pub(crate) fn model_body(ex: &mut Exporter, seg: &Segment, k: f64, opacity: f32, insets: [f64; 4]) -> craft_pptx::TextBody { fn color(ex: &Exporter, swatch: &str, tint: f32, opacity: f32) -> Option { @@ -555,257 +535,17 @@ pub(crate) fn model_body(ex: &mut Exporter, seg: &Segment, k: f64, opacity: f32, model } -/// The `` elements of a segment whose box is `r` (text space). -pub(crate) fn paragraphs(ex: &mut Exporter, seg: &Segment, r: Rect, k: f64, opacity: f32) -> String { - let mut s = String::new(); - let mut prev_end: Option<(f64, f64)> = None; - // The segment's lines, a paragraph at a time. - let steps = para_steps(seg); - for lines in seg.lines.chunk_by(|a, b| a.para == b.para && !a.last_in_para) { - let (Some(first), Some(last)) = (lines.first(), lines.last()) else { continue }; - let mut h = steps.get(&first.para).copied().flatten().unwrap_or(if first.leading > 0.0 { first.leading } else { first.size * SINGLE }); - let r_in = BASELINE_IN_LINE; - if let Some((prev_base, prev_h)) = prev_end - && lines.len() == 1 - { - // A one-line paragraph closer to the line before than its own line box allows (tight - // display type): a smaller box puts it there. - let natural = (1.0 - r_in) * prev_h + r_in * h; - let gap = first.baseline - prev_base; - if gap < natural { - h = ((gap - (1.0 - r_in) * prev_h) / r_in).max(first.size * 0.2); - } - } - let algn = align_of(seg, lines); - let mut ppr = format!(" 0.005 { - let _ = write!(ppr, " marL=\"{}\"", emu(rest * k)); - } - if ind.abs() > 0.005 { - let _ = write!(ppr, " indent=\"{}\"", emu(ind * k)); - } - } - ppr.push('>'); - let _ = write!(ppr, "", pct(h / (first.size * SINGLE))); - if let Some((prev_base, prev_h)) = prev_end { - // PowerPoint puts the next line one line box down: the rest of the previous line's box - // and the top part of this one. Space Before makes up the difference. - let natural = (1.0 - r_in) * prev_h + r_in * h; - let before = first.baseline - prev_base - natural; - if before > 0.005 { - let _ = write!(ppr, "", (before * k * 100.0).round() as i64); - } - } - let tabs: Vec = lines.iter().flat_map(|l| l.tabs.iter().copied()).collect(); - if !tabs.is_empty() { - ppr.push_str(""); - let mut sorted = tabs.clone(); - sorted.sort_by(f64::total_cmp); - sorted.dedup_by(|a, b| (*a - *b).abs() < 0.01); - for t in sorted { - let _ = write!(ppr, "", emu((t - r.x0) * k)); - } - ppr.push_str(""); - } - ppr.push_str(""); - s.push_str(""); - s.push_str(&ppr); - for (n, l) in lines.iter().enumerate() { - if n > 0 { - let _ = write!(s, "{}", rpr(ex, l.runs.first(), l.size, k, opacity, "a:rPr")); - } - let n_runs = l.runs.len(); - for (ri, run) in l.runs.iter().enumerate() { - // The undrawn space at a left-aligned line's end goes back in (invisible there), - // so removing the break doesn't join words. - let tail = if ri + 1 == n_runs && l.trailing_space && algn == "l" && !run.text.ends_with(char::is_whitespace) { " " } else { "" }; - let _ = write!(s, "{}{}{tail}", rpr(ex, Some(run), run.size, k, opacity, "a:rPr"), esc(&run.text)); - } - } - s.push_str(&rpr(ex, last.runs.last(), last.size, k, opacity, "a:endParaRPr")); - s.push_str(""); - prev_end = Some((last.baseline, h)); - } - s -} - -/// Run properties (`a:rPr`, or `a:endParaRPr` for the paragraph mark). -fn rpr(ex: &mut Exporter, run: Option<&Run>, size: f64, k: f64, opacity: f32, tag: &str) -> String { - // PowerPoint draws raised and lowered text at 2/3 of its size: state it larger. - let shifted = run.is_some_and(|r| r.y.abs() > 0.01 && r.size > 0.0); - let size = if shifted { size * 1.5 } else { size }; - let sz = ((size * k * 100.0).round() as i64).clamp(100, 400_000); - let Some(run) = run else { return format!("<{tag} lang=\"en-US\" sz=\"{sz}\" dirty=\"0\"/>") }; - let cs = run.rs.as_ref(); - let font = ex.fonts.use_face(run.face); - let mut a = format!("<{tag} lang=\"en-US\" sz=\"{sz}\""); - let italic = font.italic || cs.is_some_and(|c| c.skew.abs() > 0.01); - if font.bold { - a.push_str(" b=\"1\""); - } - if italic { - a.push_str(" i=\"1\""); - } - if let Some(st) = cs { - if st.underline { - a.push_str(" u=\"sng\""); - } - if st.strikethrough { - a.push_str(" strike=\"sngStrike\""); - } - } - // Kerning off: the composer's kerning is in the character spacing already. - a.push_str(" kern=\"400000\""); - let spc = (run.spc * k * 100.0).round() as i64; - if spc != 0 && tag == "a:rPr" { - let _ = write!(a, " spc=\"{spc}\""); - } - if shifted { - let _ = write!(a, " baseline=\"{}\"", pct(-run.y / size)); - } - a.push_str(" dirty=\"0\">"); - if let Some(st) = cs { - if st.stroke != designcraft_color::swatch::NONE - && st.stroke_weight > 0.0 - && let Some(c) = ex.color(&st.stroke, st.stroke_tint, opacity) - { - let _ = write!(a, "{c}", emu_len(st.stroke_weight * k)); - } - match ex.color(&st.fill, st.fill_tint, opacity) { - Some(c) => { - let _ = write!(a, "{c}"); - } - None => a.push_str(""), - } - } - let fam = esc(&font.family); - let _ = write!(a, ""); - a -} - -/// Reflow: the frame as one shape whose text PowerPoint sets, in the frame's columns. +/// Build shared paragraph and run model values for a composed text segment. #[allow(clippy::too_many_arguments)] fn reflow(ex: &mut Exporter, it: &Item, tfr: &TextFrame, ft: &FrameText, cs: &ComposedStory, story: &str, xf: Affine, opacity: f32, vertical: bool) { - let doc = ex.doc; - let Some(st) = doc.story(tfr.story) else { - ex.shape(it, xf, opacity, None); + if ex.doc.story(tfr.story).is_none() { + ex.shape(it, xf, opacity); return; - }; - let o = &tfr.options; + } let m = ex.to_slide * xf; let k = box_of(it.inner_bounds(), m).map_or(1.0, |(_, d)| (d.sx * d.sy).abs().sqrt()); - let mut model = reflow_model(ex, it, tfr, ft, cs, story, k, opacity, vertical); - let [t, l, b, r] = o.inset.map(|v| emu_len(v * k)); - let cols = ft.columns.len().max(1); - let mut s = format!(" 1 { - let _ = write!(s, " numCol=\"{cols}\" spcCol=\"{}\"", emu_len(o.gutter * k)); - } - if vertical { - s.push_str(" vert=\"eaVert\""); - } - let anchor = match o.vertical_justification { - designcraft_doc::VerticalJustification::Center => "ctr", - designcraft_doc::VerticalJustification::Bottom => "b", - designcraft_doc::VerticalJustification::Justify => "just", - designcraft_doc::VerticalJustification::Top => "t", - }; - let _ = write!(s, " anchor=\"{anchor}\">"); - // The paragraphs this frame shows, with the runs of their composed glyphs. - let ranges = st.para_ranges(); - // The frame's lines by paragraph, once (a filter per paragraph made many paragraphs quadratic). - let mut by_para: std::collections::HashMap> = std::collections::HashMap::new(); - for l in &ft.lines { - by_para.entry(l.para).or_default().push(l); - } - let mut first = true; - for (pi, range) in ranges.iter().enumerate() { - if range.end < ft.range.start || range.start >= ft.range.end.max(ft.range.start + 1) { - continue; - } - let lines: Vec<&Line> = by_para.get(&pi).cloned().unwrap_or_default(); - let Some(pf) = st.paras.get(pi) else { continue }; - let (pp, chars) = doc.styles.resolve_para(pf); - let algn = match pp.align { - Align::Center => "ctr", - Align::Right => "r", - Align::LeftJustified | Align::CenterJustified | Align::RightJustified => "just", - Align::FullyJustified => "dist", - _ => "l", - }; - let mut ppr = format!(" 0.005 { - let _ = write!(ppr, " marL=\"{}\"", emu(pp.left_indent * k)); - } - if pp.right_indent.abs() > 0.005 { - let _ = write!(ppr, " marR=\"{}\"", emu(pp.right_indent * k)); - } - if pp.first_line_indent.abs() > 0.005 { - let _ = write!(ppr, " indent=\"{}\"", emu(pp.first_line_indent * k)); - } - ppr.push('>'); - let size = chars.size.max(1.0); - let lead = match lines.windows(2).map(|w| w[1].baseline - w[0].baseline).find(|d| *d > 0.0) { - Some(d) => d, - None => lines.first().map_or(size * pp.auto_leading, |l| l.leading.max(1.0)), - }; - let _ = write!(ppr, "", pct(lead / (size * SINGLE))); - if !first && pp.space_before > 0.0 { - let _ = write!(ppr, "", (pp.space_before * k * 100.0).round() as i64); - } - if pp.space_after > 0.0 { - let _ = write!(ppr, "", (pp.space_after * k * 100.0).round() as i64); - } - ppr.push_str(""); - s.push_str(""); - s.push_str(&ppr); - let mut last_size = size; - for l in &lines { - let ol = out_line(ex, cs, l, story); - let mut runs: Vec = Vec::new(); - for mut r in ol.runs { - // PowerPoint sets the spacing; only tracking-sized adjustments would survive, and - // they vary line to line, so none is kept. - r.spc = 0.0; - match runs.last_mut() { - Some(p) - if p.style == r.style - && p.face.get().id() == r.face.get().id() - && (p.size - r.size).abs() < 0.005 - && (p.y - r.y).abs() < 0.01 => - { - p.text.push_str(&r.text) - } - _ => runs.push(r), - } - } - // Composer hyphens at line ends go: PowerPoint breaks the lines itself. - if l.hyphenated - && let Some(r) = runs.last_mut() - && r.text.ends_with(['-', '\u{2010}']) - { - r.text.pop(); - } - if ol.trailing_space - && let Some(r) = runs.last_mut() - && !r.text.ends_with(char::is_whitespace) - { - r.text.push(' '); - } - for run in &runs { - let _ = write!(s, "{}{}", rpr(ex, Some(run), run.size, k, opacity, "a:rPr"), esc(&run.text)); - last_size = run.size; - } - } - s.push_str(&rpr(ex, None, last_size, k, opacity, "a:endParaRPr")); - s.push_str(""); - first = false; - } - s.push_str(""); - ex.shape_with_text_model(it, xf, opacity, &s, std::mem::take(&mut model)); + let model = reflow_model(ex, it, tfr, ft, cs, story, k, opacity, vertical); + ex.shape_with_text_model(it, xf, opacity, model); } /// Reflow text in the shared text model. PowerPoint is free to break each paragraph again, but diff --git a/crates/pptx/src/xml.rs b/crates/pptx/src/xml.rs deleted file mode 100644 index 4eba791dd..000000000 --- a/crates/pptx/src/xml.rs +++ /dev/null @@ -1,75 +0,0 @@ -//! XML text helpers and DrawingML units. - -/// English Metric Units per point. -pub const EMU_PER_PT: f64 = 12_700.0; - -/// Points → EMU, saturating (input-derived geometry may be huge or not finite). -pub fn emu(pt: f64) -> i64 { - if !pt.is_finite() { - return 0; - } - (pt * EMU_PER_PT).round().clamp(-27_273_042_316_900.0, 27_273_042_316_900.0) as i64 -} - -/// A non-negative extent in EMU (DrawingML rejects negative sizes). -pub fn emu_len(pt: f64) -> i64 { - emu(pt).max(0) -} - -/// Degrees → DrawingML angle units (60 000ths of a degree), normalised to 0..360°. -pub fn angle(deg: f64) -> i64 { - if !deg.is_finite() { - return 0; - } - (deg.rem_euclid(360.0) * 60_000.0).round() as i64 % 21_600_000 -} - -/// A 0..1 fraction → DrawingML percentage units (1000ths of a percent). -pub fn pct(v: f64) -> i64 { - if !v.is_finite() { - return 0; - } - (v * 100_000.0).round().clamp(-100_000_000.0, 100_000_000.0) as i64 -} - -/// Escape text for element content and attribute values. Characters XML 1.0 can't hold are -/// dropped. -pub fn esc(s: &str) -> String { - let mut o = String::with_capacity(s.len()); - for c in s.chars() { - match c { - '&' => o.push_str("&"), - '<' => o.push_str("<"), - '>' => o.push_str(">"), - '"' => o.push_str("""), - '\'' => o.push_str("'"), - '\t' | '\n' | '\r' => o.push(c), - c if (c as u32) < 0x20 || c == '\u{FFFE}' || c == '\u{FFFF}' => {} - c => o.push(c), - } - } - o -} - -/// sRGB 0..1 → `RRGGBB`. -pub fn hex(rgb: [f32; 3]) -> String { - let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - format!("{:02X}{:02X}{:02X}", q(rgb[0]), q(rgb[1]), q(rgb[2])) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn units() { - assert_eq!(emu(1.0), 12_700); - assert_eq!(emu(f64::NAN), 0); - assert_eq!(emu_len(-3.0), 0); - assert_eq!(angle(-90.0), 16_200_000); - assert_eq!(angle(360.0), 0); - assert_eq!(pct(0.5), 50_000); - assert_eq!(esc("a Date: Fri, 13 Nov 2026 03:15:13 -0500 Subject: [PATCH 031/153] Delegate PDF/X-1a finishing to craft-prepress --- Cargo.lock | 31 +- Cargo.toml | 20 +- crates/pdf/src/pdfx1a.rs | 741 +-------------------------------------- 3 files changed, 44 insertions(+), 748 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd9646fd7..e3186cb7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1071,8 +1071,9 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 [[package]] name = "craft-cms" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ + "md-5", "rayon", ] @@ -1353,7 +1354,7 @@ dependencies = [ [[package]] name = "craft-pdf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ "bytemuck", "hayro", @@ -1381,7 +1382,7 @@ dependencies = [ [[package]] name = "craft-prepress" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ "craft-cms", "craft-pdf", @@ -3180,7 +3181,7 @@ dependencies = [ [[package]] name = "hayro" version = "0.7.1" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ "bytemuck", "hayro-interpret", @@ -3200,7 +3201,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a" [[package]] name = "hayro-cmap" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ "brotli", "hayro-postscript", @@ -3209,7 +3210,7 @@ dependencies = [ [[package]] name = "hayro-interpret" version = "0.7.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ "bitflags 2.13.2", "hayro-cmap", @@ -3227,7 +3228,7 @@ dependencies = [ [[package]] name = "hayro-jbig2" version = "0.3.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ "fearless_simd", "hayro-ccitt", @@ -3245,12 +3246,12 @@ dependencies = [ [[package]] name = "hayro-postscript" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" [[package]] name = "hayro-syntax" version = "0.7.2" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ "flate2", "hayro-ccitt", @@ -3879,7 +3880,7 @@ dependencies = [ [[package]] name = "krilla" version = "0.8.2" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" dependencies = [ "base64 0.22.1", "bumpalo", @@ -4046,6 +4047,16 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + [[package]] name = "memchr" version = "2.8.3" diff --git a/Cargo.toml b/Cargo.toml index 34e1857b6..26e155b58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/storytold/designcraft" [workspace.dependencies] # Shared reader and writer; publish the immutable revision before this consumer. -craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false } +craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.1.0", default-features = false } craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } @@ -33,7 +33,7 @@ craft-pptx = { git = "https://github.com/storytold/craft-libs", rev = "f6cab1980 # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. -craft-prepress = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["flatten"] } +craft-prepress = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.1.0", features = ["flatten"] } # 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 = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] } # internal @@ -101,7 +101,7 @@ craft-atomic = { version = "0.1.0", git = "https://github.com/storytold/craft-li craft-print = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } # ICC colour management: working spaces, transforms, black point compensation (craft-libs). -craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } +craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b267a3a66" } # ui # Shared frontend components; keep this dependency above the engine boundary. @@ -151,16 +151,16 @@ debug = 0 # Opt-in cancelable macOS termination on the pinned winit 0.30 line. [patch.crates-io] -krilla = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.8.2" } +krilla = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.8.2" } winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } # The hayro crates from craft-libs' vendor/ directory (pdfcraft's, plus the plate mode and decode # limits craft-pdf and craft-prepress need); same revision as craft-pdf. -hayro = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.1" } -hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.0" } -hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.2" } -hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.3.0" } -hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0" } -hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0" } +hayro = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.7.1" } +hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.7.0" } +hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.7.2" } +hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.3.0" } +hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.1.0" } +hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.1.0" } ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } diff --git a/crates/pdf/src/pdfx1a.rs b/crates/pdf/src/pdfx1a.rs index 67e0a583c..606f81ab3 100644 --- a/crates/pdf/src/pdfx1a.rs +++ b/crates/pdf/src/pdfx1a.rs @@ -1,743 +1,28 @@ -//! PDF/X-1a:2003: the PDF/X-4 print file flattened by craft-prepress (no transparency left; RGB -//! and ICC color converted into the output intent's CMYK; inks, overprint and live text kept), -//! written by krilla as PDF 1.4, given the PDF/X-1a identification and output intent, then -//! checked. +//! DesignCraft's PDF/X-1a export adapter: convert shared warnings and errors to app PDF results. -use std::collections::{HashMap, HashSet}; - -use craft_pdf::syntax::content::UntypedIter; -use craft_pdf::syntax::object::{Array, Dict, Name, Object, ObjectIdentifier, Stream}; -use craft_pdf::syntax::page::Resources; -use craft_pdf::write::backend::configure::PdfVersion; -use craft_prepress::flatten::pdf::ReadOptions; -use craft_prepress::flatten::{FlattenDocumentOptions, FlattenOptions, FlattenTotals, flatten_document}; +#[cfg(test)] +use craft_prepress::flatten::FlattenTotals; use crate::PdfError; -use crate::pdfx::MAX_DEPTH; -use crate::transitions::{find, int_after, rfind}; -/// `GTS_PDFXVersion` of PDF/X-1a:2003 (ISO 15930-4): the base level, PDF/X-1:2003. -const VERSION: &str = "PDF/X-1:2003"; +pub(crate) use craft_prepress::pdfx1a::PageGraphics; -/// `GTS_PDFXConformance` of PDF/X-1a:2003 (ISO 15930-4): with [`VERSION`], it names the -/// conformance level. -const CONFORMANCE: &str = "PDF/X-1a:2003"; - -/// `/RegistryName` of the ICC's registry of characterized printing conditions. An output intent -/// naming one of them needs no `/DestOutputProfile` in PDF/X. -const ICC_REGISTRY: &str = "http://www.color.org"; - -/// The graphics placed on one output page, to name in errors: the page's name and the graphics' -/// names. -#[derive(Clone, Debug, Default)] -pub(crate) struct PageGraphics { - pub label: String, - pub graphics: Vec, -} - -/// The PDF/X-1a file for `pdf`, the PDF/X-4 print file: flattened at `dpi`, written at PDF 1.4, -/// identified, checked. `pages` names each output page and its graphics for errors. Fails when -/// the flattener fails or leaves an image out, when the output intent's profile can't go into a -/// PDF 1.4 file, or when the result still has transparency or color outside DeviceCMYK, -/// DeviceGray, Separation and DeviceN; other check findings become warnings. pub(crate) fn flatten_x1a(pdf: &[u8], dpi: f32, pages: &[PageGraphics], warnings: &mut Vec) -> crate::Result> { - let dpi = if dpi.is_finite() { dpi.clamp(36.0, 2400.0) } else { 300.0 }; - let opts = FlattenDocumentOptions { - read: ReadOptions { raster_dpi: dpi, ..ReadOptions::default() }, - flatten: FlattenOptions { raster_dpi: dpi, ..FlattenOptions::default() }, - producer: "DesignCraft".into(), - version: PdfVersion::Pdf14, - pdfx_version: Some(VERSION.into()), - ..FlattenDocumentOptions::default() - }; - let (bytes, report) = flatten_document(pdf, &opts).map_err(|e| PdfError::Write(format!("PDF/X-1a: the transparency flattener failed: {e}")))?; - let unread: Vec = report - .pages - .iter() - .filter(|(_, r)| r.read.unreadable_images > 0) - .map(|(page, r)| { - let n = r.read.unreadable_images; - let images = if n == 1 { "1 image".to_string() } else { format!("{n} images") }; - let fallback = PageGraphics { label: page.saturating_add(1).to_string(), graphics: Vec::new() }; - let at = pages.get(*page).unwrap_or(&fallback); - let names: Vec = at.graphics.iter().take(5).map(|g| format!("`{g}`")).collect(); - let more = if at.graphics.len() > 5 { ", …" } else { "" }; - let placed = if names.is_empty() { String::new() } else { format!(" (graphics on it: {}{more})", names.join(", ")) }; - format!("{images} on page {}{placed}", at.label) - }) - .collect(); - if !unread.is_empty() { - return Err(PdfError::Write(format!( - "PDF/X-1a: the transparency flattener couldn't read {}, and a PDF/X file can't leave images out: replace the graphic, or export without PDF/X-1a", - unread.join("; ") - ))); - } - warnings.extend(flatten_warnings(&report.totals, dpi)); - let bytes = finish(&bytes).map_err(|e| PdfError::Write(format!("PDF/X-1a: {e}")))?; - let (fatal, other) = checks(&bytes); - if !fatal.is_empty() { - return Err(PdfError::Write(format!("PDF/X-1a: after flattening, {}", fatal.join("; ")))); - } - warnings.extend(other.into_iter().map(|issue| format!("PDF/X-1a: {issue}"))); + let (bytes, findings) = craft_prepress::pdfx1a::flatten_x1a(pdf, dpi, pages).map_err(PdfError::Write)?; + warnings.extend(findings); Ok(bytes) } -/// What the flattener's `t` (at `dpi`) tells the user: rasterized transparency, and spot inks past -/// the ink set's capacity, whose tints the flattener drops. -pub(crate) fn flatten_warnings(t: &FlattenTotals, dpi: f32) -> Vec { - let mut out = Vec::new(); - if t.complex_regions > 0 || t.complex_groups > 0 { - let at = t.lowest_dpi.map(|d| format!(", some below {dpi:.0} ppi (lowest {d:.0} ppi)")).unwrap_or_default(); - out.push(format!("PDF/X-1a: transparency flattened: {} region(s) rasterized at {dpi:.0} ppi{at}", t.complex_regions + t.complex_groups)); - } - if t.dropped_inks > 0 { - out.push(format!( - "PDF/X-1a: the document has more spot inks than the flattener can separate: {} tint(s) in the inks past its limit were dropped and won't print", - t.dropped_inks - )); - } - out +#[cfg(test)] +pub(crate) fn flatten_warnings(totals: &FlattenTotals, dpi: f32) -> Vec { + craft_prepress::pdfx1a::flatten_warnings(totals, dpi) } -/// PDF/X-1a:2003 checks on an exported file (empty = passes them): PDF 1.4 or lower, the -/// identification (`GTS_PDFXVersion` and `GTS_PDFXConformance`), a CMYK `GTS_PDFX` output intent -/// whose profile, when it has one, is ICC v2 (and without one, names a registered printing -/// condition), `/Trapped` True or False, trim boxes, no encryption or interpolation; no -/// transparency (soft masks, transparency groups, constant opacity below 1, blend modes other -/// than Normal); only DeviceCMYK, DeviceGray, Separation and DeviceN (and Indexed and Pattern -/// spaces over them), a spot's alternate space process; every font embedded. -pub fn check_pdfx1a(pdf: &[u8]) -> Vec { - let (mut issues, other) = checks(pdf); - issues.extend(other); - issues -} - -/// [`check_pdfx1a`]'s findings: transparency and color spaces first, then the rest. -fn checks(pdf: &[u8]) -> (Vec, Vec) { - let mut issues = Vec::new(); - let text = String::from_utf8_lossy(pdf); - let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).ok(); - if !pdf.starts_with(b"%PDF-1.") || pdf.get(7).is_none_or(|v| *v > b'4') { - issues.push("the PDF version must be 1.4 or lower".into()); - } - if file.as_ref().is_none_or(|f| crate::pdfx::pdfx_intents(f).is_empty()) { - issues.push("no PDF/X output intent".into()); - } - let info = |key: &[u8]| file.as_ref().and_then(|f| crate::pdfx::info_text(pdf, f, key)); - if info(b"GTS_PDFXVersion").as_deref() != Some(VERSION) { - issues.push(format!("the document information doesn't name {VERSION}")); - } - if info(b"GTS_PDFXConformance").as_deref() != Some(CONFORMANCE) { - issues.push(format!("the document information doesn't name the conformance level {CONFORMANCE}")); - } - if !text.contains("/Trapped/False") && !text.contains("/Trapped/True") && !text.contains("/Trapped /False") && !text.contains("/Trapped /True") { - issues.push("the document information needs /Trapped True or False".into()); - } - if text.contains("/Interpolate true") { - issues.push("an image asks for interpolation".into()); - } - if text.contains("/Encrypt") { - issues.push("the file is encrypted".into()); - } - let pages = text.matches("/Type/Page/").count() + text.matches("/Type/Page>").count(); - let trims = text.matches("/TrimBox").count(); - if pages > 0 && trims < pages { - issues.push("every page needs a trim box".into()); - } - let (content, fonts) = content(file.as_ref()); - issues.extend(fonts); - (content, issues) -} - -/// Transparency and color spaces, then the output intent and fonts, walked through every page's -/// content and what it uses. -fn content(file: Option<&craft_pdf::syntax::Pdf>) -> (Vec, Vec) { - let Some(file) = file else { - return (vec!["the file couldn't be read to check its transparency and color spaces".into()], Vec::new()); - }; - let mut fonts = intent_issues(file); - let mut found: Vec<(String, Vec)> = Vec::new(); - let mut font_found: Vec<(String, Vec)> = Vec::new(); - for (i, page) in file.pages().iter().enumerate() { - let mut scan = Scan::default(); - let res = page.resources(); - if let Some(group) = page.raw().get::>(b"Group") { - scan.group(&group); - } - if let Some(content) = page.page_stream() { - scan.content(content, res, 0); - } - for (list, items) in [(&mut found, scan.found), (&mut font_found, scan.font_issues)] { - for what in items { - match list.iter_mut().find(|(w, _)| *w == what) { - Some((_, pages)) => pages.push(i + 1), - None => list.push((what, vec![i + 1])), - } - } - } - } - let on_pages = |(what, pages): (String, Vec)| { - let list: Vec = pages.iter().take(10).map(|p| p.to_string()).collect(); - let more = if pages.len() > 10 { ", …" } else { "" }; - let s = if pages.len() == 1 { "" } else { "s" }; - format!("{what} on page{s} {}{more}", list.join(", ")) - }; - fonts.extend(font_found.into_iter().map(on_pages)); - (found.into_iter().map(on_pages).collect(), fonts) -} - -/// A `GTS_PDFX` output intent, as PDF/X-1a judges it. -struct Intent { - /// Its object number, when it is an indirect object. - obj: Option, - /// `/OutputCondition`, else `/OutputConditionIdentifier`, for messages. - name: String, - /// An `/OutputConditionIdentifier` other than `Custom` in the ICC registry. - registered: bool, - /// Whether it has a `/DestOutputProfile`, and the profile's object number. - has_profile: bool, - profile_obj: Option, - /// The profile's `/N` and decoded bytes. - channels: Option, - profile: Option>, -} - -impl Intent { - /// The profile's ICC major version (byte 8 of its header). - fn major(&self) -> Option { - self.profile.as_deref()?.get(8).copied() - } - - /// "ICC v4", or what keeps the version from being read. - fn version(&self) -> String { - self.major().map_or_else(|| "unreadable".to_string(), |v| format!("ICC v{v}")) - } -} - -/// The catalog's `GTS_PDFX` output intents. -fn pdfx_intents(file: &craft_pdf::syntax::Pdf) -> Vec { - let number = |n: i32| usize::try_from(n).ok(); - crate::pdfx::pdfx_intents(file) - .into_iter() - .map(|oi| { - let text = |key: &[u8]| { - oi.get::>(key) - .map(|s| String::from_utf8_lossy(s.as_bytes()).trim().to_string()) - .filter(|s| !s.is_empty()) - }; - let identifier = text(b"OutputConditionIdentifier"); - let registry = text(b"RegistryName"); - let profile = oi.get::>(b"DestOutputProfile"); - Intent { - obj: oi.obj_id().and_then(|id| number(id.obj_number)), - name: text(b"OutputCondition").or_else(|| identifier.clone()).unwrap_or_else(|| "?".into()), - registered: registry.is_some_and(|r| r.trim_end_matches('/') == ICC_REGISTRY) && identifier.as_deref().is_some_and(|i| i != "Custom"), - has_profile: oi.contains_key(b"DestOutputProfile"), - profile_obj: oi.get_ref(b"DestOutputProfile").and_then(|r| number(r.obj_number)), - channels: profile.as_ref().and_then(|p| p.dict().get::(b"N")), - profile: profile.and_then(|p| p.decoded().ok()).map(|b| b.into_owned()), - } - }) - .collect() -} - -/// What PDF/X-1a finds wrong with the file's output intent: a CMYK profile, ICC v2 (PDF 1.4 -/// allows no later version), or none for a registered printing condition. -fn intent_issues(file: &craft_pdf::syntax::Pdf) -> Vec { - let Some(oi) = pdfx_intents(file).into_iter().next() else { return Vec::new() }; - let mut issues = Vec::new(); - if oi.has_profile { - if oi.channels != Some(4) { - issues.push("the output intent has no CMYK destination profile".into()); - } - if oi.major() != Some(2) { - issues.push(format!("the output intent's profile is {}, and PDF/X-1a (PDF 1.4) allows only ICC v2 profiles", oi.version())); - } - } else if !oi.registered { - issues - .push(format!("the output intent has no destination profile and doesn't name a printing condition in the ICC registry ({ICC_REGISTRY})")); - } - issues -} - -/// krilla's PDF 1.4 file with what PDF/X-1a needs and krilla doesn't write, rewritten as one -/// revision: `GTS_PDFXConformance` in the document information and the XMP, and the output intent -/// of a registered printing condition without `/DestOutputProfile` (its profile is left out of -/// the file). An unregistered condition keeps its profile, which must then be ICC v2. +#[cfg(test)] pub(crate) fn finish(pdf: &[u8]) -> Result, String> { - let unreadable = || "the flattened file couldn't be read to add the PDF/X-1a identification".to_string(); - let layout = Layout::read(pdf).ok_or_else(unreadable)?; - let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).map_err(|_| unreadable())?; - let mut bodies: HashMap> = HashMap::new(); - let info = int_after(&layout.trailer, "/Info").ok_or_else(unreadable)?; - let body = layout.body(pdf, info).ok_or_else(unreadable)?; - let (head, close) = body.split_at_checked(rfind(body, b">>").ok_or_else(unreadable)?).ok_or_else(unreadable)?; - bodies.insert(info, [head, format!("/GTS_PDFXConformance({CONFORMANCE})").as_bytes(), close].concat()); - let xref = file.xref(); - if let Some(meta) = xref.get::>(xref.root_id()).and_then(|c| c.get_ref(b"Metadata")).and_then(|r| usize::try_from(r.obj_number).ok()) - && let Some(xmp) = layout.body(pdf, meta).and_then(xmp_with_conformance) - { - bodies.insert(meta, xmp); - } - let mut stripped: Vec = Vec::new(); - for oi in pdfx_intents(&file) { - if !oi.has_profile { - continue; - } - if !oi.registered { - if oi.major() != Some(2) { - return Err(format!( - "the output intent's profile `{}` is {}, and PDF/X-1a (PDF 1.4) allows only ICC v2 profiles: choose a CMYK working space with an ICC v2 profile, or Coated FOGRA39", - oi.name, - oi.version() - )); - } - continue; - } - let (Some(obj), Some(profile)) = (oi.obj, oi.profile_obj) else { return Err(unreadable()) }; - let body = match bodies.get(&obj) { - Some(b) => b.clone(), - None => layout.body(pdf, obj).ok_or_else(unreadable)?.to_vec(), - }; - let key = format!("/DestOutputProfile {profile} 0 R"); - let at = find(&body, key.as_bytes(), 0).ok_or_else(unreadable)?; - let (before, rest) = body.split_at_checked(at).ok_or_else(unreadable)?; - let after = rest.get(key.len()..).ok_or_else(unreadable)?; - bodies.insert(obj, [before, after].concat()); - stripped.push(profile); - } - // A profile only the stripped intents named goes too. - let dropped: HashSet = stripped.iter().copied().filter(|p| references(pdf, *p) == stripped.iter().filter(|q| *q == p).count()).collect(); - layout.write(pdf, &bodies, &dropped).ok_or_else(unreadable) + craft_prepress::pdfx1a::finish_x1a(pdf) } -/// How often `n 0 R` appears in `pdf` (a stream that happens to hold those bytes counts too, which -/// only ever keeps an object). -fn references(pdf: &[u8], n: usize) -> usize { - let needle = format!("{n} 0 R"); - let mut count = 0; - let mut from = 0; - while let Some(at) = find(pdf, needle.as_bytes(), from) { - let before = at.checked_sub(1).and_then(|i| pdf.get(i)).is_none_or(|c| !c.is_ascii_digit()); - let after = pdf.get(at + needle.len()).is_none_or(|c| !c.is_ascii_alphanumeric()); - if before && after { - count += 1; - } - from = at + needle.len(); - } - count -} - -/// The body of an uncompressed XMP metadata stream object with the Adobe `pdfx` properties of -/// the identification (`GTS_PDFXVersion`, `GTS_PDFXConformance`) added and its `/Length` updated; -/// `None` when the stream is filtered or isn't XMP. -fn xmp_with_conformance(body: &[u8]) -> Option> { - let s0 = find(body, b"stream", 0)?; - let dict = String::from_utf8_lossy(body.get(..s0)?).to_string(); - let s0 = s0 + b"stream".len(); - let s0 = s0 + body.get(s0..)?.iter().take_while(|c| **c == b'\r' || **c == b'\n').count(); - let s1 = rfind(body, b"endstream")?; - let xml = String::from_utf8_lossy(body.get(s0..s1)?).trim_end().to_string(); - if dict.contains("/Filter") || !xml.contains("") { - return None; - } - let ids = format!( - "{VERSION}\ - {CONFORMANCE}" - ); - let xml = xml.replacen("", &format!("{ids}"), 1); - let i = dict.find("/Length")?; - let rest = dict.get(i + 7..)?.trim_start(); - let end = rest.find(|c: char| !c.is_ascii_digit()).unwrap_or(rest.len()); - let dict = format!("{}/Length {}{}", dict.get(..i)?, xml.len(), rest.get(end..)?); - Some(format!("\n{}\nstream\n{xml}\nendstream\n", dict.trim()).into_bytes()) -} - -/// Where the objects of a single-revision file with a classic cross-reference table are. -struct Layout { - /// (object number, start, end), by start: from `n 0 obj` up to the next object or the table. - objects: Vec<(usize, usize, usize)>, - /// Where the cross-reference table starts. - table: usize, - /// The trailer dictionary. - trailer: String, - size: usize, -} - -impl Layout { - fn read(pdf: &[u8]) -> Option { - let sx = rfind(pdf, b"startxref")?; - let table: usize = std::str::from_utf8(pdf.get(sx + b"startxref".len()..)?).ok()?.split_whitespace().next()?.parse().ok()?; - let section = pdf.get(table..sx)?; - let t = find(section, b"trailer", 0)?; - let trailer = String::from_utf8_lossy(section.get(t + b"trailer".len()..)?).trim().to_string(); - if trailer.contains("/Prev") || trailer.contains("/XRefStm") { - return None; - } - let size = int_after(&trailer, "/Size")?; - let mut tokens = std::str::from_utf8(section.get(..t)?).ok()?.split_whitespace(); - if tokens.next()? != "xref" { - return None; - } - let mut starts: Vec<(usize, usize)> = Vec::new(); - let mut seen = HashSet::new(); - while let Some(first) = tokens.next() { - let first: usize = first.parse().ok()?; - let count: usize = tokens.next()?.parse().ok()?; - for i in 0..count { - let (offset, generation, kind) = (tokens.next()?, tokens.next()?, tokens.next()?); - if kind != "n" { - continue; - } - let n = first.checked_add(i)?; - let offset: usize = offset.parse().ok()?; - if generation != "00000" || offset >= table || n >= size || !seen.insert(n) { - return None; - } - starts.push((n, offset)); - } - } - // Free entries past the objects would only make the rewritten table longer. - let max = starts.iter().map(|(n, _)| *n).max().unwrap_or(0); - if size > max.saturating_mul(2).saturating_add(16) { - return None; - } - starts.sort_by_key(|(_, offset)| *offset); - let mut objects = Vec::with_capacity(starts.len()); - for (i, &(n, start)) in starts.iter().enumerate() { - let end = starts.get(i + 1).map_or(table, |(_, s)| *s); - if !pdf.get(start..end)?.starts_with(format!("{n} 0 obj").as_bytes()) { - return None; - } - objects.push((n, start, end)); - } - Some(Layout { objects, table, trailer, size }) - } - - /// Object `n` between `n 0 obj` and `endobj`. - fn body<'p>(&self, pdf: &'p [u8], n: usize) -> Option<&'p [u8]> { - let &(_, start, end) = self.objects.iter().find(|(m, _, _)| *m == n)?; - let obj = pdf.get(start..end)?; - obj.get(format!("{n} 0 obj").len()..rfind(obj, b"endobj")?) - } - - /// The file with the objects in `bodies` replaced and those in `dropped` left out, and a new - /// cross-reference table. - fn write(&self, pdf: &[u8], bodies: &HashMap>, dropped: &HashSet) -> Option> { - let first = self.objects.first().map_or(self.table, |o| o.1); - let mut out = pdf.get(..first)?.to_vec(); - let mut offsets = HashMap::new(); - for &(n, start, end) in &self.objects { - if dropped.contains(&n) { - continue; - } - offsets.insert(n, out.len()); - match bodies.get(&n) { - Some(body) => { - out.extend_from_slice(format!("{n} 0 obj").as_bytes()); - out.extend_from_slice(body); - out.extend_from_slice(b"endobj\n"); - } - None => out.extend_from_slice(pdf.get(start..end)?), - } - } - if !out.ends_with(b"\n") { - out.push(b'\n'); - } - let table = out.len(); - // The free entries, each pointing at the next. - let free: Vec = (1..self.size).filter(|n| !offsets.contains_key(n)).collect(); - let next: HashMap = free.iter().copied().zip(free.iter().skip(1).copied().chain([0])).collect(); - out.extend_from_slice(format!("xref\n0 {}\n{:010} 65535 f\r\n", self.size, free.first().copied().unwrap_or(0)).as_bytes()); - for n in 1..self.size { - let next = next.get(&n).copied().unwrap_or(0); - let entry = match offsets.get(&n) { - Some(offset) => format!("{offset:010} 00000 n\r\n"), - // A dropped object's number could be used again, at the next generation. - None if dropped.contains(&n) => format!("{next:010} 00001 f\r\n"), - None => format!("{next:010} 65535 f\r\n"), - }; - out.extend_from_slice(entry.as_bytes()); - } - out.extend_from_slice(format!("trailer\n{}\nstartxref\n{table}\n%%EOF\n", self.trailer).as_bytes()); - Some(out) - } -} - -/// Why a color space isn't allowed in PDF/X-1a, or `None` when it is. -fn disallowed(cs: &Object<'_>, res: &Resources<'_>, depth: usize) -> Option { - if depth > 4 { - return Some("a color space nested too deeply".into()); - } - match cs { - Object::Name(n) => match n.as_ref() { - b"DeviceCMYK" | b"CMYK" | b"DeviceGray" | b"G" | b"Pattern" => None, - b"DeviceRGB" | b"RGB" => Some("DeviceRGB color".into()), - b"Indexed" | b"I" => None, - other => match res.get_color_space(&Name::new_unescaped(other)) { - Some(named) if !matches!(named, Object::Name(_)) => disallowed(&named, res, depth + 1), - Some(Object::Name(m)) if m.as_ref() != other => disallowed(&Object::Name(m), res, depth + 1), - _ => Some(format!("the color space /{}", String::from_utf8_lossy(other))), - }, - }, - Object::Array(a) => { - let mut items = a.iter::>(); - let family = match items.next() { - Some(Object::Name(n)) => n, - _ => return Some("an unreadable color space".into()), - }; - match family.as_ref() { - b"ICCBased" => Some("ICC-based color".into()), - b"CalRGB" | b"CalGray" => Some("CIE-based (CalRGB/CalGray) color".into()), - b"Lab" => Some("Lab color".into()), - b"DeviceRGB" | b"RGB" => Some("DeviceRGB color".into()), - b"DeviceCMYK" | b"CMYK" | b"DeviceGray" | b"G" => None, - b"Indexed" | b"I" => items.next().and_then(|base| disallowed(&base, res, depth + 1)), - b"Pattern" => items.next().and_then(|base| disallowed(&base, res, depth + 1)), - b"Separation" | b"DeviceN" => { - let _names = items.next(); - let alt = items.next()?; - if let Some(why) = disallowed(&alt, res, depth + 1) { - return Some(format!("a spot color whose alternate space is {why}")); - } - let _tint = items.next(); - // NChannel attributes are PDF 1.6, past the 1.4 that PDF/X-1a:2003 allows. - let attrs = items.next().and_then(|o| match o { - Object::Dict(d) => Some(d), - _ => None, - }); - attrs - .filter(|a| a.get::>(b"Subtype").is_some_and(|n| n.as_ref() == b"NChannel") || a.contains_key(b"Process")) - .map(|_| "a DeviceN space with NChannel attributes (PDF 1.6)".into()) - } - other => Some(format!("the color space /{}", String::from_utf8_lossy(other))), - } - } - _ => Some("an unreadable color space".into()), - } -} - -/// What one page has that PDF/X-1a doesn't allow, in the order found. -#[derive(Default)] -struct Scan { - /// What was found, in the order found; `known` is the same as a set. - found: Vec, - known: HashSet, - font_issues: Vec, - seen: HashSet, - fonts: HashSet, -} - -impl Scan { - fn add(&mut self, what: String) { - if self.known.insert(what.clone()) { - self.found.push(what); - } - } - - fn space(&mut self, cs: &Object<'_>, res: &Resources<'_>) { - if let Some(why) = disallowed(cs, res, 0) { - self.add(why); - } - } - - fn content(&mut self, data: &[u8], res: &Resources<'_>, depth: usize) { - if depth > MAX_DEPTH { - return; - } - let mut ops = UntypedIter::new(data); - while let Some(op) = ops.next() { - let operator: &[u8] = op.operator; - let last = op.operands().last().cloned(); - let name = || last.clone().and_then(Object::into_name); - match operator { - b"rg" | b"RG" => self.add("DeviceRGB color".into()), - b"cs" | b"CS" => { - if let Some(n) = name() { - self.space(&Object::Name(n), res); - } - } - b"sh" => { - if let Some(sh) = name().and_then(|n| res.get_shading(&n)) { - self.shading(sh, res); - } - } - b"scn" | b"SCN" => { - if let Some(p) = name().and_then(|n| res.get_pattern(&n)) { - self.pattern(p, res, depth); - } - } - b"gs" => { - if let Some(gs) = name().and_then(|n| res.get_ext_g_state(&n)) { - self.ext_g_state(&gs, res, depth); - } - } - b"Tf" => { - let font = op.operands().next().cloned().and_then(Object::into_name).and_then(|n| res.get_font(&n)); - if let Some(font) = font { - self.font(&font, res, depth); - } - } - b"Do" => { - if let Some(xo) = name().and_then(|n| res.get_x_object(&n)) { - match xo.dict().get::>(b"Subtype").as_deref() { - Some(b"Image") => self.image(&xo, res), - Some(b"Form") => self.form(&xo, res, depth), - _ => {} - } - } - } - b"BI" | b"EI" => { - if let Some(Object::Stream(img)) = last - && let Some(cs) = img.dict().get::>(b"CS").or_else(|| img.dict().get::>(b"ColorSpace")) - { - self.space(&cs, res); - } - } - _ => {} - } - } - } - - fn ext_g_state(&mut self, gs: &Dict<'_>, res: &Resources<'_>, depth: usize) { - match gs.get::>(b"SMask") { - None => {} - Some(Object::Name(n)) if n.as_ref() == b"None" => {} - Some(mask) => { - self.add("a soft mask (transparency)".into()); - if let Some(form) = match mask { - Object::Dict(d) => d.get::>(b"G"), - _ => None, - } { - self.form(&form, res, depth); - } - } - } - for key in [&b"CA"[..], b"ca"] { - if gs.get::(key).is_some_and(|a| a < 1.0) { - self.add("constant opacity below 100% (transparency)".into()); - } - } - let normal = |n: &[u8]| matches!(n, b"Normal" | b"Compatible"); - match gs.get::>(b"BM") { - Some(Object::Name(n)) if !normal(n.as_ref()) => { - self.add(format!("the blend mode /{} (transparency)", String::from_utf8_lossy(n.as_ref()))) - } - Some(Object::Array(a)) if a.iter::>().any(|n| !normal(n.as_ref())) => { - self.add("a blend mode other than Normal (transparency)".into()) - } - _ => {} - } - } - - fn image(&mut self, img: &Stream<'_>, res: &Resources<'_>) { - if !self.seen.insert(img.obj_id()) { - return; - } - let d = img.dict(); - if d.get::>(b"SMask").is_some() { - self.add(format!("an image with a soft mask (object {}, transparency)", img.obj_id().obj_number)); - } - if d.get::(b"SMaskInData").is_some_and(|v| v != 0) { - self.add(format!("an image with transparency in its data (object {})", img.obj_id().obj_number)); - } - if d.get::(b"ImageMask") == Some(true) { - return; - } - match d.get::>(b"ColorSpace") { - Some(cs) => { - if let Some(why) = disallowed(&cs, res, 0) { - let (w, h) = (d.get::(b"Width").unwrap_or(0), d.get::(b"Height").unwrap_or(0)); - self.add(format!("{why} in an image (object {}, {w} × {h} px)", img.obj_id().obj_number)); - } - } - // JPX images may carry their own space; X-1a doesn't allow them. - None => self.add(format!("an image without a color space (object {})", img.obj_id().obj_number)), - } - } - - fn form(&mut self, form: &Stream<'_>, parent: &Resources<'_>, depth: usize) { - if depth >= MAX_DEPTH || !self.seen.insert(form.obj_id()) { - return; - } - let res = Resources::from_parent(form.dict().get::>(b"Resources").unwrap_or_default(), parent.clone()); - if let Some(group) = form.dict().get::>(b"Group") { - self.group(&group); - } - if let Ok(data) = form.decoded() { - self.content(&data, &res, depth + 1); - } - } - - fn group(&mut self, group: &Dict<'_>) { - if group.get::>(b"S").is_some_and(|s| s.as_ref() == b"Transparency") { - self.add("a transparency group".into()); - } - } - - fn shading(&mut self, sh: Object<'_>, res: &Resources<'_>) { - let dict = match sh { - Object::Dict(d) => d, - Object::Stream(s) => s.dict().clone(), - _ => return, - }; - if let Some(cs) = dict.get::>(b"ColorSpace") { - self.space(&cs, res); - } - } - - fn pattern(&mut self, p: Object<'_>, res: &Resources<'_>, depth: usize) { - match p { - Object::Dict(d) => { - if let Some(sh) = d.get::>(b"Shading") { - self.shading(sh, res); - } - if let Some(gs) = d.get::>(b"ExtGState") { - self.ext_g_state(&gs, res, depth); - } - } - Object::Stream(s) => self.form(&s, res, depth), - _ => {} - } - } - - /// Embedded: a simple font's descriptor, or a Type 0 font's descendant's, has a font file. A - /// Type 3 font's glyphs are content streams in the file, scanned like the page's. - fn font(&mut self, font: &Dict<'_>, parent: &Resources<'_>, depth: usize) { - if let Some(id) = font.obj_id() - && !self.fonts.insert(id) - { - return; - } - let name = font.get::>(b"BaseFont").map(|n| String::from_utf8_lossy(n.as_ref()).to_string()).unwrap_or_else(|| "?".into()); - let subtype = font.get::>(b"Subtype"); - let descriptor = match subtype.as_deref() { - Some(b"Type3") => return self.type3(font, parent, depth), - Some(b"Type0") => { - font.get::>(b"DescendantFonts").and_then(|a| a.iter::>().next()).and_then(|d| d.get::>(b"FontDescriptor")) - } - _ => font.get::>(b"FontDescriptor"), - }; - let embedded = descriptor.is_some_and(|d| [&b"FontFile"[..], b"FontFile2", b"FontFile3"].iter().any(|k| d.get::>(k).is_some())); - if !embedded { - self.font_issues.push(format!("the font {name} isn't embedded")); - } - } - - /// A Type 3 font's glyph procedures, with the font's resources over `parent`'s. - fn type3(&mut self, font: &Dict<'_>, parent: &Resources<'_>, depth: usize) { - if depth >= MAX_DEPTH { - return; - } - let res = Resources::from_parent(font.get::>(b"Resources").unwrap_or_default(), parent.clone()); - let Some(procs) = font.get::>(b"CharProcs") else { return }; - for key in procs.keys() { - if let Some(glyph) = procs.get::>(key.as_ref()) - && self.seen.insert(glyph.obj_id()) - && let Ok(data) = glyph.decoded() - { - self.content(&data, &res, depth + 1); - } - } - } +pub fn check_pdfx1a(pdf: &[u8]) -> Vec { + craft_prepress::pdfx1a::check_pdfx1a(pdf) } From c1fdb6b3af935c9ee5d6c7d1b80edde4ddadb67b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:19:42 -0500 Subject: [PATCH 032/153] Align PDF dependency guard with shared backend pin --- xtask/src/pdf_deps.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xtask/src/pdf_deps.rs b/xtask/src/pdf_deps.rs index 2f33f3109..fac9c633b 100644 --- a/xtask/src/pdf_deps.rs +++ b/xtask/src/pdf_deps.rs @@ -5,7 +5,8 @@ use std::process::Command; use serde_json::Value; const REPO: &str = "https://github.com/storytold/craft-libs"; -const REV: &str = "92d392cff132eaf53e13afff77f38b5c11b73aa0"; +const REV: &str = "b267a3a66"; +const COMMIT: &str = "b267a3a663a490778ae5c50a05ef75f3eb57e076"; const PACKAGES: &[(&str, &str)] = &[ ("craft-pdf", "0.1.0"), ("krilla", "0.8.2"), @@ -34,7 +35,7 @@ pub fn run(root: &Path) -> Result<(), String> { fn validate(metadata: &Value) -> Result<(), String> { let packages = metadata.get("packages").and_then(Value::as_array).ok_or("cargo metadata has no packages")?; - let source = format!("git+{REPO}?rev={REV}#{REV}"); + let source = format!("git+{REPO}?rev={REV}#{COMMIT}"); for (name, version) in PACKAGES { let matching: Vec<_> = packages.iter().filter(|p| p.get("name").and_then(Value::as_str) == Some(name)).collect(); let [package] = matching.as_slice() else { From 275b907c57ec355d6322c7228244e4086910d006 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:43:21 -0500 Subject: [PATCH 033/153] Use shared PDF SVG adapter and CSV sniffing --- Cargo.lock | 102 +++++++++++++++++++++++------- Cargo.toml | 27 ++++---- crates/images/src/lib.rs | 37 ++++++++--- crates/pdf/Cargo.toml | 3 +- crates/pdf/src/export.rs | 2 +- crates/pdf/src/export/graphics.rs | 19 ++++-- xtask/src/pdf_deps.rs | 4 +- 7 files changed, 139 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e3186cb7a..ba7157887 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1071,7 +1071,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 [[package]] name = "craft-cms" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "md-5", "rayon", @@ -1085,7 +1085,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 [[package]] name = "craft-csv" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=47bf5650b0d503b97bfb5e841746c4957897d11f#47bf5650b0d503b97bfb5e841746c4957897d11f" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=44d7ac8d942dbd87048f82b3efc3e0d14483cc8d#44d7ac8d942dbd87048f82b3efc3e0d14483cc8d" dependencies = [ "thiserror 2.0.21", ] @@ -1354,14 +1354,17 @@ dependencies = [ [[package]] name = "craft-pdf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "bytemuck", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f)", "hayro", "hayro-interpret", "hayro-syntax", "krilla", + "krilla-svg", "thiserror 2.0.21", + "usvg 0.47.0", ] [[package]] @@ -1382,7 +1385,7 @@ dependencies = [ [[package]] name = "craft-prepress" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "craft-cms", "craft-pdf", @@ -1433,21 +1436,22 @@ source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad605 [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f)", "flate2", "kurbo", "roxmltree 0.21.2", "thiserror 2.0.21", + "usvg 0.48.1", ] [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", "flate2", "kurbo", "roxmltree 0.21.2", @@ -1547,6 +1551,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -1952,7 +1965,7 @@ dependencies = [ "craft-file-formats", "craft-image-codecs", "craft-psd", - "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f)", "designcraft-color", "designcraft-fonts", "flate2", @@ -1996,7 +2009,6 @@ dependencies = [ "hayro-cmap", "hayro-interpret", "image", - "krilla-svg", "kurbo", "log", "thiserror 2.0.21", @@ -2751,6 +2763,17 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "fontdb" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2660c5e9157bf76d2db1294e4a9feba604ef610819a3b591088d0d8392a3290f" +dependencies = [ + "log", + "slotmap", + "tinyvec", +] + [[package]] name = "foreign-types" version = "0.5.0" @@ -3181,7 +3204,7 @@ dependencies = [ [[package]] name = "hayro" version = "0.7.1" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "bytemuck", "hayro-interpret", @@ -3201,7 +3224,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a" [[package]] name = "hayro-cmap" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "brotli", "hayro-postscript", @@ -3210,7 +3233,7 @@ dependencies = [ [[package]] name = "hayro-interpret" version = "0.7.0" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "bitflags 2.13.2", "hayro-cmap", @@ -3228,7 +3251,7 @@ dependencies = [ [[package]] name = "hayro-jbig2" version = "0.3.0" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "fearless_simd", "hayro-ccitt", @@ -3246,12 +3269,12 @@ dependencies = [ [[package]] name = "hayro-postscript" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" [[package]] name = "hayro-syntax" version = "0.7.2" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "flate2", "hayro-ccitt", @@ -3682,6 +3705,12 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09e54e57b4c48b40f7aec75635392b12b3421fa26fe8b4332e63138ed278459c" +[[package]] +name = "imagesize" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" + [[package]] name = "indd" version = "0.1.0" @@ -3880,7 +3909,7 @@ dependencies = [ [[package]] name = "krilla" version = "0.8.2" -source = "git+https://github.com/storytold/craft-libs?rev=b267a3a66#b267a3a663a490778ae5c50a05ef75f3eb57e076" +source = "git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f#40525913633942f84b06d4837a8d90534a55d63f" dependencies = [ "base64 0.22.1", "bumpalo", @@ -3889,7 +3918,7 @@ dependencies = [ "gif", "hayro-write", "image-webp", - "imagesize", + "imagesize 0.14.0", "indexmap", "pdf-writer", "png", @@ -3912,13 +3941,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1237d7c37b16ca9fbc2e72dde13a10d321f9a44aceee35c062bc0a43bfc7ce16" dependencies = [ "flate2", - "fontdb", + "fontdb 0.23.0", "krilla", "resvg", "skrifa 0.42.1", "smallvec", "tiny-skia 0.12.0", - "usvg", + "usvg 0.47.0", ] [[package]] @@ -5542,7 +5571,7 @@ dependencies = [ "rgb", "svgtypes", "tiny-skia 0.12.0", - "usvg", + "usvg 0.47.0", "zune-jpeg", ] @@ -7130,8 +7159,8 @@ dependencies = [ "base64 0.22.1", "data-url", "flate2", - "fontdb", - "imagesize", + "fontdb 0.23.0", + "imagesize 0.14.0", "kurbo", "log", "pico-args", @@ -7149,6 +7178,33 @@ dependencies = [ "xmlwriter", ] +[[package]] +name = "usvg" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "977d0a4abdef933f424a99fe09f95576e089b90aebc6f016a3bc813762493e91" +dependencies = [ + "base64 0.23.1", + "data-url", + "fontdb 0.24.0", + "harfrust 0.12.0", + "imagesize 0.15.0", + "kurbo", + "log", + "pico-args", + "roxmltree 0.21.2", + "simplecss", + "siphasher", + "skrifa 0.44.0", + "strict-num", + "svgtypes", + "tiny-skia-path 0.12.0", + "unicode-bidi", + "unicode-script", + "unicode-vo", + "xmlwriter", +] + [[package]] name = "utf16_iter" version = "1.0.5" diff --git a/Cargo.toml b/Cargo.toml index 26e155b58..3136c6aeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,19 +12,19 @@ repository = "https://github.com/storytold/designcraft" [workspace.dependencies] # Shared reader and writer; publish the immutable revision before this consumer. -craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.1.0", default-features = false } +craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.1.0", default-features = false } craft-document-bundle = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-indd = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } -craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.1.0", features = ["tree-text"] } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } -craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } +craft-csv = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "44d7ac8d942dbd87048f82b3efc3e0d14483cc8d", version = "=0.1.0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } @@ -33,7 +33,7 @@ craft-pptx = { git = "https://github.com/storytold/craft-libs", rev = "f6cab1980 # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. -craft-prepress = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.1.0", features = ["flatten"] } +craft-prepress = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.1.0", features = ["flatten"] } # 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 = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] } # internal @@ -84,9 +84,8 @@ vello_common = { version = "0.2", default-features = false } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] } # CMYK TIFF samples for PDF export (`image` decodes them to RGB); the version `image` uses. tiff = "0.11" -# SVG (same usvg as krilla-svg). +# SVG preview until the bounded craft-svg tree renderer is pinned into this workspace. resvg = { version = "0.47", default-features = false, features = ["text"] } -krilla-svg = "=0.8.1" # Boolean operations on Bézier paths (Pathfinder). flo_curves = "0.8" # QR codes (Object › Generate QR Code); MIT. @@ -101,7 +100,7 @@ craft-atomic = { version = "0.1.0", git = "https://github.com/storytold/craft-li craft-print = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } # ICC colour management: working spaces, transforms, black point compensation (craft-libs). -craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b267a3a66" } +craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f" } # ui # Shared frontend components; keep this dependency above the engine boundary. @@ -151,16 +150,16 @@ debug = 0 # Opt-in cancelable macOS termination on the pinned winit 0.30 line. [patch.crates-io] -krilla = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.8.2" } +krilla = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.8.2" } winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } # The hayro crates from craft-libs' vendor/ directory (pdfcraft's, plus the plate mode and decode # limits craft-pdf and craft-prepress need); same revision as craft-pdf. -hayro = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.7.1" } -hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.7.0" } -hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.7.2" } -hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.3.0" } -hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.1.0" } -hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "b267a3a66", version = "=0.1.0" } +hayro = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.7.1" } +hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.7.0" } +hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.7.2" } +hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.3.0" } +hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.1.0" } +hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.1.0" } ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs index a4c9d9b86..70af3662e 100644 --- a/crates/images/src/lib.rs +++ b/crates/images/src/lib.rs @@ -55,10 +55,9 @@ pub fn mime(bytes: &[u8]) -> &'static str { } } -/// Initialize the process-wide usvg database from the bundled and Japanese fonts, with case-aware family/PostScript resolution, fallback selection and preflighted SVG data URLs; file-backed image URLs are never read. -fn svg_options() -> &'static usvg::Options<'static> { - static OPTS: OnceLock> = OnceLock::new(); - OPTS.get_or_init(|| { +fn svg_fontdb() -> Arc { + static DB: OnceLock> = OnceLock::new(); + DB.get_or_init(|| { let mut db = usvg::fontdb::Database::new(); for f in designcraft_fonts::bundled() { db.load_font_data(f.to_vec()); @@ -70,11 +69,31 @@ fn svg_options() -> &'static usvg::Options<'static> { db.set_serif_family("Source Serif 4"); db.set_sans_serif_family("Source Sans 3"); db.set_monospace_family("JetBrains Mono"); - let font_resolver = usvg::FontResolver { select_font: select_font(), select_fallback: select_fallback() }; + Arc::new(db) + }) + .clone() +} + +/// SVG options with DesignCraft's font database and family/PostScript fallback policy. +/// The PDF adapter uses these same font choices while owning its separate parser. +pub fn svg_options_for_pdf() -> usvg::Options<'static> { + usvg::Options { + fontdb: svg_fontdb(), + font_family: "Source Sans 3".into(), + font_resolver: usvg::FontResolver { select_font: select_font(), select_fallback: select_fallback() }, + ..Default::default() + } +} + +/// Initialize the process-wide usvg options with preflighted data URLs; file-backed image URLs are never read. +fn svg_options() -> &'static usvg::Options<'static> { + static OPTS: OnceLock> = OnceLock::new(); + OPTS.get_or_init(|| { + let mut options = svg_options_for_pdf(); // Images inside an SVG come only from `data:` URLs: an `href` naming a file is not read // (it is left out, as usvg leaves out an image it can't load). let default_data = usvg::ImageHrefResolver::default_data_resolver(); - let image_href_resolver = usvg::ImageHrefResolver { + options.image_href_resolver = usvg::ImageHrefResolver { resolve_data: Box::new(move |mime, data, options| { let raster = matches!( craft_image_codecs::detect(&data), @@ -95,7 +114,7 @@ fn svg_options() -> &'static usvg::Options<'static> { }), resolve_string: Box::new(|_, _| None), }; - usvg::Options { fontdb: Arc::new(db), font_family: "Source Sans 3".into(), font_resolver, image_href_resolver, ..Default::default() } + options }) } @@ -298,14 +317,14 @@ fn charge_nested_svg(n: usize) -> Option<()> { /// Decode and structurally check SVG/SVGZ through the shared bounded parser. fn svg_text(bytes: &[u8], limit: usize) -> Option> { - let limits = craft_svg::preflight::Limits { + let limits = craft_svg::tree::Limits { bytes: limit.min(MAX_SVG_BYTES), depth: MAX_SVG_DEPTH, xml_nodes: u32::try_from(MAX_SVG_EVENTS).ok()?, refuse_entity_markup: true, ..Default::default() }; - craft_svg::preflight::text(bytes, &limits).ok() + craft_svg::tree::text(bytes, &limits).ok() } /// Parse an SVG with at most 16 MiB encoded/decoded data, 100,000 XML events and diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index 069c1162d..2edada4d5 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -14,11 +14,10 @@ designcraft-color = { workspace = true } designcraft-doc = { workspace = true } designcraft-fonts = { workspace = true } designcraft-compose = { workspace = true } -craft-pdf = { workspace = true, features = ["read", "write-pdf"] } +craft-pdf = { workspace = true, features = ["read", "write-pdf", "write-svg"] } craft-image-codecs = { workspace = true } craft-prepress = { workspace = true } craft-cms = { workspace = true } -krilla-svg = { workspace = true } designcraft-images = { workspace = true } image = { workspace = true } thiserror = { workspace = true } diff --git a/crates/pdf/src/export.rs b/crates/pdf/src/export.rs index 5462c015a..8e23fd33d 100644 --- a/crates/pdf/src/export.rs +++ b/crates/pdf/src/export.rs @@ -88,7 +88,7 @@ pub(crate) struct Exporter<'a> { /// Placed PDFs too new to embed in this version ("name is PDF 2.0"): the export fails. too_new: Vec, /// Parsed placed SVGs. - svgs: HashMap>>, + svgs: HashMap>>, pub fonts: HashMap>, pub reverse_cmaps: HashMap>>, /// Convert CMYK to RGB (PDF/A: krilla needs a CMYK output profile we don't ship yet). diff --git a/crates/pdf/src/export/graphics.rs b/crates/pdf/src/export/graphics.rs index aaa31150c..5b5b6134e 100644 --- a/crates/pdf/src/export/graphics.rs +++ b/crates/pdf/src/export/graphics.rs @@ -261,13 +261,24 @@ impl Exporter<'_> { fn graphic_placed_svg(&mut self, s: &mut Surface, g: &designcraft_doc::Graphic) -> bool { let Some(asset) = self.doc.assets.get(&g.asset).filter(|asset| designcraft_images::is_svg(&asset.data)) else { return false }; let Some(size) = Size::from_wh(g.size.0.max(1e-3) as f32, g.size.1.max(1e-3) as f32) else { return true }; - let tree = self.svgs.entry(g.asset).or_insert_with(|| designcraft_images::svg_tree(&asset.data).map(Arc::new)).clone(); + let tree = self + .svgs + .entry(g.asset) + .or_insert_with(|| { + craft_pdf::write::svg::Document::parse( + &asset.data, + craft_pdf::write::svg::Limits::default(), + designcraft_images::svg_options_for_pdf(), + ) + .ok() + .map(Arc::new) + }) + .clone(); match tree { Some(tree) => { - use krilla_svg::SurfaceExt; s.push_transform(&tf(g.xf)); - if s.draw_svg(&tree, size, krilla_svg::SvgSettings::default()).is_none() { - self.warn(format!("{}: parts of the placed SVG could not be drawn", asset.name)); + if let Err(error) = tree.draw(s, size, craft_pdf::write::svg::SvgSettings::default()) { + self.warn(format!("{}: the placed SVG could not be drawn: {error}", asset.name)); } s.pop(); } diff --git a/xtask/src/pdf_deps.rs b/xtask/src/pdf_deps.rs index fac9c633b..2b351c3c2 100644 --- a/xtask/src/pdf_deps.rs +++ b/xtask/src/pdf_deps.rs @@ -5,8 +5,8 @@ use std::process::Command; use serde_json::Value; const REPO: &str = "https://github.com/storytold/craft-libs"; -const REV: &str = "b267a3a66"; -const COMMIT: &str = "b267a3a663a490778ae5c50a05ef75f3eb57e076"; +const REV: &str = "40525913633942f84b06d4837a8d90534a55d63f"; +const COMMIT: &str = "40525913633942f84b06d4837a8d90534a55d63f"; const PACKAGES: &[(&str, &str)] = &[ ("craft-pdf", "0.1.0"), ("krilla", "0.8.2"), From 581ba450cd3a7f7ee518c1f4b5f0ad0609661b71 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:55:54 -0500 Subject: [PATCH 034/153] Delegate screen SVG parsing and rendering --- Cargo.lock | 54 +++++++- Cargo.toml | 8 +- crates/images/Cargo.toml | 1 - crates/images/src/lib.rs | 99 +++----------- crates/images/src/tests.rs | 23 ++-- crates/pdf/Cargo.toml | 1 + crates/pdf/src/export/graphics.rs | 2 +- crates/pdf/src/lib.rs | 1 + crates/pdf/src/svg_options.rs | 209 ++++++++++++++++++++++++++++++ 9 files changed, 296 insertions(+), 102 deletions(-) create mode 100644 crates/pdf/src/svg_options.rs diff --git a/Cargo.lock b/Cargo.lock index ba7157887..eb8d919c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1245,7 +1245,7 @@ dependencies = [ [[package]] name = "craft-image-codecs" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a47b78256c6acf3d7fdc4bc7cfbc25c373eac522#a47b78256c6acf3d7fdc4bc7cfbc25c373eac522" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" dependencies = [ "craft-jpeg", "exr", @@ -1275,7 +1275,7 @@ dependencies = [ [[package]] name = "craft-jpeg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a47b78256c6acf3d7fdc4bc7cfbc25c373eac522#a47b78256c6acf3d7fdc4bc7cfbc25c373eac522" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" dependencies = [ "thiserror 2.0.21", ] @@ -1411,7 +1411,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a47b78256c6acf3d7fdc4bc7cfbc25c373eac522#a47b78256c6acf3d7fdc4bc7cfbc25c373eac522" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" dependencies = [ "encoding_rs", "flate2", @@ -1433,6 +1433,21 @@ name = "craft-sheet" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" +dependencies = [ + "craft-image-codecs", + "craft-xml 0.1.0 (git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f)", + "flate2", + "kurbo", + "resvg 0.48.1", + "roxmltree 0.21.2", + "thiserror 2.0.21", + "usvg 0.48.1", +] + [[package]] name = "craft-svg" version = "0.1.0" @@ -1551,6 +1566,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -1965,13 +1989,12 @@ dependencies = [ "craft-file-formats", "craft-image-codecs", "craft-psd", - "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=40525913633942f84b06d4837a8d90534a55d63f)", + "craft-svg 0.1.0 (git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f)", "designcraft-color", "designcraft-fonts", "flate2", "image", "log", - "resvg", "skrifa 0.47.0", "tiff", ] @@ -2011,6 +2034,7 @@ dependencies = [ "image", "kurbo", "log", + "skrifa 0.47.0", "thiserror 2.0.21", "tiff", ] @@ -3943,7 +3967,7 @@ dependencies = [ "flate2", "fontdb 0.23.0", "krilla", - "resvg", + "resvg 0.47.0", "skrifa 0.42.1", "smallvec", "tiny-skia 0.12.0", @@ -5575,6 +5599,24 @@ dependencies = [ "zune-jpeg", ] +[[package]] +name = "resvg" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67e3803f97b999e80cbf7c6ecdd07a8102204d92e1633cf48783720c521196bd" +dependencies = [ + "bytemuck", + "gif", + "image-webp", + "log", + "pico-args", + "rgb", + "svgtypes", + "tiny-skia 0.12.0", + "usvg 0.48.1", + "zune-jpeg", +] + [[package]] name = "rfd" version = "0.17.2" diff --git a/Cargo.toml b/Cargo.toml index 3136c6aeb..e073298f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,15 +18,15 @@ craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435 craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-indd = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } -craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "40525913633942f84b06d4837a8d90534a55d63f", version = "=0.1.0", features = ["tree-text"] } +craft-svg = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "0d1854f1c8a5df0301604098c898bee16349151f", version = "=0.1.0", features = ["tree-render", "tree-text"] } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-csv = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "44d7ac8d942dbd87048f82b3efc3e0d14483cc8d", version = "=0.1.0" } -craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } -craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a47b78256c6acf3d7fdc4bc7cfbc25c373eac522", version = "=0.1.0" } +craft-image-codecs = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "0d1854f1c8a5df0301604098c898bee16349151f", version = "=0.1.0" } +craft-psd = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "0d1854f1c8a5df0301604098c898bee16349151f", version = "=0.1.0" } craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } craft-pptx = { git = "https://github.com/storytold/craft-libs", rev = "f6cab1980", version = "=0.1.0" } @@ -84,8 +84,6 @@ vello_common = { version = "0.2", default-features = false } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] } # CMYK TIFF samples for PDF export (`image` decodes them to RGB); the version `image` uses. tiff = "0.11" -# SVG preview until the bounded craft-svg tree renderer is pinned into this workspace. -resvg = { version = "0.47", default-features = false, features = ["text"] } # Boolean operations on Bézier paths (Pathfinder). flo_curves = "0.8" # QR codes (Object › Generate QR Code); MIT. diff --git a/crates/images/Cargo.toml b/crates/images/Cargo.toml index f2cd6b362..4adf1900c 100644 --- a/crates/images/Cargo.toml +++ b/crates/images/Cargo.toml @@ -14,7 +14,6 @@ craft-svg = { workspace = true } designcraft-color = { workspace = true } designcraft-fonts = { workspace = true } image = { workspace = true } -resvg = { workspace = true } skrifa = { workspace = true } log = { workspace = true } craft-cms = { workspace = true } diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs index 70af3662e..9cbc4b42b 100644 --- a/crates/images/src/lib.rs +++ b/crates/images/src/lib.rs @@ -1,12 +1,11 @@ -//! Placed graphics: format sniffing, sizes and decoding for the renderer and PDF export. -//! Rasters go through `image` (PNG, JPEG, GIF, WebP, TIFF, BMP) or [`psd`] (Photoshop's merged -//! composite); SVG is parsed with usvg (text set in the bundled fonts, then the shared ones) and -//! rasterised with resvg for the screen — PDF export draws the same tree as vectors. +//! Placed graphics: format-agnostic pixel operations and shared file-format delegation. +//! Raster formats are decoded by `craft-image-codecs`; SVG parsing and bounded screen rendering +//! are provided by `craft-svg`. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] use std::sync::{Arc, OnceLock}; -pub use resvg::usvg; +pub use craft_svg::tree::usvg; pub mod colour; mod eps; @@ -19,6 +18,10 @@ pub use tone::{Tone, tone}; /// CSS pixels (SVG user units) → points. pub const PT_PER_PX: f64 = 0.75; +const MAX_SVG_BYTES: usize = 16 * 1024 * 1024; +const MAX_SVG_EVENTS: usize = 100_000; +const MAX_SVG_DEPTH: usize = 64; + pub fn is_pdf(bytes: &[u8]) -> bool { file_format(bytes) == Some(craft_file_formats::Format::Pdf) } @@ -74,9 +77,8 @@ fn svg_fontdb() -> Arc { .clone() } -/// SVG options with DesignCraft's font database and family/PostScript fallback policy. -/// The PDF adapter uses these same font choices while owning its separate parser. -pub fn svg_options_for_pdf() -> usvg::Options<'static> { +/// usvg options with DesignCraft's font database and family/PostScript fallback policy. +fn svg_options() -> usvg::Options<'static> { usvg::Options { fontdb: svg_fontdb(), font_family: "Source Sans 3".into(), @@ -85,39 +87,6 @@ pub fn svg_options_for_pdf() -> usvg::Options<'static> { } } -/// Initialize the process-wide usvg options with preflighted data URLs; file-backed image URLs are never read. -fn svg_options() -> &'static usvg::Options<'static> { - static OPTS: OnceLock> = OnceLock::new(); - OPTS.get_or_init(|| { - let mut options = svg_options_for_pdf(); - // Images inside an SVG come only from `data:` URLs: an `href` naming a file is not read - // (it is left out, as usvg leaves out an image it can't load). - let default_data = usvg::ImageHrefResolver::default_data_resolver(); - options.image_href_resolver = usvg::ImageHrefResolver { - resolve_data: Box::new(move |mime, data, options| { - let raster = matches!( - craft_image_codecs::detect(&data), - Some( - craft_image_codecs::Format::Png - | craft_image_codecs::Format::Jpeg - | craft_image_codecs::Format::Gif - | craft_image_codecs::Format::WebP - ) - ); - if mime == "image/svg+xml" || (mime == "text/plain" && !raster) { - let xml = svg_text(&data, MAX_NESTED_SVG_BYTES.saturating_sub(NESTED_SVG_BYTES.get()).min(MAX_SVG_BYTES))?; - charge_nested_svg(xml.len())?; - default_data(mime, Arc::new(xml.into_owned().into_bytes()), options) - } else { - default_data(mime, data, options) - } - }), - resolve_string: Box::new(|_, _| None), - }; - options - }) -} - /// A family the SVG names that isn't bundled comes from the shared fonts (installed ones too), /// as it would in a text frame, and matches in any case, as it does there. A name that is no /// family's but a face's PostScript name (as Illustrator's SVG export writes them: @@ -292,47 +261,20 @@ fn add_face(db: &mut Arc, face: Arc = const { std::cell::Cell::new(0) }; -} - -/// Take `n` bytes of the running parse's nested-SVG budget; `None` when they don't fit. -fn charge_nested_svg(n: usize) -> Option<()> { - let used = NESTED_SVG_BYTES.get().checked_add(n).filter(|u| *u <= MAX_NESTED_SVG_BYTES)?; - NESTED_SVG_BYTES.set(used); - Some(()) -} - -/// Decode and structurally check SVG/SVGZ through the shared bounded parser. -fn svg_text(bytes: &[u8], limit: usize) -> Option> { +/// Parse SVG/SVGZ through craft-svg's bounded parser and data-only nested-image resolver. +pub fn svg_tree(bytes: &[u8]) -> Option { let limits = craft_svg::tree::Limits { - bytes: limit.min(MAX_SVG_BYTES), + bytes: MAX_SVG_BYTES, depth: MAX_SVG_DEPTH, - xml_nodes: u32::try_from(MAX_SVG_EVENTS).ok()?, refuse_entity_markup: true, + xml_nodes: MAX_SVG_EVENTS as u32, + embedded_bytes: 32 * 1024 * 1024, + embedded_depth: 4, ..Default::default() }; - craft_svg::tree::text(bytes, &limits).ok() -} - -/// Parse an SVG with at most 16 MiB encoded/decoded data, 100,000 XML events and -/// 64 nested elements. Reject internal DTD subsets before recursive conversion. -pub fn svg_tree(bytes: &[u8]) -> Option { - let xml = svg_text(bytes, MAX_SVG_BYTES)?; - NESTED_SVG_BYTES.set(0); - usvg::Tree::from_data(xml.as_bytes(), svg_options()).ok() + let mut options = svg_options(); + options.image_href_resolver = craft_svg::tree::image_resolver(limits); + craft_svg::tree::parse(bytes, &limits, &options).ok() } /// Natural size of a placed graphic in points: rasters at 72 ppi (one pixel per point), PSDs @@ -461,8 +403,7 @@ pub fn render_svg(bytes: &[u8], max_side: u32) -> Option<(Vec, u32, u32)> { let s = tree.size(); let k = max_side as f32 / s.width().max(s.height()).max(1e-3); let (w, h) = ((s.width() * k).round().max(1.0) as u32, (s.height() * k).round().max(1.0) as u32); - let mut pm = resvg::tiny_skia::Pixmap::new(w, h)?; - resvg::render(&tree, resvg::tiny_skia::Transform::from_scale(k, k), &mut pm.as_mut()); + let pm = craft_svg::tree::draw::render(&tree, craft_svg::tree::draw::tiny_skia::Transform::from_scale(k, k), w, h, 0, MAX_RGBA_BYTES).ok()?; Some((pm.take(), w, h)) } diff --git a/crates/images/src/tests.rs b/crates/images/src/tests.rs index 3e5daf8ab..fa082bd45 100644 --- a/crates/images/src/tests.rs +++ b/crates/images/src/tests.rs @@ -105,15 +105,18 @@ fn nested_svg_data_images_cannot_bypass_the_depth_limit() { } } -/// The nested SVG data of one parse adds up against one budget; the next parse starts again. +/// Each SVG parse gets a fresh shared nested-image budget. #[test] -fn nested_svg_data_shares_one_budget() { - NESTED_SVG_BYTES.set(0); - assert!(charge_nested_svg(MAX_NESTED_SVG_BYTES / 2).is_some()); - assert!(charge_nested_svg(MAX_NESTED_SVG_BYTES / 2).is_some()); - assert!(charge_nested_svg(1).is_none()); - assert!(svg_tree(SVG.as_bytes()).is_some()); - assert!(charge_nested_svg(MAX_NESTED_SVG_BYTES).is_some()); +fn nested_svg_data_budget_is_fresh_per_parse() { + let nested = r#""#; + let encoded = nested.bytes().map(|b| format!("%{b:02X}")).collect::(); + let svg = format!( + r#""# + ); + for _ in 0..2 { + let tree = svg_tree(svg.as_bytes()).unwrap(); + assert_eq!(tree.root().children().len(), 1); + } } #[test] @@ -164,8 +167,8 @@ fn svg_images_come_only_from_data_urls() { } /// The family of the font that sets each text glyph of `svg`, and the glyph's id. -fn glyph_families(svg: &str) -> Vec<(String, u16)> { - fn walk(g: &usvg::Group, tree: &usvg::Tree, out: &mut Vec<(String, u16)>) { +fn glyph_families(svg: &str) -> Vec<(String, u32)> { + fn walk(g: &usvg::Group, tree: &usvg::Tree, out: &mut Vec<(String, u32)>) { for n in g.children() { match n { usvg::Node::Group(g) => walk(g, tree, out), diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index 2edada4d5..3e1fd8774 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -13,6 +13,7 @@ designcraft-geom = { workspace = true } designcraft-color = { workspace = true } designcraft-doc = { workspace = true } designcraft-fonts = { workspace = true } +skrifa = { workspace = true } designcraft-compose = { workspace = true } craft-pdf = { workspace = true, features = ["read", "write-pdf", "write-svg"] } craft-image-codecs = { workspace = true } diff --git a/crates/pdf/src/export/graphics.rs b/crates/pdf/src/export/graphics.rs index 5b5b6134e..c463dabca 100644 --- a/crates/pdf/src/export/graphics.rs +++ b/crates/pdf/src/export/graphics.rs @@ -268,7 +268,7 @@ impl Exporter<'_> { craft_pdf::write::svg::Document::parse( &asset.data, craft_pdf::write::svg::Limits::default(), - designcraft_images::svg_options_for_pdf(), + crate::svg_options::svg_options_for_pdf(), ) .ok() .map(Arc::new) diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index b0ceaefff..eb20bf9af 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -47,6 +47,7 @@ mod forms; mod optional_content; mod pdfx; mod pdfx1a; +mod svg_options; mod transitions; pub use export::{ BookletKind, BookletOptions, booklet_pairs, drawn_children, export_booklet, export_pdf, export_pdf_with_report, item_exported, layer_exported, diff --git a/crates/pdf/src/svg_options.rs b/crates/pdf/src/svg_options.rs new file mode 100644 index 000000000..1a069e2d3 --- /dev/null +++ b/crates/pdf/src/svg_options.rs @@ -0,0 +1,209 @@ +//! DesignCraft font selection for the PDF writer's SVG parser. + +use std::sync::{Arc, OnceLock}; + +use craft_pdf::write::svg::usvg; + +fn svg_fontdb() -> Arc { + static DB: OnceLock> = OnceLock::new(); + DB.get_or_init(|| { + let mut db = usvg::fontdb::Database::new(); + for f in designcraft_fonts::bundled() { + db.load_font_data(f.to_vec()); + } + // Japanese text in SVGs: the craft-fonts faces (none without CRAFT_FONTS_DIR). + for f in designcraft_fonts::japanese_document_fonts() { + db.load_font_data(f.bytes.to_vec()); + } + db.set_serif_family("Source Serif 4"); + db.set_sans_serif_family("Source Sans 3"); + db.set_monospace_family("JetBrains Mono"); + Arc::new(db) + }) + .clone() +} + +/// SVG options with DesignCraft's font database and family/PostScript fallback policy. +/// The PDF adapter uses these same font choices while owning its separate parser. +pub(crate) fn svg_options_for_pdf() -> usvg::Options<'static> { + usvg::Options { + fontdb: svg_fontdb(), + font_family: "Source Sans 3".into(), + font_resolver: usvg::FontResolver { select_font: select_font(), select_fallback: select_fallback() }, + ..Default::default() + } +} + +/// A family the SVG names that isn't bundled comes from the shared fonts (installed ones too), +/// as it would in a text frame, and matches in any case, as it does there. A name that is no +/// family's but a face's PostScript name (as Illustrator's SVG export writes them: +/// `font-family="'MyriadPro-Regular'"`) sets the text in that face, whatever font-weight and +/// font-style say: the name already picks the weight and style, and a family's other faces would +/// contradict it. A name that is a face's legacy family name (name ID 1) or full name (ID 4), as +/// Windows names faces outside the regular/bold/italic quartet (`'Segoe UI Semibold'`, whose +/// typographic family is `Segoe UI`), sets the text in that face (the closest to font-weight and +/// font-style when the legacy family has several). Families listed before it come first. +fn select_font() -> usvg::FontSelectionFn<'static> { + use usvg::fontdb::{Family, Query, Stretch, Style, Weight}; + Box::new(move |font, db| { + let fonts = designcraft_fonts::FontDb::global(); + let spelled = |db: &usvg::fontdb::Database, name: &str| { + db.faces().flat_map(|f| &f.families).find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(n, _)| n.clone()) + }; + // (index in the font-family list, face) of the faces a name names by its legacy names. + let mut legacy: Vec<(usize, usvg::fontdb::ID)> = Vec::new(); + for (i, family) in font.families().iter().enumerate() { + if let usvg::FontFamily::Named(name) = family + && spelled(db, name).is_none() + { + if fonts.has_family(name) { + for style in fonts.styles(name) { + let face = fonts.face(name, &style); + let named = has_legacy_name(&face, name); + if let Some(id) = add_face(db, face) + && named + { + legacy.push((i, id)); + } + } + } else if postscript_face(db, name).is_none() { + if let Some((family, style)) = fonts.postscript_face(name) { + add_face(db, fonts.face(&family, &style)); + } else { + // A full name: the family is the name without its last words ("Bold"). + let mut base = name.as_str(); + for _ in 0..3 { + let Some((head, _)) = base.trim_end().rsplit_once(' ') else { break }; + base = head; + if fonts.has_family(base) { + for style in fonts.styles(base) { + let face = fonts.face(base, &style); + if has_legacy_name(&face, name) + && let Some(id) = add_face(db, face) + { + legacy.push((i, id)); + } + } + break; + } + } + } + } + } + } + let want_style = match font.style() { + usvg::FontStyle::Normal => Style::Normal, + usvg::FontStyle::Italic => Style::Italic, + usvg::FontStyle::Oblique => Style::Oblique, + }; + for (i, family) in font.families().iter().enumerate() { + match family { + usvg::FontFamily::Named(name) if spelled(db, name).is_none() => { + if let Some(id) = postscript_face(db, name) { + return Some(id); + } + let closest = legacy + .iter() + .filter(|(at, _)| *at == i) + .filter_map(|(_, id)| db.face(*id)) + .min_by_key(|f| (f.style != want_style, f.weight.0.abs_diff(font.weight()))) + .map(|f| f.id); + if closest.is_some() { + return closest; + } + } + // A family (or generic family) that matches: the query below. + _ => break, + } + } + // usvg's default query, but with each named family spelled as the database spells it: + // fontdb compares names exactly, so "DeJaVu SaNs" would quietly fall to the serif. + let names: Vec> = font + .families() + .iter() + .map(|f| match f { + usvg::FontFamily::Named(name) => Some(spelled(db, name).unwrap_or_else(|| name.clone())), + _ => None, + }) + .collect(); + let mut families: Vec = font + .families() + .iter() + .zip(&names) + .map(|(f, name)| match f { + usvg::FontFamily::Serif => Family::Serif, + usvg::FontFamily::SansSerif => Family::SansSerif, + usvg::FontFamily::Cursive => Family::Cursive, + usvg::FontFamily::Fantasy => Family::Fantasy, + usvg::FontFamily::Monospace => Family::Monospace, + usvg::FontFamily::Named(_) => Family::Name(name.as_deref().unwrap_or_default()), + }) + .collect(); + families.push(Family::Serif); + let stretch = match font.stretch() { + usvg::FontStretch::UltraCondensed => Stretch::UltraCondensed, + usvg::FontStretch::ExtraCondensed => Stretch::ExtraCondensed, + usvg::FontStretch::Condensed => Stretch::Condensed, + usvg::FontStretch::SemiCondensed => Stretch::SemiCondensed, + usvg::FontStretch::Normal => Stretch::Normal, + usvg::FontStretch::SemiExpanded => Stretch::SemiExpanded, + usvg::FontStretch::Expanded => Stretch::Expanded, + usvg::FontStretch::ExtraExpanded => Stretch::ExtraExpanded, + usvg::FontStretch::UltraExpanded => Stretch::UltraExpanded, + }; + db.query(&Query { families: &families, weight: Weight(font.weight()), stretch, style: want_style }) + }) +} + +/// Is `name` (ignoring case) a legacy family name (name ID 1) or full name (ID 4) of `face`? +fn has_legacy_name(face: &designcraft_fonts::FontFace, name: &str) -> bool { + use skrifa::MetadataProvider; + use skrifa::string::StringId; + let Some(f) = face.skrifa() else { return false }; + [StringId::FAMILY_NAME, StringId::FULL_NAME] + .into_iter() + .flat_map(|id| f.localized_strings(id)) + .take(256) + .any(|s| s.chars().map(|c| c.to_ascii_lowercase()).eq(name.chars().map(|c| c.to_ascii_lowercase()))) +} + +/// The face in the SVG's font database with PostScript name `name` (ignoring case). +fn postscript_face(db: &usvg::fontdb::Database, name: &str) -> Option { + db.faces().find(|f| f.post_script_name.eq_ignore_ascii_case(name)).map(|f| f.id) +} + +/// Characters the chosen fonts lack (Korean, Arabic…) fall back to a shared face that has them. +fn select_fallback() -> usvg::FallbackSelectionFn<'static> { + let loaded = usvg::FontResolver::default_fallback_selector(); + Box::new(move |c, exclude, db| { + loaded(c, exclude, db).or_else(|| { + let id = add_face(db, designcraft_fonts::FontDb::global().fallback_for(c, 0, None)?)?; + // usvg asks again until we run out: a face it already tried ends the search. + (!exclude.contains(&id)).then_some(id) + }) + }) +} + +/// A shared face's bytes, handed to usvg without a copy. +struct FaceBytes(Arc); + +impl AsRef<[u8]> for FaceBytes { + fn as_ref(&self) -> &[u8] { + self.0.data() + } +} + +/// Add `face` to the SVG's font database (once) and return its id there. +fn add_face(db: &mut Arc, face: Arc) -> Option { + let same = |f: &usvg::fontdb::FaceInfo| { + f.index == face.index() + && matches!(&f.source, usvg::fontdb::Source::Binary(b) if std::ptr::eq(b.as_ref().as_ref().as_ptr(), face.data().as_ptr())) + }; + if let Some(f) = db.faces().find(|f| same(f)) { + return Some(f.id); + } + let index = face.index(); + let ids = Arc::make_mut(db).load_font_source(usvg::fontdb::Source::Binary(Arc::new(FaceBytes(face)))); + // A collection loads all its faces (skipping any it can't read): pick ours by its index. + ids.into_iter().find(|id| db.face(*id).is_some_and(|f| f.index == index)) +} From 2769d0b88f69b4b37bc1b1daa93e0f6fda827f07 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:13:55 -0500 Subject: [PATCH 035/153] Move DTD and raster encoding to craft-libs --- Cargo.lock | 69 ++-- Cargo.toml | 9 +- crates/engine/Cargo.toml | 2 +- crates/engine/src/dtd.rs | 747 +-------------------------------------- crates/images/Cargo.toml | 1 + crates/mcp/Cargo.toml | 2 +- crates/pdf/Cargo.toml | 1 + crates/pptx/Cargo.toml | 3 +- crates/pptx/src/slide.rs | 6 +- crates/render/Cargo.toml | 4 +- 10 files changed, 58 insertions(+), 786 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb8d919c4..18d8ca455 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1245,7 +1245,7 @@ dependencies = [ [[package]] name = "craft-image-codecs" version = "0.1.0" -source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=f7d76eec548a77a79fd2e056e7846a8655ee3f43#f7d76eec548a77a79fd2e056e7846a8655ee3f43" dependencies = [ "craft-jpeg", "exr", @@ -1275,7 +1275,7 @@ dependencies = [ [[package]] name = "craft-jpeg" version = "0.1.0" -source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=f7d76eec548a77a79fd2e056e7846a8655ee3f43#f7d76eec548a77a79fd2e056e7846a8655ee3f43" dependencies = [ "thiserror 2.0.21", ] @@ -1411,7 +1411,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=f7d76eec548a77a79fd2e056e7846a8655ee3f43#f7d76eec548a77a79fd2e056e7846a8655ee3f43" dependencies = [ "encoding_rs", "flate2", @@ -1433,21 +1433,6 @@ name = "craft-sheet" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf" -[[package]] -name = "craft-svg" -version = "0.1.0" -source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" -dependencies = [ - "craft-image-codecs", - "craft-xml 0.1.0 (git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f)", - "flate2", - "kurbo", - "resvg 0.48.1", - "roxmltree 0.21.2", - "thiserror 2.0.21", - "usvg 0.48.1", -] - [[package]] name = "craft-svg" version = "0.1.0" @@ -1473,6 +1458,21 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=f7d76eec548a77a79fd2e056e7846a8655ee3f43#f7d76eec548a77a79fd2e056e7846a8655ee3f43" +dependencies = [ + "craft-image-codecs", + "craft-xml 0.1.0 (git+https://github.com/dylanmtaylor/craft-libs?rev=f7d76eec548a77a79fd2e056e7846a8655ee3f43)", + "flate2", + "kurbo", + "resvg 0.48.1", + "roxmltree 0.21.2", + "thiserror 2.0.21", + "usvg 0.48.1", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1566,15 +1566,6 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "craft-xml" -version = "0.1.0" -source = "git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f#0d1854f1c8a5df0301604098c898bee16349151f" -dependencies = [ - "quick-xml 0.41.0", - "thiserror 2.0.21", -] - [[package]] name = "craft-xml" version = "0.1.0" @@ -1602,6 +1593,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=f23b0f33f0d00b157476533a43cbbcfd901aa1f1#f23b0f33f0d00b157476533a43cbbcfd901aa1f1" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -1611,6 +1612,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/dylanmtaylor/craft-libs?rev=f7d76eec548a77a79fd2e056e7846a8655ee3f43#f7d76eec548a77a79fd2e056e7846a8655ee3f43" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -1885,6 +1895,7 @@ dependencies = [ "craft-prepress", "craft-print", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-xml 0.1.0 (git+https://github.com/dylanmtaylor/craft-libs?rev=f23b0f33f0d00b157476533a43cbbcfd901aa1f1)", "designcraft-color", "designcraft-compose", "designcraft-doc", @@ -1904,7 +1915,6 @@ dependencies = [ "hayro-syntax", "kurbo", "log", - "quick-xml 0.41.0", "same-file", "serde", "serde_json", @@ -1989,7 +1999,7 @@ dependencies = [ "craft-file-formats", "craft-image-codecs", "craft-psd", - "craft-svg 0.1.0 (git+https://github.com/dylanmtaylor/craft-libs?rev=0d1854f1c8a5df0301604098c898bee16349151f)", + "craft-svg 0.1.0 (git+https://github.com/dylanmtaylor/craft-libs?rev=f7d76eec548a77a79fd2e056e7846a8655ee3f43)", "designcraft-color", "designcraft-fonts", "flate2", @@ -2044,6 +2054,7 @@ name = "designcraft-pptx" version = "0.20.1" dependencies = [ "chrono", + "craft-image-codecs", "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", "craft-pptx", "designcraft-color", diff --git a/Cargo.toml b/Cargo.toml index e073298f2..a35967aca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,15 +18,16 @@ craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435 craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-indd = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } -craft-svg = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "0d1854f1c8a5df0301604098c898bee16349151f", version = "=0.1.0", features = ["tree-render", "tree-text"] } +craft-svg = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "f7d76eec548a77a79fd2e056e7846a8655ee3f43", version = "=0.1.0", features = ["tree-render", "tree-text"] } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-xml-dtd = { package = "craft-xml", git = "https://github.com/dylanmtaylor/craft-libs", rev = "f23b0f33f0d00b157476533a43cbbcfd901aa1f1", version = "=0.1.0" } craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-csv = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "44d7ac8d942dbd87048f82b3efc3e0d14483cc8d", version = "=0.1.0" } -craft-image-codecs = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "0d1854f1c8a5df0301604098c898bee16349151f", version = "=0.1.0" } -craft-psd = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "0d1854f1c8a5df0301604098c898bee16349151f", version = "=0.1.0" } +craft-image-codecs = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "f7d76eec548a77a79fd2e056e7846a8655ee3f43", version = "=0.1.0" } +craft-psd = { git = "https://github.com/dylanmtaylor/craft-libs", rev = "f7d76eec548a77a79fd2e056e7846a8655ee3f43", version = "=0.1.0" } craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } craft-pptx = { git = "https://github.com/storytold/craft-libs", rev = "f6cab1980", version = "=0.1.0" } @@ -81,7 +82,7 @@ skrifa = "0.47" harfrust = "0.13" vello_cpu = "0.2" vello_common = { version = "0.2", default-features = false } -image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] } +image = { version = "0.25", default-features = false } # CMYK TIFF samples for PDF export (`image` decodes them to RGB); the version `image` uses. tiff = "0.11" # Boolean operations on Bézier paths (Pathfinder). diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 57d196a62..6aa2e62df 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -33,8 +33,8 @@ serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } log = { workspace = true } -quick-xml = { workspace = true } craft-xml = { workspace = true } +craft-xml-dtd = { workspace = true } caseless = { workspace = true } unicode-segmentation = { workspace = true } diff --git a/crates/engine/src/dtd.rs b/crates/engine/src/dtd.rs index 6b48ba3ab..6eda1fff5 100644 --- a/crates/engine/src/dtd.rs +++ b/crates/engine/src/dtd.rs @@ -1,746 +1,3 @@ -//! Document Type Definitions: element declarations (content models) and required attributes, and -//! validation of an XML document against them (Structure ▸ Validate). +//! XML DTD command adapter backed by craft-xml. -use std::collections::HashMap; - -/// How much text parameter entities may add during expansion (a pass count is not an output -/// bound: each pass replaces whole strings, so references can inflate multiplicatively). -const MAX_EXPANDED_BYTES: usize = 1024 * 1024; - -/// A content particle: a name, a sequence, a choice, or one of those repeated (`?`, `*`, `+`). -#[derive(Clone, Debug, PartialEq)] -enum Cp { - Name(String), - Seq(Vec), - Choice(Vec), - Rep(Box, char), -} - -#[derive(Clone, Debug, PartialEq)] -enum Model { - Empty, - Any, - /// `(#PCDATA | a | b)*`: text and these elements in any order. - Mixed(Vec), - Children(Cp), -} - -/// A parsed DTD. -#[derive(Clone, Debug, Default)] -pub struct Dtd { - elements: Vec<(String, Model)>, - /// Element → its `#REQUIRED` attributes. - required: HashMap>, -} - -/// A problem found by validation: where (an element path such as `Root/story[2]`) and what. -#[derive(Clone, Debug, PartialEq, serde::Serialize)] -pub struct Problem { - pub path: String, - pub message: String, -} - -impl Dtd { - /// Declared element names, in order. - pub fn element_names(&self) -> impl Iterator { - self.elements.iter().map(|(n, _)| n.as_str()) - } - - fn model(&self, name: &str) -> Option<&Model> { - self.elements.iter().find(|(n, _)| n == name).map(|(_, m)| m) - } -} - -/// Markup declarations (``) with comments removed and quoted strings kept whole. -fn declarations(src: &str) -> Vec { - let mut out = Vec::new(); - let mut rest = src; - while let Some(i) = rest.find("").map_or("", |j| &rest[j + 3..]); - continue; - } - let mut quote = None; - let mut end = rest.len(); - for (k, c) in rest.char_indices().skip(2) { - match (quote, c) { - (Some(q), c) if c == q => quote = None, - (None, '"' | '\'') => quote = Some(c), - (None, '>') => { - end = k; - break; - } - _ => {} - } - } - out.push(rest[2..end].to_string()); - rest = rest.get(end + 1..).unwrap_or(""); - } - out -} - -fn tokens(s: &str) -> Vec { - let mut out = Vec::new(); - let mut cur = String::new(); - for c in s.chars() { - if c.is_whitespace() || "(),|?*+".contains(c) { - if !cur.is_empty() { - out.push(std::mem::take(&mut cur)); - } - if !c.is_whitespace() { - out.push(c.to_string()); - } - } else { - cur.push(c); - } - } - if !cur.is_empty() { - out.push(cur); - } - out -} - -/// How deeply groups may nest in a content model; parsing, matching (`ends`) and printing (`show`) -/// recurse once per level. -const MAX_CONTENT_MODEL_DEPTH: usize = 64; - -/// Parse a content particle from `t[*i..]`, inside `depth` enclosing groups. -fn particle(t: &[String], i: &mut usize, depth: usize) -> Result { - let base = match t.get(*i).map(String::as_str) { - Some("(") => { - if depth >= MAX_CONTENT_MODEL_DEPTH { - return Err(format!("content model nested more than {MAX_CONTENT_MODEL_DEPTH} levels")); - } - *i += 1; - let mut items = vec![particle(t, i, depth + 1)?]; - let mut sep = None; - loop { - match t.get(*i).map(String::as_str) { - Some(")") => { - *i += 1; - break; - } - Some(s @ ("," | "|")) => { - if sep.is_some_and(|p| p != s) { - return Err("mixed `,` and `|` in one group".into()); - } - sep = Some(if s == "," { "," } else { "|" }); - *i += 1; - items.push(particle(t, i, depth + 1)?); - } - other => return Err(format!("unexpected `{}` in a content model", other.unwrap_or("end"))), - } - } - if sep == Some("|") { Cp::Choice(items) } else { Cp::Seq(items) } - } - Some(n) if !"),|?*+".contains(n) => { - *i += 1; - Cp::Name(n.to_string()) - } - other => return Err(format!("unexpected `{}` in a content model", other.unwrap_or("end"))), - }; - Ok(match t.get(*i).map(String::as_str) { - Some(r @ ("?" | "*" | "+")) => { - *i += 1; - Cp::Rep(Box::new(base), r.chars().next().unwrap_or('*')) - } - _ => base, - }) -} - -fn parse_model(s: &str) -> Result { - let s = s.trim(); - match s { - "EMPTY" => return Ok(Model::Empty), - "ANY" => return Ok(Model::Any), - _ => {} - } - if s.contains("#PCDATA") { - let t = tokens(&s.replace("#PCDATA", " #PCDATA ")); - return Ok(Model::Mixed(t.into_iter().filter(|x| !"()|*".contains(x.as_str()) && x != "#PCDATA").collect())); - } - let t = tokens(s); - let mut i = 0; - let cp = particle(&t, &mut i, 0)?; - if i != t.len() { - return Err(format!("unexpected `{}` after the content model", t[i])); - } - Ok(Model::Children(cp)) -} - -/// How deeply parameter entities may nest: a reference in the DTD is level 1, a reference in its -/// value level 2, and so on. -const MAX_PARAMETER_ENTITY_DEPTH: usize = 64; - -/// Expand `%name;` references, one level per round, until a round changes nothing. -fn expand_parameter_entities(src: &str, ents: &[(String, String)]) -> Result { - let mut entities = HashMap::new(); - for (name, value) in ents { - entities.entry(name.as_str()).or_insert(value.as_str()); - } - let limit = src.len().saturating_add(MAX_EXPANDED_BYTES); - let append = |out: &mut String, value: &str| -> Result<(), String> { - if value.len() > limit.saturating_sub(out.len()) { - return Err(format!("parameter entities expand the DTD beyond {limit} bytes")); - } - out.push_str(value); - Ok(()) - }; - let too_deep = || format!("parameter entities refer to themselves or nest deeper than {MAX_PARAMETER_ENTITY_DEPTH} levels"); - let mut text = src.to_string(); - // The round after the deepest level changes nothing, hence one more round than levels. - for _ in 0..=MAX_PARAMETER_ENTITY_DEPTH { - let mut out = String::with_capacity(text.len()); - let mut rest = text.as_str(); - let mut changed = false; - while let Some(i) = rest.find('%') { - let (prefix, reference) = rest.split_at(i); - append(&mut out, prefix)?; - // `find` points to an ASCII `%`, so both split positions are UTF-8 boundaries. - rest = reference.split_at(1).1; - // A name has no whitespace and no `%` (`parse` ends a name at whitespace), whatever its - // length: the search for the closing `;` stops at the first of those, so the searches of - // a round never overlap and a round stays linear in the text. - let end = - rest.bytes().position(|b| matches!(b, b';' | b'%') || b.is_ascii_whitespace()).filter(|&k| rest.as_bytes().get(k) == Some(&b';')); - if let Some(end) = end - && let Some(value) = rest.get(..end).and_then(|name| entities.get(name)) - { - // A value that is its own reference (``) never gets anywhere. - if reference.get(..end + 2) == Some(*value) { - return Err(too_deep()); - } - append(&mut out, value)?; - changed = true; - rest = rest.split_at(end + 1).1; - continue; - } - append(&mut out, "%")?; - } - append(&mut out, rest)?; - text = out; - if !changed { - return Ok(text); - } - } - Err(too_deep()) -} - -/// Parse a DTD (its element and attribute-list declarations; parameter entities are expanded). -pub fn parse(src: &str) -> Result { - // Parameter entities first, then expand references and read the declarations again. - let mut ents: Vec<(String, String)> = Vec::new(); - for d in declarations(src) { - if let Some(rest) = d.strip_prefix("ENTITY") { - let rest = rest.trim_start(); - if let Some(rest) = rest.strip_prefix('%') { - let rest = rest.trim_start(); - let name: String = rest.chars().take_while(|c| !c.is_whitespace()).collect(); - let val = rest[name.len()..].trim(); - let q = val.chars().next().unwrap_or('"'); - if let Some(v) = val.strip_prefix(q).and_then(|v| v.find(q).map(|e| &v[..e])) { - ents.push((name, v.to_string())); - } - } - } - } - let text = expand_parameter_entities(src, &ents)?; - let mut dtd = Dtd::default(); - for d in declarations(&text) { - if let Some(rest) = d.strip_prefix("ELEMENT") { - let rest = rest.trim_start(); - let name: String = rest.chars().take_while(|c| !c.is_whitespace()).collect(); - let model = parse_model(&rest[name.len()..]).map_err(|e| format!(": {e}"))?; - dtd.elements.push((name, model)); - } else if let Some(rest) = d.strip_prefix("ATTLIST") { - // name (attr type default)*; only `#REQUIRED` matters here. - let mut words = Vec::new(); - let mut cur = String::new(); - let mut quote = None; - let mut group = 0; - for c in rest.chars() { - match (quote, c) { - (Some(q), c) if c == q => { - quote = None; - cur.push(c); - } - (Some(_), c) => cur.push(c), - (None, '"' | '\'') => { - quote = Some(c); - cur.push(c); - } - (None, '(') => { - group += 1; - cur.push(c); - } - (None, ')') => { - group -= 1; - cur.push(c); - } - (None, c) if c.is_whitespace() && group == 0 => { - if !cur.is_empty() { - words.push(std::mem::take(&mut cur)); - } - } - (None, c) => cur.push(c), - } - } - if !cur.is_empty() { - words.push(cur); - } - let Some((el, attrs)) = words.split_first() else { continue }; - let mut k = 0; - while k + 2 < attrs.len() { - let name = &attrs[k]; - let default = attrs.get(k + 2).map(String::as_str).unwrap_or(""); - if default == "#REQUIRED" { - dtd.required.entry(el.clone()).or_default().push(name.clone()); - } - k += if default == "#FIXED" { 4 } else { 3 }; - } - } - } - if dtd.elements.is_empty() { - return Err("no element declarations".into()); - } - Ok(dtd) -} - -/// What `ends` has worked out for one sequence of children: the end positions of a group (by the -/// address of its node, which lives as long as the model) from a position. -type EndsMemo = HashMap<(usize, usize), Vec>; - -/// End positions after `cp` matches a prefix of `seq[i..]`. -/// -/// The positions of each group are worked out once per start position (`memo`): nested repetitions -/// such as `((a*)*)*` ask the inner group for the same positions again and again, which is -/// exponential in their depth without it. -fn ends(cp: &Cp, seq: &[&str], i: usize, memo: &mut EndsMemo) -> Vec { - if let Cp::Name(n) = cp { - return if seq.get(i) == Some(&n.as_str()) { vec![i + 1] } else { vec![] }; - } - let key = (std::ptr::from_ref(cp) as usize, i); - if let Some(done) = memo.get(&key) { - return done.clone(); - } - let mut out: Vec = match cp { - Cp::Name(_) => vec![], - // Keep the positions after each item a set: as a list of every path, `(a?, a?, …)` doubles per item. - Cp::Seq(items) => { - let mut at = vec![i]; - for c in items { - let mut next = Vec::new(); - for p in at { - next.extend(ends(c, seq, p, memo)); - } - next.sort_unstable(); - next.dedup(); - at = next; - } - at - } - Cp::Choice(items) => { - let mut all = Vec::new(); - for c in items { - all.extend(ends(c, seq, i, memo)); - } - all - } - Cp::Rep(c, r) => { - let mut all = if *r == '+' { vec![] } else { vec![i] }; - let mut frontier = ends(c, seq, i, memo); - if *r == '?' { - all.extend(frontier); - } else { - while !frontier.is_empty() { - let new: Vec = frontier.iter().copied().filter(|p| !all.contains(p)).collect(); - all.extend(&new); - let mut next = Vec::new(); - for p in new.into_iter().filter(|&p| p > i) { - next.extend(ends(c, seq, p, memo).into_iter().filter(|p| !all.contains(p))); - } - // Two paths to one position are one position: kept apart they multiply per round. - next.sort_unstable(); - next.dedup(); - frontier = next; - } - } - all - } - }; - out.sort_unstable(); - out.dedup(); - memo.insert(key, out.clone()); - out -} - -fn show(cp: &Cp) -> String { - match cp { - Cp::Name(n) => n.clone(), - Cp::Seq(v) => format!("({})", v.iter().map(show).collect::>().join(", ")), - Cp::Choice(v) => format!("({})", v.iter().map(show).collect::>().join(" | ")), - Cp::Rep(c, r) => format!("{}{r}", show(c)), - } -} - -/// An element of the document being validated. -struct Node { - name: String, - attrs: Vec, - children: Vec, - text: bool, -} - -fn read_tree(xml: &str) -> Result { - use quick_xml::events::Event; - let mut r = quick_xml::Reader::from_str(xml); - let mut stack: Vec = Vec::new(); - let node = |e: &quick_xml::events::BytesStart| Node { - name: String::from_utf8_lossy(e.name().as_ref()).to_string(), - attrs: e.attributes().flatten().map(|a| String::from_utf8_lossy(a.key.as_ref()).to_string()).collect(), - children: vec![], - text: false, - }; - loop { - match r.read_event().map_err(|e| e.to_string())? { - Event::Start(e) => stack.push(node(&e)), - Event::Empty(e) => { - let n = node(&e); - match stack.last_mut() { - Some(p) => p.children.push(n), - None => return Ok(n), - } - } - Event::End(_) => { - let n = stack.pop().ok_or("unbalanced end tag")?; - match stack.last_mut() { - Some(p) => p.children.push(n), - None => return Ok(n), - } - } - Event::Text(t) => { - if let Some(p) = stack.last_mut() - && !t.iter().all(u8::is_ascii_whitespace) - { - p.text = true; - } - } - Event::CData(_) | Event::GeneralRef(_) => { - if let Some(p) = stack.last_mut() { - p.text = true; - } - } - Event::Eof => return Err("no root element".into()), - _ => {} - } - } -} - -fn check(dtd: &Dtd, n: &Node, path: &str, out: &mut Vec) { - let mut problem = |m: String| out.push(Problem { path: path.to_string(), message: m }); - match dtd.model(&n.name) { - None => problem(format!("`{}` isn't declared in the DTD", n.name)), - Some(Model::Any) => {} - Some(Model::Empty) => { - if n.text || !n.children.is_empty() { - problem(format!("`{}` should be empty", n.name)); - } - } - Some(Model::Mixed(allowed)) => { - for c in &n.children { - if !allowed.contains(&c.name) { - problem(format!("`{}` isn't allowed in `{}`", c.name, n.name)); - } - } - } - Some(Model::Children(cp)) => { - if n.text { - problem(format!("`{}` can't contain text", n.name)); - } - let seq: Vec<&str> = n.children.iter().map(|c| c.name.as_str()).collect(); - if !ends(cp, &seq, 0, &mut EndsMemo::new()).contains(&seq.len()) { - let got = if seq.is_empty() { "nothing".to_string() } else { seq.join(", ") }; - problem(format!("`{}` contains {got}; the DTD asks for {}", n.name, show(cp))); - } - } - } - for a in dtd.required.get(&n.name).into_iter().flatten() { - if !n.attrs.contains(a) { - problem(format!("`{}` is missing the required attribute `{a}`", n.name)); - } - } - let mut seen: HashMap<&str, usize> = HashMap::new(); - for c in &n.children { - let k = seen.entry(c.name.as_str()).or_default(); - *k += 1; - check(dtd, c, &format!("{path}/{}[{k}]", c.name), out); - } -} - -/// Validate `xml` against `dtd`; an empty list when it's valid. -pub fn validate(dtd: &Dtd, xml: &str) -> Vec { - match read_tree(xml) { - Ok(root) => { - let mut out = Vec::new(); - let path = root.name.clone(); - check(dtd, &root, &path, &mut out); - out - } - Err(e) => vec![Problem { path: String::new(), message: format!("not well-formed XML: {e}") }], - } -} - -#[cfg(test)] -mod tests { - use super::*; - - const DTD: &str = r##" - - - - - - - - - - b" kind (photo|chart) "photo">"##; - - #[test] - fn content_models_and_required_attributes() { - let dtd = parse(DTD).unwrap(); - assert_eq!(dtd.element_names().count(), 8); - let ok = r#"T <em>x</em>ab
    "#; - assert_eq!(validate(&dtd, ok), vec![]); - let bad = r#"no title
    x