From f5ddad30a2eeac01e322447ff133bec9542d2c10 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 27 Oct 2026 03:36:08 +0800 Subject: [PATCH] Support the usefont NFSS form \usefont{encoding}{family}{series}{shape} was unsupported, so a class using the low-level font selection left its arguments in the text. The inline NFSS parser now recognizes the four braced groups, maps the family code through the same cmss/lmss/phv/pcr/cmtt table as \fontfamily, and sets bold/italic/slanted/small caps from the series and shape. The existing nfss test gains an assertion that \usefont{T1}{cmss}{b}{n} selects SansBold. --- docs/TEX_CORE.md | 2 +- src/route_d.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 96faadd..a095bad 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -356,7 +356,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,MonoItalic,Sans,SansBold,SansItalic,SmallCaps,Slanted,BoldSlanted}` plus the math faces. 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; inline `\textbf`/`\emph`/`\textit`/`\texttt`/`\textsf` groups and `{\bfseries ...}` declaration groups (including nesting) split a paragraph into styled runs, each shaped by its own TFM and painted with its own face; nested faces compose relatively (`\emph` inside bold yields bold italic via cmbxti10, `\emph` inside italic returns upright, `\sffamily\bfseries` yields cmssbx10 and `\sffamily\itshape` cmssi10, while bold-italic sans falls back to cmssbx10 as TeX's OT1 substitution does; `\scshape`/`\textsc` select cmcsc10, with the last shape command winning and bold small caps substituting cmbx10; `\slshape`/`\textsl` select cmsl10 (or cmbxsl10 with a bold series) instead of the italic cmti10, and `\ttfamily\itshape` selects cmitt10; declarations such as `\bfseries` also switch the face for the rest of their group, and `\em` toggles emphasis relative to the current run); styled runs are stored on the paragraph unit, so incremental edits rebuild them through `Engine::edit_runs` instead of flattening the paragraph back to one face; true family/series/shape switching remains open; route-D PDFs now embed all twelve Knuth Computer Modern Type1 programs (cmr10, cmbx10, cmti10, cmbxti10, cmtt10, cmss10, cmssbx10, cmssi10, cmcsc10, cmsl10, cmbxsl10, cmitt10) as simple fonts (PFB `Length1/2/3`, `/Encoding /Differences` from each program's built-in encoding, `/Widths` in 1000-unit text space from that face's TFM advances) for the characters the program encodes and a Liberation Type0 subset for the uncovered ones on the same page; `pdffonts` reports `Type 1` faces (CMR10, CMBX10, ...) and `pdftotext` still extracts the text. `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 glyphs at the box's own size when a mid-paragraph size command split the run (otherwise the paragraph's base size) (`\fontsize` and `\tiny`...`\Huge` scale the shaped metrics and default interline glue, while `\hsize`, `\parindent` and authored skips stay absolute) 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,MonoItalic,Sans,SansBold,SansItalic,SmallCaps,Slanted,BoldSlanted}` plus the math faces. 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 and `\usefont{encoding}{family}{series}{shape}` plus legacy `\rm`/`\sf`/`\tt`/`\bf`/`\it`/`\sl`/`\up`/`\normalfont` declarations are parsed; inline `\textbf`/`\emph`/`\textit`/`\texttt`/`\textsf` groups and `{\bfseries ...}` declaration groups (including nesting) split a paragraph into styled runs, each shaped by its own TFM and painted with its own face; nested faces compose relatively (`\emph` inside bold yields bold italic via cmbxti10, `\emph` inside italic returns upright, `\sffamily\bfseries` yields cmssbx10 and `\sffamily\itshape` cmssi10, while bold-italic sans falls back to cmssbx10 as TeX's OT1 substitution does; `\scshape`/`\textsc` select cmcsc10, with the last shape command winning and bold small caps substituting cmbx10; `\slshape`/`\textsl` select cmsl10 (or cmbxsl10 with a bold series) instead of the italic cmti10, and `\ttfamily\itshape` selects cmitt10; declarations such as `\bfseries` also switch the face for the rest of their group, and `\em` toggles emphasis relative to the current run); styled runs are stored on the paragraph unit, so incremental edits rebuild them through `Engine::edit_runs` instead of flattening the paragraph back to one face; true family/series/shape switching remains open; route-D PDFs now embed all twelve Knuth Computer Modern Type1 programs (cmr10, cmbx10, cmti10, cmbxti10, cmtt10, cmss10, cmssbx10, cmssi10, cmcsc10, cmsl10, cmbxsl10, cmitt10) as simple fonts (PFB `Length1/2/3`, `/Encoding /Differences` from each program's built-in encoding, `/Widths` in 1000-unit text space from that face's TFM advances) for the characters the program encodes and a Liberation Type0 subset for the uncovered ones on the same page; `pdffonts` reports `Type 1` faces (CMR10, CMBX10, ...) and `pdftotext` still extracts the text. `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 glyphs at the box's own size when a mid-paragraph size command split the run (otherwise the paragraph's base size) (`\fontsize` and `\tiny`...`\Huge` scale the shaped metrics and default interline glue, while `\hsize`, `\parindent` and authored skips stay absolute) 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. diff --git a/src/route_d.rs b/src/route_d.rs index 30249d7..3ff9c75 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -9307,6 +9307,26 @@ enum Family { /// Paragraph-level NFSS declarations: family, series and shape prefixes such /// as `{\sffamily\bfseries ...}` are folded into the built-in faces. +/// Parse `\usefont{encoding}{family}{series}{shape}` and return the family, +/// series and shape codes plus the remaining text. +fn usefont_command(text: &str) -> Option<(String, String, String, &str)> { + let rest = text.strip_prefix("\\usefont")?.trim_start(); + let (_, used) = braced_group(rest)?; + let rest = rest[used..].trim_start(); + let (family, used) = braced_group(rest)?; + let rest = rest[used..].trim_start(); + let (series, used) = braced_group(rest)?; + let rest = rest[used..].trim_start(); + let (shape, used) = braced_group(rest)?; + let rest = &rest[used..]; + Some(( + family.trim().to_string(), + series.trim().to_string(), + shape.trim().to_string(), + rest, + )) +} + 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") { @@ -9406,6 +9426,13 @@ fn nfss_font_with_size( if let Some((size, rest)) = font_size_command(text) { font_size = Some(size); text = rest; + } else if let Some((code, series, shape, rest)) = usefont_command(text) { + family = nfss_family_name(&code); + bold = series.contains('b'); + italic = shape == "it"; + slanted = shape == "sl"; + small_caps = shape == "sc"; + text = rest; } else if let Some((name, rest)) = nfss_braced(text, "\\fontfamily") { family = nfss_family_name(&name); text = rest; @@ -20334,6 +20361,10 @@ mod nfss_tests { paragraph_font("\\fontfamily{cmss}\\selectfont Sans"), ParagraphFont::Sans ); + assert_eq!( + paragraph_font("\\usefont{T1}{cmss}{b}{n} Sans bold"), + ParagraphFont::SansBold + ); assert_eq!( paragraph_font("\\fontseries{bx}\\selectfont Bold"), ParagraphFont::Bold