Paint inline NFSS runs with their own faces

This commit is contained in:
gloamxun committed 2026-10-24 02:09:56 +08:00
1 parent 67898d6a8a
commit 53fabe95db
6 files changed
+223 -17

No files matched your search

+54
View File
@@ -11,6 +11,9 @@ pub struct HorizontalBox {
/// TeX italic correction in scaled points.
#[serde(default)]
pub italic: i64,
/// Font face for this glyph when it comes from a styled run.
#[serde(default)]
pub font: Option<ParagraphFont>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
@@ -389,6 +392,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
depth: 0,
character: Some(character),
italic: 0,
font: None,
}));
}
}
@@ -852,6 +856,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
depth: 0,
character: Some('-'),
italic: 0,
font: None,
}));
} else if *no_break_width > 0 {
line.push(HorizontalItem::Box(HorizontalBox {
@@ -860,6 +865,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
depth: 0,
character: None,
italic: 0,
font: None,
}));
}
}
@@ -1196,6 +1202,7 @@ pub fn text_items_with_font(
depth: font.depth(character),
character: Some(character),
italic: font.italic(character),
font: None,
}));
let boundary = character_index + 1;
// TeX's defaults for American English are \lefthyphenmin=2 and
@@ -1267,6 +1274,52 @@ pub fn layout_paragraph_with_font(
layout_items(&items, style)
}
/// Lay out a paragraph whose text is split into styled runs.
///
/// Each run is shaped with the TFM of its face, so bold and italic runs get
/// their own advances; a single interword glue is inserted at a run boundary
/// when the source text had whitespace there.
pub fn layout_paragraph_runs(
runs: &[(ParagraphFont, String)],
style: &ParagraphStyle,
) -> Paragraph {
let mut items = Vec::new();
let mut previous: Option<(ParagraphFont, String)> = None;
for (font, text) in runs {
if text.is_empty() {
continue;
}
if let Some((previous_font, previous_text)) = &previous {
let boundary_space = previous_text
.chars()
.next_back()
.is_some_and(char::is_whitespace)
|| text.chars().next().is_some_and(char::is_whitespace);
if boundary_space {
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 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()));
}
let mut paragraph = layout_items(&items, style);
paragraph.font = runs.first().map(|run| run.0).unwrap_or_default();
paragraph
}
pub fn layout_paragraph(text: &str, style: &ParagraphStyle) -> Paragraph {
let items = text_items(text, style);
layout_items(&items, style)
@@ -1455,6 +1508,7 @@ mod tests {
depth,
character: Some(character),
italic: 0,
font: None,
})
};
let items = vec![