Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

perf(fonts): reuse native Brotli for WOFF2 decoding

ldm03f50eeb16620
diff --git a/Cargo.lock b/Cargo.lock
index 97091259f..3957c42f6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2751,6 +2751,7 @@ dependencies = [
name = "moli-layout"
version = "0.1.0"
dependencies = [
+ "brotlic",
"euclid",
"kurbo",
"moli-image",
diff --git a/moli-layout/Cargo.toml b/moli-layout/Cargo.toml
index 900311b10..cefce6d20 100644
--- a/moli-layout/Cargo.toml
+++ b/moli-layout/Cargo.toml
@@ -5,6 +5,7 @@ edition = "2024"
license.workspace = true
[dependencies]
+brotlic = "0.8.2"
euclid = "=0.22.14"
kurbo = "=0.13.1"
moli-image = { path = "../moli-image" }
@@ -14,7 +15,7 @@ stylo = "0.20"
stylo_taffy = { git = "https://github.com/DioxusLabs/blitz", rev = "5081c65811a4396f5a99b2e0aca542a4a4a6606f", default-features = false, features = ["std", "block", "flexbox", "grid", "floats"] }
taffy = { version = "=0.13.0", default-features = false, features = ["std", "block_layout", "flexbox", "grid", "float_layout", "content_size", "calc", "detailed_layout_info"] }
thiserror = "2.0.17"
-wuff = "0.2.9"
+wuff = { version = "0.2.9", default-features = false, features = ["z"] }
[lints]
workspace = true
diff --git a/moli-layout/src/text.rs b/moli-layout/src/text.rs
index 5e04e4863..3bf92cd18 100644
--- a/moli-layout/src/text.rs
+++ b/moli-layout/src/text.rs
@@ -941,13 +941,29 @@ fn decode_web_font_bytes(bytes: &[u8]) -> Result<Arc<[u8]>, WebFontRegistrationE
let decoded = match bytes.get(..4) {
Some(b"wOFF") => wuff::decompress_woff1(bytes)
.map_err(|_| WebFontRegistrationError::DecodeFailed { format: "WOFF" })?,
- Some(b"wOF2") => wuff::decompress_woff2(bytes)
- .map_err(|_| WebFontRegistrationError::DecodeFailed { format: "WOFF2" })?,
+ Some(b"wOF2") => {
+ wuff::decompress_woff2_with_custom_brotli(bytes, &mut decompress_woff2_brotli)
+ .map_err(|_| WebFontRegistrationError::DecodeFailed { format: "WOFF2" })?
+ }
_ => bytes.to_vec(),
};
Ok(Arc::from(decoded))
}
+fn decompress_woff2_brotli(
+ compressed: &[u8],
+ expected_size: usize,
+) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
+ // wuff validates this exact size before calling us. A fixed output slice
+ // preserves its decompression limit while sharing the native Brotli codec.
+ let mut output = vec![0; expected_size];
+ let written = brotlic::decompress(compressed, &mut output)?;
+ if written != expected_size {
+ return Err(wuff::WuffErr::GenericError.into());
+ }
+ Ok(output)
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -1237,6 +1253,44 @@ mod tests {
}
}
+ #[test]
+ fn woff2_brotli_requires_exact_decoded_size() {
+ let compressed = [11, 2, 128, b'h', b'e', b'l', b'l', b'o', 3];
+ assert_eq!(decompress_woff2_brotli(&compressed, 5).unwrap(), b"hello");
+ for expected_size in [0, 4, 6] {
+ assert!(
+ decompress_woff2_brotli(&compressed, expected_size).is_err(),
+ "decoded length must match {expected_size} exactly"
+ );
+ }
+ }
+
+ #[test]
+ fn woff2_brotli_rejects_incomplete_and_corrupt_streams() {
+ let compressed = [11, 2, 128, b'h', b'e', b'l', b'l', b'o', 3];
+ for end in 0..compressed.len() {
+ assert!(
+ decompress_woff2_brotli(&compressed[..end], 5).is_err(),
+ "a {end}-byte prefix must not decode successfully"
+ );
+ }
+ assert!(decompress_woff2_brotli(&[0; 9], 5).is_err());
+ }
+
+ #[test]
+ fn woff2_accepts_padding_inside_declared_compressed_block() {
+ let expected = decode_web_font_bytes(TEST_WOFF2).unwrap();
+ let compressed_size = u32::from_be_bytes(TEST_WOFF2[20..24].try_into().unwrap());
+ assert_eq!(compressed_size, 573);
+ // The fixture has three padding bytes after its Brotli stream. Some
+ // encoders count this padding in totalCompressedSize; wuff accepts it.
+ for padding in 1..=3 {
+ let mut padded_font = TEST_WOFF2.to_vec();
+ padded_font[20..24].copy_from_slice(&(compressed_size + padding).to_be_bytes());
+ assert_eq!(decode_web_font_bytes(&padded_font).unwrap(), expected);
+ }
+ }
+
#[test]
fn stable_slot_replacement_rebuilds_initialized_font_collection() {
let mut services =