diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 5b20799..0ae0c73 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1,12 +1,14 @@ pub mod cmr10; pub mod math; pub mod paragraph; +pub mod render; pub use cmr10::Cmr10Metrics; pub use paragraph::{ HorizontalBox, HorizontalItem, Paragraph, ParagraphStyle, layout_items, layout_paragraph, layout_paragraph_with_font, text_items, text_items_with_font, }; +pub use render::{DisplayItem, DisplayList}; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; @@ -1250,6 +1252,28 @@ mod tests { assert_eq!(halign.cell_box(2, 0), Some((30, 110))); } + #[test] + fn halign_display_list_paints_cells_and_rules() { + let unit = Unit::halign_from_source(1, r"A & B \\ \hline C & D", 2, 10); + let document = Document::new(1000, vec![unit]); + let engine = Engine::layout(&document); + let list = engine.pages()[0].display_list(); + let texts = list + .items + .iter() + .filter(|item| matches!(item, DisplayItem::Text { .. })) + .count(); + let rules = list + .items + .iter() + .filter(|item| matches!(item, DisplayItem::Rule { .. })) + .count(); + assert_eq!(texts, 4); + assert_eq!(rules, 1); + let svg = list.to_svg(); + assert!(svg.contains(">A")); + assert!(svg.contains(", +} + +impl DisplayList { + pub fn push(&mut self, item: DisplayItem) { + self.items.push(item); + } + + /// Offset every primitive, used when placing a nested table on a page. + pub fn place(&mut self, dx: i64, dy: i64) { + for item in &mut self.items { + match item { + DisplayItem::Text { x, y, .. } => { + *x += dx; + *y += dy; + } + DisplayItem::Rule { x, y, .. } => { + *x += dx; + *y += dy; + } + } + } + } + + /// Render the list as a standalone SVG document, in PostScript points. + pub fn to_svg(&self) -> String { + let width = self.width as f64 / 65_536.0; + let height = self.height as f64 / 65_536.0; + let mut out = format!( + "\n" + ); + for item in &self.items { + match item { + DisplayItem::Text { x, y, size, text } => { + let x = *x as f64 / 65_536.0; + let y = *y as f64 / 65_536.0; + let size = *size as f64 / 65_536.0; + out.push_str(&format!( + "{}\n", + escape(text) + )); + } + DisplayItem::Rule { + x, + y, + width, + height, + } => { + let x = *x as f64 / 65_536.0; + let y = *y as f64 / 65_536.0; + let width = *width as f64 / 65_536.0; + let height = *height as f64 / 65_536.0; + out.push_str(&format!( + "\n" + )); + } + } + } + out.push_str("\n"); + out + } +} + +fn escape(text: &str) -> String { + text.replace('&', "&") + .replace('<', "<") + .replace('>', ">") +} + +impl Halign { + /// Convert the table into text and rule primitives, with the table origin + /// at `(0, 0)`. + pub fn display_list(&self) -> DisplayList { + let mut list = DisplayList { + width: self.width, + height: self.height, + items: Vec::new(), + }; + let mut y = 0_i64; + for (row_index, row) in self.rows.iter().enumerate() { + if row.rule { + let height = 26_214; // 0.4pt in scaled points. + list.push(DisplayItem::Rule { + x: 0, + y, + width: self.width, + height, + }); + y += height; + continue; + } + let baseline = y + (self.line_height as f64 * 0.8) as i64; + for (index, cell) in row.cells.iter().enumerate() { + if let Some((x, _)) = self.cell_box(row_index, index) { + list.push(DisplayItem::Text { + x, + y: baseline, + size: self.line_height, + text: cell.text.clone(), + }); + } + } + y += self.line_height; + } + list + } +} + +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 { + let mut list = DisplayList::default(); + let mut y = 0_i64; + for item in &self.items { + match item { + VerticalItem::Box(box_) => { + y += box_.height; + } + VerticalItem::Rule { width, height } => { + list.push(DisplayItem::Rule { + x: 0, + y, + width: *width, + height: *height, + }); + y += *height; + } + VerticalItem::Glue(height) => { + y += (*height).max(0); + } + VerticalItem::Halign(halign) => { + let mut nested = halign.display_list(); + nested.place(0, y); + list.items.extend(nested.items); + y += halign.height; + } + VerticalItem::Penalty(_) | VerticalItem::Mark(_) | VerticalItem::Float(_) => {} + VerticalItem::Insertion(insertion) => { + y += insertion.height; + } + } + } + list.width = 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 f977181..867ef93 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -245,3 +245,5 @@ v1 remains the production renderer; the route-D core is additive and tested inde `\mathop` uses the Op spacing class (3mu against ordinary atoms). `\mathstrut` is a zero-width strut with 7pt height and 2pt depth. + +M3 painter prototype: `Page::display_list()` / `Halign::display_list()` produce absolute text and rule primitives, and `DisplayList::to_svg()` writes a standalone SVG. Paragraph text and math are not yet painted.