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
@@ -1270,6 +1270,27 @@ impl Unit {
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a paragraph unit mixing styled runs with inline formulas.
|
||||
pub fn paragraph_with_style_and_segments(
|
||||
id: UnitId,
|
||||
source: impl Into<String>,
|
||||
style: ParagraphStyle,
|
||||
segments: Vec<crate::paragraph::InlineSegment>,
|
||||
) -> Self {
|
||||
let source = source.into();
|
||||
let paragraph = crate::paragraph::layout_paragraph_segments(&segments, &style);
|
||||
Self {
|
||||
id,
|
||||
source,
|
||||
items: vec![VerticalItem::Paragraph(paragraph)],
|
||||
paragraph_style: Some(style),
|
||||
paragraph_runs: None,
|
||||
space_before: 0,
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a paragraph unit with both an explicit style and font face.
|
||||
pub fn paragraph_with_style_and_face(
|
||||
id: UnitId,
|
||||
|
||||
@@ -14,6 +14,16 @@ pub struct HorizontalBox {
|
||||
/// Font face for this glyph when it comes from a styled run.
|
||||
#[serde(default)]
|
||||
pub font: Option<ParagraphFont>,
|
||||
/// The inline formula this box paints, when it came from `$...$`.
|
||||
#[serde(default)]
|
||||
pub math: Option<Box<crate::math::MathAtom>>,
|
||||
}
|
||||
|
||||
/// A run of paragraph material: text in one face or an inline formula.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum InlineSegment {
|
||||
Text(ParagraphFont, String),
|
||||
Math(crate::math::MathAtom),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
@@ -610,6 +620,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
|
||||
character: Some(character),
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -1076,6 +1087,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
character: Some('-'),
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
}));
|
||||
} else if *no_break_width > 0 {
|
||||
line.push(HorizontalItem::Box(HorizontalBox {
|
||||
@@ -1085,6 +1097,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
character: None,
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -1428,6 +1441,7 @@ pub fn text_items_with_font(
|
||||
character: Some(character),
|
||||
italic: font.italic(character),
|
||||
font: None,
|
||||
math: None,
|
||||
}));
|
||||
let boundary = character_index + 1;
|
||||
// TeX's defaults for American English are \lefthyphenmin=2 and
|
||||
@@ -1550,6 +1564,90 @@ pub fn layout_paragraph_runs(
|
||||
paragraph
|
||||
}
|
||||
|
||||
/// Lay out a paragraph mixing styled text with inline formulas.
|
||||
pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphStyle) -> Paragraph {
|
||||
let mut items = Vec::new();
|
||||
let mut previous: Option<(ParagraphFont, String)> = None;
|
||||
let mut seen_content = false;
|
||||
for segment in segments {
|
||||
match segment {
|
||||
InlineSegment::Text(font, text) => {
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let starts_with_space = text.chars().next().is_some_and(char::is_whitespace);
|
||||
let boundary_space = match &previous {
|
||||
Some((_previous_font, previous_text)) => {
|
||||
previous_text
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_some_and(char::is_whitespace)
|
||||
|| starts_with_space
|
||||
}
|
||||
None => seen_content && starts_with_space,
|
||||
};
|
||||
if boundary_space {
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(
|
||||
previous.as_ref().map_or(*font, |(font, _)| *font),
|
||||
);
|
||||
items.push(HorizontalItem::Glue {
|
||||
width: metrics.space(),
|
||||
stretch: metrics.stretch(),
|
||||
shrink: metrics.shrink(),
|
||||
order: 0,
|
||||
});
|
||||
}
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*font);
|
||||
let mut run = text_items_with_font(text, style, &metrics);
|
||||
for item in &mut run {
|
||||
if let HorizontalItem::Box(box_) = item {
|
||||
box_.font = Some(*font);
|
||||
}
|
||||
}
|
||||
items.extend(run);
|
||||
previous = Some((*font, text.clone()));
|
||||
seen_content = true;
|
||||
}
|
||||
InlineSegment::Math(atom) => {
|
||||
if let Some((previous_font, previous_text)) = &previous
|
||||
&& previous_text
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_some_and(char::is_whitespace)
|
||||
{
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*previous_font);
|
||||
items.push(HorizontalItem::Glue {
|
||||
width: metrics.space(),
|
||||
stretch: metrics.stretch(),
|
||||
shrink: metrics.shrink(),
|
||||
order: 0,
|
||||
});
|
||||
}
|
||||
let math = crate::math::layout_math(atom, &crate::cmr10::Cmr10Metrics::default());
|
||||
items.push(HorizontalItem::Box(HorizontalBox {
|
||||
width: math.width,
|
||||
height: math.height,
|
||||
depth: math.depth,
|
||||
character: None,
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: Some(Box::new(atom.clone())),
|
||||
}));
|
||||
previous = None;
|
||||
seen_content = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
scale_items_for_size(&mut items, style.font_size);
|
||||
let mut paragraph = layout_items(&items, &sized_style(style));
|
||||
paragraph.font = match segments.first() {
|
||||
Some(InlineSegment::Text(font, _)) => *font,
|
||||
_ => ParagraphFont::Roman,
|
||||
};
|
||||
paragraph.font_size = style.font_size;
|
||||
paragraph
|
||||
}
|
||||
|
||||
pub fn layout_paragraph(text: &str, style: &ParagraphStyle) -> Paragraph {
|
||||
let mut items = text_items(text, style);
|
||||
scale_items_for_size(&mut items, style.font_size);
|
||||
@@ -1798,6 +1896,48 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_math_boxes_sit_between_words() {
|
||||
let style = ParagraphStyle {
|
||||
line_width: 300 * 65_536,
|
||||
par_indent: 0,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let atom = crate::math::parse_math("x^2");
|
||||
let segments = vec![
|
||||
InlineSegment::Text(ParagraphFont::Roman, "Value ".into()),
|
||||
InlineSegment::Math(atom.clone()),
|
||||
InlineSegment::Text(ParagraphFont::Roman, " tail".into()),
|
||||
];
|
||||
let paragraph = layout_paragraph_segments(&segments, &style);
|
||||
let boxes: Vec<&HorizontalBox> = paragraph
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter_map(|item| match item {
|
||||
HorizontalItem::Box(box_) => Some(box_),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let math = boxes
|
||||
.iter()
|
||||
.find(|box_| box_.math.is_some())
|
||||
.expect("math box");
|
||||
assert!(math.character.is_none());
|
||||
assert_eq!(
|
||||
math.width,
|
||||
crate::math::layout_math(&atom, &crate::cmr10::Cmr10Metrics::default()).width
|
||||
);
|
||||
let glues = paragraph
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter(|item| matches!(item, HorizontalItem::Glue { .. }))
|
||||
.count();
|
||||
assert!(glues >= 2, "spaces on both sides of the formula");
|
||||
assert!(paragraph.font_size == 655_360);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_at_keeps_each_part_last_line_descent() {
|
||||
let boxed = |character: char, depth: i64| {
|
||||
@@ -1808,6 +1948,7 @@ mod tests {
|
||||
character: Some(character),
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
})
|
||||
};
|
||||
let items = vec![
|
||||
|
||||
@@ -529,6 +529,17 @@ impl Page {
|
||||
for item in line {
|
||||
match item {
|
||||
HorizontalItem::Box(box_) => {
|
||||
if let Some(math) = &box_.math {
|
||||
let math_font = Cmr10Metrics::default();
|
||||
Self::paint_math(
|
||||
math,
|
||||
&math_font,
|
||||
x,
|
||||
baseline,
|
||||
size,
|
||||
&mut list.items,
|
||||
);
|
||||
}
|
||||
if let Some(character) = box_.character {
|
||||
list.push(DisplayItem::Text {
|
||||
font: box_.font.map_or(face, paragraph_face),
|
||||
|
||||
Reference in new issue
Block a user