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

+1 -1
View File
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
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`). 513 workspace tests are green and no oracle
PDF opt-in with `?engine=route-d`). 515 workspace tests are green and no oracle
fixture is ignored.
Status after G1 (P0-P2): the paragraph model reproduces TeX's passes,
+2 -2
View File
@@ -353,7 +353,7 @@ M3 painter: `Page::display_list()` and `Halign::display_list()` produce absolute
PDF writer: `crates/texrra-tex-core/src/pdf.rs` writes multi-page PDF 1.4 with five embedded Liberation Type0/CID faces (Identity-H + ToUnicode + `/W` arrays), filled rectangles, diagonal rules, raw-RGB `/XObject` images, `/Annots` URI links and arbitrary `/special` operators. Ligature substitution and line-end italic correction are implemented; full NFSS family switching remains open.
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; paragraph-level NFSS declarations (`\rmfamily`, `\sffamily`, `\ttfamily`, `\bfseries`, `\itshape`, `\texttt`, `\textsf`) select those faces; true family/series/shape switching and Type1/CM outlines remain open. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; each glyph box also carries the TFM height/depth, and the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, and ligature boxes paint their component letters, so `pdftotext` keeps word boundaries instead of inserting spaces between letters.
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; paragraph-level NFSS declarations (`\rmfamily`, `\sffamily`, `\ttfamily`, `\bfseries`, `\itshape`, `\texttt`, `\textsf`) select those faces and the `\fontfamily`/`\fontseries`/`\fontshape`/`\fontencoding`/`\selectfont` command forms plus legacy `\rm`/`\sf`/`\tt`/`\bf`/`\it`/`\sl`/`\up`/`\normalfont` declarations are parsed; true family/series/shape switching and Type1/CM outlines remain open. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; each glyph box also carries the TFM height/depth, and the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, and ligature boxes paint their component letters, so `pdftotext` keeps word boundaries instead of inserting spaces between letters.
M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments.
@@ -367,7 +367,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`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
The crate currently has 347 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 513 tests (347 route-D unit, 21 engine binary unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
The crate currently has 347 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 515 tests (347 route-D unit, 23 engine binary unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
### M0 incremental demo
+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);