Capture vertical material in vbox and vtop

This commit is contained in:
gloamxun committed 2026-10-19 14:34:22 +08:00
1 parent 5e9552d785
commit 7ad3c327ec
8 files changed
+99 -14

No files matched your search

+3 -3
View File
@@ -319,8 +319,8 @@ A document-level `\renewcommand{\maketitle}` replaces the engine title block;
class and package files that replace `\maketitle` are ignored so template
title code cannot break the native layout. Known gaps for full document
classes: `\parbox`/`\minipage` are block-level rather than inline, and
`\vbox` and `\vtop` map to a full-width block box rather than true nested
VBox material.
`\vbox`/`\vtop` capture vertical material but do not form an independent
natural-width box with its own baseline.
`\footnote{...}` captures its content, emits a superscript marker, reserves
space on the page where the paragraph ends, and paints the notes with a
@@ -486,7 +486,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`,
| Area | Implemented | Remaining |
| --- | --- | --- |
| Boxes | `\rule`, inline framed `\fbox`, block `\parbox`/`\minipage`, `\vbox`/`\vtop` (as full-width block), inline `\mbox`/`\makebox`, `\raisebox`, `center`, `\hrule` | true nested VBox material |
| Boxes | `\rule`, inline framed `\fbox`, block `\parbox`/`\minipage`, `\vbox`/`\vtop` (vertical material capture), inline `\mbox`/`\makebox`, `\raisebox`, `center`, `\hrule` | independent VBox natural width/baseline |
| Fonts | serif/sans/mono faces, bold/italic/mono/sans declarations, group scoping, true scaled small caps | — |
| Paragraphs | Knuth-Plass breaking, TeX hyphenation, justification, `\centering`/`\raggedright`/`\raggedleft`, `\parindent` (17pt default), `\hfill`/`\hfil` | — |
| Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure `[htbp]` placement (deferred top floats, bottom/page/here), table page breaks | table float queue, `\textfraction` |
+18
View File
@@ -479,6 +479,7 @@ struct Parser {
inline_boxes: Vec<InlineBoxSpec>,
minipage: Option<MinipageBuilder>,
footnote_capture: Option<Vec<Run>>,
vbox_stack: Vec<(Vec<Block>, Vec<Option<SourceLocation>>)>,
footnote_counter: u32,
page_decoration_capture: Option<(
bool,
@@ -538,6 +539,7 @@ impl Parser {
inline_boxes: Vec::new(),
minipage: None,
footnote_capture: None,
vbox_stack: Vec::new(),
footnote_counter: 0,
page_decoration_capture: None,
float_placement: String::new(),
@@ -887,6 +889,22 @@ impl Parser {
self.framed_box = Some(Vec::new());
}
}
expanded::FrontendEventKind::VBoxStart => {
self.flush_paragraph();
let blocks = std::mem::take(&mut self.document.blocks);
let sources = std::mem::take(&mut self.document.block_sources);
self.vbox_stack.push((blocks, sources));
}
expanded::FrontendEventKind::VBoxEnd => {
self.flush_paragraph();
let Some((blocks, sources)) = self.vbox_stack.pop() else {
return;
};
let inner_blocks = std::mem::replace(&mut self.document.blocks, blocks);
let inner_sources = std::mem::replace(&mut self.document.block_sources, sources);
self.document.blocks.extend(inner_blocks);
self.document.block_sources.extend(inner_sources);
}
expanded::FrontendEventKind::ParboxStart { width } => {
self.flush_paragraph();
self.box_frame = false;
+2
View File
@@ -93,6 +93,8 @@ pub enum FrontendEventKind {
ParboxStart {
width: common::Scaled,
},
VBoxStart,
VBoxEnd,
HorizontalRule {
height: common::Scaled,
width: Option<common::Scaled>,
+7 -5
View File
@@ -12,7 +12,8 @@ geometry-aware `\textwidth`/`\textheight` named length registers; table of
contents and figure/table lists with dotted leaders; native PDF rectangles; unified pagination;
`\centering`/`\raggedright`/`\raggedleft` declarations and environments;
chapter headings with chapter-prefixed section numbering and page breaks;
deferred top-float queue for figures; document-level custom `\maketitle`
deferred top-float queue for figures; document-level custom `\maketitle`;
`\vbox`/`\vtop` vertical-material capture with inline splicing
(`build_positioned_pages` is the only page builder and no separate break
planner remains).
@@ -23,8 +24,9 @@ planner remains).
tune `\textfraction`/`\topfraction`, and support more than a one-page
lookahead.
## 2. True vertical boxes (remaining)
## 2. True vertical boxes (partial)
- `\vbox`/`\vtop` currently map to a full-width block `\parbox`. A real VBox
needs a vertical-material capture with its own natural width, baseline, and
nested line/box children in the positioned renderer.
- `\vbox`/`\vtop` now capture nested vertical material (rules, skips,
paragraphs) and splice it into the surrounding column. A fully independent
VBox still needs its own natural width, baseline, and nested line/box
children in the positioned renderer.
+5 -4
View File
@@ -13,7 +13,7 @@ the source changes; the SVG preview is refreshed per page.
- Headings: numbered `\section`/`\subsection`/`\subsubsection` and starred
`\section*` variants (starred headings do not advance the counter).
- Box material: `\rule` (raise/width/height), inline framed `\fbox`, block
`\parbox`/`\minipage`, `\vbox`/`\vtop` (full-width block approximation),
`\parbox`/`\minipage`, `\vbox`/`\vtop` (vertical material capture),
inline `\mbox`, `\makebox[width][align]`, `\raisebox`, `center`, `\hrule`
with `width`/`height`.
- Fonts: Liberation Serif/Sans/Mono faces, bold/italic/monospace/sans
@@ -64,8 +64,9 @@ See `docs/ROADMAP.md` for concrete implementation plans.
## Known approximations / gaps
- Boxes: `\parbox`/`\minipage` are block-level; `\vbox`/`\vtop` map to a
full-width block rather than true nested VBox material.
- Boxes: `\parbox`/`\minipage` are block-level; `\vbox`/`\vtop` capture
and splice vertical material but do not expose an independent natural width
or box baseline.
- Pages: the figure queue only looks one page ahead; tables are not queued and
there is no `\textfraction`/`\topfraction` tuning. Pagination is performed by
the positioned renderer; the separate page-break planner was removed.
@@ -79,7 +80,7 @@ See `docs/ROADMAP.md` for concrete implementation plans.
- `examples/hello.tex`, `examples/boxes.tex`, `examples/compat.tex` compile to
one A4 page each; the arXiv template project compiles to 3 Letter pages.
- `cargo fmt --check` passes; `cargo test --offline` (38 tests: 34 compiler
- `cargo fmt --check` passes; `cargo test --offline` (39 tests: 35 compiler
integration tests + 4 server tests) covers the three examples, the arXiv
fixture, floats/newpage, page styles and `\thispagestyle`, SVG/PDF output,
project API, static frontend assets, path traversal, the legacy
-2
View File
@@ -28,8 +28,6 @@ const PRELUDE: &str = r#"
\def\today{__TEXRRA_TODAY__}
\def\maketitle{\texrramaketitle}
\def\noindent{\texrranoindent}
\def\vbox#1{\parbox{\textwidth}{#1}}
\def\vtop#1{\parbox{\textwidth}{#1}}
\def\hfill{\hskip0pt plus 1fil\relax}
\def\hfil{\hskip0pt plus 1fil\relax}
\def\allowbreak{\penalty0}
+37
View File
@@ -3442,6 +3442,31 @@ fn parbox_primitive(
Ok(())
}
fn vbox_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let content = read_unexpanded_argument(input, token, "expects box content")?;
let inner = strip_braced_tokens(&content).to_vec();
write_event(input, token, FrontendEventKind::VBoxStart);
let trace_key = token.trace_key();
let mut expansion = inner;
expansion.push(control_sequence_token(input, "texrravboxend", trace_key));
input
.as_mut()
.expansions_mut()
.extend(expansion.into_iter().rev());
Ok(())
}
fn vbox_end_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
write_event(input, token, FrontendEventKind::VBoxEnd);
Ok(())
}
fn fbox_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -6728,6 +6753,18 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"texrraframedboxend",
texlang::command::BuiltIn::new_execution(framed_box_end_primitive),
);
built_ins.insert(
"vbox",
texlang::command::BuiltIn::new_execution(vbox_primitive),
);
built_ins.insert(
"vtop",
texlang::command::BuiltIn::new_execution(vbox_primitive),
);
built_ins.insert(
"texrravboxend",
texlang::command::BuiltIn::new_execution(vbox_end_primitive),
);
built_ins.insert(
"texrrarule",
texlang::command::BuiltIn::new_execution(rule_primitive),
+27
View File
@@ -1022,3 +1022,30 @@ Body text.
);
assert!(svg.contains("Body"), "body text missing: {svg}");
}
#[test]
fn vbox_captures_vertical_material() {
let source = r#"\documentclass{article}
\begin{document}
Before.
\vbox{%
\hrule width 40mm height 2pt
\vskip 4pt
\hrule width 20mm height 2pt
}
After.
\end{document}
"#;
let value = fresh_preview(source, "vbox.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
assert_eq!(
svg.matches("fill=\"#000000\"/>").count(),
2,
"expected two rules in vbox: {svg}"
);
assert!(svg.contains("width=\"40.15"), "wide rule missing: {svg}");
assert!(svg.contains("width=\"20.07"), "narrow rule missing: {svg}");
assert!(svg.contains(">Before.</text>"), "text before vbox missing");
assert!(svg.contains(">After</text>"), "text after vbox missing");
}