Queue top table floats and let text flow around them

This commit is contained in:
gloamxun committed 2026-10-19 14:42:06 +08:00
1 parent 7ad3c327ec
commit 6658a155fc
6 files changed
+210 -70

No files matched your search

+6 -2
View File
@@ -351,7 +351,11 @@ units or fractions of `\linewidth`. Images with transparency are not supported
by the current PDF dependency. Basic `tabular` rows become equal-width PDF by the current PDF dependency. Basic `tabular` rows become equal-width PDF
columns; `l`, `c`, `r`, and paragraph (`p{...}`) columns are supported, as are columns; `l`, `c`, `r`, and paragraph (`p{...}`) columns are supported, as are
full-width `\hline` and `booktabs` rules. Spanning cells, partial `\cline` rules, full-width `\hline` and `booktabs` rules. Spanning cells, partial `\cline` rules,
and multi-page table headers are not implemented. `\label`, `\ref`, and `\cite` and multi-page table headers are not implemented. Figures honor `[htbp]`
placement hints (`[t]` is deferred to the top of the next page, `[b]` to the
page bottom, `[p]` to a float page, `[h]` stays in the flow); tables honor
`[t]` by deferring the caption and rows to the next page and `[p]` with a
float page. `\label`, `\ref`, and `\cite`
resolve section, figure, table, and bibliography numbers. `\bibliography{file}` resolve section, figure, table, and bibliography numbers. `\bibliography{file}`
reads a local `.bib` file (the `.bib` extension is optional) and formats common reads a local `.bib` file (the `.bib` extension is optional) and formats common
author/editor, title, journal, booktitle, volume, number, pages, publisher, author/editor, title, journal, booktitle, volume, number, pages, publisher,
@@ -489,7 +493,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`,
| Boxes | `\rule`, inline framed `\fbox`, block `\parbox`/`\minipage`, `\vbox`/`\vtop` (vertical material capture), inline `\mbox`/`\makebox`, `\raisebox`, `center`, `\hrule` | independent VBox natural width/baseline | | 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 | — | | 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` | — | | 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` | | Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure `[htbp]` placement (deferred top floats, bottom/page/here), table `[t]`/`[p]` queues | table bottom/float-page queues, `\textfraction` |
| Contents | `\tableofcontents`, `\listoffigures`, `\listoftables` generated from numbered headings and captions, dotted leaders, page numbers resolved after pagination | per-chapter book/report contents | | Contents | `\tableofcontents`, `\listoffigures`, `\listoftables` generated from numbered headings and captions, dotted leaders, page numbers resolved after pagination | per-chapter book/report contents |
| Pages | A4/Letter/... page sizes, margins/geometry, automatic pagination, `\newpage`, `\pagebreak`, plain/fancy page styles, `\thispagestyle`, fancyhdr L/C/R headers and footers | — | | Pages | A4/Letter/... page sizes, margins/geometry, automatic pagination, `\newpage`, `\pagebreak`, plain/fancy page styles, `\thispagestyle`, fancyhdr L/C/R headers and footers | — |
| Classes/packages | built-in + local classes, option protocol, local package options, base font sizes 10/11/12pt, chapter headings and numbering, chapter page breaks, document-level custom `\maketitle` | class-specific formatting beyond chapter breaks | | Classes/packages | built-in + local classes, option protocol, local package options, base font sizes 10/11/12pt, chapter headings and numbering, chapter page breaks, document-level custom `\maketitle` | class-specific formatting beyond chapter breaks |
+1 -1
View File
@@ -2086,7 +2086,7 @@ impl Parser {
{ {
if !self.table_placement.is_empty() if !self.table_placement.is_empty()
&& !self.table_placement.contains('h') && !self.table_placement.contains('h')
&& (self.table_placement.contains('t') || self.table_placement.contains('p')) && self.table_placement.contains('p')
{ {
self.push_block(Block::PageBreak); self.push_block(Block::PageBreak);
} }
+4 -3
View File
@@ -20,9 +20,10 @@ planner remains).
## 1. Float tuning and table queue ## 1. Float tuning and table queue
- Figures with `[t]` are queued and placed at the top of the next page while - Figures with `[t]` are queued and placed at the top of the next page while
following text fills the current page. Remaining work: queue table floats, following text fills the current page. Tables with `[t]` queue their caption
tune `\textfraction`/`\topfraction`, and support more than a one-page and rows the same way. Remaining work: queue table `[b]`/float-page
lookahead. placements, tune `\textfraction`/`\topfraction`, and support more than a
one-page lookahead.
## 2. True vertical boxes (partial) ## 2. True vertical boxes (partial)
+8 -6
View File
@@ -29,7 +29,8 @@ the source changes; the SVG preview is refreshed per page.
Figures honor `[htbp]` hints: `[t]` is queued and placed at the top of the Figures honor `[htbp]` hints: `[t]` is queued and placed at the top of the
next page while following text fills the current page, `[b]` sits at the page next page while following text fills the current page, `[b]` sits at the page
bottom, `[p]` takes a float page, and `[h]` stays in the text flow. Tables bottom, `[p]` takes a float page, and `[h]` stays in the text flow. Tables
still use immediate page breaks. Page styles: queue `[t]` captions and rows at the top of the next page and take a float
page for `[p]`; table `[b]`/`[h]` stay in the text flow. Page styles:
default `plain` page numbers, `\pagestyle{empty|plain|fancy}`, default `plain` page numbers, `\pagestyle{empty|plain|fancy}`,
`\thispagestyle`, fancyhdr `\fancyhead`/`\fancyfoot` (L/C/R) and `\thispagestyle`, fancyhdr `\fancyhead`/`\fancyfoot` (L/C/R) and
`\lhead`/`\chead`/`\rhead`/`\lfoot`/`\cfoot`/`\rfoot`, with `\thepage` `\lhead`/`\chead`/`\rhead`/`\lfoot`/`\cfoot`/`\rfoot`, with `\thepage`
@@ -67,9 +68,10 @@ See `docs/ROADMAP.md` for concrete implementation plans.
- Boxes: `\parbox`/`\minipage` are block-level; `\vbox`/`\vtop` capture - Boxes: `\parbox`/`\minipage` are block-level; `\vbox`/`\vtop` capture
and splice vertical material but do not expose an independent natural width and splice vertical material but do not expose an independent natural width
or box baseline. or box baseline.
- Pages: the figure queue only looks one page ahead; tables are not queued and - Pages: the figure/table queues only look one page ahead; table `[b]` and
there is no `\textfraction`/`\topfraction` tuning. Pagination is performed by float-page queues are not implemented and there is no
the positioned renderer; the separate page-break planner was removed. `\textfraction`/`\topfraction` tuning. Pagination is performed by the
positioned renderer; the separate page-break planner was removed.
- Notes: footnotes inside captions, floats, or arbitrary boxes are not - Notes: footnotes inside captions, floats, or arbitrary boxes are not
collected; the insertion area currently follows the page where the paragraph collected; the insertion area currently follows the page where the paragraph
ends rather than the exact reference line. ends rather than the exact reference line.
@@ -80,9 +82,9 @@ See `docs/ROADMAP.md` for concrete implementation plans.
- `examples/hello.tex`, `examples/boxes.tex`, `examples/compat.tex` compile to - `examples/hello.tex`, `examples/boxes.tex`, `examples/compat.tex` compile to
one A4 page each; the arXiv template project compiles to 3 Letter pages. one A4 page each; the arXiv template project compiles to 3 Letter pages.
- `cargo fmt --check` passes; `cargo test --offline` (39 tests: 35 compiler - `cargo fmt --check` passes; `cargo test --offline` (40 tests: 36 compiler
integration tests + 4 server tests) covers the three examples, the arXiv integration tests + 4 server tests) covers the three examples, the arXiv
fixture, floats/newpage, page styles and `\thispagestyle`, SVG/PDF output, fixture, figure/table floats, newpage, page styles and `\thispagestyle`, SVG/PDF output,
project API, static frontend assets, path traversal, the legacy project API, static frontend assets, path traversal, the legacy
`/texrra` endpoint, and page-hash cache stability. `/texrra` endpoint, and page-hash cache stability.
- Warm preview: hello ~0.6ms, arXiv ~13ms (release server, page layout cached). - Warm preview: hello ~0.6ms, arXiv ~13ms (release server, page layout cached).
+151 -58
View File
@@ -182,10 +182,25 @@ fn top_skip_gap(ascent: i64, top_skip: i64) -> i64 {
struct DeferredFloat { struct DeferredFloat {
anchor: PageAnchor, anchor: PageAnchor,
element: PositionedFigure, pieces: Vec<DeferredPiece>,
height: i64, height: i64,
} }
enum DeferredPiece {
Image(PositionedFigure),
Table {
element: PositionedTable,
offset: i64,
},
Line {
content: PositionedLineContent,
offset: i64,
x: i64,
centered: bool,
ragged: bool,
},
}
fn flush_deferred( fn flush_deferred(
pages: &mut Vec<PositionedPage>, pages: &mut Vec<PositionedPage>,
cursor: &mut i64, cursor: &mut i64,
@@ -209,15 +224,39 @@ fn flush_deferred(
*cursor = top_skip; *cursor = top_skip;
} }
let y = *cursor; let y = *cursor;
pages let page = pages
.last_mut() .last_mut()
.expect("the page list starts with one page") .expect("the page list starts with one page");
.items for piece in float.pieces {
.push(PositionedPageItem::Image { match piece {
anchor: float.anchor, DeferredPiece::Image(element) => page.items.push(PositionedPageItem::Image {
element: float.element, anchor: float.anchor,
y, element,
}); y,
}),
DeferredPiece::Table { element, offset } => {
page.items.push(PositionedPageItem::Table {
anchor: float.anchor,
element,
y: y.saturating_add(offset),
})
}
DeferredPiece::Line {
content,
offset,
x,
centered,
ragged,
} => page.items.push(PositionedPageItem::Line(PositionedLine {
anchor: float.anchor,
content,
y: y.saturating_add(offset),
x,
centered,
ragged,
})),
}
}
*cursor = y.saturating_add(float.height); *cursor = y.saturating_add(float.height);
} }
Some(*cursor) Some(*cursor)
@@ -1845,13 +1884,11 @@ pub(super) fn build_positioned_pages(
caption, caption,
alignments, alignments,
rules, rules,
.. placement,
} => { } => {
pre_display_size = None; pre_display_size = None;
let mut line_index = 0; let caption_lines = caption.as_ref().map_or_else(Vec::new, |caption| {
let mut has_caption_lines = false; measure_positioned_lines(
if let Some(caption) = caption {
for line in measure_positioned_lines(
caption, caption,
10, 10,
content_width_mm, content_width_mm,
@@ -1863,48 +1900,9 @@ pub(super) fn build_positioned_pages(
math_metrics, math_metrics,
math_layout, math_layout,
cache, cache,
) { )
let anchor = PageAnchor { });
block: block_index, let table = layout_table(
line: line_index,
};
let line_gap = if line_index == 0 && has_previous_block {
pending_gap
} else {
0
};
position_box(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
anchor,
line.height,
line.ascent,
line_gap.saturating_add(line.leading_before),
page_goal,
top_skip,
max_depth,
);
pages
.last_mut()
.expect("the page list starts with one page")
.items
.push(PositionedPageItem::Line(PositionedLine {
anchor,
content: line.content,
y: cursor,
x: 0,
centered: true,
ragged: false,
}));
cursor = cursor.saturating_add(line.height);
line_index += 1;
has_caption_lines = true;
}
}
if let Some(table) = layout_table(
rows, rows,
alignments, alignments,
rules, rules,
@@ -1918,7 +1916,102 @@ pub(super) fn build_positioned_pages(
math_layout, math_layout,
cache, cache,
base_font_size, base_font_size,
) { );
let caption_height = caption_lines.iter().map(|line| line.height).sum::<i64>();
let caption_gap = if caption_lines.is_empty() {
0
} else {
(normal_line_height as f64 * 0.25).round() as i64
};
let table_height = table.as_ref().map_or(0, |table| table.height);
let total_height = caption_height
.saturating_add(caption_gap)
.saturating_add(table_height);
let table_top = placement.contains('t')
&& !placement.contains('h')
&& !placement.contains('p')
&& deferred.is_empty()
&& pages.last().is_some_and(|page| !page.items.is_empty())
&& total_height <= page_goal;
if table_top {
let Some(table) = table else {
return None;
};
let mut pieces = Vec::new();
let mut offset = 0;
for line in caption_lines {
pieces.push(DeferredPiece::Line {
content: line.content,
offset,
x: 0,
centered: true,
ragged: false,
});
offset = offset.saturating_add(line.height);
}
if !pieces.is_empty() {
offset = offset.saturating_add(caption_gap);
}
pieces.push(DeferredPiece::Table {
element: table,
offset,
});
deferred.push(DeferredFloat {
anchor: PageAnchor {
block: block_index,
line: 0,
},
pieces,
height: total_height,
});
pending_gap = 0;
has_previous_block = false;
continue;
}
let mut line_index = 0;
let mut has_caption_lines = false;
for line in caption_lines {
let anchor = PageAnchor {
block: block_index,
line: line_index,
};
let line_gap = if line_index == 0 && has_previous_block {
pending_gap
} else {
0
};
position_box(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
anchor,
line.height,
line.ascent,
line_gap.saturating_add(line.leading_before),
page_goal,
top_skip,
max_depth,
);
pages
.last_mut()
.expect("the page list starts with one page")
.items
.push(PositionedPageItem::Line(PositionedLine {
anchor,
content: line.content,
y: cursor,
x: 0,
centered: true,
ragged: false,
}));
cursor = cursor.saturating_add(line.height);
line_index += 1;
has_caption_lines = true;
}
if let Some(table) = table {
let PositionedTable { let PositionedTable {
rows: table_rows, rows: table_rows,
column_count, column_count,
@@ -2037,7 +2130,7 @@ pub(super) fn build_positioned_pages(
if defer_here { if defer_here {
deferred.push(DeferredFloat { deferred.push(DeferredFloat {
anchor, anchor,
element: figure, pieces: vec![DeferredPiece::Image(figure)],
height: figure_height, height: figure_height,
}); });
} else if bottom_float { } else if bottom_float {
+40
View File
@@ -805,6 +805,46 @@ Body text.
} }
} }
#[test]
fn top_table_moves_to_the_next_page_and_text_flows() {
let source = r#"\documentclass{article}
\begin{document}
Before table.
\begin{table}[t]
\caption{Top table}
\begin{tabular}{ll}
Alpha & Beta \\
Gamma & Delta \\
\end{tabular}
\end{table}
After table.
\end{document}
"#;
let value = fresh_preview(source, "table-float.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
assert_eq!(pages(&value).len(), 2, "top table should start a new page");
assert!(
!page_svg(&value, 0).contains("Top"),
"table caption should not stay on page 1: {}",
page_svg(&value, 0)
);
assert!(
page_svg(&value, 0).contains("After"),
"text after the table should fill page 1"
);
let second = page_svg(&value, 1);
assert!(
second.contains(">op</text>"),
"table caption missing from page 2: {second}"
);
assert!(
second.contains(">Alpha</text>"),
"table body missing from page 2: {second}"
);
}
#[test] #[test]
fn table_of_contents_lists_numbered_sections_and_pages() { fn table_of_contents_lists_numbered_sections_and_pages() {
let source = r"\documentclass{article} let source = r"\documentclass{article}