From 6714591704611bb419f07fb2464c2b863c7f6e3b Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 22:21:10 +0800 Subject: [PATCH] Use the minimal font tier for ASCII route-D PDFs --- crates/texrra-tex-core/src/lib.rs | 1 + crates/texrra-tex-core/src/pdf.rs | 92 ++++++++++++++++++++++++++++--- docs/TEX_CORE.md | 4 +- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 20c14ce..ca1f93a 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1965,6 +1965,7 @@ mod tests { let pdf = crate::pdf::display_list_to_pdf(&list, 400 * 65_536); let text = String::from_utf8_lossy(&pdf); assert_eq!(text.matches("/Subtype /Type0").count(), 1); + assert!(pdf.len() < 60_000, "{}", pdf.len()); assert!(!text.contains("/BaseFont /LiberationSerif-Bold")); assert!(!text.contains("/BaseFont /LiberationMono")); } diff --git a/crates/texrra-tex-core/src/pdf.rs b/crates/texrra-tex-core/src/pdf.rs index e439315..79aadcc 100755 --- a/crates/texrra-tex-core/src/pdf.rs +++ b/crates/texrra-tex-core/src/pdf.rs @@ -17,7 +17,7 @@ struct PdfFace { font: OnceLock>, } -static FACES: [PdfFace; 6] = [ +static FULL_FACES: [PdfFace; 6] = [ PdfFace { bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Regular.ttf"), base: "LiberationSerif", @@ -56,8 +56,74 @@ static FACES: [PdfFace; 6] = [ }, ]; -fn face(font: FontFace) -> &'static PdfFace { - &FACES[font as usize] +static MIN_FACES: [PdfFace; 6] = [ + PdfFace { + bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Regular.ttf"), + base: "LiberationSerif", + italic_angle: 0, + font: OnceLock::new(), + }, + PdfFace { + bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Bold.ttf"), + base: "LiberationSerif-Bold", + italic_angle: 0, + font: OnceLock::new(), + }, + PdfFace { + bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Italic.ttf"), + base: "LiberationSerif-Italic", + italic_angle: -12, + font: OnceLock::new(), + }, + PdfFace { + bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-BoldItalic.ttf"), + base: "LiberationSerif-BoldItalic", + italic_angle: -12, + font: OnceLock::new(), + }, + PdfFace { + bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationMono-Regular.ttf"), + base: "LiberationMono", + italic_angle: 0, + font: OnceLock::new(), + }, + PdfFace { + bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSans-Regular.ttf"), + base: "LiberationSans", + italic_angle: 0, + font: OnceLock::new(), + }, +]; + +fn minimal_text_char(ch: char) -> bool { + ch.is_ascii() + || matches!( + ch, + '\u{00A0}' + | '\u{00A9}' + | '\u{00AE}' + | '\u{00B0}' + | '\u{00B1}' + | '\u{00B5}' + | '\u{00B7}' + | '\u{00D7}' + | '\u{2013}' + | '\u{2014}' + | '\u{2018}' + | '\u{2019}' + | '\u{201C}' + | '\u{201D}' + | '\u{2022}' + | '\u{2026}' + | '\u{00B9}' + | '\u{00B2}' + | '\u{00B3}' + | '\u{2070}'..='\u{2079}' | '\u{2248}' | '\u{223C}' | '\u{03BC}' + ) +} + +fn face(font: FontFace, faces: &'static [PdfFace; 6]) -> &'static PdfFace { + &faces[font as usize] } fn face_font(face: &'static PdfFace) -> &'static Font<'static> { @@ -146,7 +212,12 @@ fn font_resource(font: FontFace) -> &'static str { } } -fn page_content(list: &DisplayList, height: i64, image_names: &BTreeMap) -> String { +fn page_content( + list: &DisplayList, + height: i64, + image_names: &BTreeMap, + faces: &'static [PdfFace; 6], +) -> String { let mut content = String::from("q\n0 0 0 rg\n"); for item in &list.items { match item { @@ -163,7 +234,7 @@ fn page_content(list: &DisplayList, height: i64, image_names: &BTreeMap", glyph_hex(face(*font), text)) + format!("<{}>", glyph_hex(face(*font, faces), text)) )); } DisplayItem::Rule { @@ -245,8 +316,15 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec } } } + let minimal = pages.iter().all(|page| { + page.items.iter().all(|item| match item { + DisplayItem::Text { text, .. } => text.chars().all(minimal_text_char), + _ => true, + }) + }); + let faces: &'static [PdfFace; 6] = if minimal { &MIN_FACES } else { &FULL_FACES }; let mut font_ids = [0usize; 6]; - for (slot, face) in FACES.iter().enumerate() { + for (slot, face) in faces.iter().enumerate() { if !used_faces[slot] { continue; } @@ -363,7 +441,7 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec for (index, slot) in &used { names.insert(*index, format!("Im{slot}")); } - let content = page_content(list, height, &names); + let content = page_content(list, height, &names, faces); objects.push( format!( "<< /Length {} >>\nstream\n{}endstream", diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index ff1de7a..5e70f28 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -232,7 +232,7 @@ for a later overlaid slash. | M2 vertical/page builder | partial | vsize, topskip, insertions, marks, Rust output hook, top/bottom floats, float-page/text fractions, rules, vskip, penalties; interpreted `\output` absent | | M3 halign | partial | natural columns, spans, omit, hline rows, templates, noalign skip, rules; no PDF painter | | M4 math | partial | atoms, scripts, fractions, roots, delimiters, accents, operators, classes, braces, boxed, negated; no math painter/shrink wrapping | -| M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; all five text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and `/W` glyph widths; no Base-14 dependency; SVG carries family/weight/style | +| M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and width arrays; only faces used on a page are embedded, and ASCII documents use the 110-glyph minimal tier (one-page Roman PDF is about 21 KB); no Base-14 dependency; SVG carries family/weight/style | | M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; class/package execution absent | | M7 incremental integration | partial | route-D preview/PDF/edit endpoints with content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote/center mapping; editor integration and inline math pending | @@ -291,7 +291,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. all five text faces embed Liberation Serif/Mono as Type0/Identity-H with `/W` widths; 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); 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