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

+20
View File
@@ -1244,6 +1244,26 @@ impl Unit {
Self::paragraph_with_style_and_face(id, source, style, ParagraphFont::Roman)
}
/// Build a paragraph unit whose text is split into styled runs.
pub fn paragraph_with_style_and_runs(
id: UnitId,
source: impl Into<String>,
style: ParagraphStyle,
runs: Vec<(ParagraphFont, String)>,
) -> Self {
let source = source.into();
let paragraph = crate::paragraph::layout_paragraph_runs(&runs, &style);
Self {
id,
source,
items: vec![VerticalItem::Paragraph(paragraph)],
paragraph_style: Some(style),
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,
+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![
+12 -8
View File
@@ -250,6 +250,16 @@ fn escape(text: &str) -> String {
.replace('>', "&gt;")
}
fn paragraph_face(font: ParagraphFont) -> FontFace {
match font {
ParagraphFont::Roman => FontFace::Roman,
ParagraphFont::Bold => FontFace::Bold,
ParagraphFont::Italic => FontFace::Italic,
ParagraphFont::Mono => FontFace::Mono,
ParagraphFont::Sans => FontFace::Sans,
}
}
fn character_text(character: char) -> String {
match character {
'\u{FB00}' => "ff".to_owned(),
@@ -441,13 +451,7 @@ impl Page {
y += *height + *depth;
}
VerticalItem::Paragraph(paragraph) => {
let face = match paragraph.font {
ParagraphFont::Roman => FontFace::Roman,
ParagraphFont::Bold => FontFace::Bold,
ParagraphFont::Italic => FontFace::Italic,
ParagraphFont::Mono => FontFace::Mono,
ParagraphFont::Sans => FontFace::Sans,
};
let face = paragraph_face(paragraph.font);
let lines = paragraph.lines.len().max(1) as i64;
let line_height = (paragraph.height / lines).max(1);
let size = 10 * 65_536;
@@ -479,7 +483,7 @@ impl Page {
HorizontalItem::Box(box_) => {
if let Some(character) = box_.character {
list.push(DisplayItem::Text {
font: face,
font: box_.font.map_or(face, paragraph_face),
x,
y: baseline,
size,