Map fontspec families to engine faces
fontspec declarations were neither consumed nor mapped, so
\newfontfamily\headingfont{...} leaked into the text. Consume \setmainfont,
\setsansfont and \setmonofont (the engine's Roman/Sans/Mono faces stand in for
those choices) and turn \newfontfamily/\renewfontfamily into an alias macro that
switches to the closest NFSS family by name: mono/typewriter/courier to cmtt,
sans/helvetica/arial to cmss, otherwise cmr. Optional bracket options are
skipped.
A preview test declares all four commands, renders \headingfont Sans words. and
checks no warnings, the sans-serif face and no leaked declaration text. Docs
also record that complex scripts (Devanagari, Thai, emoji) still have no shaping
fallback and render as missing glyphs in the PDF.
This commit is contained in:
1 parent
81da180ab7
commit
619eb19f72
3 files changed
+72
-6
No files matched your search
@@ -2321,6 +2321,19 @@ fn expand_multirow(source: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// Map a fontspec family name to one of the engine's NFSS families.
|
||||
fn fontspec_family(name: &str) -> &'static str {
|
||||
let lowered = name.to_ascii_lowercase();
|
||||
if lowered.contains("mono") || lowered.contains("typewriter") || lowered.contains("courier") {
|
||||
"cmtt"
|
||||
} else if lowered.contains("sans") || lowered.contains("helvetica") || lowered.contains("arial")
|
||||
{
|
||||
"cmss"
|
||||
} else {
|
||||
"cmr"
|
||||
}
|
||||
}
|
||||
|
||||
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
// Full-width floats behave like their single-column counterparts here.
|
||||
let source = source
|
||||
@@ -2407,6 +2420,38 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = trimmed
|
||||
.strip_prefix("\\newfontfamily")
|
||||
.or_else(|| trimmed.strip_prefix("\\renewfontfamily"))
|
||||
{
|
||||
let rest = rest.trim_start();
|
||||
if let Some((name, used)) = control_sequence(rest) {
|
||||
let mut rest = rest[used..].trim_start();
|
||||
if let Some(stripped) = rest.strip_prefix('[')
|
||||
&& let Some((_options, tail)) = stripped.split_once(']')
|
||||
{
|
||||
rest = tail.trim_start();
|
||||
}
|
||||
if let Some((font, _)) = braced_group(rest) {
|
||||
geometry.macros.push(MacroDefinition {
|
||||
name,
|
||||
args: 0,
|
||||
default: None,
|
||||
params: None,
|
||||
body: format!("\\fontfamily{{{}}}\\selectfont", fontspec_family(font)),
|
||||
});
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with("\\setmainfont")
|
||||
|| trimmed.starts_with("\\setsansfont")
|
||||
|| trimmed.starts_with("\\setmonofont")
|
||||
{
|
||||
// The engine's Roman/Sans/Mono faces already stand in for the
|
||||
// document's chosen families; consume the declaration.
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with("\\newenvironment") || trimmed.starts_with("\\renewenvironment") {
|
||||
if let Some(definition) = environment_definition(trimmed) {
|
||||
geometry.environments.push(definition);
|
||||
@@ -15359,6 +15404,24 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fontspec_families_map_to_engine_faces() {
|
||||
let source = "\\documentclass{article}\n\\usepackage{fontspec}\n\\setmainfont{Liberation Serif}\n\\setsansfont{Liberation Sans}\n\\newfontfamily\\headingfont[Ligatures=TeX]{Liberation Sans}\n\\begin{document}\n\n\\headingfont Sans words.\n\n\\end{document}\n";
|
||||
let json = preview_json(source).expect("preview");
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||
assert_eq!(
|
||||
value["warnings"].as_array().map(Vec::len),
|
||||
Some(0),
|
||||
"{}",
|
||||
value["warnings"]
|
||||
);
|
||||
let svg = value["pages"][0]["svg"].as_str().expect("svg");
|
||||
assert!(svg.contains("sans-serif"), "{svg}");
|
||||
assert!(svg.contains(">S</text>"), "{svg}");
|
||||
assert!(!svg.contains("newfontfamily"), "{svg}");
|
||||
assert!(!svg.contains("setmainfont"), "{svg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_tables_keep_the_outer_structure() {
|
||||
let source = r"\begin{tabular}{ll}
|
||||
|
||||
Reference in new issue
Block a user