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

+151 -58
View File
@@ -182,10 +182,25 @@ fn top_skip_gap(ascent: i64, top_skip: i64) -> i64 {
struct DeferredFloat {
anchor: PageAnchor,
element: PositionedFigure,
pieces: Vec<DeferredPiece>,
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<PositionedPage>,
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::<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 {
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 {