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

Generated
+1
View File
@@ -966,6 +966,7 @@ dependencies = [
name = "texrra-tex-core"
version = "0.1.0"
dependencies = [
"flate2",
"hyphenate",
"rusttype",
"serde",
+1 -1
View File
@@ -129,7 +129,7 @@ section/figure/table references, numeric citations from a manual bibliography or
local `.bib` file, and natbib-style author-year citations for the arXiv template. A4 pages use 25 mm margins unless
`geometry` sets them. The PDF always embeds its Liberation Serif/Mono/Sans
faces (and any CJK fallback), so glyphs do not depend on fonts installed in the
reader; PDFs are therefore larger than Base-14 output but render reliably.
reader; font programs and ToUnicode maps are Flate-compressed, so PDFs are larger than Base-14 output but render reliably.
### LaTeX compatibility layer
+1
View File
@@ -6,6 +6,7 @@ license = "MIT OR Apache-2.0"
description = "Experimental incremental TeX-compatible typesetting core (route D)"
[dependencies]
flate2 = "1.1"
rusttype = "0.8.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
+1
View File
@@ -2455,6 +2455,7 @@ mod tests {
assert!(text.contains("/BaseFont /LiberationSerif"));
assert!(text.contains("/Subtype /Type0"));
assert!(text.contains("/FontFile2"));
assert!(text.contains("/Filter /FlateDecode"));
assert!(text.contains("/ToUnicode"));
assert!(text.contains("Tj"));
assert!(text.trim_end().ends_with("%%EOF"));
+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);
}
+1 -1
View File
@@ -294,7 +294,7 @@ deliberately open rather than approximated:
- **M1**: TeX tolerance/demerits are still fitted to a few fixtures; multi-width
pdfTeX line breaks and full hyphenation demerits are not reproduced generally.
- **M5**: `cmr10.tfm` is parsed at runtime with widths, glyph heights/depths and kern pairs. only used text faces embed Liberation Serif/Mono as Type0/Identity-H with width arrays (ASCII uses the minimal tier); ff/fi/fl/ffi/ffl ligatures come from TFM, and CMR10 italic corrections are applied at line ends; full NFSS family switching is not implemented.
- **M5**: `cmr10.tfm` is parsed at runtime with widths, glyph heights/depths and kern pairs. only used text faces embed Liberation Serif/Mono as Type0/Identity-H with width arrays (ASCII uses the minimal tier); the FontFile2 and ToUnicode streams use Flate compression; ff/fi/fl/ffi/ffl ligatures come from TFM, and CMR10 italic corrections are applied at line ends; full NFSS family switching is not implemented.
- **M6**: `\output`, `\insert`, mark/float output policies, class-file execution and
math limit policies are only partially modeled; `OutputRoutine::wants_break` can force a page break between units, and insertion material is stacked by class from the page bottom; `\output` is still Rust code rather than interpreted tokens.
- Page hashes cover rendered items/insertions/marks/floats, not source-dependency