Select built-in faces from NFSS declarations

This commit is contained in:
gloamxun committed 2026-10-24 02:04:14 +08:00
1 parent f6726439ed
commit df43cade62
3 files changed
+135 -3

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`). 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,
+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; 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
+132
View File
@@ -925,7 +925,96 @@ fn take_braced_argument(text: &mut String, command: &str) -> Option<String> {
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(">)</text>"), "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);
}
}