Render inline math in sized paragraphs

Inline math returned None whenever the paragraph base size was not 10pt, so a
12pt or \large paragraph printed its dollar delimiters literally. Keep the math
segment at every size: the box width still scales with the paragraph, while the
math glyphs are pinned to the 10pt metric baseline by setting the box's size
field, so paint positions and glyph sizes stay consistent (slightly smaller
than the surrounding text, which is documented).

The old sized-paragraph test now expects a math box and no literal dollars; a
new 12pt preview test checks italic math glyphs and no "$" with no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 01:34:07 +08:00
1 parent 65a132d7b1
commit b3fbbc8dda
4 files changed
+34 -16

No files matched your search

+27 -10
View File
@@ -10168,13 +10168,8 @@ fn inline_math_segments(
}
}
}
// Math metrics are laid out at 10pt, so a paragraph that mixes math with
// another base size keeps its literal delimiters instead of painting the
// formula at the wrong size. Colour, link and sized runs are safe at any
// base size.
if found_math && style.font_size != 655_360 {
return None;
}
// Math metrics are laid out at 10pt; its glyphs stay at that size inside a
// larger paragraph (the box width still scales with the paragraph).
(found_math || found_link || found_size || found_color).then_some(segments)
}
@@ -16768,6 +16763,22 @@ left & right \\
);
}
#[test]
fn inline_math_renders_at_twelve_point() {
let source = "\\documentclass[12pt]{article}\n\\begin{document}\n\nInline $x^2$ math.\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let svg = value["pages"][0]["svg"].as_str().expect("svg");
assert!(svg.contains("font-style=\"italic\""), "{svg}");
assert!(!svg.contains('$'), "{svg}");
}
#[test]
fn inline_colours_and_links_work_at_twelve_point() {
let source = "\\documentclass[12pt]{article}\n\\begin{document}\n\nA \\textcolor{red}{red word} and a \\href{https://example.com}{link} and x\\textsuperscript{2}.\n\n\\end{document}\n";
@@ -17781,7 +17792,7 @@ left & right \\
}
#[test]
fn inline_math_stays_literal_in_sized_paragraphs() {
fn inline_math_renders_in_sized_paragraphs() {
let blocks = paragraphs("\\large Value $x$ tail.").expect("paragraphs");
let document = build_document(&blocks, &PageGeometry::default());
let paragraph = match &document.units[0].items[0] {
@@ -17797,8 +17808,14 @@ left & right \\
_ => None,
})
.collect();
assert!(boxes.iter().all(|box_| box_.math.is_none()));
assert!(boxes.iter().any(|box_| box_.character == Some('$')));
assert!(
boxes.iter().any(|box_| box_.math.is_some()),
"math box missing"
);
assert!(
!boxes.iter().any(|box_| box_.character == Some('$')),
"literal dollar left"
);
}
#[test]