diff --git a/Cargo.lock b/Cargo.lock index 1af51632f3..fe2585f596 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1307,6 +1307,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" @@ -4740,6 +4750,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==")); +}