Distinguish slanted from italic and add italic typewriter

Add cmsl10/cmbxsl10/cmitt10 faces so \slshape and \textsl no longer reuse the italic cmti10 and \ttfamily\itshape reaches cmitt10. The NFSS state tracks italic, slanted and small caps separately, with last-shape-wins semantics for \em and the shape commands.
This commit is contained in:
gloamxun committed 2026-10-24 12:58:41 +08:00
1 parent 6df38ced25
commit aa4d86d735
15 files changed
+328 -34

No files matched your search

+92 -21
View File
@@ -1041,8 +1041,12 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
ParagraphFont::SansBold => (Family::Sans, true, false),
ParagraphFont::SansItalic => (Family::Sans, false, true),
ParagraphFont::SmallCaps => (Family::Roman, false, false),
ParagraphFont::Slanted => (Family::Roman, false, false),
ParagraphFont::BoldSlanted => (Family::Roman, true, false),
ParagraphFont::MonoItalic => (Family::Mono, false, true),
};
let mut small_caps = base == ParagraphFont::SmallCaps;
let mut slanted = matches!(base, ParagraphFont::Slanted | ParagraphFont::BoldSlanted);
let mut text = raw.trim_start();
let mut consumed = false;
loop {
@@ -1055,7 +1059,8 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
|| name.contains('s') && name.contains('b');
text = rest;
} else if let Some((name, rest)) = nfss_braced(text, "\\fontshape") {
italic = name == "it" || name == "sl";
italic = name == "it";
slanted = name == "sl";
small_caps = name == "sc";
text = rest;
} else if let Some(rest) = nfss_braced(text, "\\fontencoding").map(|(_, rest)| rest) {
@@ -1064,14 +1069,17 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
family = Family::Roman;
bold = false;
italic = false;
slanted = false;
small_caps = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\em") {
if small_caps {
small_caps = false;
italic = true;
if italic || slanted {
italic = false;
slanted = false;
} else {
italic = !italic;
italic = true;
slanted = false;
small_caps = false;
}
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\rm") {
@@ -1091,19 +1099,23 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\it") {
italic = true;
slanted = false;
small_caps = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\sl") {
italic = true;
italic = false;
slanted = true;
small_caps = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\up") {
italic = false;
slanted = false;
small_caps = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\sc") {
small_caps = true;
italic = false;
slanted = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\rmfamily") {
family = Family::Roman;
@@ -1122,19 +1134,23 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\itshape") {
italic = true;
slanted = false;
small_caps = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\slshape") {
italic = true;
italic = false;
slanted = true;
small_caps = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\upshape") {
italic = false;
slanted = false;
small_caps = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\scshape") {
small_caps = true;
italic = false;
slanted = false;
text = rest;
} else if let Some(rest) = nfss_declaration(text, "\\selectfont") {
text = rest;
@@ -1148,19 +1164,43 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
return None;
}
let font = match family {
Family::Mono => ParagraphFont::Mono,
Family::Sans => match (bold, italic) {
(true, _) => ParagraphFont::SansBold,
(false, true) => ParagraphFont::SansItalic,
(false, false) => ParagraphFont::Sans,
},
Family::Roman => match (bold, italic, small_caps) {
(_, true, _) if bold => ParagraphFont::BoldItalic,
(_, true, _) => ParagraphFont::Italic,
(true, false, _) => ParagraphFont::Bold,
(false, false, true) => ParagraphFont::SmallCaps,
(false, false, false) => ParagraphFont::Roman,
},
Family::Mono => {
if italic || slanted {
ParagraphFont::MonoItalic
} else {
ParagraphFont::Mono
}
}
Family::Sans => {
if bold {
ParagraphFont::SansBold
} else if italic || slanted {
ParagraphFont::SansItalic
} else {
ParagraphFont::Sans
}
}
Family::Roman => {
if italic {
if bold {
ParagraphFont::BoldItalic
} else {
ParagraphFont::Italic
}
} else if slanted {
if bold {
ParagraphFont::BoldSlanted
} else {
ParagraphFont::Slanted
}
} else if bold {
ParagraphFont::Bold
} else if small_caps {
ParagraphFont::SmallCaps
} else {
ParagraphFont::Roman
}
}
};
Some((font, text.to_string()))
}
@@ -1239,7 +1279,8 @@ fn inline_wrapper(rest: &str, base: ParagraphFont) -> Option<(ParagraphFont, &st
let font = match command {
"\\textbf" => base.bolded(),
"\\emph" => base.emphasized(),
"\\textit" | "\\textsl" => base.italicized(),
"\\textit" => base.italicized(),
"\\textsl" => base.slanted(),
"\\texttt" => ParagraphFont::Mono,
"\\textsf" => base.sans_family(),
"\\textmd" => base.medium(),
@@ -2273,6 +2314,28 @@ mod nfss_tests {
assert_eq!(paragraph_font("\\normalfont Reset"), ParagraphFont::Roman);
}
#[test]
fn slanted_and_italic_shapes_stay_distinct() {
assert_eq!(paragraph_font("\\slshape slanted"), ParagraphFont::Slanted);
assert_eq!(paragraph_font("\\itshape italic"), ParagraphFont::Italic);
assert_eq!(
paragraph_font("\\bfseries\\slshape bold slanted"),
ParagraphFont::BoldSlanted
);
assert_eq!(
paragraph_font("\\ttfamily\\itshape tt italic"),
ParagraphFont::MonoItalic
);
assert_eq!(
paragraph_font("\\slshape\\itshape italic"),
ParagraphFont::Italic
);
assert_eq!(
paragraph_font("\\ttfamily\\slshape tt slanted"),
ParagraphFont::MonoItalic
);
}
#[test]
fn legacy_declarations_stay_bounded() {
assert_eq!(paragraph_font("\\tt Legacy mono"), ParagraphFont::Mono);
@@ -2306,6 +2369,14 @@ mod inline_tests {
);
}
#[test]
fn textsl_selects_the_slanted_shape() {
let runs = inline_runs("\\textsl{slanted}", ParagraphFont::Roman).expect("runs");
assert_eq!(runs[0], (ParagraphFont::Slanted, "slanted".to_string()));
let runs = inline_runs("\\textbf{\\textsl{slanted}}", ParagraphFont::Roman).expect("runs");
assert_eq!(runs[0], (ParagraphFont::BoldSlanted, "slanted".to_string()));
}
#[test]
fn textsc_composes_with_the_surrounding_face() {
let runs = inline_runs("\\textsc{small}", ParagraphFont::Roman).expect("runs");