Embed a single monospace face to shrink PDFs

This commit is contained in:
gloamxun committed 2026-10-19 21:42:17 +08:00
1 parent 2076da8f21
commit ea14b3edbf
5 files changed
+41 -5

No files matched your search

+3 -3
View File
@@ -128,7 +128,7 @@ See `docs/ROADMAP.md` for concrete implementation plans.
3.6 MB and `pdffonts` reports only embedded CID TrueType faces); this avoids 3.6 MB and `pdffonts` reports only embedded CID TrueType faces); this avoids
viewer-dependent glyph substitution. Tables use natural column widths and viewer-dependent glyph substitution. Tables use natural column widths and
centered rules; preview and PDF share the same table line coordinates. centered rules; preview and PDF share the same table line coordinates.
- `cargo fmt --check` passes; `cargo test --offline` (56 tests: 52 compiler - `cargo fmt --check` passes; `cargo test --offline` (57 tests: 53 compiler
integration tests + 4 server tests) covers the three examples, the arXiv integration tests + 4 server tests) covers the three examples, the arXiv
fixture, figure/table floats (top and bottom), newpage, page styles and `\thispagestyle`, SVG/PDF output, fixture, figure/table floats (top and bottom), newpage, page styles and `\thispagestyle`, SVG/PDF output,
project API, static frontend assets, path traversal, the legacy project API, static frontend assets, path traversal, the legacy
@@ -137,9 +137,9 @@ See `docs/ROADMAP.md` for concrete implementation plans.
## Verification snapshot ## Verification snapshot
- Compiler integration tests: 52; server tests: 4; total 56. - Compiler integration tests: 53; server tests: 4; total 57.
- `cargo fmt --check` passes. - `cargo fmt --check` passes.
- arXiv fixture: 3 Letter pages, ~3.6 MB, embedded Liberation faces only. - arXiv fixture: 3 Letter pages, ~2.6 MB; four serif faces and one monospace face embedded (bold/italic monospace falls back to the regular face).
- Title/author/date/Introduction baselines within 0.3 pt of the XeLaTeX - Title/author/date/Introduction baselines within 0.3 pt of the XeLaTeX
reference; page 1 keeps the closing section-2 line above the footnote. reference; page 1 keeps the closing section-2 line above the footnote.
- Table 61.1 mm wide (reference 61.2 mm) with `\multicolumn` spans and - Table 61.1 mm wide (reference 61.2 mm) with `\multicolumn` spans and
+2 -2
View File
@@ -2199,7 +2199,7 @@ impl RenderResources {
let mono_family = inputs let mono_family = inputs
.monospace_fonts .monospace_fonts
.clone() .clone()
.map(|family| document.add_font_family(family)); .map(|family| document.add_font_family_single(family));
let cjk_family = inputs let cjk_family = inputs
.cjk_family() .cjk_family()
.map(|family| document.add_font_family(family)); .map(|family| document.add_font_family(family));
@@ -2289,7 +2289,7 @@ impl PreviewResources {
let mono_family = inputs let mono_family = inputs
.monospace_fonts .monospace_fonts
.clone() .clone()
.map(|family| font_cache.add_font_family(family)); .map(|family| font_cache.add_font_family_single(family));
let cjk_family = inputs let cjk_family = inputs
.cjk_family() .cjk_family()
.map(|family| font_cache.add_font_family(family)); .map(|family| font_cache.add_font_family(family));
+19
View File
@@ -109,6 +109,25 @@ fn arxiv_author_orcid_links_have_pdf_annotations() {
); );
} }
#[test]
fn arxiv_pdf_embeds_a_single_monospace_face() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures")
.join("arxiv-template")
.join("template.tex");
let source = std::fs::read_to_string(&path).expect("read arXiv fixture");
let file_name = path.to_string_lossy().to_string();
let pdf = CompilerSession::default()
.compile(&source, &file_name)
.expect("arXiv PDF");
let text = String::from_utf8_lossy(&pdf);
let faces = text.matches("FontFile2").count();
assert!(
faces <= 5,
"arXiv PDF should embed four serif faces plus one monospace face, got {faces}"
);
}
#[test] #[test]
fn arxiv_fixture_is_three_letter_pages() { fn arxiv_fixture_is_three_letter_pages() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+10
View File
@@ -115,6 +115,16 @@ impl FontCache {
} }
/// Adds the given font family to the cache and returns a reference to it. /// Adds the given font family to the cache and returns a reference to it.
pub fn add_font_family_single(&mut self, family: FontFamily<FontData>) -> FontFamily<Font> {
let font = self.add_font(family.regular);
FontFamily {
regular: font,
bold: font,
italic: font,
bold_italic: font,
}
}
pub fn add_font_family(&mut self, family: FontFamily<FontData>) -> FontFamily<Font> { pub fn add_font_family(&mut self, family: FontFamily<FontData>) -> FontFamily<Font> {
FontFamily { FontFamily {
regular: self.add_font(family.regular), regular: self.add_font(family.regular),
+7
View File
@@ -608,6 +608,13 @@ impl Document {
self.context.font_cache.add_font_family(font_family) self.context.font_cache.add_font_family(font_family)
} }
pub fn add_font_family_single(
&mut self,
font_family: fonts::FontFamily<fonts::FontData>,
) -> fonts::FontFamily<fonts::Font> {
self.context.font_cache.add_font_family_single(font_family)
}
/// Returns the font cache used by this document. /// Returns the font cache used by this document.
/// ///
/// You can use the font cache to get the default font and to query glyph metrics for a font. /// You can use the font cache to get the default font and to query glyph metrics for a font.