diff --git a/README.md b/README.md index 068eb57..04e7661 100644 --- a/README.md +++ b/README.md @@ -487,7 +487,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`, | Boxes | `\rule`, inline framed `\fbox`, block `\parbox`/`\minipage`, `\vbox`/`\vtop` (as full-width block), inline `\mbox`/`\makebox`, `\raisebox`, `center`, `\hrule` | true nested VBox material | | 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/table `[htbp]` placement (top/bottom/page/here approximations) | deferred float queue, `\textfraction` | +| 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` | | 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 | 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 b4548bc..8d0c26a 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -1979,11 +1979,7 @@ impl Parser { self.figure_number += 1; } self.current_reference = self.figure_number.to_string(); - if self.figure_active - && !self.float_placement.is_empty() - && !self.float_placement.contains('h') - && (self.float_placement.contains('t') || self.float_placement.contains('p')) - { + if self.figure_active && self.float_placement.contains('p') { self.push_block(Block::PageBreak); } let index = self.document.blocks.len(); diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 67c379b..99129ab 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -11,15 +11,17 @@ styles (plain/fancy, `\thispagestyle`, fancyhdr headers/footers, `\thepage`); 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 +chapter headings with chapter-prefixed section numbering and page breaks; +deferred top-float queue for figures (`build_positioned_pages` is the only page builder and no separate break planner remains). -## 1. Deferred float queue +## 1. Float tuning and table queue -- Figures/tables currently place immediately from their source position. Add a - per-page float queue with `\textfraction`/`\topfraction` tuning so a later - float can move ahead of text while preserving source order numbers. +- 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. ## 2. True vertical boxes (remaining) diff --git a/docs/STATUS.md b/docs/STATUS.md index f156e15..52643cc 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -25,8 +25,11 @@ the source changes; the SVG preview is refreshed per page. `\addtolength`/`\noindent` aware), and `\hfill`/`\hfil` infinite glue. - Pages: A4/A5/B5/Letter/Legal/Executive, landscape, geometry margins and text block, geometry-aware `\textwidth`/`\textheight`/`\linewidth`/ - `\columnwidth` (as live named length registers), automatic pagination, `\newpage`/`\pagebreak`; figures/tables - honor `[htbp]` hints with top/bottom/page/here approximations. Page styles: + `\columnwidth` (as live named length registers), automatic pagination, `\newpage`/`\pagebreak`. + 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: default `plain` page numbers, `\pagestyle{empty|plain|fancy}`, `\thispagestyle`, fancyhdr `\fancyhead`/`\fancyfoot` (L/C/R) and `\lhead`/`\chead`/`\rhead`/`\lfoot`/`\cfoot`/`\rfoot`, with `\thepage` @@ -59,10 +62,9 @@ See `docs/ROADMAP.md` for concrete implementation plans. - Boxes: `\parbox`/`\minipage` are block-level; `\vbox`/`\vtop` map to a full-width block rather than true nested VBox material. -- Pages: float placement is immediate rather than a deferred float queue - (no `\textfraction` tuning, no reordering around later text). Pagination is - performed by the positioned renderer; the separate page-break planner was - removed. +- 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. - 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. diff --git a/src/rust_typeset/pdf/layout.rs b/src/rust_typeset/pdf/layout.rs index bb05500..64cab0d 100644 --- a/src/rust_typeset/pdf/layout.rs +++ b/src/rust_typeset/pdf/layout.rs @@ -180,8 +180,52 @@ fn top_skip_gap(ascent: i64, top_skip: i64) -> i64 { top_skip.saturating_sub(ascent).max(0) } +struct DeferredFloat { + anchor: PageAnchor, + element: PositionedFigure, + height: i64, +} + +fn flush_deferred( + pages: &mut Vec, + cursor: &mut i64, + deferred: &mut Vec, + page_goal: i64, + top_skip: i64, +) -> Option { + if deferred.is_empty() { + return None; + } + if pages.last().is_none_or(|page| !page.items.is_empty()) { + pages.push(PositionedPage::default()); + *cursor = top_skip; + } else { + *cursor = (*cursor).max(top_skip); + } + while !deferred.is_empty() { + let float = deferred.remove(0); + if *cursor > top_skip && cursor.saturating_add(float.height) > page_goal { + pages.push(PositionedPage::default()); + *cursor = top_skip; + } + let y = *cursor; + pages + .last_mut() + .expect("the page list starts with one page") + .items + .push(PositionedPageItem::Image { + anchor: float.anchor, + element: float.element, + y, + }); + *cursor = y.saturating_add(float.height); + } + Some(*cursor) +} + fn position_box( pages: &mut Vec, + deferred: &mut Vec, cursor: &mut i64, page_breaks: &HashSet, anchor: PageAnchor, @@ -217,6 +261,13 @@ fn position_box( pages.push(PositionedPage::default()); } *cursor = top_skip_gap(ascent, top_skip); + if let Some(bottom) = flush_deferred(pages, cursor, deferred, page_goal, top_skip) { + *cursor = bottom.saturating_add(block_gap); + if cursor.saturating_add(height) > page_goal { + pages.push(PositionedPage::default()); + *cursor = top_skip_gap(ascent, top_skip); + } + } } else if page_is_empty { *cursor = page_top_gap; } else { @@ -1197,10 +1248,15 @@ fn block_page_numbers(pages: &[PositionedPage]) -> HashMap { let mut starts = HashMap::new(); for (page_index, page) in pages.iter().enumerate() { for item in &page.items { - if let PositionedPageItem::Line(line) = item - && line.anchor.block != usize::MAX - { - starts.entry(line.anchor.block).or_insert(page_index); + match item { + PositionedPageItem::Line(line) if line.anchor.block != usize::MAX => { + starts.entry(line.anchor.block).or_insert(page_index); + } + PositionedPageItem::Image { anchor, .. } + | PositionedPageItem::Table { anchor, .. } => { + starts.entry(anchor.block).or_insert(page_index); + } + _ => {} } } } @@ -1260,6 +1316,7 @@ pub(super) fn build_positioned_pages( let heading_gap = (normal_line_height as f64 * 0.65).round() as i64; let page_goal = scaled_points(content_height_mm * PDF_POINTS_PER_INCH / 25.4).0 as i64; let mut pages = vec![PositionedPage::default()]; + let mut deferred: Vec = Vec::new(); let mut block_end_pages: HashMap = HashMap::new(); let mut block_start_pages: HashMap = HashMap::new(); let mut cursor = 0_i64; @@ -1297,6 +1354,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1379,6 +1437,7 @@ pub(super) fn build_positioned_pages( }); position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1469,6 +1528,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1517,6 +1577,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1551,6 +1612,7 @@ pub(super) fn build_positioned_pages( let rule_height = i64::from(height.0).max(1); position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1638,6 +1700,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1718,6 +1781,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1811,6 +1875,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1881,6 +1946,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -1964,7 +2030,17 @@ pub(super) fn build_positioned_pages( .saturating_add(block_gap) .saturating_add(figure_height) <= page_goal; - if bottom_float { + let deferred_top = + placement.contains('t') && !placement.contains('h') && !placement.contains('p'); + let defer_here = + deferred_top && !pages.last().is_none_or(|page| page.items.is_empty()); + if defer_here { + deferred.push(DeferredFloat { + anchor, + element: figure, + height: figure_height, + }); + } else if bottom_float { let y = page_goal.saturating_sub(figure_height); pages .last_mut() @@ -1979,6 +2055,7 @@ pub(super) fn build_positioned_pages( } else { position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -2000,8 +2077,10 @@ pub(super) fn build_positioned_pages( }); cursor = cursor.saturating_add(figure_height); } - pending_gap = paragraph_gap + (normal_line_height as f64 * 0.15).round() as i64; - has_previous_block = true; + if !defer_here { + pending_gap = paragraph_gap + (normal_line_height as f64 * 0.15).round() as i64; + has_previous_block = true; + } } Block::Bibliography(items) => { pre_display_size = None; @@ -2044,6 +2123,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -2094,6 +2174,7 @@ pub(super) fn build_positioned_pages( }; position_box( &mut pages, + &mut deferred, &mut cursor, page_breaks, anchor, @@ -2143,6 +2224,7 @@ pub(super) fn build_positioned_pages( pages.push(PositionedPage::default()); cursor = 0; } + flush_deferred(&mut pages, &mut cursor, &mut deferred, page_goal, top_skip); pending_gap = 0; has_previous_block = false; pre_display_size = None; @@ -2150,6 +2232,15 @@ pub(super) fn build_positioned_pages( } } + flush_deferred(&mut pages, &mut cursor, &mut deferred, page_goal, top_skip); + for (page_index, page) in pages.iter().enumerate() { + for item in &page.items { + if let PositionedPageItem::Image { anchor, .. } = item { + block_start_pages.insert(anchor.block, page_index); + } + } + } + append_page_footnotes( model, &mut pages, diff --git a/tests/typeset.rs b/tests/typeset.rs index f461d18..582ecdb 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -347,6 +347,22 @@ After text. "[t] figure on page 1" ); assert!(page_svg(&top, 1).contains("().ok()) + .unwrap_or_else(|| panic!("[t] image has no y coordinate: {}", page_svg(&top, 1))); + assert!( + top_image_y < 30.0, + "[t] figure was not placed at the top of page 2: {top_image_y}" + ); let here = compile("h"); assert_eq!(here["status"], "ok", "{}", here["message"]);