From df43cade623f0101b1526e5f3b41300a89c3f46c Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sat, 24 Oct 2026 02:04:14 +0800 Subject: [PATCH] Select built-in faces from NFSS declarations --- docs/ROUTE_D_PLAN.md | 2 +- docs/TEX_CORE.md | 4 +- src/route_d.rs | 132 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 3 deletions(-) diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index 5f54f02..6016d29 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -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`). 525 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 513 workspace tests are green and no oracle fixture is ignored. Status after G1 (P0-P2): the paragraph model reproduces TeX's passes, diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 53bba1c..2c9084f 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -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; true NFSS family switching remains 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; 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 525 tests (347 route-D unit, 18 engine binary unit, 15 engine unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 v1 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 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. ### M0 incremental demo diff --git a/src/route_d.rs b/src/route_d.rs index f7cb2fa..4a85d5a 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -925,7 +925,96 @@ fn take_braced_argument(text: &mut String, command: &str) -> Option { None } +/// 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, + } + 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") { + 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") { + text = rest; + } else if let Some(rest) = text.strip_prefix("\\selectfont") { + text = rest; + } else { + matched = false; + } + if !matched { + break; + } + consumed = true; + text = text.trim_start(); + } + if !consumed { + return None; + } + let font = match family { + Family::Mono => ParagraphFont::Mono, + Family::Sans => ParagraphFont::Sans, + Family::Roman => { + if bold { + ParagraphFont::Bold + } else if italic { + ParagraphFont::Italic + } else { + ParagraphFont::Roman + } + } + }; + Some((font, text.to_string())) +} + fn parse_paragraph(raw: &str) -> SourceParagraph { + if let Some((font, body)) = nfss_font(raw) { + return SourceParagraph { + text: body, + font, + line_height: 12, + math: false, + left_skip: 0, + centered: false, + right_aligned: false, + space_before: 0, + space_stretch: 0, + space_shrink: 0, + page_break: false, + equation_number: None, + equation_tag: None, + }; + } if let Some(inner) = raw .strip_prefix("\\begin{equation}") .and_then(|text| text.strip_suffix("\\end{equation}")) @@ -994,6 +1083,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph { ("\\section", ParagraphFont::Bold, 18), ("\\textbf", ParagraphFont::Bold, 12), ("\\emph", ParagraphFont::Italic, 12), + ("\\texttt", ParagraphFont::Mono, 12), + ("\\textsf", ParagraphFont::Sans, 12), ]; for (command, font, line_height) in candidates { if let Some(text) = command_body(raw, command) { @@ -1618,3 +1709,44 @@ d assert!(json.contains(">)"), "close tag parenthesis"); } } + +#[cfg(test)] +mod nfss_tests { + use super::*; + + fn paragraph_font(raw: &str) -> ParagraphFont { + paragraphs(raw).expect("paragraphs")[0].font + } + + #[test] + fn declarations_select_built_in_faces() { + assert_eq!(paragraph_font("\\sffamily Sans text"), ParagraphFont::Sans); + assert_eq!(paragraph_font("\\ttfamily Mono text"), ParagraphFont::Mono); + assert_eq!(paragraph_font("\\bfseries Bold text"), ParagraphFont::Bold); + assert_eq!( + paragraph_font("\\itshape Italic text"), + ParagraphFont::Italic + ); + assert_eq!( + paragraph_font("\\sffamily\\bfseries Bold sans"), + ParagraphFont::Sans + ); + assert_eq!( + paragraph_font("\\rmfamily\\mdseries\\upshape Roman"), + ParagraphFont::Roman + ); + } + + #[test] + fn declarations_do_not_leak_into_the_text() { + let blocks = paragraphs("\\sffamily\\bfseries Fancy text").expect("paragraphs"); + assert_eq!(blocks[0].text, "Fancy text"); + } + + #[test] + fn texttt_and_textsf_select_faces() { + assert_eq!(paragraph_font("\\texttt{code}"), ParagraphFont::Mono); + assert_eq!(paragraph_font("\\textsf{sans}"), ParagraphFont::Sans); + assert_eq!(paragraph_font("\\texttt{code words}"), ParagraphFont::Mono); + } +}