Offset math scripts by the base italic correction

This commit is contained in:
gloamxun committed 2026-10-20 21:34:09 +08:00
1 parent 6a7e79fa03
commit 6dd4f6bf83
3 files changed
+34 -5

No files matched your search

+29 -1
View File
@@ -197,6 +197,22 @@ pub fn delimiter_scale(body: &MathBox) -> f64 {
}
/// Lay out a math atom at the current style size.
/// TeX italic correction of the rightmost glyph of an atom. It is added
/// before a superscript/subscript so italic bases do not collide with scripts.
pub fn italic_correction(atom: &MathAtom, font: &Cmr10Metrics) -> i64 {
match atom {
MathAtom::Symbol(character) => font.italic(*character),
MathAtom::Row(atoms) => atoms
.iter()
.rev()
.map(|atom| italic_correction(atom, font))
.find(|correction| *correction != 0)
.unwrap_or(0),
MathAtom::Script { base, .. } => italic_correction(base, font),
_ => 0,
}
}
pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
match atom {
MathAtom::Symbol(character) => {
@@ -428,8 +444,9 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
.max(sup_box.as_ref().map(|boxed| boxed.width).unwrap_or(0));
let sup_height = sup_box.as_ref().map_or(0, |boxed| boxed.height);
let sub_depth = sub_box.as_ref().map_or(0, |boxed| boxed.depth);
let italic = italic_correction(base, font);
MathBox {
width: base_box.width + scaled(script_width, 0.7),
width: base_box.width + italic + scaled(script_width, 0.7),
height: base_box
.height
.max(scaled(base_box.height, 0.6) + scaled(sup_height, 0.7)),
@@ -1121,6 +1138,17 @@ fn parse_script(chars: &[char], index: usize) -> (MathAtom, usize) {
mod tests {
use super::*;
#[test]
fn italic_base_offsets_scripts() {
let font = Cmr10Metrics::default();
let base = parse_math("f");
let scripted = parse_math("f^2");
let italic = italic_correction(&base, &font);
assert!(italic > 0);
let base_box = layout_math(&base, &font);
let scripted_box = layout_math(&scripted, &font);
assert!(scripted_box.width >= base_box.width + italic);
}
#[test]
fn math_symbol_uses_font_width() {
let font = Cmr10Metrics::default();
+3 -2
View File
@@ -567,12 +567,13 @@ impl Page {
MathAtom::Script { base, sub, sup } => {
let base_box = math::layout_math(base, font);
Self::paint_math(base, font, x, baseline, size, list);
let italic = math::italic_correction(base, font);
let small = (size as f64 * 0.7) as i64;
if let Some(sup) = sup {
Self::paint_math(
sup,
font,
x + base_box.width,
x + base_box.width + italic,
baseline - base_box.height * 2 / 3,
small,
list,
@@ -582,7 +583,7 @@ impl Page {
Self::paint_math(
sub,
font,
x + base_box.width,
x + base_box.width + italic,
baseline + base_box.depth * 2 / 3,
small,
list,
+2 -2
View File
@@ -252,7 +252,7 @@ M3 painter: `Page::display_list()` and `Halign::display_list()` produce absolute
PDF writer: `crates/texrra-tex-core/src/pdf.rs` writes multi-page PDF 1.4 with five embedded Liberation Type0/CID faces (Identity-H + ToUnicode + `/W` arrays), filled rectangles, diagonal rules, raw-RGB `/XObject` images, `/Annots` URI links and arbitrary `/special` operators. Ligature substitution and line-end italic correction are implemented; full NFSS family switching remains open.
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end italic correction is applied; true NFSS family switching remains open.
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections are applied; true NFSS family switching remains open.
M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments.
@@ -262,7 +262,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
The crate currently has 134 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 217 tests (134 route-D unit, 1 route-D integration, 71 v1 integration, 6 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
The crate currently has 135 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 218 tests (135 route-D unit, 1 route-D integration, 71 v1 integration, 6 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
### M0 incremental demo