diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index c1f359e..14163fc 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1371,6 +1371,37 @@ mod tests { crate::render::DisplayItem::Rule { width, .. } if *width == 100 * 65_536 ))); } + + #[test] + fn insertion_material_sits_at_the_page_bottom() { + let page = Page { + items: vec![ + VerticalItem::Box(VerticalBox { + width: 100 * 65_536, + height: 100 * 65_536, + }), + VerticalItem::Insertion(Insertion { + class: 0, + height: 20 * 65_536, + items: vec![VerticalItem::Rule { + width: 100 * 65_536, + height: 2 * 65_536, + }], + }), + ], + insertions: Vec::new(), + marks: Vec::new(), + floats: Vec::new(), + sources: Vec::new(), + hash: 0, + }; + let list = page.display_list_with_goal(700 * 65_536); + let rule = list.items.iter().find_map(|item| match item { + crate::render::DisplayItem::Rule { y, .. } => Some(*y), + _ => None, + }); + assert!(rule.expect("insertion rule") >= 697 * 65_536); + } #[test] fn link_rectangles_become_pdf_annotations() { use crate::render::DisplayList; diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index f6775eb..1ed877f 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -230,14 +230,26 @@ impl Page { /// Paint the page's vertical material. Paragraph text is not yet part of /// the route-D box model, so opaque boxes advance the cursor only. pub fn display_list(&self) -> DisplayList { + self.display_list_with_goal(0) + } + + /// 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 { let mut list = DisplayList::default(); let mut y = 0_i64; let items: Vec = self + .items + .iter() + .filter(|item| !matches!(item, VerticalItem::Insertion(_))) + .cloned() + .collect(); + let insertion_items: Vec = self .items .iter() .flat_map(|item| match item { VerticalItem::Insertion(insertion) => insertion.items.clone(), - _ => vec![item.clone()], + _ => Vec::new(), }) .collect(); for item in &items { @@ -307,16 +319,39 @@ impl Page { } } } - list.width = self - .items - .iter() - .map(|item| match item { - VerticalItem::Halign(halign) => halign.width, - VerticalItem::Rule { width, .. } => *width, - _ => 0, - }) - .max() - .unwrap_or(0); + if !insertion_items.is_empty() { + let insertion_page = Page { + items: insertion_items, + insertions: Vec::new(), + marks: Vec::new(), + floats: Vec::new(), + sources: Vec::new(), + hash: 0, + }; + let mut insertion_list = insertion_page.display_list(); + let top = if page_goal > 0 { + (page_goal - insertion_list.height).max(y) + } else { + y + }; + insertion_list.place(0, top); + let width = insertion_list.width.max(list.width); + y = top + insertion_list.height; + list.items.extend(insertion_list.items); + list.images.extend(insertion_list.images); + list.width = list.width.max(width); + } + list.width = list.width.max( + self.items + .iter() + .map(|item| match item { + VerticalItem::Halign(halign) => halign.width, + VerticalItem::Rule { width, .. } => *width, + _ => 0, + }) + .max() + .unwrap_or(0), + ); list.height = y; list } diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index cd270e3..86f3fd8 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -259,3 +259,5 @@ M6 (partial): display-list Link rectangles become PDF /Annots /Subtype /Link /UR M7 (partial): DisplayList::special injects raw PDF content-stream operators; SVG previews show a comment. Driver-level \special parsing and coordinate transforms remain. M2 (partial): insertion material is expanded into the page display list at its reserved position, so footnote/float material is no longer dropped by the painter; page-bottom placement and rules remain to be modelled. + +M2 (partial): Page::display_list_with_goal places insertion material at page_goal minus its height; page builders can pass the real goal. Footnote rules and float-page policy remain.