Use shared MIME Base64 codec for document exports

This commit is contained in:
Dylan M. Taylor
2026-11-15 09:38:36 -05:00
parent e098c5c85e
commit d80d64fb97
5 changed files with 29 additions and 12 deletions
Generated
+11
View File
@@ -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",
+2
View File
@@ -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" }
+1
View File
@@ -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
+1 -12
View File
@@ -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 `<table>` elements, images inline, a
/// rule between pages.
+14
View File
@@ -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=="));
}