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

+134 -6
View File
@@ -1068,6 +1068,71 @@ fn nfss_font(raw: &str) -> Option<(ParagraphFont, String)> {
Some((font, text.to_string()))
}
/// Split a paragraph containing inline style commands into styled runs.
///
/// Only whole-group commands are recognised (`\textbf{...}` and friends); the
/// braces are removed and their contents become a run in the requested face.
fn inline_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, String)>> {
const COMMANDS: [(&str, Option<ParagraphFont>); 11] = [
("\\textbf", Some(ParagraphFont::Bold)),
("\\emph", Some(ParagraphFont::Italic)),
("\\textit", Some(ParagraphFont::Italic)),
("\\textsl", Some(ParagraphFont::Italic)),
("\\texttt", Some(ParagraphFont::Mono)),
("\\textsf", Some(ParagraphFont::Sans)),
("\\textmd", None),
("\\textnormal", None),
("\\textup", None),
("\\textrm", None),
("\\textsc", None),
];
let mut runs: Vec<(ParagraphFont, String)> = Vec::new();
let mut plain = String::new();
let mut rest = text;
let mut found = false;
loop {
let mut next: Option<(usize, usize, Option<ParagraphFont>)> = None;
for (command, font) in COMMANDS {
let Some(position) = rest.find(command) else {
continue;
};
let after = &rest[position + command.len()..];
if !after.starts_with('{') {
continue;
}
if next.is_none_or(|(best, _, _)| position < best) {
next = Some((position, command.len(), font));
}
}
let Some((position, length, font)) = next else {
break;
};
let body_start = position + length + 1;
let Some(offset) = rest[body_start..].find('}') else {
break;
};
let body_end = body_start + offset;
plain.push_str(&rest[..position]);
if !plain.is_empty() {
runs.push((base, std::mem::take(&mut plain)));
}
let body = &rest[body_start..body_end];
if !body.is_empty() {
runs.push((font.unwrap_or(base), body.to_string()));
}
rest = &rest[body_end + 1..];
found = true;
}
if !found {
return None;
}
plain.push_str(rest);
if !plain.is_empty() {
runs.push((base, plain));
}
Some(runs)
}
fn parse_paragraph(raw: &str) -> SourceParagraph {
if let Some((font, body)) = nfss_font(raw) {
return SourceParagraph {
@@ -1283,12 +1348,20 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
space_skip: geometry.space_skip,
xspace_skip: geometry.xspace_skip,
};
let mut unit = Unit::paragraph_with_style_and_face(
index as u64,
paragraph.text.clone(),
style,
paragraph.font,
);
let mut unit = match inline_runs(&paragraph.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;
unit.space_shrink = paragraph.space_shrink;
@@ -1850,3 +1923,58 @@ mod nfss_tests {
assert_eq!(paragraph_font("\\texttt{code words}"), ParagraphFont::Mono);
}
}
#[cfg(test)]
mod inline_tests {
use super::*;
#[test]
fn inline_commands_split_runs() {
let runs = inline_runs("Plain \\textbf{bold} tail", ParagraphFont::Roman).expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Roman, "Plain ".to_string()),
(ParagraphFont::Bold, "bold".to_string()),
(ParagraphFont::Roman, " tail".to_string()),
]
);
}
#[test]
fn plain_paragraphs_have_no_runs() {
assert!(inline_runs("no commands here", ParagraphFont::Sans).is_none());
}
#[test]
fn base_font_fills_plain_segments() {
let runs = inline_runs("\\texttt{code} and text", ParagraphFont::Sans).expect("runs");
assert_eq!(runs[0], (ParagraphFont::Mono, "code".to_string()));
assert_eq!(runs[1], (ParagraphFont::Sans, " and text".to_string()));
}
#[test]
fn styled_runs_paint_with_their_face() {
let document = Document::new(
550 * 65_536,
vec![Unit::paragraph_with_style_and_runs(
1,
"plain bold",
ParagraphStyle::default(),
vec![
(ParagraphFont::Roman, "plain ".to_string()),
(ParagraphFont::Bold, "bold".to_string()),
],
)],
);
let engine = Engine::layout_paged(&document).expect("pages");
let list = engine.pages()[0].display_list_with_goal(document.page_goal);
assert!(list.items.iter().any(|item| matches!(
item,
texrra_tex_core::render::DisplayItem::Text {
font: texrra_tex_core::render::FontFace::Bold,
..
}
)));
}
}