Support NFSS command forms and legacy declarations

This commit is contained in:
gloamxun committed 2026-10-24 02:06:44 +08:00
1 parent df43cade62
commit 67898d6a8a
3 files changed
+135 -35

No files matched your search

+132 -32
View File
@@ -925,54 +925,125 @@ fn take_braced_argument(text: &mut String, command: &str) -> Option<String> {
None
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Family {
Roman,
Sans,
Mono,
}
/// 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,
fn nfss_family_name(name: &str) -> Family {
let lower = name.to_ascii_lowercase();
if lower.starts_with("cmss") || lower.starts_with("lmss") || lower.starts_with("phv") {
Family::Sans
} else if lower.starts_with("cmtt") || lower.starts_with("lmtt") || lower.starts_with("pcr") {
Family::Mono
} else {
Family::Roman
}
}
fn nfss_braced<'a>(text: &'a str, command: &str) -> Option<(String, &'a str)> {
let rest = text.strip_prefix(command)?;
let rest = rest.trim_start().strip_prefix('{')?;
let end = rest.find('}')?;
Some((rest[..end].to_string(), rest[end + 1..].trim_start()))
}
fn nfss_declaration<'a>(text: &'a str, command: &str) -> Option<&'a str> {
let rest = text.strip_prefix(command)?;
if rest
.chars()
.next()
.is_some_and(|character| character.is_ascii_alphabetic())
{
return None;
}
Some(rest)
}
fn nfss_font(raw: &str) -> Option<(ParagraphFont, String)> {
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") {
if let Some((name, rest)) = nfss_braced(text, "\\fontfamily") {
family = nfss_family_name(&name);
text = rest;
} else if let Some((name, rest)) = nfss_braced(text, "\\fontseries") {
bold = name.contains('b')
|| name.contains("bx")
|| name.contains('s') && name.contains('b');
text = rest;
} else if let Some((name, rest)) = nfss_braced(text, "\\fontshape") {
italic = name == "it" || name == "sl";
text = rest;
} else if let Some(rest) = nfss_braced(text, "\\fontencoding").map(|(_, rest)| rest) {
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\normalfont") {
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") {
} else if let Some(rest) = nfss_declaration(text, "\\rm") {
family = Family::Roman;
text = rest;
} else if let Some(rest) = text.strip_prefix("\\selectfont") {
} else if let Some(rest) = nfss_declaration(text, "\\sf") {
family = Family::Sans;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\tt") {
family = Family::Mono;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\bf") {
bold = true;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\md") {
bold = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\it") {
italic = true;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\sl") {
italic = true;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\up") {
italic = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\sc") {
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\rmfamily") {
family = Family::Roman;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\sffamily") {
family = Family::Sans;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\ttfamily") {
family = Family::Mono;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\bfseries") {
bold = true;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\mdseries") {
bold = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\itshape") {
italic = true;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\slshape") {
italic = true;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\upshape") {
italic = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\scshape") {
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\selectfont") {
text = rest;
} else {
matched = false;
}
if !matched {
break;
}
consumed = true;
@@ -1743,6 +1814,35 @@ mod nfss_tests {
assert_eq!(blocks[0].text, "Fancy text");
}
#[test]
fn nfss_commands_configure_the_face() {
assert_eq!(
paragraph_font("\\fontfamily{cmss}\\selectfont Sans"),
ParagraphFont::Sans
);
assert_eq!(
paragraph_font("\\fontseries{bx}\\selectfont Bold"),
ParagraphFont::Bold
);
assert_eq!(
paragraph_font("\\fontshape{it}\\selectfont Italic"),
ParagraphFont::Italic
);
assert_eq!(
paragraph_font("\\fontencoding{T1}\\selectfont Roman"),
ParagraphFont::Roman
);
assert_eq!(paragraph_font("\\normalfont Reset"), ParagraphFont::Roman);
}
#[test]
fn legacy_declarations_stay_bounded() {
assert_eq!(paragraph_font("\\tt Legacy mono"), ParagraphFont::Mono);
assert_eq!(paragraph_font("\\bf Legacy bold"), ParagraphFont::Bold);
let blocks = paragraphs("\\item bullet").expect("paragraphs");
assert_eq!(blocks[0].text, "\\item bullet");
}
#[test]
fn texttt_and_textsf_select_faces() {
assert_eq!(paragraph_font("\\texttt{code}"), ParagraphFont::Mono);