Consume mark commands written inside running text

The line-start forms of \markboth and \markright were handled, but an inline
"Body \markright{Short title} text." leaked the command. A body pass now finds
either command anywhere, records the left/right marks for the running heads,
and removes the command from the text (malformed brackets are left untouched).

A fancy-header test puts \markright inside a paragraph and checks the header
shows "Short title" with no warnings and no leaked command.
This commit is contained in:
gloamxun committed 2026-10-27 02:53:27 +08:00
1 parent 5dda1a948a
commit 1227d82f02
4 files changed
+74 -6

No files matched your search

+1 -1
View File
@@ -51,7 +51,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the 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 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 PDF opt-in with `?engine=route-d`; flat `/preview` and project previews now
prefer route-D with a v1 fallback). 1036 workspace tests are green and no oracle prefer route-D with a v1 fallback). 1037 workspace tests are green and no oracle
fixture is ignored. Preview JSON also carries a `warnings` array naming fixture is ignored. Preview JSON also carries a `warnings` array naming
environments route-D cannot lay out and control words that survived layout, so environments route-D cannot lay out and control words that survived layout, so
unsupported source is reported instead of being silently typeset. unsupported source is reported instead of being silently typeset.
+2 -2
View File
@@ -14,12 +14,12 @@ cargo fmt --all -- --check
cargo test --workspace --offline cargo test --workspace --offline
``` ```
The workspace currently passes **1036 tests**: The workspace currently passes **1037 tests**:
| Suite | Tests | | Suite | Tests |
| --- | --- | | --- | --- |
| route-D unit (engine) | 437 | | route-D unit (engine) | 437 |
| engine binary unit | 407 | | engine binary unit | 408 |
| server integration | 63 | | server integration | 63 |
| v1 integration | 71 | | v1 integration | 71 |
| pdfLaTeX oracle | 42 | | pdfLaTeX oracle | 42 |
+4 -3
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. 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 1036 tests (437 route-D unit, 407 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 63 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 1037 tests (437 route-D unit, 408 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 63 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 ### M0 incremental demo
@@ -498,7 +498,8 @@ deliberately open rather than approximated:
`\lhead`/`\chead`/`\rhead`/`\lfoot`/`\cfoot`/`\rfoot` shorthands paint `\lhead`/`\chead`/`\rhead`/`\lfoot`/`\cfoot`/`\rfoot` shorthands paint
L/C/R running heads at `top margin - headsep` and feet at L/C/R running heads at `top margin - headsep` and feet at
`text height + footskip`, expand `\thepage` per page, resolve `text height + footskip`, expand `\thepage` per page, resolve
`\leftmark`/`\rightmark` from the last `\markboth`/`\markright` or, `\leftmark`/`\rightmark` from the last `\markboth`/`\markright`
(including marks written inside running text) or,
when no explicit mark is set, from the current numbered heading on that page, when no explicit mark is set, from the current numbered heading on that page,
draw draw
`\headrulewidth`, and suppress the head and number on a title page; the `\headrulewidth`, and suppress the head and number on a title page; the
@@ -523,7 +524,7 @@ deliberately open rather than approximated:
## Limits and remaining gaps ## Limits and remaining gaps
The suite currently stands at 1036 passing tests, with the workspace clean. The suite currently stands at 1037 passing tests, with the workspace clean.
Route-D serves both flat `/preview` and project preview/PDF requests by default Route-D serves both flat `/preview` and project preview/PDF requests by default
(automatic v1 fallback; `?engine=v1` or `TEXRRA_ENGINE=v1` opts out). The (automatic v1 fallback; `?engine=v1` or `TEXRRA_ENGINE=v1` opts out). The
objective's areas are implemented to the limits below; complex-script shaping is objective's areas are implemented to the limits below; complex-script shaping is
+67
View File
@@ -6442,6 +6442,7 @@ fn geometry_and_body_with_images(
body = expand_skip_registers(&body, &mut skip_registers); body = expand_skip_registers(&body, &mut skip_registers);
geometry.skip_registers = skip_registers; geometry.skip_registers = skip_registers;
body = expand_links(&body); body = expand_links(&body);
body = strip_inline_marks(&body, &mut geometry);
let today = today_string(); let today = today_string();
body = expand_today(&body, &today); body = expand_today(&body, &today);
body = expand_lipsum(&body); body = expand_lipsum(&body);
@@ -9914,6 +9915,52 @@ fn expand_phantoms(text: &str) -> String {
output output
} }
/// Consume `\markboth`/`\markright` that appear inside running text (the
/// line-start forms are handled while the page geometry is read) and record the
/// most recent marks for the running heads.
fn strip_inline_marks(body: &str, geometry: &mut PageGeometry) -> String {
let mut output = String::new();
let mut rest = body;
loop {
let mut next: Option<(usize, &str)> = None;
for command in ["\\markboth", "\\markright"] {
if let Some(index) = rest.find(command) {
let better = next.is_none_or(|(known, _)| index < known);
if better {
next = Some((index, command));
}
}
}
let Some((index, command)) = next else {
output.push_str(rest);
break;
};
output.push_str(&rest[..index]);
let after = rest[index + command.len()..].trim_start();
let (left, tail) = if command == "\\markboth" {
let Some((left, used)) = braced_group(after) else {
output.push_str(command);
rest = &rest[index + command.len()..];
continue;
};
(Some(left.trim().to_string()), after[used..].trim_start())
} else {
(None, after)
};
let Some((right, used)) = braced_group(tail) else {
output.push_str(command);
rest = &rest[index + command.len()..];
continue;
};
if let Some(left) = left {
geometry.running_left = Some(left);
}
geometry.running_right = Some(right.trim().to_string());
rest = &tail[used..];
}
output
}
/// Keep the visible label of `\hyperref[label]{text}`, `\hyperlink{name}{text}` /// Keep the visible label of `\hyperref[label]{text}`, `\hyperlink{name}{text}`
/// and `\hypertarget{name}{text}`; internal PDF targets are not modelled. /// and `\hypertarget{name}{text}`; internal PDF targets are not modelled.
fn strip_hyperref_commands(text: &str) -> String { fn strip_hyperref_commands(text: &str) -> String {
@@ -16843,6 +16890,26 @@ A definition.
assert!(!text.contains("??"), "{text}"); assert!(!text.contains("??"), "{text}");
} }
#[test]
fn inline_marks_set_the_running_head() {
let source = "\\documentclass{article}\n\\pagestyle{fancy}\n\\fancyhf{}\n\\fancyhead[R]{\\rightmark}\n\\begin{document}\n\nBody \\markright{Short title} text.\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"]
);
assert!(!value.to_string().contains("markright"), "{json}");
let texts = svg_texts(&json);
let header = texts
.iter()
.find(|(text, _, _)| text == "Short title")
.expect("header");
assert!(header.2 < 0.0, "header should sit above the body");
}
#[test] #[test]
fn autoref_uses_the_float_kind() { fn autoref_uses_the_float_kind() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{figure}[h]\n\\rule{10pt}{10pt}\n\\caption{Cap}\\label{fig:a}\n\\end{figure}\n\nSee \\autoref{fig:a}.\n\n\\end{document}\n"; let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{figure}[h]\n\\rule{10pt}{10pt}\n\\caption{Cap}\\label{fig:a}\n\\end{figure}\n\nSee \\autoref{fig:a}.\n\n\\end{document}\n";