From 75c4b01db3f9abe15b1757654d87151a8d2bb7d3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:53:04 -0500 Subject: [PATCH 1/3] Use shared RunLength encoding for PDF image streams --- Cargo.lock | 27 +++++++++++++++++++++++++++ Cargo.toml | 2 ++ crates/pdf/Cargo.toml | 1 + crates/pdf/src/lib.rs | 34 +--------------------------------- crates/pdf/src/tests.rs | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 53fead994..76a1d2a94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1007,6 +1007,17 @@ name = "craft-sheet" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430955825553ab7b976f4#bb57d422d0bc842cbde430955825553ab7b976f4" +[[package]] +name = "craft-stream-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430955825553ab7b976f4#bb57d422d0bc842cbde430955825553ab7b976f4" +dependencies = [ + "fax", + "hayro-ccitt", + "hayro-jbig2", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -2284,6 +2295,7 @@ dependencies = [ name = "gridcraft-pdf" version = "0.19.1" dependencies = [ + "craft-stream-codecs", "harfrust", "sha2 0.11.1", "skrifa", @@ -2448,6 +2460,21 @@ dependencies = [ "foldhash 0.2.0", ] +[[package]] +name = "hayro-ccitt" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a" + +[[package]] +name = "hayro-jbig2" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69374b3668dd45aeb3d3145cda68f2c7b4f223aaa2511e67d076f1c7d741388d" +dependencies = [ + "hayro-ccitt", +] + [[package]] name = "heck" version = "0.5.0" diff --git a/Cargo.toml b/Cargo.toml index debe80119..304a54ee8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,8 @@ gridcraft-math = { path = "crates/math" } # craft-libs revision. # craft-update: app-specific pretend versions apply to startup and manual checks. craft-update = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "=0.1.0", features = ["egui"] } +# PDF image-stream RunLength encoding, at the existing shared revision. +craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } # Compound files ([MS-CFB]) and password-encrypted OOXML packages ([MS-OFFCRYPTO]). craft-cfb = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } craft-offcrypto = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index 9a32a8620..a0710392f 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -10,6 +10,7 @@ readme = "README.md" workspace = true [dependencies] +craft-stream-codecs.workspace = true harfrust = "=0.12.0" skrifa = { workspace = true } unicode-bidi = "=0.3.18" diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index db22568c6..fabd00f2e 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -979,39 +979,7 @@ pub struct PdfDoc { /// PackBits-style run-length encoding (PDF `RunLengthDecode`). fn run_length(data: &[u8]) -> Vec { - let mut out = Vec::with_capacity(data.len() / 2 + 16); - let mut i = 0; - while i < data.len() { - let b = data[i]; - let mut run = 1; - while i + run < data.len() && run < 128 && data[i + run] == b { - run += 1; - } - if run >= 2 { - out.push((257 - run) as u8); - out.push(b); - i += run; - continue; - } - // Literal run until the next repeat of 3+ or 128 bytes. - let start = i; - let mut len = 0; - while i < data.len() && len < 128 { - if i + 2 < data.len() && data[i] == data[i + 1] && data[i] == data[i + 2] { - break; - } - i += 1; - len += 1; - } - if len == 0 { - // Next is a repeat; loop will handle it. - continue; - } - out.push((len - 1) as u8); - out.extend_from_slice(&data[start..start + len]); - } - out.push(128); - out + craft_stream_codecs::runlength::encode(data) } /// Longest Info string written, in characters; longer values are cut on a character boundary. diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index b8b2fdb30..85911eeed 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -1239,3 +1239,21 @@ fn symbol_text_is_never_shaped_as_arabic() -> Result<(), Box> { assert_eq!(text_width(Font::Symbol, 10.0, "\u{03B1}ب"), f32::from(Font::Symbol.char_width('\u{03B1}') + Font::Symbol.char_width('ب')) / 100.0); Ok(()) } + +#[test] +fn shared_run_length_preserves_pixels_at_packet_boundaries() { + for len in [0, 1, 2, 3, 127, 128, 129, 255, 256, 257, 1024] { + for seed in [0u8, 127, 128, 255] { + let repeats = vec![seed; len]; + let literals: Vec = (0..len).map(|i| seed.wrapping_add(i as u8)).collect(); + let pairs: Vec = (0..len).map(|i| (i / 2) as u8).collect(); + for pixels in [repeats, literals, pairs] { + let encoded = run_length(&pixels); + assert_eq!(encoded.last(), Some(&128)); + assert_eq!(rle_decode(&encoded), pixels); + } + } + } + // Two equal bytes may now terminate a literal packet. PDF pixel values stay exact. + assert_eq!(run_length(&[1, 2, 2, 3]), [0, 1, 255, 2, 0, 3, 128]); +} From d217e8272d0c22bfe65fa26fd5c2ae964c206691 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:33:42 -0500 Subject: [PATCH 2/3] Fix shared codec dependency for external GridCraft builds --- Cargo.lock | 5 ++--- Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76a1d2a94..dcb7d1ea2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1010,7 +1010,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430 [[package]] name = "craft-stream-codecs" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430955825553ab7b976f4#bb57d422d0bc842cbde430955825553ab7b976f4" +source = "git+https://github.com/storytold/craft-libs?rev=201601fa2d5dab0f1aa0e6f2581751bac088a495#201601fa2d5dab0f1aa0e6f2581751bac088a495" dependencies = [ "fax", "hayro-ccitt", @@ -2469,8 +2469,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a" [[package]] name = "hayro-jbig2" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69374b3668dd45aeb3d3145cda68f2c7b4f223aaa2511e67d076f1c7d741388d" +source = "git+https://github.com/storytold/craft-libs?rev=201601fa2d5dab0f1aa0e6f2581751bac088a495#201601fa2d5dab0f1aa0e6f2581751bac088a495" dependencies = [ "hayro-ccitt", ] diff --git a/Cargo.toml b/Cargo.toml index 304a54ee8..e7e3f7623 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,8 +31,8 @@ gridcraft-math = { path = "crates/math" } # craft-libs revision. # craft-update: app-specific pretend versions apply to startup and manual checks. craft-update = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "=0.1.0", features = ["egui"] } -# PDF image-stream RunLength encoding, at the existing shared revision. -craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } +# PDF RunLength encoding; this revision also fixes the vendored JBIG2 dependency for Git consumers. +craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "201601fa2d5dab0f1aa0e6f2581751bac088a495", version = "0.1.0" } # Compound files ([MS-CFB]) and password-encrypted OOXML packages ([MS-OFFCRYPTO]). craft-cfb = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } craft-offcrypto = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } From 24e40de94fe8fc6241bcaadfb24aeaff0905104f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:02:36 -0500 Subject: [PATCH 3/3] Pin PDF RunLength dependency to merged shared fix --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dcb7d1ea2..7c2e9d37d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1010,7 +1010,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430 [[package]] name = "craft-stream-codecs" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=201601fa2d5dab0f1aa0e6f2581751bac088a495#201601fa2d5dab0f1aa0e6f2581751bac088a495" +source = "git+https://github.com/storytold/craft-libs?rev=f7e0d7fd5577e102b4634bcbe062e649eb68165f#f7e0d7fd5577e102b4634bcbe062e649eb68165f" dependencies = [ "fax", "hayro-ccitt", @@ -2469,7 +2469,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a" [[package]] name = "hayro-jbig2" version = "0.3.0" -source = "git+https://github.com/storytold/craft-libs?rev=201601fa2d5dab0f1aa0e6f2581751bac088a495#201601fa2d5dab0f1aa0e6f2581751bac088a495" +source = "git+https://github.com/storytold/craft-libs?rev=f7e0d7fd5577e102b4634bcbe062e649eb68165f#f7e0d7fd5577e102b4634bcbe062e649eb68165f" dependencies = [ "hayro-ccitt", ] diff --git a/Cargo.toml b/Cargo.toml index e7e3f7623..341e3a460 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ gridcraft-math = { path = "crates/math" } # craft-update: app-specific pretend versions apply to startup and manual checks. craft-update = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "=0.1.0", features = ["egui"] } # PDF RunLength encoding; this revision also fixes the vendored JBIG2 dependency for Git consumers. -craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "201601fa2d5dab0f1aa0e6f2581751bac088a495", version = "0.1.0" } +craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "f7e0d7fd5577e102b4634bcbe062e649eb68165f", version = "0.1.0" } # Compound files ([MS-CFB]) and password-encrypted OOXML packages ([MS-OFFCRYPTO]). craft-cfb = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } craft-offcrypto = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }