Compose nested inline faces relative to the surrounding run

This commit is contained in:
gloamxun committed 2026-10-24 02:15:08 +08:00
1 parent 0e8999671b
commit 4cf64a4d55
6 files changed
+156 -38

No files matched your search

+93 -35
View File
@@ -964,10 +964,15 @@ fn nfss_declaration<'a>(text: &'a str, command: &str) -> Option<&'a str> {
Some(rest)
}
fn nfss_font(raw: &str) -> Option<(ParagraphFont, String)> {
let mut family = Family::Roman;
let mut bold = false;
let mut italic = false;
fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)> {
let (mut family, mut bold, mut italic) = match base {
ParagraphFont::Roman => (Family::Roman, false, false),
ParagraphFont::Bold => (Family::Roman, true, false),
ParagraphFont::Italic => (Family::Roman, false, true),
ParagraphFont::BoldItalic => (Family::Roman, true, true),
ParagraphFont::Mono => (Family::Mono, false, false),
ParagraphFont::Sans => (Family::Sans, false, false),
};
let mut text = raw.trim_start();
let mut consumed = false;
loop {
@@ -1055,15 +1060,12 @@ fn nfss_font(raw: &str) -> Option<(ParagraphFont, String)> {
let font = match family {
Family::Mono => ParagraphFont::Mono,
Family::Sans => ParagraphFont::Sans,
Family::Roman => {
if bold {
ParagraphFont::Bold
} else if italic {
ParagraphFont::Italic
} else {
ParagraphFont::Roman
}
}
Family::Roman => match (bold, italic) {
(true, true) => ParagraphFont::BoldItalic,
(true, false) => ParagraphFont::Bold,
(false, true) => ParagraphFont::Italic,
(false, false) => ParagraphFont::Roman,
},
};
Some((font, text.to_string()))
}
@@ -1118,27 +1120,39 @@ fn braced_group(rest: &str) -> Option<(&str, usize)> {
None
}
const INLINE_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),
const INLINE_COMMANDS: [&str; 11] = [
"\\textbf",
"\\emph",
"\\textit",
"\\textsl",
"\\texttt",
"\\textsf",
"\\textmd",
"\\textnormal",
"\\textup",
"\\textrm",
"\\textsc",
];
/// Match a styled wrapper command at the start of `rest`.
fn inline_wrapper(rest: &str) -> Option<(Option<ParagraphFont>, &str, usize)> {
for (command, font) in INLINE_COMMANDS {
/// Match a styled wrapper command at the start of `rest`, composing its face
/// with the surrounding one (`\\emph` toggles italic inside a bold run, etc.).
fn inline_wrapper(rest: &str, base: ParagraphFont) -> Option<(ParagraphFont, &str, usize)> {
for command in INLINE_COMMANDS {
let Some(after) = rest.strip_prefix(command) else {
continue;
};
if let Some((inner, length)) = braced_group(after) {
let font = match command {
"\\textbf" => base.bolded(),
"\\emph" => base.emphasized(),
"\\textit" | "\\textsl" => base.italicized(),
"\\texttt" => ParagraphFont::Mono,
"\\textsf" => ParagraphFont::Sans,
"\\textmd" => base.medium(),
"\\textup" => base.upright(),
"\\textrm" => base.roman_family(),
"\\textnormal" => ParagraphFont::Roman,
_ => base,
};
return Some((font, inner, command.len() + length));
}
}
@@ -1155,20 +1169,20 @@ fn parse_runs(
while index < text.len() {
let rest = &text[index..];
if let Some((inner, length)) = braced_group(rest) {
let (inner_font, inner_text) = match nfss_font(inner) {
let (inner_font, inner_text) = match nfss_font(inner, font) {
Some((declared, body)) => (declared, body),
None => (font, inner.to_string()),
};
if nfss_font(inner).is_some() {
if nfss_font(inner, font).is_some() {
*found = true;
}
parse_runs(&inner_text, inner_font, runs, found);
index += length;
continue;
}
if let Some((wrapper_font, inner, length)) = inline_wrapper(rest) {
if let Some((wrapper_font, inner, length)) = inline_wrapper(rest, font) {
*found = true;
parse_runs(inner, wrapper_font.unwrap_or(font), runs, found);
parse_runs(inner, wrapper_font, runs, found);
index += length;
continue;
}
@@ -1195,7 +1209,7 @@ fn parse_runs(
}
fn parse_paragraph(raw: &str) -> SourceParagraph {
if let Some((font, body)) = nfss_font(raw) {
if let Some((font, body)) = nfss_font(raw, ParagraphFont::Roman) {
return SourceParagraph {
text: body,
font,
@@ -2038,13 +2052,57 @@ mod inline_tests {
runs,
vec![
(ParagraphFont::Bold, "bold ".to_string()),
(ParagraphFont::Italic, "italic".to_string()),
(ParagraphFont::BoldItalic, "italic".to_string()),
(ParagraphFont::Bold, " bold".to_string()),
(ParagraphFont::Roman, " plain".to_string()),
]
);
}
#[test]
fn emph_inside_bold_is_bold_italic() {
let runs = inline_runs(
"{\\bfseries bold \\emph{bold italic} bold}",
ParagraphFont::Roman,
)
.expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Bold, "bold ".to_string()),
(ParagraphFont::BoldItalic, "bold italic".to_string()),
(ParagraphFont::Bold, " bold".to_string()),
]
);
}
#[test]
fn emph_inside_italic_returns_upright() {
let runs = inline_runs("{\\itshape it \\emph{up} it}", ParagraphFont::Roman).expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Italic, "it ".to_string()),
(ParagraphFont::Roman, "up".to_string()),
(ParagraphFont::Italic, " it".to_string()),
]
);
}
#[test]
fn textmd_inside_bold_drops_weight() {
let runs =
inline_runs("\\textbf{a \\textmd{plain} b}", ParagraphFont::Roman).expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Bold, "a ".to_string()),
(ParagraphFont::Roman, "plain".to_string()),
(ParagraphFont::Bold, " b".to_string()),
]
);
}
#[test]
fn wrapper_groups_keep_nested_braces() {
let runs = inline_runs("\\textbf{a {b} c}", ParagraphFont::Roman).expect("runs");