Select built-in faces from NFSS declarations

This commit is contained in:
gloamxun committed 2026-10-24 02:04:14 +08:00
1 parent f6726439ed
commit df43cade62
3 files changed
+135 -3

No files matched your search

+132
View File
@@ -925,7 +925,96 @@ fn take_braced_argument(text: &mut String, command: &str) -> Option<String> {
None
}
/// Paragraph-level NFSS declarations: family, series and shape prefixes such
/// as `{\sffamily\bfseries ...}` are folded into the built-in faces.
fn nfss_font(raw: &str) -> Option<(ParagraphFont, String)> {
#[derive(Clone, Copy, PartialEq, Eq)]
enum Family {
Roman,
Sans,
Mono,
}
let mut family = Family::Roman;
let mut bold = false;
let mut italic = false;
let mut text = raw.trim_start();
let mut consumed = false;
loop {
let mut matched = true;
if let Some(rest) = text.strip_prefix("\\rmfamily") {
family = Family::Roman;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\sffamily") {
family = Family::Sans;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\ttfamily") {
family = Family::Mono;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\bfseries") {
bold = true;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\mdseries") {
bold = false;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\itshape") {
italic = true;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\slshape") {
italic = true;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\upshape") {
italic = false;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\scshape") {
text = rest;
} else if let Some(rest) = text.strip_prefix("\\selectfont") {
text = rest;
} else {
matched = false;
}
if !matched {
break;
}
consumed = true;
text = text.trim_start();
}
if !consumed {
return None;
}
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
}
}
};
Some((font, text.to_string()))
}
fn parse_paragraph(raw: &str) -> SourceParagraph {
if let Some((font, body)) = nfss_font(raw) {
return SourceParagraph {
text: body,
font,
line_height: 12,
math: false,
left_skip: 0,
centered: false,
right_aligned: false,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
};
}
if let Some(inner) = raw
.strip_prefix("\\begin{equation}")
.and_then(|text| text.strip_suffix("\\end{equation}"))
@@ -994,6 +1083,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
("\\section", ParagraphFont::Bold, 18),
("\\textbf", ParagraphFont::Bold, 12),
("\\emph", ParagraphFont::Italic, 12),
("\\texttt", ParagraphFont::Mono, 12),
("\\textsf", ParagraphFont::Sans, 12),
];
for (command, font, line_height) in candidates {
if let Some(text) = command_body(raw, command) {
@@ -1618,3 +1709,44 @@ d
assert!(json.contains(">)</text>"), "close tag parenthesis");
}
}
#[cfg(test)]
mod nfss_tests {
use super::*;
fn paragraph_font(raw: &str) -> ParagraphFont {
paragraphs(raw).expect("paragraphs")[0].font
}
#[test]
fn declarations_select_built_in_faces() {
assert_eq!(paragraph_font("\\sffamily Sans text"), ParagraphFont::Sans);
assert_eq!(paragraph_font("\\ttfamily Mono text"), ParagraphFont::Mono);
assert_eq!(paragraph_font("\\bfseries Bold text"), ParagraphFont::Bold);
assert_eq!(
paragraph_font("\\itshape Italic text"),
ParagraphFont::Italic
);
assert_eq!(
paragraph_font("\\sffamily\\bfseries Bold sans"),
ParagraphFont::Sans
);
assert_eq!(
paragraph_font("\\rmfamily\\mdseries\\upshape Roman"),
ParagraphFont::Roman
);
}
#[test]
fn declarations_do_not_leak_into_the_text() {
let blocks = paragraphs("\\sffamily\\bfseries Fancy text").expect("paragraphs");
assert_eq!(blocks[0].text, "Fancy text");
}
#[test]
fn texttt_and_textsf_select_faces() {
assert_eq!(paragraph_font("\\texttt{code}"), ParagraphFont::Mono);
assert_eq!(paragraph_font("\\textsf{sans}"), ParagraphFont::Sans);
assert_eq!(paragraph_font("\\texttt{code words}"), ParagraphFont::Mono);
}
}