Skip to content

Commit c664bc5

Browse files
author
yasuo
committed
pdf: let a store built from bytes name its math font, so a browser build's equations are not laid out against a face with no MATH table
1 parent b87e643 commit c664bc5

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎crates/rustyfi-pdf/src/ttf.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,27 @@ impl TtfFontStore {
217217
self
218218
}
219219

220+
/// Point the MATH font at `font`, as `default-font.satysfi-hash`'s
221+
/// optional `"math"` abbrev does for a store built from a font root.
222+
///
223+
/// Without this a store built from bytes has no math default at all, so
224+
/// `get-initial-context` leaves `Context::math_font` at its seed — the
225+
/// LATIN regular face. A text face has no `MATH` table, and every constant
226+
/// math layout depends on (axis height, fraction-bar thickness, script
227+
/// shifts, big-operator variants) then falls back to a guess: limits land
228+
/// on top of their operator, fraction bars vanish, fences stop stretching.
229+
/// It renders, and it is unmistakably wrong.
230+
///
231+
/// Takes a [`FontKey`] rather than an abbrev for the same reason
232+
/// [`Self::with_script_default`] does — a name the store does not carry
233+
/// cannot be accepted and then silently do nothing. Get one from
234+
/// [`Self::abbrev_key`].
235+
#[must_use]
236+
pub fn with_math_default(mut self, font: FontKey) -> Self {
237+
self.math_default = Some(font);
238+
self
239+
}
240+
220241
/// Builder used only by [`crate::fonts::FontRegistry::build_store`]:
221242
/// construct a store with the three default slots already
222243
/// loaded (via [`Self::load`]) plus every other configured abbrev's

‎crates/rustyfi-pdf/tests/math_font.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,62 @@ fn styled_math_renders_through_cid_pipeline() {
334334
path.display()
335335
);
336336
}
337+
338+
/// A store built from BYTES can be given a math default, and without one the
339+
/// math font falls back to the Latin face.
340+
///
341+
/// This is the browser build's whole problem: `from_bytes_with_abbrevs` sets
342+
/// `math_default: None`, so `Context::math_font` stays at its seed — the text
343+
/// face — which has no `MATH` table. Every constant math layout reads then
344+
/// falls back to a guess and an equation collapses: limits land on their
345+
/// operator, fraction bars vanish, fences stop stretching. It renders, so
346+
/// nothing errors; it is just wrong.
347+
#[test]
348+
fn a_byte_built_store_takes_a_math_default() {
349+
let path = need_math_font!();
350+
let math = std::fs::read(&path).expect("read math font");
351+
let text = match std::fs::read(
352+
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lib-rustyfi/dist/fonts/Junicode.ttf"),
353+
) {
354+
Ok(bytes) => bytes,
355+
Err(_) => {
356+
eprintln!("skipping: the bundled text face is absent — run download-fonts.sh");
357+
return;
358+
}
359+
};
360+
361+
let store = TtfFontStore::from_bytes_with_abbrevs(
362+
text,
363+
None,
364+
None,
365+
[("lmmath".to_string(), math)],
366+
"test fonts",
367+
)
368+
.expect("both faces must parse");
369+
370+
// The control: this is what the browser build ships today.
371+
assert!(
372+
store.default_math_font().is_none(),
373+
"a byte-built store must not acquire a math default by itself",
374+
);
375+
376+
let key = store.abbrev_key("lmmath").expect("the abbrev was registered");
377+
let store = store.with_math_default(key);
378+
assert_eq!(
379+
store.default_math_font(),
380+
Some(key),
381+
"with_math_default must point the math font at the face given",
382+
);
383+
// And it has to be a DIFFERENT face from the text one, or the fix is
384+
// pointing math back at the font that caused the problem.
385+
assert_ne!(key, FontKey(0), "the math face must not be the Latin slot");
386+
assert!(
387+
store.math_constants(key).is_some(),
388+
"the math default must resolve to a face with a MATH table",
389+
);
390+
assert!(
391+
store.math_constants(FontKey(0)).is_none(),
392+
"the bundled text face must have no MATH table — otherwise this test \
393+
proves nothing about why the math default is needed",
394+
);
395+
}

0 commit comments

Comments
 (0)