Support the usefont NFSS form

\usefont{encoding}{family}{series}{shape} was unsupported, so a class using the
low-level font selection left its arguments in the text. The inline NFSS parser
now recognizes the four braced groups, maps the family code through the same
cmss/lmss/phv/pcr/cmtt table as \fontfamily, and sets bold/italic/slanted/small
caps from the series and shape.

The existing nfss test gains an assertion that
\usefont{T1}{cmss}{b}{n} selects SansBold.
This commit is contained in:
gloamxun committed 2026-10-27 03:36:08 +08:00
1 parent c3aa4e9dad
commit f5ddad30a2
2 files changed
+32 -1

No files matched your search

+31
View File
@@ -9307,6 +9307,26 @@ enum Family {
/// Paragraph-level NFSS declarations: family, series and shape prefixes such
/// as `{\sffamily\bfseries ...}` are folded into the built-in faces.
/// Parse `\usefont{encoding}{family}{series}{shape}` and return the family,
/// series and shape codes plus the remaining text.
fn usefont_command(text: &str) -> Option<(String, String, String, &str)> {
let rest = text.strip_prefix("\\usefont")?.trim_start();
let (_, used) = braced_group(rest)?;
let rest = rest[used..].trim_start();
let (family, used) = braced_group(rest)?;
let rest = rest[used..].trim_start();
let (series, used) = braced_group(rest)?;
let rest = rest[used..].trim_start();
let (shape, used) = braced_group(rest)?;
let rest = &rest[used..];
Some((
family.trim().to_string(),
series.trim().to_string(),
shape.trim().to_string(),
rest,
))
}
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") {
@@ -9406,6 +9426,13 @@ fn nfss_font_with_size(
if let Some((size, rest)) = font_size_command(text) {
font_size = Some(size);
text = rest;
} else if let Some((code, series, shape, rest)) = usefont_command(text) {
family = nfss_family_name(&code);
bold = series.contains('b');
italic = shape == "it";
slanted = shape == "sl";
small_caps = shape == "sc";
text = rest;
} else if let Some((name, rest)) = nfss_braced(text, "\\fontfamily") {
family = nfss_family_name(&name);
text = rest;
@@ -20334,6 +20361,10 @@ mod nfss_tests {
paragraph_font("\\fontfamily{cmss}\\selectfont Sans"),
ParagraphFont::Sans
);
assert_eq!(
paragraph_font("\\usefont{T1}{cmss}{b}{n} Sans bold"),
ParagraphFont::SansBold
);
assert_eq!(
paragraph_font("\\fontseries{bx}\\selectfont Bold"),
ParagraphFont::Bold