//! A small display-list painter for route-D layout output. //! //! This is deliberately independent from v1's `DocumentModel` renderer. It //! turns laid-out `\halign` tables and page rules into absolute text/rule //! primitives so the route-D core can be inspected before the PDF backend is //! wired up. use crate::cmr10::Cmr10Metrics; use crate::math::{self, MathAtom}; use crate::{Halign, HorizontalItem, Page, VerticalItem}; /// One absolute drawing primitive in scaled points. #[derive(Clone, Debug, PartialEq, Eq)] pub enum DisplayItem { Text { x: i64, y: i64, size: i64, text: String, }, Rule { x: i64, y: i64, width: i64, height: i64, }, } /// A page-local list of drawing primitives. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct DisplayList { pub width: i64, pub height: i64, pub items: Vec, } 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::Math { atom, height, depth, .. } => { let font = Cmr10Metrics::new(); Self::paint_math(atom, &font, 0, y + *height, 655_360, &mut list.items); y += *height + *depth; } VerticalItem::Paragraph(paragraph) => { let lines = paragraph.lines.len().max(1) as i64; let line_height = (paragraph.height / lines).max(1); let size = ((line_height as f64) * 0.7) as i64; for line in ¶graph.lines { let baseline = y + ((line_height as f64) * 0.8) as i64; let mut x = 0_i64; for item in line { match item { HorizontalItem::Box(box_) => { if let Some(character) = box_.character { list.push(DisplayItem::Text { x, y: baseline, size, text: character.to_string(), }); } x += box_.width; } HorizontalItem::Glue { width, .. } => x += *width, HorizontalItem::Penalty { .. } | HorizontalItem::Discretionary { .. } => {} } } y += line_height; } } 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 } fn paint_math( atom: &MathAtom, font: &Cmr10Metrics, x: i64, baseline: i64, size: i64, list: &mut Vec, ) { match atom { MathAtom::Symbol(character) => { if !character.is_whitespace() { list.push(DisplayItem::Text { x, y: baseline, size, text: character.to_string(), }); } } MathAtom::Text(text) => { let mut cursor = x; for character in text.chars() { list.push(DisplayItem::Text { x: cursor, y: baseline, size, text: character.to_string(), }); cursor += font.width(character); } } MathAtom::Row(atoms) => { let mut cursor = x; for child in atoms { let width = math::layout_math(child, font).width; Self::paint_math(child, font, cursor, baseline, size, list); cursor += width; } } MathAtom::Script { base, sub, sup } => { let base_box = math::layout_math(base, font); Self::paint_math(base, font, x, baseline, size, list); let small = (size as f64 * 0.7) as i64; if let Some(sup) = sup { Self::paint_math( sup, font, x + base_box.width, baseline - base_box.height * 2 / 3, small, list, ); } if let Some(sub) = sub { Self::paint_math( sub, font, x + base_box.width, baseline + base_box.depth * 2 / 3, small, list, ); } } MathAtom::Fraction { numerator, denominator, } => { let num = math::layout_math(numerator, font); let den = math::layout_math(denominator, font); let width = num.width.max(den.width); Self::paint_math( numerator, font, x + (width - num.width) / 2, baseline - 8 * 65_536, size, list, ); Self::paint_math( denominator, font, x + (width - den.width) / 2, baseline + 12 * 65_536, size, list, ); list.push(DisplayItem::Rule { x, y: baseline - 2 * 65_536, width, height: 26_214, }); } MathAtom::Sqrt { radicand, .. } => { let box_ = math::layout_math(radicand, font); list.push(DisplayItem::Text { x, y: baseline, size, text: "√".to_string(), }); Self::paint_math(radicand, font, x + 4 * 65_536, baseline, size, list); list.push(DisplayItem::Rule { x: x + 4 * 65_536, y: baseline - box_.height - 65_536, width: box_.width, height: 26_214, }); } MathAtom::Delimited { left, right, body } => { let body_box = math::layout_math(body, font); if *left != '.' { list.push(DisplayItem::Text { x, y: baseline, size, text: left.to_string(), }); } Self::paint_math(body, font, x + font.width(*left), baseline, size, list); if *right != '.' { list.push(DisplayItem::Text { x: x + font.width(*left) + body_box.width, y: baseline, size, text: right.to_string(), }); } } MathAtom::Accent { accent, base } => { let base_box = math::layout_math(base, font); Self::paint_math(base, font, x, baseline, size, list); list.push(DisplayItem::Text { x: x + base_box.width / 2, y: baseline - base_box.height - 65_536, size, text: accent.to_string(), }); } MathAtom::Underline { base } | MathAtom::Negated { base } => { let base_box = math::layout_math(base, font); Self::paint_math(base, font, x, baseline, size, list); list.push(DisplayItem::Rule { x, y: baseline + base_box.depth, width: base_box.width, height: 26_214, }); } MathAtom::Boxed { body } => { let body_box = math::layout_math(body, font); Self::paint_math(body, font, x + 65_536, baseline, size, list); list.push(DisplayItem::Rule { x, y: baseline - body_box.height - 65_536, width: body_box.width + 131_072, height: 26_214, }); list.push(DisplayItem::Rule { x, y: baseline + body_box.depth + 65_536, width: body_box.width + 131_072, height: 26_214, }); } MathAtom::Phantom { body, horizontal, vertical, } => { if *horizontal || !*vertical { Self::paint_math(body, font, x, baseline, size, list); } } MathAtom::StackLines(lines) => { let height = 12 * 65_536; for (index, line) in lines.iter().enumerate() { Self::paint_math(line, font, x, baseline + index as i64 * height, size, list); } } MathAtom::Stack { top, base, above } => { Self::paint_math(base, font, x, baseline, size, list); let base_box = math::layout_math(base, font); let top_box = math::layout_math(top, font); Self::paint_math( top, font, x + (base_box.width - top_box.width) / 2, if *above { baseline - base_box.height - top_box.depth } else { baseline + base_box.depth + top_box.height }, size, list, ); } MathAtom::Brace { base, .. } | MathAtom::Smash { body: base, .. } | MathAtom::Limits { base, .. } | MathAtom::Classed { body: base, .. } => { Self::paint_math(base, font, x, baseline, size, list) } MathAtom::Binomial { numerator, denominator, } => { let num = math::layout_math(numerator, font); let den = math::layout_math(denominator, font); let width = num.width.max(den.width); Self::paint_math( numerator, font, x + (width - num.width) / 2, baseline - 8 * 65_536, size, list, ); Self::paint_math( denominator, font, x + (width - den.width) / 2, baseline + 12 * 65_536, size, list, ); } MathAtom::BigOperator { name } => { for (index, character) in name.chars().enumerate() { list.push(DisplayItem::Text { x: x + index as i64 * font.width(character), y: baseline, size, text: character.to_string(), }); } } MathAtom::SizedDelimiter { delimiter, .. } => { list.push(DisplayItem::Text { x, y: baseline, size, text: delimiter.to_string(), }); } MathAtom::Strut => {} } } }