From d80d64fb974b9ba1582e8753987d2c23700a6434 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 09:38:36 -0500 Subject: [PATCH 1/3] Use shared MIME Base64 codec for document exports --- Cargo.lock | 11 +++++++++++ Cargo.toml | 2 ++ crates/export/Cargo.toml | 1 + crates/export/src/as_html.rs | 13 +------------ crates/export/src/tests.rs | 14 ++++++++++++++ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a259e0fe5e..af60a84c79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1298,6 +1298,16 @@ dependencies = [ "objc2-app-kit 0.3.2", ] +[[package]] +name = "craft-mime" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f29e82f1a7a009e68312bd2e84adab72be23344d#f29e82f1a7a009e68312bd2e84adab72be23344d" +dependencies = [ + "chrono", + "craft-codepage", + "encoding_rs", +] + [[package]] name = "craft-opc" version = "0.1.0" @@ -4699,6 +4709,7 @@ dependencies = [ name = "pdfcraft-export" version = "0.21.1" dependencies = [ + "craft-mime", "craft-opc", "craft-rtf", "craft-xml", diff --git a/Cargo.toml b/Cargo.toml index 8bd2a5fb8c..0d9ed6c189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,8 @@ craft-pdf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0" # The writer the RTF export maps onto (storytold/craft-libs#793), at storytold/craft-libs#1467: # its font and colour lookups take constant time, as PdfCraft's own writer did. craft-rtf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "f29e82f1a7a009e68312bd2e84adab72be23344d" } +# Shared Base64 encoder; use the existing RTF revision to reuse its codepage dependency. +craft-mime = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "f29e82f1a7a009e68312bd2e84adab72be23344d" } craft-csv = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-xml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index f4164d46ff..4ff2f0dfb9 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -11,6 +11,7 @@ repository.workspace = true workspace = true [dependencies] +craft-mime.workspace = true craft-opc.workspace = true craft-rtf.workspace = true craft-xml.workspace = true diff --git a/crates/export/src/as_html.rs b/crates/export/src/as_html.rs index e2b3379ac2..7d70359edd 100644 --- a/crates/export/src/as_html.rs +++ b/crates/export/src/as_html.rs @@ -57,18 +57,7 @@ pub(crate) fn esc(s: &str) -> String { o } -pub(crate) fn base64(data: &[u8]) -> String { - const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - let mut s = String::with_capacity(data.len().div_ceil(3) * 4); - for c in data.chunks(3) { - let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32; - s.push(A[(n >> 18) as usize & 63] as char); - s.push(A[(n >> 12) as usize & 63] as char); - s.push(if c.len() > 1 { A[(n >> 6) as usize & 63] as char } else { '=' }); - s.push(if c.len() > 2 { A[n as usize & 63] as char } else { '=' }); - } - s -} +pub(crate) use craft_mime::transfer::base64; /// One HTML file: headings and paragraphs, tables as real `` elements, images inline, a /// rule between pages. diff --git a/crates/export/src/tests.rs b/crates/export/src/tests.rs index 4ee522a70f..68d3a569ee 100644 --- a/crates/export/src/tests.rs +++ b/crates/export/src/tests.rs @@ -1428,3 +1428,17 @@ fn a_long_bulleted_list_keeps_every_bullet_with_its_item() { assert_eq!(paragraphs.len(), 150, "{paragraphs:?}"); assert!(paragraphs.iter().enumerate().all(|(n, p)| *p == format!("\u{F0B7} item {n};")), "{paragraphs:?}"); } + +#[test] +fn shared_base64_keeps_padded_unwrapped_export_bytes() { + for (input, expected) in + [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] + { + assert_eq!(base64(input.as_bytes()), expected); + } + assert_eq!(base64(&[251, 255, 255]), "+///"); + let encoded = base64(&[255; 256]); + assert_eq!(encoded.len(), 344); + assert!(!encoded.contains(['\r', '\n'])); + assert!(encoded.ends_with("/w==")); +} From 5410dc1cfcc1e99db6c11383810d2d86a3a070e9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:11:27 -0500 Subject: [PATCH 2/3] xfa: avoid rescanning prototype-only child lists --- crates/xfa/src/proto.rs | 24 +++++++++++++++++++++++- crates/xfa/src/proto/tests.rs | 13 +++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/crates/xfa/src/proto.rs b/crates/xfa/src/proto.rs index ae1505804e..a34294ae71 100644 --- a/crates/xfa/src/proto.rs +++ b/crates/xfa/src/proto.rs @@ -75,6 +75,7 @@ pub(crate) fn expand(template: Node) -> (Option, Vec) { search: MAX_SEARCH, work: MAX_WORK, active: HashMap::new(), + non_rendering_children: HashSet::new(), missing: false, external: false, looped: false, @@ -126,6 +127,9 @@ struct Writer<'a> { work: usize, /// The elements being written around the current one: inheriting from one is a loop. active: HashMap, + /// Nodes whose direct children are all omitted prototype definitions (or absent). + /// The DOM is immutable; remembering these avoids repeated no-output child walks. + non_rendering_children: HashSet, missing: bool, external: bool, looped: bool, @@ -465,7 +469,25 @@ impl<'a> Writer<'a> { self.out.push('>'); // Rich text is taken whole from the nearest layer that has any. let cut = if tag == "exData" { layers.iter().position(|l| has_content(l, true)).map_or(layers.len(), |i| i + 1) } else { layers.len() }; - let merged = layers.get(..cut).unwrap_or(&layers); + // Do this only after charging every original child and selecting the rich-text + // layer boundary. Omitting a proto-only layer from child traversal must never + // reduce its work charge or change which exData layer wins. + let merged: Vec<_> = layers + .get(..cut) + .unwrap_or(&layers) + .iter() + .copied() + .filter(|layer| { + if self.non_rendering_children.contains(&layer.id()) { + return false; + } + if layer.children().all(|child| child.is_element() && child.tag_name().name() == "proto") { + self.non_rendering_children.insert(layer.id()); + return false; + } + true + }) + .collect(); // Data content: the nearest layer's that has any (empty content overrides nothing). let text_layer = merged.iter().position(|l| has_content(l, false)).unwrap_or(0); let chosen = if matches!(tag, "ui" | "value") { diff --git a/crates/xfa/src/proto/tests.rs b/crates/xfa/src/proto/tests.rs index ba7f6f8316..35d3f64562 100644 --- a/crates/xfa/src/proto/tests.rs +++ b/crates/xfa/src/proto/tests.rs @@ -399,3 +399,16 @@ fn a_som_expression_past_its_length_limit_is_not_followed() { assert_eq!(field(&t, "long").common.h, None); assert!(close(field(&t, "short").common.h, 2.0 * MM)); } + +#[test] +fn proto_only_child_cache_keeps_attributes_text_and_rich_text_boundary() { + let xml = form(r#""#, r##""##); + let (t, warnings) = parse(&xml).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(field(&t, "first").common.w, field(&t, "second").common.w); + assert!(field(&t, "first").common.w.is_some()); + let text = expanded(r##""##); + assert!(text.matches("kept").count() >= 2, "{text}"); + let rich = expanded(r##""##); + assert_eq!(rich.matches("hidden").count(), 1, "nearest nonempty exData still wins: {rich}"); +} From a0bc1b347700d28678537a02b68e4237e58b6e9e Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:53:25 +0100 Subject: [PATCH 3/3] Revert "xfa: avoid rescanning prototype-only child lists" This reverts commit 5410dc1c. The XFA prototype-walk optimization is unrelated to the #778 Base64 cutover; keep it for its own PR with a measured benchmark (craft-libs#778 consumer PRs stay single-purpose). --- crates/xfa/src/proto.rs | 24 +----------------------- crates/xfa/src/proto/tests.rs | 13 ------------- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/crates/xfa/src/proto.rs b/crates/xfa/src/proto.rs index a34294ae71..ae1505804e 100644 --- a/crates/xfa/src/proto.rs +++ b/crates/xfa/src/proto.rs @@ -75,7 +75,6 @@ pub(crate) fn expand(template: Node) -> (Option, Vec) { search: MAX_SEARCH, work: MAX_WORK, active: HashMap::new(), - non_rendering_children: HashSet::new(), missing: false, external: false, looped: false, @@ -127,9 +126,6 @@ struct Writer<'a> { work: usize, /// The elements being written around the current one: inheriting from one is a loop. active: HashMap, - /// Nodes whose direct children are all omitted prototype definitions (or absent). - /// The DOM is immutable; remembering these avoids repeated no-output child walks. - non_rendering_children: HashSet, missing: bool, external: bool, looped: bool, @@ -469,25 +465,7 @@ impl<'a> Writer<'a> { self.out.push('>'); // Rich text is taken whole from the nearest layer that has any. let cut = if tag == "exData" { layers.iter().position(|l| has_content(l, true)).map_or(layers.len(), |i| i + 1) } else { layers.len() }; - // Do this only after charging every original child and selecting the rich-text - // layer boundary. Omitting a proto-only layer from child traversal must never - // reduce its work charge or change which exData layer wins. - let merged: Vec<_> = layers - .get(..cut) - .unwrap_or(&layers) - .iter() - .copied() - .filter(|layer| { - if self.non_rendering_children.contains(&layer.id()) { - return false; - } - if layer.children().all(|child| child.is_element() && child.tag_name().name() == "proto") { - self.non_rendering_children.insert(layer.id()); - return false; - } - true - }) - .collect(); + let merged = layers.get(..cut).unwrap_or(&layers); // Data content: the nearest layer's that has any (empty content overrides nothing). let text_layer = merged.iter().position(|l| has_content(l, false)).unwrap_or(0); let chosen = if matches!(tag, "ui" | "value") { diff --git a/crates/xfa/src/proto/tests.rs b/crates/xfa/src/proto/tests.rs index 35d3f64562..ba7f6f8316 100644 --- a/crates/xfa/src/proto/tests.rs +++ b/crates/xfa/src/proto/tests.rs @@ -399,16 +399,3 @@ fn a_som_expression_past_its_length_limit_is_not_followed() { assert_eq!(field(&t, "long").common.h, None); assert!(close(field(&t, "short").common.h, 2.0 * MM)); } - -#[test] -fn proto_only_child_cache_keeps_attributes_text_and_rich_text_boundary() { - let xml = form(r#""#, r##""##); - let (t, warnings) = parse(&xml).unwrap(); - assert!(warnings.is_empty(), "{warnings:?}"); - assert_eq!(field(&t, "first").common.w, field(&t, "second").common.w); - assert!(field(&t, "first").common.w.is_some()); - let text = expanded(r##""##); - assert!(text.matches("kept").count() >= 2, "{text}"); - let rich = expanded(r##""##); - assert_eq!(rich.matches("hidden").count(), 1, "nearest nonempty exData still wins: {rich}"); -}