diff --git a/Cargo.lock b/Cargo.lock index 125ae9e..fd1ab51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -966,6 +966,7 @@ dependencies = [ name = "texrra-tex-core" version = "0.1.0" dependencies = [ + "flate2", "hyphenate", "rusttype", "serde", diff --git a/README.md b/README.md index eb5e491..1aa31e2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/crates/texrra-tex-core/Cargo.toml b/crates/texrra-tex-core/Cargo.toml index ed13cb7..3fd0f53 100644 --- a/crates/texrra-tex-core/Cargo.toml +++ b/crates/texrra-tex-core/Cargo.toml @@ -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" diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 67badb9..47719bc 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -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")); diff --git a/crates/texrra-tex-core/src/pdf.rs b/crates/texrra-tex-core/src/pdf.rs index 74c52c1..8b1c7ce 100755 --- a/crates/texrra-tex-core/src/pdf.rs +++ b/crates/texrra-tex-core/src/pdf.rs @@ -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 { + 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 ) .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); } diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index faf0c3e..fe4581b 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -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