Consume catcode assignments for every character

Only \catcode`\@ assignments were handled; a class file setting the catcode of
# or _ left the assignment in the text. The canonicalisation pass now consumes
any successfully parsed \catcode assignment (backtick, bare or code-point
target), applying the value only to @ (11/12); other targets are recorded by
their consumption but do not change scanning yet, since the scanner still has
a single modelled catcode.

A preview test sets # and _ to 12, then renders "#hash and a_b." with no
warnings and no leaked assignment.
This commit is contained in:
gloamxun committed 2026-10-27 01:04:23 +08:00
1 parent b16e5a53e8
commit 54a0926552
3 files changed
+34 -8

No files matched your search

+1 -1
View File
@@ -49,7 +49,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
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`; flat `/preview` and project previews now
prefer route-D with a v1 fallback). 1001 workspace tests are green and no oracle
prefer route-D with a v1 fallback). 1002 workspace tests are green and no oracle
fixture is ignored. Preview JSON also carries a `warnings` array naming
environments route-D cannot lay out and control words that survived layout, so
unsupported source is reported instead of being silently typeset.
+6 -5
View File
@@ -370,7 +370,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`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address; those fallback glyphs keep their Unicode text under PDF extraction, so accented and Greek words round-trip; Type1 faces carry a ToUnicode CMap built from the requested characters, so glyphs whose names are ambiguous in the Adobe Glyph List (capital Omega, which would otherwise extract as U+2126) keep their intended code points. `Engine::edit` preserves the face when reflowing an edited paragraph.
The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 1001 tests (437 route-D unit, 374 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 62 server, 4 engine unit, 1 core unit), with none ignored. The flat `/preview` endpoint and the project preview/PDF endpoints now try route-D first and fall back to v1 on error (`?engine=v1` or `TEXRRA_ENGINE=v1` forces v1; the flat response carries `X-Preview-Engine`, naming the engine used). A geometry-less project now defaults to Letter, matching pdfLaTeX, where the old v1 path used A4.
The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 1002 tests (437 route-D unit, 375 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 62 server, 4 engine unit, 1 core unit), with none ignored. The flat `/preview` endpoint and the project preview/PDF endpoints now try route-D first and fall back to v1 on error (`?engine=v1` or `TEXRRA_ENGINE=v1` forces v1; the flat response carries `X-Preview-Engine`, naming the engine used). A geometry-less project now defaults to Letter, matching pdfLaTeX, where the old v1 path used A4.
### M0 incremental demo
@@ -544,10 +544,11 @@ are:
the body expand their bodies (including `\csname`) when read, while a plain
`\def` stays lazy. A runtime `\def` also parses its parameter text, so a
body-embedded `\def\foo#1{...}` (undelimited or delimited) expands with its
arguments instead of leaking. A `\catcode` assignment is recognised for `@`
(backtick, bare or code-point target, values 11/12), so class files that set
the catcode directly instead of using `\makeatletter` work; catcodes for any
other character are still unmodelled, and there is no token stream. A runtime
arguments instead of leaking. A `\catcode` assignment is consumed for any
target (backtick, bare or code-point form); only `@` (values 11/12) changes
scanning, so class files that set it directly instead of using
`\makeatletter` work, while other catcode values are recorded but not applied
yet. There is no token stream. A runtime
`\csname ... \endcsname` (also inside ordinary macro bodies) expands its
contents and resolves the named macro, yielding nothing when undefined.
`\futurelet\alias\command <token>` binds the looked-ahead token to the
+27 -2
View File
@@ -5750,11 +5750,13 @@ fn canonicalize_at_catcodes(source: &str) -> String {
}
"catcode" => {
if let Some((target, value, next)) = catcode_assignment(&characters, end) {
// Only the @ catcode changes scanning; the other
// assignments are consumed so they do not leak as text.
if target == '@' {
at_letter = value == 11;
index = next;
continue;
}
index = next;
continue;
}
}
_ => {}
@@ -15767,6 +15769,29 @@ A definition.
assert!(!text.contains("makeatletter"), "{text}");
}
#[test]
fn other_catcode_assignments_are_consumed() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\catcode`\\#=12\n\\catcode`\\_=12\n\n#hash and a_b.\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let svg = value["pages"][0]["svg"].as_str().expect("svg");
let text: String = svg
.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>()
.split_whitespace()
.collect();
assert!(text.contains("#hash"), "{text}");
assert!(text.contains("a_b."), "{text}");
assert!(!text.contains("catcode"), "{text}");
}
#[test]
fn afterassignment_runs_after_the_assignment() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\newcount\\a\n\\def\\hello{H}\n\\afterassignment\\hello\\a=5\n\nText.\n\n\\end{document}\n";