2 Commits
Author SHA1 Message Date
gloamxun 830e1f851b Reserve textfloatsep between floats and body text 2026-10-21 12:51:01 +08:00
gloamxun 1014713ec0 Reserve floatsep between stacked floats 2026-10-21 12:46:24 +08:00
4 changed files with 433 additions and 73 deletions

No files matched your search

+3 -1
View File
@@ -473,7 +473,9 @@ Route-D float placement mirrors TeX fractions:
- `Document::top_fraction_percent` defaults to 70 (`\topfraction`); a taller top float keeps body text off its page.
- `Document::bottom_fraction_percent` defaults to 30 (`\bottomfraction`); a taller bottom float moves to the next page.
- `Document::top_number`, `bottom_number` and `total_number` default to TeX's 2, 1 and 12; they cap top floats, bottom floats and all floats on one page.
- `Engine::edit` applies the float fractions and number limits, so incremental layout matches full layout.
- `Document::float_sep` defaults to 12pt (`\floatsep`); stacked top floats or stacked bottom floats reserve that gap between each other, in full layout and after `Engine::edit`.
- `Document::text_float_sep` defaults to 20pt (`\textfloatsep`); a page that carries both floats and body text reserves one such gap between them. `Page::display_list_with_layout(page_goal, text_float_sep)` mirrors that in rendered output, while `display_list_with_goal` keeps the historic no-gap behaviour for callers without document settings.
- `Engine::edit` applies the float fractions, number limits and separations, so incremental layout matches full layout.
These glyphs are rasterized at
600 dpi and shared by PDF and SVG. Ordinary math atom glyphs are rasterized from
+401 -67
View File
@@ -191,6 +191,10 @@ pub struct Float {
pub placement: FloatPlacement,
pub width: i64,
pub height: i64,
/// TeX floatsep: space inserted before this float when another float of
/// the same placement already sits on the page.
#[serde(default)]
pub gap_before: i64,
/// Vertical material carried by the float. Empty for a dimension-only float.
#[serde(default)]
pub content: Vec<VerticalItem>,
@@ -405,6 +409,7 @@ impl Unit {
placement,
width,
height,
gap_before: 0,
content: Vec::new(),
})],
)
@@ -573,12 +578,28 @@ pub struct Document {
/// first insertion class on a page. Zero by default for compatibility.
#[serde(default)]
pub insertion_skip: i64,
/// TeX floatsep: vertical space between two floats of the same placement
/// on one page. Defaults to 12pt.
#[serde(default = "default_float_sep")]
pub float_sep: i64,
/// TeX's `\textfloatsep`: space between body text and the float area at
/// the top or bottom of a text page. Defaults to 20pt.
#[serde(default = "default_text_float_sep")]
pub text_float_sep: i64,
}
fn default_text_fraction() -> u8 {
50
}
fn default_float_sep() -> i64 {
12 * 65_536
}
fn default_text_float_sep() -> i64 {
20 * 65_536
}
fn default_bottom_fraction() -> u8 {
30
}
@@ -614,6 +635,8 @@ impl Document {
total_number: default_total_number(),
insertion_classes: Vec::new(),
insertion_skip: 0,
float_sep: default_float_sep(),
text_float_sep: default_text_float_sep(),
}
}
@@ -704,7 +727,7 @@ fn top_float_height(page: &Page) -> i64 {
page.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Top)
.map(|float| float.height)
.map(|float| float.height + float.gap_before)
.sum()
}
@@ -712,10 +735,21 @@ fn bottom_float_height(page: &Page) -> i64 {
page.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Bottom)
.map(|float| float.height)
.map(|float| float.height + float.gap_before)
.sum()
}
/// Space reserved above the bottom float area: the floats themselves plus
/// `\textfloatsep` once the page carries a bottom float.
fn bottom_float_area(page: &Page, text_float_sep: i64) -> i64 {
let height = bottom_float_height(page);
if height > 0 {
height + text_float_sep.max(0)
} else {
height
}
}
/// TeX `\topfraction`: a top float taller than this share of the page keeps
/// the rest of the page free of body text.
fn top_float_exceeds_page_fraction(page: &Page, page_goal: i64, percent: u8) -> bool {
@@ -723,6 +757,17 @@ fn top_float_exceeds_page_fraction(page: &Page, page_goal: i64, percent: u8) ->
height > 0 && page.items.is_empty() && height * 100 > i64::from(percent) * page_goal
}
fn is_body_item(item: &VerticalItem) -> bool {
matches!(
item,
VerticalItem::Box(_)
| VerticalItem::Rule { .. }
| VerticalItem::Halign(_)
| VerticalItem::Paragraph(_)
| VerticalItem::Math { .. }
)
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Checkpoint {
/// Index of the next unit to execute.
@@ -834,6 +879,10 @@ impl Engine {
used = document.top_skip;
insertion_height = 0;
}
if is_body_item(item) && current.items.is_empty() && top_float_height(&current) > 0
{
used += document.text_float_sep.max(0);
}
match item {
VerticalItem::Rule { height, .. } => {
if top_float_exceeds_page_fraction(
@@ -848,7 +897,9 @@ impl Engine {
if current.items.is_empty() {
used = document.top_skip.max(*height);
} else if used + *height
> document.page_goal - insertion_height - bottom_float_height(&current)
> document.page_goal
- insertion_height
- bottom_float_area(&current, document.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip.max(*height);
@@ -871,7 +922,9 @@ impl Engine {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(vertical_box.height);
} else if used + vertical_box.height
> document.page_goal - insertion_height - bottom_float_height(&current)
> document.page_goal
- insertion_height
- bottom_float_area(&current, document.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip.max(vertical_box.height);
@@ -888,7 +941,7 @@ impl Engine {
document.page_goal
- used
- insertion_height
- bottom_float_height(&current)
- bottom_float_area(&current, document.text_float_sep)
};
if !current.items.is_empty() && available < halign.line_height {
pages.push(std::mem::replace(&mut current, Page::empty()));
@@ -924,7 +977,9 @@ impl Engine {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(paragraph.height);
} else if used + paragraph.height
> document.page_goal - insertion_height - bottom_float_height(&current)
> document.page_goal
- insertion_height
- bottom_float_area(&current, document.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip.max(paragraph.height);
@@ -948,7 +1003,9 @@ impl Engine {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(amount);
} else if used + amount
> document.page_goal - insertion_height - bottom_float_height(&current)
> document.page_goal
- insertion_height
- bottom_float_area(&current, document.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip.max(amount);
@@ -1064,23 +1121,36 @@ impl Engine {
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.map(|entry| entry.height)
.map(|entry| entry.height + entry.gap_before)
.sum();
let top_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.count();
let top_gap = if top_count > 0 {
document.float_sep.max(0)
} else {
0
};
let text_gap = if top_count == 0 {
document.text_float_sep.max(0)
} else {
0
};
let fits_on_current = !current.items.is_empty()
&& top_count < usize::from(document.top_number)
&& current.floats.len() < usize::from(document.total_number)
&& used + float.height <= document.page_goal - insertion_height
&& (existing_top + float.height) * 100
&& used + text_gap + top_gap + float.height
<= document.page_goal - insertion_height
&& (existing_top + top_gap + float.height) * 100
<= i64::from(document.top_fraction_percent)
* document.page_goal;
if fits_on_current {
used += float.height;
current.floats.push(float.clone());
let mut placed = float.clone();
placed.gap_before = top_gap;
used += text_gap + top_gap + float.height;
current.floats.push(placed);
continue;
}
if !current.items.is_empty() || !current.insertions.is_empty() {
@@ -1091,31 +1161,55 @@ impl Engine {
if current.items.is_empty() {
used = used.max(document.top_skip);
}
used += float.height;
current.floats.push(float.clone());
} else if !current.items.is_empty()
&& current
let gap = if current
.floats
.iter()
.any(|entry| entry.placement == FloatPlacement::Top)
{
document.float_sep.max(0)
} else {
0
};
let mut placed = float.clone();
placed.gap_before = gap;
used += gap + float.height;
current.floats.push(placed);
} else {
let existing_bottom = bottom_float_height(&current);
let bottom_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Bottom)
.count()
< usize::from(document.bottom_number)
&& current.floats.len() < usize::from(document.total_number)
&& used + float.height
<= document.page_goal
- insertion_height
- bottom_float_height(&current)
&& (used - document.top_skip - bottom_float_height(&current)) * 100
>= document.text_fraction_percent as i64 * document.page_goal
&& (float.height + bottom_float_height(&current)) * 100
<= document.bottom_fraction_percent as i64 * document.page_goal
{
current.floats.push(float.clone());
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
.count();
let bottom_gap = if bottom_count > 0 {
document.float_sep.max(0)
} else {
0
};
let text_gap = if bottom_count == 0 {
document.text_float_sep.max(0)
} else {
0
};
if !current.items.is_empty()
&& bottom_count < usize::from(document.bottom_number)
&& current.floats.len() < usize::from(document.total_number)
&& used + text_gap + bottom_gap + float.height
<= document.page_goal - insertion_height - existing_bottom
&& (used - document.top_skip - existing_bottom) * 100
>= document.text_fraction_percent as i64 * document.page_goal
&& (float.height + bottom_gap + existing_bottom) * 100
<= document.bottom_fraction_percent as i64 * document.page_goal
{
let mut placed = float.clone();
placed.gap_before = bottom_gap;
current.floats.push(placed);
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
}
}
}
}
@@ -1281,6 +1375,10 @@ impl Engine {
used = next.top_skip;
insertion_height = 0;
}
if is_body_item(item) && current.items.is_empty() && top_float_height(&current) > 0
{
used += next.text_float_sep.max(0);
}
match item {
VerticalItem::Rule { height, .. } => {
if top_float_exceeds_page_fraction(
@@ -1295,7 +1393,9 @@ impl Engine {
if current.items.is_empty() {
used = next.top_skip.max(*height);
} else if used + *height
> next.page_goal - insertion_height - bottom_float_height(&current)
> next.page_goal
- insertion_height
- bottom_float_area(&current, next.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip.max(*height);
@@ -1318,7 +1418,9 @@ impl Engine {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(vertical_box.height);
} else if used + vertical_box.height
> next.page_goal - insertion_height - bottom_float_height(&current)
> next.page_goal
- insertion_height
- bottom_float_area(&current, next.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip.max(vertical_box.height);
@@ -1332,7 +1434,9 @@ impl Engine {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(halign.height);
} else if used + halign.height
> next.page_goal - insertion_height - bottom_float_height(&current)
> next.page_goal
- insertion_height
- bottom_float_area(&current, next.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip.max(halign.height);
@@ -1355,7 +1459,9 @@ impl Engine {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(paragraph.height);
} else if used + paragraph.height
> next.page_goal - insertion_height - bottom_float_height(&current)
> next.page_goal
- insertion_height
- bottom_float_area(&current, next.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip.max(paragraph.height);
@@ -1379,7 +1485,9 @@ impl Engine {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(amount);
} else if used + amount
> next.page_goal - insertion_height - bottom_float_height(&current)
> next.page_goal
- insertion_height
- bottom_float_area(&current, next.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip.max(amount);
@@ -1443,22 +1551,35 @@ impl Engine {
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.map(|entry| entry.height)
.map(|entry| entry.height + entry.gap_before)
.sum();
let top_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.count();
let top_gap = if top_count > 0 {
next.float_sep.max(0)
} else {
0
};
let text_gap = if top_count == 0 {
next.text_float_sep.max(0)
} else {
0
};
let fits_on_current = !current.items.is_empty()
&& top_count < usize::from(next.top_number)
&& current.floats.len() < usize::from(next.total_number)
&& used + float.height <= next.page_goal - insertion_height
&& (existing_top + float.height) * 100
&& used + text_gap + top_gap + float.height
<= next.page_goal - insertion_height
&& (existing_top + top_gap + float.height) * 100
<= i64::from(next.top_fraction_percent) * next.page_goal;
if fits_on_current {
used += float.height;
current.floats.push(float.clone());
let mut placed = float.clone();
placed.gap_before = top_gap;
used += text_gap + top_gap + float.height;
current.floats.push(placed);
continue;
}
if !current.items.is_empty() || !current.insertions.is_empty() {
@@ -1466,29 +1587,55 @@ impl Engine {
used = next.top_skip;
insertion_height = 0;
}
used += float.height;
current.floats.push(float.clone());
} else if !current.items.is_empty()
&& current
let gap = if current
.floats
.iter()
.any(|entry| entry.placement == FloatPlacement::Top)
{
next.float_sep.max(0)
} else {
0
};
let mut placed = float.clone();
placed.gap_before = gap;
used += gap + float.height;
current.floats.push(placed);
} else {
let existing_bottom = bottom_float_height(&current);
let bottom_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Bottom)
.count()
< usize::from(next.bottom_number)
&& current.floats.len() < usize::from(next.total_number)
&& used + float.height
<= next.page_goal - insertion_height - bottom_float_height(&current)
&& (used - next.top_skip - bottom_float_height(&current)) * 100
>= next.text_fraction_percent as i64 * next.page_goal
&& (float.height + bottom_float_height(&current)) * 100
<= next.bottom_fraction_percent as i64 * next.page_goal
{
current.floats.push(float.clone());
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
.count();
let bottom_gap = if bottom_count > 0 {
next.float_sep.max(0)
} else {
0
};
let text_gap = if bottom_count == 0 {
next.text_float_sep.max(0)
} else {
0
};
if !current.items.is_empty()
&& bottom_count < usize::from(next.bottom_number)
&& current.floats.len() < usize::from(next.total_number)
&& used + text_gap + bottom_gap + float.height
<= next.page_goal - insertion_height - existing_bottom
&& (used - next.top_skip - existing_bottom) * 100
>= next.text_fraction_percent as i64 * next.page_goal
&& (float.height + bottom_gap + existing_bottom) * 100
<= next.bottom_fraction_percent as i64 * next.page_goal
{
let mut placed = float.clone();
placed.gap_before = bottom_gap;
current.floats.push(placed);
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
}
}
}
}
@@ -1826,6 +1973,7 @@ mod tests {
height: 10,
}),
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top,
width: 40,
height: 95,
@@ -1857,6 +2005,7 @@ mod tests {
height: 10,
}),
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top,
width: 40,
height: 20,
@@ -1868,7 +2017,9 @@ mod tests {
}),
],
};
let engine = Engine::layout(&Document::new(100, vec![unit]));
let mut document = Document::new(100, vec![unit]);
document.text_float_sep = 0;
let engine = Engine::layout(&document);
assert_eq!(engine.layout.pages.len(), 1);
assert_eq!(engine.layout.pages[0].floats.len(), 1);
assert_eq!(engine.layout.pages[0].items.len(), 2);
@@ -1891,6 +2042,7 @@ mod tests {
);
document.float_page_fraction_percent = 60;
document.text_fraction_percent = 10;
document.text_float_sep = 0;
assert_eq!(Engine::layout(&document).pages().len(), 2);
document.float_page_fraction_percent = 10;
assert_eq!(Engine::layout(&document).pages().len(), 1);
@@ -1995,6 +2147,7 @@ mod tests {
);
document.top_number = 1;
document.total_number = 10;
document.text_float_sep = 0;
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 3);
for page in engine.pages() {
@@ -2022,6 +2175,7 @@ mod tests {
document.bottom_number = 5;
document.total_number = 1;
document.text_fraction_percent = 0;
document.text_float_sep = 0;
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2);
assert_eq!(engine.pages()[0].floats.len(), 1);
@@ -2046,8 +2200,9 @@ mod tests {
);
document.top_number = 1;
document.total_number = 10;
document.text_float_sep = 0;
let mut engine = Engine::layout(&document);
let edited = Document::new(
let mut edited = Document::new(
1000,
vec![
Unit::paragraph(0, "alpha changed"),
@@ -2056,6 +2211,7 @@ mod tests {
Unit::float(3, FloatPlacement::Top, 10, 100),
],
);
edited.text_float_sep = 0;
engine.edit(&edited, 1, "alpha changed");
assert_eq!(engine.pages().len(), 2);
for page in engine.pages() {
@@ -2320,6 +2476,7 @@ mod tests {
height: 30,
}),
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Bottom,
width: 40,
height: 30,
@@ -2333,6 +2490,7 @@ mod tests {
};
let mut document = Document::new(60, vec![unit]);
document.bottom_fraction_percent = 100;
document.text_float_sep = 0;
let engine = Engine::layout(&document);
assert_eq!(engine.layout.pages.len(), 2);
assert_eq!(engine.layout.pages[0].floats.len(), 1);
@@ -2352,6 +2510,7 @@ mod tests {
height: 80,
}),
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Bottom,
width: 40,
height: 50,
@@ -2378,6 +2537,7 @@ mod tests {
paragraph_style: None,
items: vec![
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top,
width: 40,
height: 80,
@@ -2392,6 +2552,7 @@ mod tests {
let mut document = Document::new(100, vec![unit]);
document.top_skip = 0;
document.float_page_fraction_percent = 10;
document.text_float_sep = 0;
let engine = Engine::layout(&document);
assert_eq!(
engine.layout.pages.len(),
@@ -2704,12 +2865,14 @@ mod tests {
marks: Vec::new(),
floats: vec![
Float {
gap_before: 0,
placement: FloatPlacement::Top,
width: 50 * 65_536,
height: 15 * 65_536,
content: Vec::new(),
},
Float {
gap_before: 0,
placement: FloatPlacement::Bottom,
width: 60 * 65_536,
height: 10 * 65_536,
@@ -3204,6 +3367,7 @@ mod tests {
let mut document = Document::new(100, Vec::new());
document.top_skip = 0;
document.text_fraction_percent = 0;
document.text_float_sep = 0;
document.units.push(Unit::vertical(
1,
vec![VerticalItem::Box(VerticalBox {
@@ -3237,6 +3401,7 @@ mod tests {
fn float_content_is_painted_at_the_top_of_the_page() {
let mut document = Document::new(1000, Vec::new());
document.top_skip = 0;
document.text_float_sep = 0;
document.units.push(Unit::float_with_content(
1,
FloatPlacement::Top,
@@ -3322,6 +3487,174 @@ mod tests {
assert!(svg.contains("<rect"));
}
#[test]
fn floatsep_separates_stacked_top_floats() {
let mut document = Document::new(
1000,
vec![
Unit::float(1, FloatPlacement::Top, 100, 100),
Unit::float(2, FloatPlacement::Top, 100, 100),
Unit::paragraph(3, "body"),
],
);
document.top_skip = 0;
document.float_sep = 50;
let mut engine = Engine::layout(&document);
let page = &engine.pages()[0];
assert_eq!(page.floats.len(), 2);
assert_eq!(page.floats[0].gap_before, 0);
assert_eq!(page.floats[1].gap_before, 50);
let ys: Vec<i64> = page
.display_list()
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Rule { y, height: 100, .. } => Some(*y),
_ => None,
})
.collect();
assert_eq!(ys, vec![0, 150]);
engine.edit(&document, 3, "changed body");
assert_eq!(engine.pages()[0].floats[1].gap_before, 50);
}
#[test]
fn text_float_sep_keeps_body_below_top_floats() {
let mut document = Document::new(
1000,
vec![
Unit::float(1, FloatPlacement::Top, 100, 100),
Unit::float(2, FloatPlacement::Top, 100, 100),
Unit::rule(3, 100, 10),
],
);
document.top_skip = 0;
document.float_sep = 50;
document.text_float_sep = 30;
let engine = Engine::layout(&document);
let page = &engine.pages()[0];
assert_eq!(page.floats.len(), 2);
let ys: Vec<i64> = page
.display_list_with_layout(1000, document.text_float_sep)
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Rule { y, height: 10, .. } => Some(*y),
_ => None,
})
.collect();
assert_eq!(
ys,
vec![280],
"body starts after both floats, floatsep, and textfloatsep"
);
}
#[test]
fn text_float_sep_separates_body_from_bottom_floats() {
fn document(text_float_sep: i64) -> Document {
let mut document = Document::new(
1000,
vec![
Unit::rule(1, 100, 800),
Unit::float_with_content(
2,
FloatPlacement::Bottom,
100,
80,
vec![VerticalItem::Rule {
width: 100,
height: 80,
}],
),
],
);
document.top_skip = 0;
document.text_float_sep = text_float_sep;
document
}
assert_eq!(Engine::layout(&document(0)).pages().len(), 1);
assert_eq!(
Engine::layout(&document(200)).pages().len(),
2,
"a 200pt separation no longer leaves room for the bottom float"
);
let document = document(30);
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 1);
let ys: Vec<i64> = engine.pages()[0]
.display_list_with_layout(1000, 30)
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Rule { y, height: 80, .. } => Some(*y),
_ => None,
})
.collect();
assert_eq!(ys, vec![920], "bottom float sits below the text separation");
}
#[test]
fn text_float_sep_is_counted_once_for_a_late_top_float() {
let mut document = Document::new(
1000,
vec![
Unit::rule(1, 100, 10),
Unit::float(2, FloatPlacement::Top, 100, 100),
Unit::rule(3, 100, 10),
],
);
document.top_skip = 0;
document.text_float_sep = 30;
let engine = Engine::layout(&document);
let page = &engine.pages()[0];
assert_eq!(page.floats.len(), 1);
let ys: Vec<i64> = page
.display_list_with_layout(1000, document.text_float_sep)
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Rule { y, height: 10, .. } => Some(*y),
_ => None,
})
.collect();
assert_eq!(
ys,
vec![130, 140],
"the late float reserves one textfloatsep"
);
}
#[test]
fn incremental_layout_honours_text_float_sep() {
let mut document = Document::new(
1000,
vec![
Unit::float(1, FloatPlacement::Top, 100, 100),
Unit::paragraph(2, "body"),
],
);
document.top_skip = 0;
document.text_float_sep = 30;
let mut engine = Engine::layout(&document);
fn body_y(engine: &Engine) -> i64 {
engine.pages()[0]
.display_list_with_layout(1000, 30)
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Text { y, text, .. } if text == "b" => Some(*y),
_ => None,
})
.min()
.expect("body glyph")
}
let initial = body_y(&engine);
assert!(initial >= 130, "body starts below the reserved separation");
engine.edit(&document, 2, "changed body");
assert!(body_y(&engine) >= 130);
}
#[test]
fn float_height_covers_its_content() {
let unit = Unit::float_with_content(
@@ -3362,7 +3695,7 @@ mod tests {
}
#[test]
fn mark_and_float_constructors_build_page_items() {
let document = Document::new(
let mut document = Document::new(
100,
vec![
Unit::mark(1, 0, "Intro"),
@@ -3376,6 +3709,7 @@ mod tests {
),
],
);
document.text_float_sep = 0;
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 1);
assert_eq!(engine.pages()[0].marks[0].text, "Intro");
+28 -4
View File
@@ -312,13 +312,19 @@ impl Page {
/// Paint the page, placing insertion material at `page_goal` when it is
/// positive. A zero goal keeps insertion material in normal flow.
pub fn display_list_with_goal(&self, page_goal: i64) -> DisplayList {
self.display_list_with_layout(page_goal, 0)
}
/// Paint the page with TeX's `\textfloatsep` kept between body text and a
/// float area, matching the space the page builder reserved.
pub fn display_list_with_layout(&self, page_goal: i64, text_float_sep: i64) -> DisplayList {
let mut list = DisplayList::default();
let mut y;
let top_height: i64 = self
.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Top)
.map(|float| float.height)
.map(|float| float.height + float.gap_before)
.sum();
let mut float_y = 0_i64;
for float in self
@@ -326,10 +332,26 @@ impl Page {
.iter()
.filter(|float| float.placement == FloatPlacement::Top)
{
float_y += float.gap_before;
Self::paint_float(float, 0, float_y, &mut list);
float_y += float.height;
}
y = top_height;
let has_body = self.items.iter().any(|item| {
matches!(
item,
VerticalItem::Box(_)
| VerticalItem::Rule { .. }
| VerticalItem::Halign(_)
| VerticalItem::Paragraph(_)
| VerticalItem::Math { .. }
| VerticalItem::Glue(_)
)
});
y = if top_height > 0 && has_body {
top_height + text_float_sep.max(0)
} else {
top_height
};
let body_width = self
.items
.iter()
@@ -494,15 +516,17 @@ impl Page {
.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Bottom)
.map(|float| float.height)
.map(|float| float.height + float.gap_before)
.sum();
if bottom_height > 0 {
let mut float_y = (page_goal - bottom_height).max(y);
let bottom_area = bottom_height + text_float_sep.max(0);
let mut float_y = (page_goal - bottom_area).max(y) + text_float_sep.max(0);
for float in self
.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Bottom)
{
float_y += float.gap_before;
Self::paint_float(float, 0, float_y, &mut list);
float_y += float.height;
}
+1 -1
View File
@@ -264,7 +264,7 @@ M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic
M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments.
M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, respect `top_fraction_percent` (TeX `\topfraction`, default 70%) and `bottom_fraction_percent` (TeX `\bottomfraction`, default 30%), and can take a dedicated float page. The incremental edit path applies the same float fractions, float number limits, insertion-class limits and per-class `skip` rules as full layout.
M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, respect `top_fraction_percent` (TeX `\topfraction`, default 70%) and `bottom_fraction_percent` (TeX `\bottomfraction`, default 30%), and can take a dedicated float page. The incremental edit path applies the same float fractions, float number limits, insertion-class limits and per-class `skip` rules as full layout. `Document::float_sep` (TeX `\floatsep`, default 12pt) reserves a gap between stacked floats of the same placement in both paths. `Document::text_float_sep` (TeX `\textfloatsep`, default 20pt) reserves exactly one gap between the float block and the body text on a page that has both, again in both paths; `Page::display_list_with_layout(page_goal, text_float_sep)` applies the same gap when rendering, while `display_list_with_goal` stays gap-free for callers that do not know the document settings.
M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the painter uses 10pt glyphs in 12pt rows. Rules and spans are painted; tall tables split at row boundaries (`Halign::split_for_height`) with column widths preserved.