Collect footnotes inside figure and table captions

This commit is contained in:
gloamxun committed 2026-10-19 18:39:16 +08:00
1 parent 6414770de7
commit 0c615c8e82
5 files changed
+68 -9

No files matched your search

+33 -3
View File
@@ -1114,10 +1114,18 @@ fn footnote_marker(number: u32) -> String {
}
fn footnote_numbers_for_block(model: &DocumentModel, block_index: usize) -> Vec<u32> {
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;