Lay out inline math in paragraphs
This commit is contained in:
1 parent
aae046a2f0
commit
e008741aa5
7 files changed
+346
-13
No files matched your search
+168
-10
@@ -2278,6 +2278,102 @@ fn nfss_font_with_size(
|
||||
/// Handles `\textbf{...}`-style wrappers, declaration groups such as
|
||||
/// `{\bfseries ...}` and arbitrary nesting of both, including braces inside a
|
||||
/// group. Plain text inherits the surrounding face.
|
||||
/// Find the closing delimiter of an inline formula.
|
||||
fn math_end(text: &str, closing: &str) -> Option<usize> {
|
||||
if closing == "$" {
|
||||
let bytes = text.as_bytes();
|
||||
let mut index = 0;
|
||||
while index < bytes.len() {
|
||||
match bytes[index] {
|
||||
b'\\' => index += 2,
|
||||
b'$' => return Some(index),
|
||||
_ => index += 1,
|
||||
}
|
||||
}
|
||||
None
|
||||
} else {
|
||||
text.find(closing)
|
||||
}
|
||||
}
|
||||
|
||||
/// Split text into literal pieces and `$...$` / `\(...\)` formulas.
|
||||
fn split_inline_math(text: &str) -> Vec<(bool, String)> {
|
||||
let mut pieces = Vec::new();
|
||||
let mut literal = String::new();
|
||||
let mut index = 0;
|
||||
while index < text.len() {
|
||||
let rest = &text[index..];
|
||||
if rest.starts_with("$$") {
|
||||
literal.push_str("$$");
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
let opener = if rest.starts_with("\\(") {
|
||||
Some((2, "\\)"))
|
||||
} else if rest.starts_with('$') {
|
||||
Some((1, "$"))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some((open_len, closing)) = opener {
|
||||
let after = &rest[open_len..];
|
||||
if let Some(end) = math_end(after, closing) {
|
||||
if !literal.is_empty() {
|
||||
pieces.push((false, std::mem::take(&mut literal)));
|
||||
}
|
||||
pieces.push((true, after[..end].to_string()));
|
||||
index += open_len + end + closing.len();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let character = rest.chars().next().expect("non-empty tail");
|
||||
if rest.starts_with('\\') {
|
||||
literal.push(character);
|
||||
if let Some(next) = rest[character.len_utf8()..].chars().next() {
|
||||
literal.push(next);
|
||||
index += character.len_utf8() + next.len_utf8();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
literal.push(character);
|
||||
index += character.len_utf8();
|
||||
}
|
||||
if !literal.is_empty() {
|
||||
pieces.push((false, literal));
|
||||
}
|
||||
pieces
|
||||
}
|
||||
|
||||
/// Build mixed text/math segments when the paragraph contains inline math.
|
||||
///
|
||||
/// Math metrics are measured at 10pt, so sized paragraphs keep the literal
|
||||
/// delimiters until per-size math layout exists.
|
||||
fn inline_math_segments(
|
||||
text: &str,
|
||||
base: ParagraphFont,
|
||||
style: &ParagraphStyle,
|
||||
) -> Option<Vec<texrra_tex_core::paragraph::InlineSegment>> {
|
||||
if style.font_size != 655_360 {
|
||||
return None;
|
||||
}
|
||||
let runs = inline_runs(text, base).unwrap_or_else(|| vec![(base, text.to_string())]);
|
||||
let mut segments = Vec::new();
|
||||
let mut found = false;
|
||||
for (font, run_text) in runs {
|
||||
for (is_math, piece) in split_inline_math(&run_text) {
|
||||
if is_math {
|
||||
found = true;
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Math(parse_math(
|
||||
&piece,
|
||||
)));
|
||||
} else if !piece.is_empty() {
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Text(font, piece));
|
||||
}
|
||||
}
|
||||
}
|
||||
found.then_some(segments)
|
||||
}
|
||||
|
||||
fn inline_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, String)>> {
|
||||
let mut runs = Vec::new();
|
||||
let mut found = false;
|
||||
@@ -3350,19 +3446,27 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
space_skip: geometry.space_skip,
|
||||
xspace_skip: geometry.xspace_skip,
|
||||
};
|
||||
let mut unit = match inline_runs(¶graph.text, paragraph.font) {
|
||||
Some(runs) => Unit::paragraph_with_style_and_runs(
|
||||
let mut unit = match inline_math_segments(¶graph.text, paragraph.font, &style) {
|
||||
Some(segments) => Unit::paragraph_with_style_and_segments(
|
||||
index as u64,
|
||||
paragraph.text.clone(),
|
||||
style,
|
||||
runs,
|
||||
),
|
||||
None => Unit::paragraph_with_style_and_face(
|
||||
index as u64,
|
||||
paragraph.text.clone(),
|
||||
style,
|
||||
paragraph.font,
|
||||
style.clone(),
|
||||
segments,
|
||||
),
|
||||
None => match inline_runs(¶graph.text, paragraph.font) {
|
||||
Some(runs) => Unit::paragraph_with_style_and_runs(
|
||||
index as u64,
|
||||
paragraph.text.clone(),
|
||||
style,
|
||||
runs,
|
||||
),
|
||||
None => Unit::paragraph_with_style_and_face(
|
||||
index as u64,
|
||||
paragraph.text.clone(),
|
||||
style,
|
||||
paragraph.font,
|
||||
),
|
||||
},
|
||||
};
|
||||
unit.space_before = paragraph.space_before;
|
||||
unit.space_stretch = paragraph.space_stretch;
|
||||
@@ -4241,6 +4345,60 @@ A definition.
|
||||
assert_eq!(document.units[3].space_stretch, 2 * SCALE);
|
||||
assert_eq!(document.units[3].space_shrink, SCALE);
|
||||
}
|
||||
#[test]
|
||||
fn inline_math_becomes_math_boxes() {
|
||||
let blocks = paragraphs("Value $x^2$ tail.").expect("paragraphs");
|
||||
let document = build_document(&blocks, &PageGeometry::default());
|
||||
let paragraph = match &document.units[0].items[0] {
|
||||
VerticalItem::Paragraph(paragraph) => paragraph,
|
||||
_ => panic!("expected a paragraph unit"),
|
||||
};
|
||||
let math = paragraph
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.find_map(|item| match item {
|
||||
texrra_tex_core::paragraph::HorizontalItem::Box(box_) if box_.math.is_some() => {
|
||||
Some(box_)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.expect("inline math box");
|
||||
assert!(math.character.is_none());
|
||||
assert!(math.width > 0);
|
||||
assert_eq!(document.units[0].source, "Value $x^2$ tail.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_math_paints_italic_glyphs() {
|
||||
let json = preview_json("Value $x^2$ tail.").expect("preview");
|
||||
assert!(
|
||||
json.contains("font-style=") && json.contains("italic"),
|
||||
"math letters use the italic face"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_math_stays_literal_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] {
|
||||
VerticalItem::Paragraph(paragraph) => paragraph,
|
||||
_ => panic!("expected a paragraph unit"),
|
||||
};
|
||||
let boxes: Vec<_> = paragraph
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter_map(|item| match item {
|
||||
texrra_tex_core::paragraph::HorizontalItem::Box(box_) => Some(box_),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert!(boxes.iter().all(|box_| box_.math.is_none()));
|
||||
assert!(boxes.iter().any(|box_| box_.character == Some('$')));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_width_sets_the_paragraph_measure() {
|
||||
let long =
|
||||
|
||||
Reference in new issue
Block a user