mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-16 07:42:39 +00:00
Merge pull request #7449 from storytold/extract/shared-export-base64
export: replace local Base64 encoder with existing craft-mime
This commit is contained in:
5 files changed
+29
-12
No files matched your search
Generated
+11
@@ -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",
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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=="));
|
||||
}
|
||||
Reference in new issue
Block a user