From 0c615c8e82275dd8ebb44a08d37a728b3b229a47 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Mon, 19 Oct 2026 18:39:16 +0800 Subject: [PATCH] Collect footnotes inside figure and table captions --- README.md | 5 +++-- docs/ROADMAP.md | 3 ++- docs/STATUS.md | 7 ++++--- src/rust_typeset/pdf/layout.rs | 36 +++++++++++++++++++++++++++++++--- tests/typeset.rs | 26 ++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b6aaee8..44638d9 100644 --- a/README.md +++ b/README.md @@ -344,8 +344,9 @@ separator rule at the bottom of the page. Footnote lines are painted in body coordinates so they stay below the text block rather than overlapping it. `\thanks{...}` is a footnote in the title or author block and is painted at the bottom of the first page. Its measured height is reserved before body -placement, so the last body line stays above the footnote area. Footnotes inside captions, floats, or arbitrary -boxes are not collected yet. +placement, so the last body line stays above the footnote area. Footnotes +inside figure and table captions are collected and follow the caption's page; +footnotes inside floats or arbitrary boxes are not collected yet. Basic `\input{file}` imports are read relative to the main `.tex` file. Imported files participate in source mapping and frontend cache invalidation. Local style diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index d416ba8..127737c 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -20,7 +20,8 @@ author-year citations and plainnat bibliography formatting with duplicate-year (`build_positioned_pages` is the only page builder and no separate break planner remains); conditional Base-14 font embedding and natural-width centered table layout; `\multicolumn` column spans with alignment and partial -`\cmidrule(r){a-b}`/`\cline{a-b}` rules. +`\cmidrule(r){a-b}`/`\cline{a-b}` rules; footnote capture in figure/table +captions with the insertion area reserved on the caption page. ## 1. Float tuning and table queue diff --git a/docs/STATUS.md b/docs/STATUS.md index 93de13c..f031a90 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -96,8 +96,9 @@ See `docs/ROADMAP.md` for concrete implementation plans. 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 follows the page where the paragraph ends +- Notes: footnotes inside figure/table captions are collected and painted on + the caption's page; footnotes inside floats or arbitrary boxes are not + collected yet. The insertion area follows the page where the paragraph ends rather than the exact reference line. Heading keep-with-next is approximate, so a section heading can be left at the bottom of a page with its body on the next page. @@ -115,7 +116,7 @@ See `docs/ROADMAP.md` for concrete implementation plans. - ASCII-only documents use non-embedded Base-14 Times/Courier (arXiv PDF is about 0.7 MB); documents with accented/non-WinAnsi text embed Liberation faces. Tables use natural column widths and centered rules. -- `cargo fmt --check` passes; `cargo test --offline` (50 tests: 46 compiler +- `cargo fmt --check` passes; `cargo test --offline` (51 tests: 47 compiler integration tests + 4 server tests) covers the three examples, the arXiv fixture, figure/table floats (top and bottom), newpage, page styles and `\thispagestyle`, SVG/PDF output, project API, static frontend assets, path traversal, the legacy diff --git a/src/rust_typeset/pdf/layout.rs b/src/rust_typeset/pdf/layout.rs index 7dcfcab..0e9f5b0 100644 --- a/src/rust_typeset/pdf/layout.rs +++ b/src/rust_typeset/pdf/layout.rs @@ -1114,10 +1114,18 @@ fn footnote_marker(number: u32) -> String { } fn footnote_numbers_for_block(model: &DocumentModel, block_index: usize) -> Vec { - let Some(Block::Paragraph(runs)) = model.blocks.get(block_index) else { + let Some(block) = model.blocks.get(block_index) else { return Vec::new(); }; let mut numbers = Vec::new(); + let runs = match block { + Block::Paragraph(runs) + | Block::Heading { runs, .. } + | Block::FramedBox { runs, .. } + | Block::Centered { runs } => runs.as_slice(), + Block::Image { caption, .. } => caption.as_deref().unwrap_or(&[]), + _ => &[], + }; for run in runs { if let Some(number) = run.footnote_number && !numbers.contains(&number) @@ -2822,6 +2830,8 @@ pub(super) fn build_positioned_pages( return None; } let figure_height = figure.height; + let caption_has_footnotes = + !footnote_numbers_for_block(model, block_index).is_empty(); let anchor = PageAnchor { block: block_index, line: 0, @@ -2830,13 +2840,16 @@ pub(super) fn build_positioned_pages( let bottom_float = placement.contains('b') && !placement.contains('h') && !placement.contains('t') + && !caption_has_footnotes && cursor > 0 && cursor .saturating_add(block_gap) .saturating_add(figure_height) <= page_goal; - let deferred_top = - placement.contains('t') && !placement.contains('h') && !placement.contains('p'); + let deferred_top = placement.contains('t') + && !placement.contains('h') + && !placement.contains('p') + && !caption_has_footnotes; let defer_here = deferred_top && !pages.last().is_none_or(|page| page.items.is_empty()); if defer_here { @@ -2882,6 +2895,23 @@ pub(super) fn build_positioned_pages( }); cursor = cursor.saturating_add(figure_height); } + let caption_reserve = footnote_reserve( + model, + block_index, + content_width_mm, + metrics, + font_cache, + cjk_family, + mono_family, + sans_family, + math_metrics, + math_layout, + cache, + ); + if caption_reserve > 0 && !defer_here { + cursor = cursor.saturating_add(caption_reserve); + block_end_pages.insert(block_index, pages.len().saturating_sub(1)); + } if !defer_here { pending_gap = paragraph_gap + (normal_line_height as f64 * 0.15).round() as i64; has_previous_block = true; diff --git a/tests/typeset.rs b/tests/typeset.rs index d92d182..4cf559e 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -1431,3 +1431,29 @@ See \href{https://example.com}{the example site} for details. assert!(svg.contains("fill=\"#008000\""), "{svg}"); assert!(svg.contains(">example"), "{svg}"); } + +#[test] +fn caption_footnotes_follow_the_figure_page() { + let source = r"\documentclass{article} +\usepackage{graphicx} +\begin{document} +Before.\newpage +\begin{figure}[h] +\includegraphics[width=0.5\textwidth]{examples/plot.png} +\caption{A caption with a footnote\footnote{Caption footnote text.}} +\end{figure} +After text. +\end{document}"; + let value = fresh_preview(source, "caption-footnote.tex"); + assert_eq!(value["status"], "ok", "{value}"); + let pages = pages(&value); + assert!(pages.len() >= 2, "expected a new page before the figure"); + assert!( + !page_svg(&value, 0).contains(">Caption"), + "caption footnote leaked onto the first page" + ); + assert!( + page_svg(&value, 1).contains(">Caption"), + "caption footnote should be painted on the figure page" + ); +}