Use the minimal font tier for ASCII route-D PDFs

This commit is contained in:
gloamxun committed 2026-10-20 22:21:10 +08:00
1 parent 295093ae5a
commit 6714591704
3 files changed
+88 -9

No files matched your search

+1
View File
@@ -1965,6 +1965,7 @@ mod tests {
let pdf = crate::pdf::display_list_to_pdf(&list, 400 * 65_536); let pdf = crate::pdf::display_list_to_pdf(&list, 400 * 65_536);
let text = String::from_utf8_lossy(&pdf); let text = String::from_utf8_lossy(&pdf);
assert_eq!(text.matches("/Subtype /Type0").count(), 1); 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 /LiberationSerif-Bold"));
assert!(!text.contains("/BaseFont /LiberationMono")); assert!(!text.contains("/BaseFont /LiberationMono"));
} }
+85 -7
View File
@@ -17,7 +17,7 @@ struct PdfFace {
font: OnceLock<Font<'static>>, font: OnceLock<Font<'static>>,
} }
static FACES: [PdfFace; 6] = [ static FULL_FACES: [PdfFace; 6] = [
PdfFace { PdfFace {
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Regular.ttf"), bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Regular.ttf"),
base: "LiberationSerif", base: "LiberationSerif",
@@ -56,8 +56,74 @@ static FACES: [PdfFace; 6] = [
}, },
]; ];
fn face(font: FontFace) -> &'static PdfFace { static MIN_FACES: [PdfFace; 6] = [
&FACES[font as usize] 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> { 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<usize, String>) -> String { fn page_content(
list: &DisplayList,
height: i64,
image_names: &BTreeMap<usize, String>,
faces: &'static [PdfFace; 6],
) -> String {
let mut content = String::from("q\n0 0 0 rg\n"); let mut content = String::from("q\n0 0 0 rg\n");
for item in &list.items { for item in &list.items {
match item { match item {
@@ -163,7 +234,7 @@ fn page_content(list: &DisplayList, height: i64, image_names: &BTreeMap<usize, S
pt(*size), pt(*size),
pt(*x), pt(*x),
pt(height - *y), pt(height - *y),
format!("<{}>", glyph_hex(face(*font), text)) format!("<{}>", glyph_hex(face(*font, faces), text))
)); ));
} }
DisplayItem::Rule { DisplayItem::Rule {
@@ -245,8 +316,15 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
} }
} }
} }
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]; let mut font_ids = [0usize; 6];
for (slot, face) in FACES.iter().enumerate() { for (slot, face) in faces.iter().enumerate() {
if !used_faces[slot] { if !used_faces[slot] {
continue; continue;
} }
@@ -363,7 +441,7 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
for (index, slot) in &used { for (index, slot) in &used {
names.insert(*index, format!("Im{slot}")); names.insert(*index, format!("Im{slot}"));
} }
let content = page_content(list, height, &names); let content = page_content(list, height, &names, faces);
objects.push( objects.push(
format!( format!(
"<< /Length {} >>\nstream\n{}endstream", "<< /Length {} >>\nstream\n{}endstream",
+2 -2
View File
@@ -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 | | 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 | | 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 | | 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 | | 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 | | 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 - **M1**: TeX tolerance/demerits are still fitted to a few fixtures; multi-width
pdfTeX line breaks and full hyphenation demerits are not reproduced generally. 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 - **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. 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 - Page hashes cover rendered items/insertions/marks/floats, not source-dependency