diff --git a/README.md b/README.md index 3d23485..5cdf829 100644 --- a/README.md +++ b/README.md @@ -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 columns; `l`, `c`, `r`, and paragraph (`p{...}`) columns are supported, as are 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}` reads a local `.bib` file (the `.bib` extension is optional) and formats common 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 | | 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` | +| 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 | | 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 | diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index 7c72bb1..3a1e6cc 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -2086,7 +2086,7 @@ impl Parser { { if !self.table_placement.is_empty() && !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); } diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index bef53ec..55aeaf3 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -20,9 +20,10 @@ planner remains). ## 1. Float tuning and table queue - 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, - tune `\textfraction`/`\topfraction`, and support more than a one-page - lookahead. + following text fills the current page. Tables with `[t]` queue their caption + and rows the same way. Remaining work: queue table `[b]`/float-page + placements, tune `\textfraction`/`\topfraction`, and support more than a + one-page lookahead. ## 2. True vertical boxes (partial) diff --git a/docs/STATUS.md b/docs/STATUS.md index 7df1168..8168a9f 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -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 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 - 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}`, `\thispagestyle`, fancyhdr `\fancyhead`/`\fancyfoot` (L/C/R) and `\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 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. +- Pages: the figure/table queues only look one page ahead; table `[b]` and + float-page queues are not implemented and there is no + `\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 collected; the insertion area currently follows the page where the paragraph 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 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 - 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 `/texrra` endpoint, and page-hash cache stability. - Warm preview: hello ~0.6ms, arXiv ~13ms (release server, page layout cached). diff --git a/src/rust_typeset/pdf/layout.rs b/src/rust_typeset/pdf/layout.rs index 64cab0d..4f49864 100644 --- a/src/rust_typeset/pdf/layout.rs +++ b/src/rust_typeset/pdf/layout.rs @@ -182,10 +182,25 @@ fn top_skip_gap(ascent: i64, top_skip: i64) -> i64 { struct DeferredFloat { anchor: PageAnchor, - element: PositionedFigure, + pieces: Vec, 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( pages: &mut Vec, cursor: &mut i64, @@ -209,15 +224,39 @@ fn flush_deferred( *cursor = top_skip; } let y = *cursor; - pages + let page = pages .last_mut() - .expect("the page list starts with one page") - .items - .push(PositionedPageItem::Image { - anchor: float.anchor, - element: float.element, - y, - }); + .expect("the page list starts with one page"); + for piece in float.pieces { + match piece { + DeferredPiece::Image(element) => page.items.push(PositionedPageItem::Image { + anchor: float.anchor, + 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); } Some(*cursor) @@ -1845,13 +1884,11 @@ pub(super) fn build_positioned_pages( caption, alignments, rules, - .. + placement, } => { pre_display_size = None; - let mut line_index = 0; - let mut has_caption_lines = false; - if let Some(caption) = caption { - for line in measure_positioned_lines( + let caption_lines = caption.as_ref().map_or_else(Vec::new, |caption| { + measure_positioned_lines( caption, 10, content_width_mm, @@ -1863,48 +1900,9 @@ pub(super) fn build_positioned_pages( math_metrics, math_layout, cache, - ) { - 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) = layout_table( + ) + }); + let table = layout_table( rows, alignments, rules, @@ -1918,7 +1916,102 @@ pub(super) fn build_positioned_pages( math_layout, cache, base_font_size, - ) { + ); + let caption_height = caption_lines.iter().map(|line| line.height).sum::(); + 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 { rows: table_rows, column_count, @@ -2037,7 +2130,7 @@ pub(super) fn build_positioned_pages( if defer_here { deferred.push(DeferredFloat { anchor, - element: figure, + pieces: vec![DeferredPiece::Image(figure)], height: figure_height, }); } else if bottom_float { diff --git a/tests/typeset.rs b/tests/typeset.rs index 2089dab..817b824 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -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"), + "table caption missing from page 2: {second}" + ); + assert!( + second.contains(">Alpha"), + "table body missing from page 2: {second}" + ); +} + #[test] fn table_of_contents_lists_numbered_sections_and_pages() { let source = r"\documentclass{article}