Compress embedded font and ToUnicode streams

Route-D PDFs embedded full FontFile2 programs and ToUnicode CMaps uncompressed. Deflate both streams and declare /Filter /FlateDecode while keeping /Length1 as the uncompressed font length. The minimal route-D PDF now renders to 13.5 KB, and the pdfTeX raster comparison still matches.
This commit is contained in:
gloamxun committed 2026-10-21 08:19:02 +08:00
1 parent aeacf53518
commit e052a0d0f3
6 files changed
+25 -7

No files matched your search

+20 -5
View File
@@ -4,13 +4,22 @@
//! and raw-RGB image XObjects.
use std::collections::BTreeMap;
use std::io::Write as _;
use std::sync::OnceLock;
use flate2::Compression;
use flate2::write::ZlibEncoder;
use rusttype::Font;
use crate::cmr10::Cmr10Metrics;
use crate::render::{DisplayItem, DisplayList, FontFace};
fn deflate(bytes: &[u8]) -> Vec<u8> {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
encoder.write_all(bytes).expect("deflate font stream");
encoder.finish().expect("finish font stream")
}
struct PdfFace {
bytes: &'static [u8],
base: &'static str,
@@ -364,18 +373,24 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
)
.into_bytes(),
);
let compressed = deflate(face.bytes);
let mut font_stream = format!(
"<< /Length {} /Length1 {} >>\nstream\n",
face.bytes.len(),
"<< /Length {} /Length1 {} /Filter /FlateDecode >>\nstream\n",
compressed.len(),
face.bytes.len()
)
.into_bytes();
font_stream.extend_from_slice(face.bytes);
font_stream.extend_from_slice(&compressed);
font_stream.extend_from_slice(b"\nendstream");
objects.push(font_stream);
let cmap = tounicode_cmap(face);
let mut cmap_object = format!("<< /Length {} >>\nstream\n", cmap.len()).into_bytes();
cmap_object.extend_from_slice(&cmap);
let compressed_cmap = deflate(&cmap);
let mut cmap_object = format!(
"<< /Length {} /Filter /FlateDecode >>\nstream\n",
compressed_cmap.len()
)
.into_bytes();
cmap_object.extend_from_slice(&compressed_cmap);
cmap_object.extend_from_slice(b"\nendstream");
objects.push(cmap_object);
}