Place top floats on the page they appear on

This commit is contained in:
gloamxun committed 2026-10-23 23:15:56 +08:00
1 parent a78e0c1d0d
commit db1cc98338
6 files changed
+101 -21

No files matched your search

+6 -5
View File
@@ -6450,7 +6450,7 @@ mod tests {
}
#[test]
fn paged_layout_defers_a_late_top_float() {
fn paged_layout_puts_a_late_top_float_on_its_own_page() {
let mut document = Document::new(
100,
vec![
@@ -6461,10 +6461,11 @@ mod tests {
);
document.text_float_sep = 0;
let layout = Engine::layout_paged(&document).expect("a late float still builds");
assert_eq!(layout.pages().len(), 2);
assert!(layout.pages()[0].floats.is_empty());
assert_eq!(layout.pages()[1].floats.len(), 1);
assert_eq!(layout.pages()[1].items.len(), 1);
// TeX installs the float at the top of the page it is seen on, so the
// float and both rules share one page.
assert_eq!(layout.pages().len(), 1);
assert_eq!(layout.pages()[0].floats.len(), 1);
assert_eq!(layout.pages()[0].items.len(), 2);
}
#[test]
+19 -7
View File
@@ -323,13 +323,11 @@ impl PageBuilder {
height: float_height,
} => {
if height > 0
&& (has_body
|| top_count >= self.top_number
|| float_total >= self.total_number)
&& (top_count >= self.top_number || float_total >= self.total_number)
{
// TeX never places a top float below body material,
// nor above the `\topnumber`/`\totalnumber` limits:
// the float starts the next page instead.
// TeX never puts more top floats on one page than
// `\topnumber`/`\totalnumber` allow: the float starts
// the next page instead.
let end = index.max(start);
let badness =
self.badness(height, reserved, stretch, shrink, stretch_order);
@@ -354,7 +352,21 @@ impl PageBuilder {
self.insertion_overflow(nodes, start, end, held),
));
}
height += (*float_height).max(0);
// TeX installs a `[t]` float at the top of the page it is
// seen on, pushing the body down by its height plus
// `\textfloatsep` (or `\floatsep` between stacked
// floats). Only a page that breaks before the float moves
// it to the next page.
if height > 0 {
height += (*float_height).max(0)
+ if has_float {
self.float_sep.max(0)
} else {
self.text_float_sep.max(0)
};
} else {
height += (*float_height).max(0);
}
has_float = true;
top_count += 1;
float_total += 1;
+66 -1
View File
@@ -10,7 +10,7 @@ use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use texrra_tex_core::paragraph::{HorizontalItem, ParagraphStyle};
use texrra_tex_core::{Document, Engine, Unit, VerticalItem, layout_paragraph};
use texrra_tex_core::{Document, Engine, FloatPlacement, Unit, VerticalItem, layout_paragraph};
/// Scaled points per TeX point.
const PT: i64 = 65_536;
@@ -935,6 +935,71 @@ fn pages_split_paragraphs_like_pdftex() {
}
}
/// A `[t]` float deferred to the next page top reserves its height plus
/// `\textfloatsep`; body breaks must stay in step with pdfLaTeX.
#[test]
fn top_floats_reserve_page_space_like_pdftex() {
let paragraphs: Vec<String> = (0..80)
.map(|index| format!("p{index} alpha beta gamma"))
.collect();
let float_at = 9_usize;
let mut body = String::new();
for (index, paragraph) in paragraphs.iter().enumerate() {
body.push_str(paragraph);
body.push_str("\\par\n");
if index == float_at {
body.push_str("\\begin{figure}[t]\\rule{100pt}{50pt}\\end{figure}\n");
}
}
let source = format!(
"\\documentclass[10pt]{{article}}\n\
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
\\pagestyle{{empty}}\n\
\\begin{{document}}\n\
\\hsize=345pt \\parindent=15pt \\parskip=0pt \\topskip=12pt \\vsize=550pt\n\
{body}\\end{{document}}\n"
);
let Some(reference) = pdftex_pages(&source) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let style = ParagraphStyle {
line_width: 345 * PT,
par_indent: 15 * PT,
line_height: 12 * PT,
..ParagraphStyle::default()
};
let mut units = Vec::new();
let mut next_id = 1_u64;
for (index, paragraph) in paragraphs.iter().enumerate() {
units.push(Unit::paragraph_with_style(
next_id,
paragraph.clone(),
style.clone(),
));
next_id += 1;
if index == float_at {
units.push(Unit::float(next_id, FloatPlacement::Top, 100 * PT, 50 * PT));
next_id += 1;
}
}
let mut document = Document::new(550 * PT, units);
document.top_skip = 12 * PT;
document.text_float_sep = 20 * PT;
let engine = Engine::layout_paged(&document).expect("top floats are page-buildable");
let ours: Vec<String> = engine.pages().iter().map(page_alnum_text).collect();
assert_eq!(
ours.len(),
reference.len(),
"route-D produced {} pages, pdfLaTeX {}",
ours.len(),
reference.len()
);
for (index, (ours, reference)) in ours.iter().zip(&reference).enumerate() {
assert_eq!(ours, reference, "body text on page {}", index + 1);
}
}
/// `\footnotetext` raises insertion material that is reserved at the page
/// bottom. With slack on each page, the body breaks and per-page body text
/// must stay in step with pdfLaTeX even though the note text is not compared.