Scope NFSS declarations to the rest of their group

A bare \bfseries or \em in a paragraph now switches the face from that point to the end of the enclosing group, instead of being emitted as literal text. Inline declarations recurse into the remaining body so nested groups keep their own scope.
This commit is contained in:
gloamxun committed 2026-10-24 12:55:13 +08:00
1 parent e303f7852f
commit 6df38ced25
4 files changed
+56 -4

No files matched your search

+52
View File
@@ -1066,6 +1066,14 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
italic = false;
small_caps = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\em") {
if small_caps {
small_caps = false;
italic = true;
} else {
italic = !italic;
}
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\rm") {
family = Family::Roman;
text = rest;
@@ -1256,6 +1264,11 @@ fn parse_runs(
let mut index = 0;
while index < text.len() {
let rest = &text[index..];
if let Some((declared, body)) = nfss_font(rest, font) {
*found = true;
parse_runs(&body, declared, runs, found);
return;
}
if let Some((inner, length)) = braced_group(rest) {
let (inner_font, inner_text) = match nfss_font(inner, font) {
Some((declared, body)) => (declared, body),
@@ -2319,6 +2332,45 @@ mod inline_tests {
assert_eq!(runs[1], (ParagraphFont::Sans, " and text".to_string()));
}
#[test]
fn declarations_switch_the_following_text() {
let runs = inline_runs("plain \\bfseries bold", ParagraphFont::Roman).expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Roman, "plain ".to_string()),
(ParagraphFont::Bold, "bold".to_string()),
]
);
}
#[test]
fn mid_paragraph_declarations_stay_inside_their_group() {
let runs = inline_runs("a {\\bfseries b} c", ParagraphFont::Roman).expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Roman, "a ".to_string()),
(ParagraphFont::Bold, "b".to_string()),
(ParagraphFont::Roman, " c".to_string()),
]
);
}
#[test]
fn em_toggles_emphasis() {
let runs = inline_runs("a \\em b", ParagraphFont::Roman).expect("runs");
assert_eq!(runs[0], (ParagraphFont::Roman, "a ".to_string()));
assert_eq!(runs[1].0, ParagraphFont::Italic);
assert_eq!(runs[1].1, "b");
let runs = inline_runs("a {\\bfseries b \\em c}", ParagraphFont::Roman).expect("runs");
assert!(
runs.iter()
.any(|(font, _)| *font == ParagraphFont::BoldItalic)
);
assert!(inline_runs("\\emph{x}", ParagraphFont::Roman).is_some());
}
#[test]
fn declaration_groups_switch_the_face() {
let runs = inline_runs("{\\bfseries bold} normal", ParagraphFont::Roman).expect("runs");