Read sfcode assignments from route-D sources

This commit is contained in:
gloamxun committed 2026-10-23 14:07:37 +08:00
1 parent de35d1bcae
commit aea6c289ec
3 files changed
+44 -5

No files matched your search

+2 -2
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`). 433 workspace tests are green and no oracle
PDF opt-in with `?engine=route-d`). 434 workspace tests are green and no oracle
fixture is ignored.
Known gaps, in dependency order:
@@ -57,7 +57,7 @@ Known gaps, in dependency order:
`\adjdemerits`, `\doublehyphendemerits`, `\finalhyphendemerits`,
source-level `\hyphenation{...}` exceptions and `\parshape` measures,
`\parfillskip`, `\lineskip`, `\lineskiplimit`, `\topskip`, `\maxdepth`,
`\splittopskip` and the demerit registers,
`\splittopskip`, the demerit registers and `\sfcode`,
`\hyphenpenalty`, `\exhyphenpenalty`, `\hyphenation` exceptions,
`\hangindent`/`\hangafter`/`\parshape` line geometry, `\parfillskip`
with or without `fil`, default `\sfcode` sentence spacing,
+3 -2
View File
@@ -76,7 +76,8 @@ exactly as in TeX. Each line can take its measure and left offset from
service reads `\parshape=...`, `\parfillskip`, `\lineskip`,
`\lineskiplimit`, `\topskip`, `\maxdepth`, `\splittopskip` and the demerit
registers (`\hyphenpenalty`, `\exhyphenpenalty`, `\doublehyphendemerits`,
`\finalhyphendemerits`, `\linepenalty`, `\adjdemerits`) from the source); the
`\finalhyphendemerits`, `\linepenalty`, `\adjdemerits`) and `\sfcode`
assignments from the source); the
display list
honours the same offsets. The bounds and hyphen demerits are `ParagraphStyle`
fields mirroring `\pretolerance`, `\tolerance`, `\hyphenpenalty` and
@@ -352,7 +353,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 289 unit tests plus seven route-D integration tests and twenty-seven oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 433 tests (289 route-D unit, 7 route-D integration, 27 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 8 binary units), with none ignored. v1 remains the live server path.
The crate currently has 289 unit tests plus seven route-D integration tests and twenty-seven oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 434 tests (289 route-D unit, 7 route-D integration, 27 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 9 binary units), with none ignored. v1 remains the live server path.
### M0 incremental demo
+39 -1
View File
@@ -135,6 +135,7 @@ struct PageGeometry {
final_hyphen_demerits: Option<i64>,
line_penalty: Option<i64>,
adj_demerits: Option<i64>,
space_factor_codes: Vec<(char, i64)>,
}
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight`/`\vsize`/
@@ -188,6 +189,8 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.lineskip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "lineskiplimit") {
geometry.lineskiplimit = Some(value);
} else if let Some((character, value)) = sfcode_assignment(trimmed) {
geometry.space_factor_codes.push((character, value));
} else if let Some(value) = integer_assignment(trimmed, "hyphenpenalty") {
geometry.hyphen_penalty = Some(value);
} else if let Some(value) = integer_assignment(trimmed, "exhyphenpenalty") {
@@ -322,6 +325,27 @@ fn braced_argument<'a>(line: &'a str, command: &str) -> Option<&'a str> {
None
}
/// Read a `\sfcode` assignment such as ``\sfcode`x=2000`` or `\sfcode46=1000`.
fn sfcode_assignment(line: &str) -> Option<(char, i64)> {
let rest = line.strip_prefix("\\sfcode")?;
let rest = rest.trim_start();
let rest = rest.strip_prefix('=').unwrap_or(rest).trim_start();
let (character, rest) = if let Some(rest) = rest.strip_prefix('`') {
let rest = rest.strip_prefix('\\').unwrap_or(rest);
let mut characters = rest.chars();
let character = characters.next()?;
(character, characters.as_str())
} else {
let end = rest
.find(|character: char| !character.is_ascii_digit())
.unwrap_or(rest.len());
let code: u32 = rest[..end].parse().ok()?;
(char::from_u32(code)?, &rest[end..])
};
let rest = rest.trim_start().strip_prefix('=')?;
Some((character, rest.trim().parse().ok()?))
}
/// Read a plain integer assignment such as `\tolerance=1000`.
fn glue_assignment(line: &str, name: &str) -> Option<(i64, i64, i64)> {
let rest = line.strip_prefix('\\')?;
@@ -681,7 +705,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
.unwrap_or(0),
par_fillskip_fil: geometry.par_fillskip.map(|(_, _, fil)| fil).unwrap_or(true),
frenchspacing: geometry.french_spacing.unwrap_or(false),
space_factor_codes: Vec::new(),
space_factor_codes: geometry.space_factor_codes.clone(),
baselineskip: geometry.baseline_skip.unwrap_or(12 * 65536),
lineskip: geometry.lineskip.unwrap_or(65536),
lineskiplimit: geometry.lineskiplimit.unwrap_or(0),
@@ -973,4 +997,18 @@ mod source_tests {
assert_eq!(style.line_penalty, 20);
assert_eq!(style.adj_demerits, 999);
}
#[test]
fn geometry_reads_sfcode_assignments() {
let source = "\\documentclass{article}\n\\sfcode61=1000\n\\sfcode`x=2000\n\\begin{document}\n\nSome words here.\n\n\\end{document}\n";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.space_factor_codes, vec![('=', 1000), ('x', 2000)]);
assert!(!body.contains("sfcode"));
let paragraphs = paragraphs(&body).expect("paragraphs");
let document = build_document(&paragraphs, &geometry);
let style = document.units[0]
.paragraph_style
.clone()
.expect("paragraph style");
assert_eq!(style.space_factor_codes, geometry.space_factor_codes);
}
}