Keep in-text floats in the body flow
This commit is contained in:
1 parent
830e1f851b
commit
d2df1907be
4 files changed
+235
-20
No files matched your search
@@ -475,6 +475,7 @@ Route-D float placement mirrors TeX fractions:
|
||||
- `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.
|
||||
- `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.
|
||||
- `FloatPlacement::Here` keeps an `[h]` float in the body flow: `Document::in_text_sep` (TeX `\intextsep`, default 12pt) separates it from the surrounding text, the float moves to the next page when it does not fit, and it counts against `\totalnumber`. `Page::display_list_with_layout` paints the float at the same flow position the page builder reserved.
|
||||
- `Engine::edit` applies the float fractions, number limits and separations, so incremental layout matches full layout.
|
||||
|
||||
These glyphs are rasterized at
|
||||
|
||||
@@ -183,9 +183,11 @@ pub struct Mark {
|
||||
pub enum FloatPlacement {
|
||||
Top,
|
||||
Bottom,
|
||||
/// `[h]`: the float stays in the body flow, surrounded by `\intextsep`.
|
||||
Here,
|
||||
}
|
||||
|
||||
/// Deferred float material. Only top placement is implemented so far.
|
||||
/// Deferred float material for the top, bottom, or in-text placements.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Float {
|
||||
pub placement: FloatPlacement,
|
||||
@@ -195,6 +197,10 @@ pub struct Float {
|
||||
/// the same placement already sits on the page.
|
||||
#[serde(default)]
|
||||
pub gap_before: i64,
|
||||
/// TeX's `\intextsep` below an in-text float, so the renderer advances the
|
||||
/// body flow by the same gap the page builder reserved.
|
||||
#[serde(default)]
|
||||
pub gap_after: i64,
|
||||
/// Vertical material carried by the float. Empty for a dimension-only float.
|
||||
#[serde(default)]
|
||||
pub content: Vec<VerticalItem>,
|
||||
@@ -410,6 +416,7 @@ impl Unit {
|
||||
width,
|
||||
height,
|
||||
gap_before: 0,
|
||||
gap_after: 0,
|
||||
content: Vec::new(),
|
||||
})],
|
||||
)
|
||||
@@ -586,6 +593,10 @@ pub struct Document {
|
||||
/// the top or bottom of a text page. Defaults to 20pt.
|
||||
#[serde(default = "default_text_float_sep")]
|
||||
pub text_float_sep: i64,
|
||||
/// TeX's `\intextsep`: space above and below an `[h]` float that stays in
|
||||
/// the body flow. Defaults to 12pt.
|
||||
#[serde(default = "default_in_text_sep")]
|
||||
pub in_text_sep: i64,
|
||||
}
|
||||
|
||||
fn default_text_fraction() -> u8 {
|
||||
@@ -600,6 +611,10 @@ fn default_text_float_sep() -> i64 {
|
||||
20 * 65_536
|
||||
}
|
||||
|
||||
fn default_in_text_sep() -> i64 {
|
||||
12 * 65_536
|
||||
}
|
||||
|
||||
fn default_bottom_fraction() -> u8 {
|
||||
30
|
||||
}
|
||||
@@ -637,6 +652,7 @@ impl Document {
|
||||
insertion_skip: 0,
|
||||
float_sep: default_float_sep(),
|
||||
text_float_sep: default_text_float_sep(),
|
||||
in_text_sep: default_in_text_sep(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -741,6 +757,14 @@ fn bottom_float_height(page: &Page) -> i64 {
|
||||
|
||||
/// Space reserved above the bottom float area: the floats themselves plus
|
||||
/// `\textfloatsep` once the page carries a bottom float.
|
||||
/// True when a page already carries float material at the top of the body
|
||||
/// column, so a first body item must not reset the vertical cursor.
|
||||
fn page_has_top_float_material(page: &Page) -> bool {
|
||||
page.floats
|
||||
.iter()
|
||||
.any(|float| matches!(float.placement, FloatPlacement::Top | FloatPlacement::Here))
|
||||
}
|
||||
|
||||
fn bottom_float_area(page: &Page, text_float_sep: i64) -> i64 {
|
||||
let height = bottom_float_height(page);
|
||||
if height > 0 {
|
||||
@@ -894,7 +918,7 @@ impl Engine {
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
if current.items.is_empty() {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = document.top_skip.max(*height);
|
||||
} else if used + *height
|
||||
> document.page_goal
|
||||
@@ -919,7 +943,7 @@ impl Engine {
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = document.top_skip.max(vertical_box.height);
|
||||
} else if used + vertical_box.height
|
||||
> document.page_goal
|
||||
@@ -935,14 +959,15 @@ impl Engine {
|
||||
current.items.push(item.clone());
|
||||
}
|
||||
VerticalItem::Halign(halign) => {
|
||||
let mut available = if current.items.is_empty() {
|
||||
document.page_goal - document.top_skip
|
||||
} else {
|
||||
document.page_goal
|
||||
- used
|
||||
- insertion_height
|
||||
- bottom_float_area(¤t, document.text_float_sep)
|
||||
};
|
||||
let mut available =
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
document.page_goal - document.top_skip
|
||||
} else {
|
||||
document.page_goal
|
||||
- used
|
||||
- insertion_height
|
||||
- bottom_float_area(¤t, document.text_float_sep)
|
||||
};
|
||||
if !current.items.is_empty() && available < halign.line_height {
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip;
|
||||
@@ -956,7 +981,7 @@ impl Engine {
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = document.top_skip.max(chunk.height);
|
||||
} else {
|
||||
used += chunk.height;
|
||||
@@ -974,7 +999,7 @@ impl Engine {
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = document.top_skip.max(paragraph.height);
|
||||
} else if used + paragraph.height
|
||||
> document.page_goal
|
||||
@@ -1000,7 +1025,7 @@ impl Engine {
|
||||
insertion_height = 0;
|
||||
}
|
||||
let amount = height + depth;
|
||||
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = document.top_skip.max(amount);
|
||||
} else if used + amount
|
||||
> document.page_goal
|
||||
@@ -1174,6 +1199,41 @@ impl Engine {
|
||||
placed.gap_before = gap;
|
||||
used += gap + float.height;
|
||||
current.floats.push(placed);
|
||||
} else if float.placement == FloatPlacement::Here {
|
||||
let has_content =
|
||||
!current.items.is_empty() || !current.floats.is_empty();
|
||||
let sep_in = if has_content {
|
||||
document.in_text_sep.max(0)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let sep_out = document.in_text_sep.max(0);
|
||||
let fits_here = has_content
|
||||
&& current.floats.len() < usize::from(document.total_number)
|
||||
&& used + sep_in + float.height + sep_out
|
||||
<= document.page_goal
|
||||
- insertion_height
|
||||
- bottom_float_area(¤t, document.text_float_sep);
|
||||
if fits_here {
|
||||
let mut placed = float.clone();
|
||||
placed.gap_before = sep_in;
|
||||
placed.gap_after = sep_out;
|
||||
used += sep_in + float.height + sep_out;
|
||||
current.items.push(VerticalItem::Float(placed.clone()));
|
||||
current.floats.push(placed);
|
||||
} else {
|
||||
if !current.items.is_empty() || !current.floats.is_empty() {
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
let mut placed = float.clone();
|
||||
placed.gap_before = 0;
|
||||
placed.gap_after = sep_out;
|
||||
used = used.max(float.height) + sep_out;
|
||||
current.items.push(VerticalItem::Float(placed.clone()));
|
||||
current.floats.push(placed);
|
||||
}
|
||||
} else {
|
||||
let existing_bottom = bottom_float_height(¤t);
|
||||
let bottom_count = current
|
||||
@@ -1390,7 +1450,7 @@ impl Engine {
|
||||
used = next.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
if current.items.is_empty() {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = next.top_skip.max(*height);
|
||||
} else if used + *height
|
||||
> next.page_goal
|
||||
@@ -1415,7 +1475,7 @@ impl Engine {
|
||||
used = next.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = next.top_skip.max(vertical_box.height);
|
||||
} else if used + vertical_box.height
|
||||
> next.page_goal
|
||||
@@ -1431,7 +1491,7 @@ impl Engine {
|
||||
current.items.push(item.clone());
|
||||
}
|
||||
VerticalItem::Halign(halign) => {
|
||||
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = next.top_skip.max(halign.height);
|
||||
} else if used + halign.height
|
||||
> next.page_goal
|
||||
@@ -1456,7 +1516,7 @@ impl Engine {
|
||||
used = next.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = next.top_skip.max(paragraph.height);
|
||||
} else if used + paragraph.height
|
||||
> next.page_goal
|
||||
@@ -1482,7 +1542,7 @@ impl Engine {
|
||||
used = next.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = next.top_skip.max(amount);
|
||||
} else if used + amount
|
||||
> next.page_goal
|
||||
@@ -1600,6 +1660,41 @@ impl Engine {
|
||||
placed.gap_before = gap;
|
||||
used += gap + float.height;
|
||||
current.floats.push(placed);
|
||||
} else if float.placement == FloatPlacement::Here {
|
||||
let has_content =
|
||||
!current.items.is_empty() || !current.floats.is_empty();
|
||||
let sep_in = if has_content {
|
||||
next.in_text_sep.max(0)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let sep_out = next.in_text_sep.max(0);
|
||||
let fits_here = has_content
|
||||
&& current.floats.len() < usize::from(next.total_number)
|
||||
&& used + sep_in + float.height + sep_out
|
||||
<= next.page_goal
|
||||
- insertion_height
|
||||
- bottom_float_area(¤t, next.text_float_sep);
|
||||
if fits_here {
|
||||
let mut placed = float.clone();
|
||||
placed.gap_before = sep_in;
|
||||
placed.gap_after = sep_out;
|
||||
used += sep_in + float.height + sep_out;
|
||||
current.items.push(VerticalItem::Float(placed.clone()));
|
||||
current.floats.push(placed);
|
||||
} else {
|
||||
if !current.items.is_empty() || !current.floats.is_empty() {
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = next.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
let mut placed = float.clone();
|
||||
placed.gap_before = 0;
|
||||
placed.gap_after = sep_out;
|
||||
used = used.max(float.height) + sep_out;
|
||||
current.items.push(VerticalItem::Float(placed.clone()));
|
||||
current.floats.push(placed);
|
||||
}
|
||||
} else {
|
||||
let existing_bottom = bottom_float_height(¤t);
|
||||
let bottom_count = current
|
||||
@@ -1974,6 +2069,7 @@ mod tests {
|
||||
}),
|
||||
VerticalItem::Float(Float {
|
||||
gap_before: 0,
|
||||
gap_after: 0,
|
||||
placement: FloatPlacement::Top,
|
||||
width: 40,
|
||||
height: 95,
|
||||
@@ -2006,6 +2102,7 @@ mod tests {
|
||||
}),
|
||||
VerticalItem::Float(Float {
|
||||
gap_before: 0,
|
||||
gap_after: 0,
|
||||
placement: FloatPlacement::Top,
|
||||
width: 40,
|
||||
height: 20,
|
||||
@@ -2477,6 +2574,7 @@ mod tests {
|
||||
}),
|
||||
VerticalItem::Float(Float {
|
||||
gap_before: 0,
|
||||
gap_after: 0,
|
||||
placement: FloatPlacement::Bottom,
|
||||
width: 40,
|
||||
height: 30,
|
||||
@@ -2511,6 +2609,7 @@ mod tests {
|
||||
}),
|
||||
VerticalItem::Float(Float {
|
||||
gap_before: 0,
|
||||
gap_after: 0,
|
||||
placement: FloatPlacement::Bottom,
|
||||
width: 40,
|
||||
height: 50,
|
||||
@@ -2538,6 +2637,7 @@ mod tests {
|
||||
items: vec![
|
||||
VerticalItem::Float(Float {
|
||||
gap_before: 0,
|
||||
gap_after: 0,
|
||||
placement: FloatPlacement::Top,
|
||||
width: 40,
|
||||
height: 80,
|
||||
@@ -2866,6 +2966,7 @@ mod tests {
|
||||
floats: vec![
|
||||
Float {
|
||||
gap_before: 0,
|
||||
gap_after: 0,
|
||||
placement: FloatPlacement::Top,
|
||||
width: 50 * 65_536,
|
||||
height: 15 * 65_536,
|
||||
@@ -2873,6 +2974,7 @@ mod tests {
|
||||
},
|
||||
Float {
|
||||
gap_before: 0,
|
||||
gap_after: 0,
|
||||
placement: FloatPlacement::Bottom,
|
||||
width: 60 * 65_536,
|
||||
height: 10 * 65_536,
|
||||
@@ -3929,4 +4031,111 @@ mod tests {
|
||||
let decoded: Vec<Checkpoint> = serde_json::from_str(&encoded).unwrap();
|
||||
assert_eq!(engine.checkpoints(), decoded.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn here_float_reserves_intextsep_above_and_below() {
|
||||
let mut document = Document::new(
|
||||
1000,
|
||||
vec![
|
||||
Unit::rule(1, 100, 10),
|
||||
Unit::float(2, FloatPlacement::Here, 40, 20),
|
||||
Unit::rule(3, 100, 10),
|
||||
],
|
||||
);
|
||||
document.top_skip = 0;
|
||||
document.in_text_sep = 30;
|
||||
document.text_float_sep = 0;
|
||||
let engine = Engine::layout(&document);
|
||||
assert_eq!(engine.pages().len(), 1);
|
||||
let list = engine.pages()[0].display_list_with_layout(1000, 0);
|
||||
let float_y = list
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
DisplayItem::Rule {
|
||||
y,
|
||||
width: 40,
|
||||
height: 20,
|
||||
..
|
||||
} => Some(*y),
|
||||
_ => None,
|
||||
})
|
||||
.expect("here float is painted");
|
||||
assert_eq!(float_y, 40, "intextsep keeps the float below the body");
|
||||
let body: Vec<i64> = list
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
DisplayItem::Rule {
|
||||
y,
|
||||
width: 100,
|
||||
height: 10,
|
||||
..
|
||||
} => Some(*y),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(body, vec![0, 90], "intextsep separates the float and body");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn here_float_moves_to_a_new_page_when_it_does_not_fit() {
|
||||
let mut document = Document::new(
|
||||
100,
|
||||
vec![
|
||||
Unit::rule(1, 100, 70),
|
||||
Unit::float(2, FloatPlacement::Here, 40, 20),
|
||||
Unit::rule(3, 100, 10),
|
||||
],
|
||||
);
|
||||
document.top_skip = 0;
|
||||
document.in_text_sep = 10;
|
||||
document.text_float_sep = 0;
|
||||
let engine = Engine::layout(&document);
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
assert!(engine.pages()[0].floats.is_empty());
|
||||
let page = &engine.pages()[1];
|
||||
assert_eq!(page.floats.len(), 1);
|
||||
assert_eq!(page.floats[0].placement, FloatPlacement::Here);
|
||||
assert_eq!(page.items.len(), 2, "float and trailing body item");
|
||||
let ys: Vec<(i64, i64)> = page
|
||||
.display_list_with_layout(100, 0)
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
DisplayItem::Rule { y, height, .. } => Some((*y, *height)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(ys, vec![(0, 20), (30, 10)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_layout_keeps_here_floats_in_flow() {
|
||||
let mut document = Document::new(
|
||||
1000,
|
||||
vec![
|
||||
Unit::paragraph(0, "alpha"),
|
||||
Unit::float(1, FloatPlacement::Here, 40, 20),
|
||||
Unit::paragraph(2, "beta"),
|
||||
],
|
||||
);
|
||||
document.in_text_sep = 30;
|
||||
document.text_float_sep = 0;
|
||||
let mut engine = Engine::layout(&document);
|
||||
let mut edited = Document::new(
|
||||
1000,
|
||||
vec![
|
||||
Unit::paragraph(0, "alpha changed"),
|
||||
Unit::float(1, FloatPlacement::Here, 40, 20),
|
||||
Unit::paragraph(2, "beta"),
|
||||
],
|
||||
);
|
||||
edited.text_float_sep = 0;
|
||||
edited.in_text_sep = 30;
|
||||
engine.edit(&edited, 0, "alpha changed");
|
||||
assert_eq!(engine.pages().len(), 1);
|
||||
assert_eq!(engine.pages()[0].floats.len(), 1);
|
||||
assert_eq!(engine.pages()[0].floats[0].placement, FloatPlacement::Here);
|
||||
}
|
||||
}
|
||||
@@ -455,6 +455,11 @@ impl Page {
|
||||
list.items.extend(nested.items);
|
||||
y += halign.height;
|
||||
}
|
||||
VerticalItem::Float(float) if float.placement == FloatPlacement::Here => {
|
||||
y += float.gap_before;
|
||||
Self::paint_float(float, 0, y, &mut list);
|
||||
y += float.height + float.gap_after;
|
||||
}
|
||||
VerticalItem::Penalty(_) | VerticalItem::Mark(_) | VerticalItem::Float(_) => {}
|
||||
VerticalItem::Insertion(insertion) => {
|
||||
y += insertion.height;
|
||||
|
||||
+1
-1
@@ -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. `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.
|
||||
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. `FloatPlacement::Here` models `[h]` floats: they keep their place in the body flow with `Document::in_text_sep` (TeX `\intextsep`, default 12pt) above and below, move to the next page when they do not fit, and count against `\totalnumber`. The page stores the float's `gap_after` so the display list advances the flow by the same amount the builder reserved.
|
||||
|
||||
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.
|
||||
|
||||
|
||||
Reference in new issue
Block a user