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:
gloamxun committed 2026-10-27 00:41:36 +08:00
1 parent 81da180ab7
commit 619eb19f72
3 files changed
+72 -6

No files matched your search

+1 -1
View File
@@ -49,7 +49,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
PDF opt-in with `?engine=route-d`; flat `/preview` and project previews now
prefer route-D with a v1 fallback). 994 workspace tests are green and no oracle
prefer route-D with a v1 fallback). 995 workspace tests are green and no oracle
fixture is ignored. Preview JSON also carries a `warnings` array naming
environments route-D cannot lay out and control words that survived layout, so
unsupported source is reported instead of being silently typeset.
+8 -5
View File
@@ -370,7 +370,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address; those fallback glyphs keep their Unicode text under PDF extraction, so accented and Greek words round-trip; Type1 faces carry a ToUnicode CMap built from the requested characters, so glyphs whose names are ambiguous in the Adobe Glyph List (capital Omega, which would otherwise extract as U+2126) keep their intended code points. `Engine::edit` preserves the face when reflowing an edited paragraph.
The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 994 tests (437 route-D unit, 367 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 62 server, 4 engine unit, 1 core unit), with none ignored. The flat `/preview` endpoint and the project preview/PDF endpoints now try route-D first and fall back to v1 on error (`?engine=v1` or `TEXRRA_ENGINE=v1` forces v1; the flat response carries `X-Preview-Engine`, naming the engine used). A geometry-less project now defaults to Letter, matching pdfLaTeX, where the old v1 path used A4.
The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 995 tests (437 route-D unit, 368 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 62 server, 4 engine unit, 1 core unit), with none ignored. The flat `/preview` endpoint and the project preview/PDF endpoints now try route-D first and fall back to v1 on error (`?engine=v1` or `TEXRRA_ENGINE=v1` forces v1; the flat response carries `X-Preview-Engine`, naming the engine used). A geometry-less project now defaults to Letter, matching pdfLaTeX, where the old v1 path used A4.
### M0 incremental demo
@@ -559,10 +559,13 @@ are:
table are not merged into the outer display list, and `\valign` remains
unmodelled.
- **Fonts**: the twelve Computer Modern faces and four math faces are embedded
as Type1 with per-glyph Liberation fallback. Greek math commands now map to
their Unicode letters and render through the math-italic fallback (widths
approximate the TFM metrics); arbitrary NFSS families and Unicode coverage
beyond Greek and Latin accents remain open.
as Type1 with per-glyph Liberation fallback. Greek math commands map to their
Unicode letters and render through the math-italic fallback (widths
approximate the TFM metrics). fontspec's `\setmainfont`/`\setsansfont`/
`\setmonofont` are consumed, and `\newfontfamily` maps a family to the
closest engine face by name (sans/mono/serif). Complex scripts such as
Devanagari, Thai and emoji have no shaping fallback and render as missing
glyphs; arbitrary external font families remain open.
- **Inline runs**: colour/link/sized runs split at any base size; inline math
still falls back to literal delimiters when the paragraph's base size is not
10pt, because math metrics are measured at 10pt.
+63
View File
@@ -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}