Add a display-list painter for halign pages
This commit is contained in:
1 parent
bdf3279aed
commit
d07ff59d31
3 files changed
+214
No files matched your search
@@ -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</text>"));
|
||||
assert!(svg.contains("<rect"));
|
||||
}
|
||||
#[test]
|
||||
fn mark_and_float_constructors_build_page_items() {
|
||||
let document = Document::new(
|
||||
|
||||
Executable
+188
@@ -0,0 +1,188 @@
|
||||
//! 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::{Halign, 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<DisplayItem>,
|
||||
}
|
||||
|
||||
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!(
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{width:.3}pt\" height=\"{height:.3}pt\" viewBox=\"0 0 {width:.3} {height:.3}\">\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!(
|
||||
"<text x=\"{x:.3}\" y=\"{y:.3}\" font-size=\"{size:.3}\" font-family=\"serif\">{}</text>\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!(
|
||||
"<rect x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" fill=\"black\"/>\n"
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
out.push_str("</svg>\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
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
Reference in new issue
Block a user