1029 lines
37 KiB
Rust
Executable File
1029 lines
37 KiB
Rust
Executable File
//! 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::paragraph::ParagraphFont;
|
|
use crate::{Float, FloatPlacement, Halign, HorizontalItem, Page, VerticalItem};
|
|
|
|
/// One absolute drawing primitive in scaled points.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub enum FontFace {
|
|
Roman,
|
|
Bold,
|
|
Italic,
|
|
BoldItalic,
|
|
Mono,
|
|
Sans,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub enum DisplayItem {
|
|
Text {
|
|
x: i64,
|
|
y: i64,
|
|
size: i64,
|
|
text: String,
|
|
font: FontFace,
|
|
},
|
|
Rule {
|
|
x: i64,
|
|
y: i64,
|
|
width: i64,
|
|
height: i64,
|
|
},
|
|
Line {
|
|
x1: i64,
|
|
y1: i64,
|
|
x2: i64,
|
|
y2: i64,
|
|
width: i64,
|
|
},
|
|
Image {
|
|
x: i64,
|
|
y: i64,
|
|
width: i64,
|
|
height: i64,
|
|
index: usize,
|
|
},
|
|
Link {
|
|
x: i64,
|
|
y: i64,
|
|
width: i64,
|
|
height: i64,
|
|
url: String,
|
|
},
|
|
Special(String),
|
|
}
|
|
|
|
/// A raw RGB image that can be painted as a PDF XObject.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct PdfImage {
|
|
pub width: u32,
|
|
pub height: u32,
|
|
pub rgb: Vec<u8>,
|
|
}
|
|
|
|
/// 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>,
|
|
pub images: Vec<PdfImage>,
|
|
}
|
|
|
|
impl DisplayList {
|
|
pub fn push(&mut self, item: DisplayItem) {
|
|
self.items.push(item);
|
|
}
|
|
|
|
pub fn special(&mut self, content: &str) {
|
|
self.items.push(DisplayItem::Special(content.to_string()));
|
|
}
|
|
|
|
pub fn link(&mut self, x: i64, y: i64, width: i64, height: i64, url: &str) {
|
|
self.items.push(DisplayItem::Link {
|
|
x,
|
|
y,
|
|
width,
|
|
height,
|
|
url: url.to_string(),
|
|
});
|
|
}
|
|
|
|
pub fn add_image(&mut self, image: PdfImage) -> usize {
|
|
self.images.push(image);
|
|
self.images.len() - 1
|
|
}
|
|
|
|
/// 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;
|
|
}
|
|
DisplayItem::Line { x1, y1, x2, y2, .. } => {
|
|
*x1 += dx;
|
|
*y1 += dy;
|
|
*x2 += dx;
|
|
*y2 += dy;
|
|
}
|
|
DisplayItem::Image { x, y, .. } | DisplayItem::Link { x, y, .. } => {
|
|
*x += dx;
|
|
*y += dy;
|
|
}
|
|
DisplayItem::Special(_) => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 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,
|
|
font,
|
|
} => {
|
|
let x = *x as f64 / 65_536.0;
|
|
let y = *y as f64 / 65_536.0;
|
|
let size = *size as f64 / 65_536.0;
|
|
let family = if matches!(font, FontFace::Mono) {
|
|
"monospace"
|
|
} else if matches!(font, FontFace::Sans) {
|
|
"sans-serif"
|
|
} else {
|
|
"serif"
|
|
};
|
|
let weight = if matches!(font, FontFace::Bold | FontFace::BoldItalic) {
|
|
" font-weight=\"bold\""
|
|
} else {
|
|
""
|
|
};
|
|
let style = if matches!(font, FontFace::Italic | FontFace::BoldItalic) {
|
|
" font-style=\"italic\""
|
|
} else {
|
|
""
|
|
};
|
|
out.push_str(&format!(
|
|
"<text x=\"{x:.3}\" y=\"{y:.3}\" font-size=\"{size:.3}\" font-family=\"{family}\"{weight}{style}>{}</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"
|
|
));
|
|
}
|
|
DisplayItem::Link {
|
|
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=\"none\" stroke=\"#008000\"/>
|
|
"
|
|
));
|
|
}
|
|
DisplayItem::Special(content) => {
|
|
out.push_str(&format!(
|
|
"<!-- special: {} -->
|
|
",
|
|
escape(content)
|
|
));
|
|
}
|
|
DisplayItem::Line {
|
|
x1,
|
|
y1,
|
|
x2,
|
|
y2,
|
|
width,
|
|
} => {
|
|
let x1 = *x1 as f64 / 65_536.0;
|
|
let y1 = *y1 as f64 / 65_536.0;
|
|
let x2 = *x2 as f64 / 65_536.0;
|
|
let y2 = *y2 as f64 / 65_536.0;
|
|
let width = *width as f64 / 65_536.0;
|
|
out.push_str(&format!(
|
|
"<line x1=\"{x1:.3}\" y1=\"{y1:.3}\" x2=\"{x2:.3}\" y2=\"{y2:.3}\" stroke=\"#000000\" stroke-width=\"{width:.3}\"/>\n"
|
|
));
|
|
}
|
|
DisplayItem::Image {
|
|
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=\"#cccccc\"/>\n"
|
|
));
|
|
}
|
|
}
|
|
}
|
|
out.push_str("</svg>\n");
|
|
out
|
|
}
|
|
}
|
|
|
|
fn escape(text: &str) -> String {
|
|
text.replace('&', "&")
|
|
.replace('<', "<")
|
|
.replace('>', ">")
|
|
}
|
|
|
|
fn character_text(character: char) -> String {
|
|
match character {
|
|
'\u{FB00}' => "ff".to_owned(),
|
|
'\u{FB01}' => "fi".to_owned(),
|
|
'\u{FB02}' => "fl".to_owned(),
|
|
'\u{FB03}' => "ffi".to_owned(),
|
|
'\u{FB04}' => "ffl".to_owned(),
|
|
_ => character.to_string(),
|
|
}
|
|
}
|
|
|
|
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(),
|
|
images: 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 {
|
|
font: FontFace::Roman,
|
|
x,
|
|
y: baseline,
|
|
size: 10 * 65_536,
|
|
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 {
|
|
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 {
|
|
self.display_list_with_layout(page_goal, 0)
|
|
}
|
|
|
|
/// Paint the page with TeX's `\textfloatsep` kept between body text and a
|
|
/// float area, matching the space the page builder reserved.
|
|
pub fn display_list_with_layout(&self, page_goal: i64, text_float_sep: i64) -> DisplayList {
|
|
let mut list = DisplayList::default();
|
|
let mut y;
|
|
let top_height: i64 = self
|
|
.floats
|
|
.iter()
|
|
.filter(|float| float.placement == FloatPlacement::Top)
|
|
.map(|float| float.height + float.gap_before)
|
|
.sum();
|
|
let mut float_y = 0_i64;
|
|
for float in self
|
|
.floats
|
|
.iter()
|
|
.filter(|float| float.placement == FloatPlacement::Top)
|
|
{
|
|
float_y += float.gap_before;
|
|
Self::paint_float(float, 0, float_y, &mut list);
|
|
float_y += float.height;
|
|
}
|
|
let has_body = self.items.iter().any(|item| {
|
|
matches!(
|
|
item,
|
|
VerticalItem::Box(_)
|
|
| VerticalItem::Rule { .. }
|
|
| VerticalItem::Halign(_)
|
|
| VerticalItem::Paragraph(_)
|
|
| VerticalItem::Math { .. }
|
|
| VerticalItem::Glue(_)
|
|
)
|
|
});
|
|
y = if top_height > 0 && has_body {
|
|
top_height + text_float_sep.max(0)
|
|
} else {
|
|
top_height
|
|
};
|
|
let body_width = self
|
|
.items
|
|
.iter()
|
|
.map(|item| match item {
|
|
VerticalItem::Halign(halign) => halign.width,
|
|
VerticalItem::Rule { width, .. } => *width,
|
|
VerticalItem::Box(box_) => box_.width,
|
|
VerticalItem::Paragraph(paragraph) => paragraph.width,
|
|
VerticalItem::Math { width, .. } => *width,
|
|
_ => 0,
|
|
})
|
|
.max()
|
|
.unwrap_or(0);
|
|
let items: Vec<VerticalItem> = self
|
|
.items
|
|
.iter()
|
|
.filter(|item| !matches!(item, VerticalItem::Insertion(_)))
|
|
.cloned()
|
|
.collect();
|
|
for item in &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 face = match paragraph.font {
|
|
ParagraphFont::Roman => FontFace::Roman,
|
|
ParagraphFont::Bold => FontFace::Bold,
|
|
ParagraphFont::Italic => FontFace::Italic,
|
|
ParagraphFont::Mono => FontFace::Mono,
|
|
ParagraphFont::Sans => FontFace::Sans,
|
|
};
|
|
let lines = paragraph.lines.len().max(1) as i64;
|
|
let line_height = (paragraph.height / lines).max(1);
|
|
let size = 10 * 65_536;
|
|
for (line_index, line) in paragraph.lines.iter().enumerate() {
|
|
let indent = paragraph.line_indents.get(line_index).copied().unwrap_or(0);
|
|
let baseline = y + paragraph
|
|
.line_baselines
|
|
.get(line_index)
|
|
.copied()
|
|
.unwrap_or(((line_height as f64) * 0.8) as i64);
|
|
let natural: i64 = line
|
|
.iter()
|
|
.map(|item| match item {
|
|
HorizontalItem::Box(box_) => box_.width,
|
|
HorizontalItem::Glue { width, .. } => *width,
|
|
HorizontalItem::Penalty { .. }
|
|
| HorizontalItem::Discretionary { .. } => 0,
|
|
})
|
|
.sum();
|
|
let mut x = if paragraph.centered {
|
|
paragraph.left_skip + indent + ((paragraph.width - natural) / 2).max(0)
|
|
} else if paragraph.right_aligned {
|
|
paragraph.left_skip + indent + (paragraph.width - natural).max(0)
|
|
} else {
|
|
paragraph.left_skip + indent
|
|
};
|
|
for item in line {
|
|
match item {
|
|
HorizontalItem::Box(box_) => {
|
|
if let Some(character) = box_.character {
|
|
list.push(DisplayItem::Text {
|
|
font: face,
|
|
x,
|
|
y: baseline,
|
|
size,
|
|
text: character_text(character),
|
|
});
|
|
}
|
|
x += box_.width;
|
|
}
|
|
HorizontalItem::Glue { width, .. } => x += *width,
|
|
HorizontalItem::Penalty { .. }
|
|
| HorizontalItem::Discretionary { .. } => {}
|
|
}
|
|
}
|
|
}
|
|
y += paragraph.height;
|
|
}
|
|
VerticalItem::Halign(halign) => {
|
|
let mut nested = halign.display_list();
|
|
nested.place(0, y);
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
let mut class_groups: Vec<(u8, Vec<VerticalItem>)> = Vec::new();
|
|
let inline_insertions = self.items.iter().filter_map(|item| match item {
|
|
VerticalItem::Insertion(insertion) => Some(insertion),
|
|
_ => None,
|
|
});
|
|
for insertion in inline_insertions.chain(self.insertions.iter()) {
|
|
if let Some((_, items)) = class_groups
|
|
.iter_mut()
|
|
.find(|(class, _)| *class == insertion.class)
|
|
{
|
|
items.extend(insertion.items.iter().cloned());
|
|
} else {
|
|
class_groups.push((insertion.class, insertion.items.clone()));
|
|
}
|
|
}
|
|
class_groups.sort_by_key(|(class, _)| *class);
|
|
let body_bottom = y;
|
|
let mut insertion_bottom = page_goal;
|
|
for (class, items) in class_groups {
|
|
let insertion_page = Page {
|
|
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 {
|
|
(insertion_bottom - insertion_list.height).max(body_bottom)
|
|
} else {
|
|
y
|
|
};
|
|
insertion_list.place(0, top);
|
|
let width = insertion_list.width.max(list.width);
|
|
if class == 0 && page_goal > 0 && top > body_bottom + 26_214 {
|
|
let separator_width = (body_width * 2 / 5).max(65_536);
|
|
list.push(DisplayItem::Rule {
|
|
x: 0,
|
|
y: top - 26_214,
|
|
width: separator_width,
|
|
height: 26_214,
|
|
});
|
|
}
|
|
y = top + insertion_list.height;
|
|
list.items.extend(insertion_list.items);
|
|
list.images.extend(insertion_list.images);
|
|
list.width = list.width.max(width);
|
|
insertion_bottom = top.saturating_sub(131_072);
|
|
}
|
|
if page_goal > 0 {
|
|
let bottom_height: i64 = self
|
|
.floats
|
|
.iter()
|
|
.filter(|float| float.placement == FloatPlacement::Bottom)
|
|
.map(|float| float.height + float.gap_before)
|
|
.sum();
|
|
if bottom_height > 0 {
|
|
let bottom_area = bottom_height + text_float_sep.max(0);
|
|
let mut float_y = (page_goal - bottom_area).max(y) + text_float_sep.max(0);
|
|
for float in self
|
|
.floats
|
|
.iter()
|
|
.filter(|float| float.placement == FloatPlacement::Bottom)
|
|
{
|
|
float_y += float.gap_before;
|
|
Self::paint_float(float, 0, float_y, &mut list);
|
|
float_y += float.height;
|
|
}
|
|
y = y.max(float_y);
|
|
}
|
|
}
|
|
list.width = list.width.max(
|
|
self.items
|
|
.iter()
|
|
.map(|item| match item {
|
|
VerticalItem::Halign(halign) => halign.width,
|
|
VerticalItem::Rule { width, .. } => *width,
|
|
VerticalItem::Paragraph(paragraph) => paragraph.width,
|
|
VerticalItem::Math { width, .. } => *width,
|
|
_ => 0,
|
|
})
|
|
.max()
|
|
.unwrap_or(0),
|
|
);
|
|
list.height = y;
|
|
list
|
|
}
|
|
|
|
/// Paint a float's vertical material, or a dimension rectangle when the
|
|
/// float carries no content.
|
|
fn paint_float(float: &Float, x: i64, y: i64, list: &mut DisplayList) {
|
|
if float.content.is_empty() {
|
|
list.push(DisplayItem::Rule {
|
|
x,
|
|
y,
|
|
width: float.width,
|
|
height: float.height,
|
|
});
|
|
list.width = list.width.max(float.width);
|
|
return;
|
|
}
|
|
let page = Page {
|
|
items: float.content.clone(),
|
|
insertions: Vec::new(),
|
|
marks: Vec::new(),
|
|
floats: Vec::new(),
|
|
sources: Vec::new(),
|
|
hash: 0,
|
|
};
|
|
let mut content = page.display_list();
|
|
content.place(x, y);
|
|
list.items.extend(content.items);
|
|
list.images.extend(content.images);
|
|
list.width = list.width.max(content.width);
|
|
}
|
|
fn paint_math(
|
|
atom: &MathAtom,
|
|
font: &Cmr10Metrics,
|
|
x: i64,
|
|
baseline: i64,
|
|
size: i64,
|
|
list: &mut Vec<DisplayItem>,
|
|
) {
|
|
match atom {
|
|
MathAtom::Space(_) => {}
|
|
MathAtom::Symbol(character) => {
|
|
if !character.is_whitespace() {
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x,
|
|
y: baseline,
|
|
size,
|
|
text: character.to_string(),
|
|
});
|
|
}
|
|
}
|
|
MathAtom::Text(text) => {
|
|
let mut cursor = x;
|
|
for character in text.chars() {
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
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 italic = math::italic_correction(base, font);
|
|
let small = (size as f64 * 0.7) as i64;
|
|
if let Some(sup) = sup {
|
|
Self::paint_math(
|
|
sup,
|
|
font,
|
|
x + base_box.width + italic,
|
|
baseline - base_box.height * 2 / 3,
|
|
small,
|
|
list,
|
|
);
|
|
}
|
|
if let Some(sub) = sub {
|
|
Self::paint_math(
|
|
sub,
|
|
font,
|
|
x + base_box.width + italic,
|
|
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);
|
|
let bracket = (size as f64 * 1.2) as i64;
|
|
let inner_x = x + (width - num.width) / 2 + 2 * 65_536;
|
|
Self::paint_math(numerator, font, inner_x, baseline - 8 * 65_536, size, list);
|
|
Self::paint_math(
|
|
denominator,
|
|
font,
|
|
x + (width - den.width) / 2 + 2 * 65_536,
|
|
baseline + 12 * 65_536,
|
|
size,
|
|
list,
|
|
);
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x,
|
|
y: baseline,
|
|
size: bracket,
|
|
text: "(".to_string(),
|
|
});
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x: x + width + 4 * 65_536,
|
|
y: baseline,
|
|
size: bracket,
|
|
text: ")".to_string(),
|
|
});
|
|
list.push(DisplayItem::Rule {
|
|
x,
|
|
y: baseline - 2 * 65_536,
|
|
width,
|
|
height: 26_214,
|
|
});
|
|
}
|
|
MathAtom::Sqrt { radicand, degree } => {
|
|
let box_ = math::layout_math(radicand, font);
|
|
let small = (size as f64 * 0.7) as i64;
|
|
let mut radical_x = x;
|
|
if let Some(degree) = degree {
|
|
let degree_box = math::layout_math(degree, font);
|
|
Self::paint_math(degree, font, x, baseline - box_.height - small, small, list);
|
|
radical_x += degree_box.width;
|
|
}
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x: radical_x,
|
|
y: baseline,
|
|
size,
|
|
text: "√".to_string(),
|
|
});
|
|
Self::paint_math(radicand, font, radical_x + 4 * 65_536, baseline, size, list);
|
|
list.push(DisplayItem::Rule {
|
|
x: radical_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);
|
|
let scale = math::delimiter_scale(&body_box);
|
|
let delimiter_size = (size as f64 * scale) as i64;
|
|
let left_width = (font.width(*left) as f64 * scale) as i64;
|
|
let delimiter_shift = ((delimiter_size - size) as f64 * 0.35) as i64;
|
|
if *left != '.' {
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x,
|
|
y: baseline + delimiter_shift,
|
|
size: delimiter_size,
|
|
text: left.to_string(),
|
|
});
|
|
}
|
|
Self::paint_math(body, font, x + left_width, baseline, size, list);
|
|
if *right != '.' {
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x: x + left_width + body_box.width,
|
|
y: baseline + delimiter_shift,
|
|
size: delimiter_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 {
|
|
font: FontFace::Roman,
|
|
x: x + base_box.width / 2,
|
|
y: baseline - base_box.height - 65_536,
|
|
size,
|
|
text: accent.to_string(),
|
|
});
|
|
}
|
|
MathAtom::Underline { 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::Cancel {
|
|
base,
|
|
forward,
|
|
backward,
|
|
} => {
|
|
let base_box = math::layout_math(base, font);
|
|
Self::paint_math(base, font, x, baseline, size, list);
|
|
if *forward {
|
|
list.push(DisplayItem::Line {
|
|
x1: x,
|
|
y1: baseline + base_box.depth,
|
|
x2: x + base_box.width,
|
|
y2: baseline - base_box.height,
|
|
width: 26_214,
|
|
});
|
|
}
|
|
if *backward {
|
|
list.push(DisplayItem::Line {
|
|
x1: x,
|
|
y1: baseline - base_box.height,
|
|
x2: x + base_box.width,
|
|
y2: baseline + base_box.depth,
|
|
width: 26_214,
|
|
});
|
|
}
|
|
}
|
|
MathAtom::Negated { base } => {
|
|
let base_box = math::layout_math(base, font);
|
|
Self::paint_math(base, font, x, baseline, size, list);
|
|
list.push(DisplayItem::Line {
|
|
x1: x,
|
|
y1: baseline + base_box.depth,
|
|
x2: x + base_box.width,
|
|
y2: baseline - base_box.height,
|
|
width: 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::Limits { 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 {
|
|
let sup_box = math::layout_math(sup, font);
|
|
Self::paint_math(
|
|
sup,
|
|
font,
|
|
x + (base_box.width - sup_box.width).max(0) / 2,
|
|
baseline - base_box.height - small,
|
|
small,
|
|
list,
|
|
);
|
|
}
|
|
if let Some(sub) = sub {
|
|
let sub_box = math::layout_math(sub, font);
|
|
Self::paint_math(
|
|
sub,
|
|
font,
|
|
x + (base_box.width - sub_box.width).max(0) / 2,
|
|
baseline + base_box.depth + small,
|
|
small,
|
|
list,
|
|
);
|
|
}
|
|
}
|
|
MathAtom::Brace { base, above } => {
|
|
let base_box = math::layout_math(base, font);
|
|
Self::paint_math(base, font, x, baseline, size, list);
|
|
let thickness = (size / 8).max(1);
|
|
let y = if *above {
|
|
baseline - base_box.height - size / 2
|
|
} else {
|
|
baseline + base_box.depth + size / 2
|
|
};
|
|
list.push(DisplayItem::Rule {
|
|
x,
|
|
y,
|
|
width: base_box.width,
|
|
height: thickness,
|
|
});
|
|
let tick = size / 2;
|
|
for tick_x in [x, x + base_box.width - thickness] {
|
|
list.push(DisplayItem::Rule {
|
|
x: tick_x,
|
|
y: if *above { y + thickness } else { y - tick },
|
|
width: thickness,
|
|
height: tick,
|
|
});
|
|
}
|
|
}
|
|
MathAtom::Smash { body: 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);
|
|
let bracket = (size as f64 * 1.2) as i64;
|
|
Self::paint_math(
|
|
numerator,
|
|
font,
|
|
x + (width - num.width) / 2 + 2 * 65_536,
|
|
baseline - 8 * 65_536,
|
|
size,
|
|
list,
|
|
);
|
|
Self::paint_math(
|
|
denominator,
|
|
font,
|
|
x + (width - den.width) / 2 + 2 * 65_536,
|
|
baseline + 12 * 65_536,
|
|
size,
|
|
list,
|
|
);
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x,
|
|
y: baseline,
|
|
size: bracket,
|
|
text: "(".to_string(),
|
|
});
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x: x + width + 4 * 65_536,
|
|
y: baseline,
|
|
size: bracket,
|
|
text: ")".to_string(),
|
|
});
|
|
}
|
|
MathAtom::BigOperator { name } => {
|
|
for (index, character) in name.chars().enumerate() {
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x: x + index as i64 * font.width(character),
|
|
y: baseline,
|
|
size,
|
|
text: character.to_string(),
|
|
});
|
|
}
|
|
}
|
|
MathAtom::SizedDelimiter { delimiter, .. } => {
|
|
list.push(DisplayItem::Text {
|
|
font: FontFace::Roman,
|
|
x,
|
|
y: baseline,
|
|
size,
|
|
text: delimiter.to_string(),
|
|
});
|
|
}
|
|
MathAtom::Strut => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn tall_delimiters_are_lowered_to_stay_centered() {
|
|
let atom = crate::math::parse_math(r"\left(\frac{a}{b}\right)");
|
|
let mut items = Vec::new();
|
|
Page::paint_math(
|
|
&atom,
|
|
&Cmr10Metrics::default(),
|
|
0,
|
|
100,
|
|
10 * 65_536,
|
|
&mut items,
|
|
);
|
|
let paren_y = items
|
|
.iter()
|
|
.find_map(|item| match item {
|
|
DisplayItem::Text { text, y, .. } if text == "(" => Some(*y),
|
|
_ => None,
|
|
})
|
|
.expect("left delimiter");
|
|
let body_y = items
|
|
.iter()
|
|
.find_map(|item| match item {
|
|
DisplayItem::Text { text, y, .. } if text == "a" => Some(*y),
|
|
_ => None,
|
|
})
|
|
.expect("body glyph");
|
|
assert!(paren_y > body_y, "tall delimiter should sit lower");
|
|
}
|
|
#[test]
|
|
fn negated_math_draws_a_diagonal_line() {
|
|
let mut items = Vec::new();
|
|
Page::paint_math(
|
|
&MathAtom::Negated {
|
|
base: Box::new(MathAtom::Symbol('=')),
|
|
},
|
|
&Cmr10Metrics::default(),
|
|
0,
|
|
0,
|
|
10 * 65_536,
|
|
&mut items,
|
|
);
|
|
assert!(
|
|
items
|
|
.iter()
|
|
.any(|item| matches!(item, DisplayItem::Line { .. }))
|
|
);
|
|
assert!(
|
|
!items
|
|
.iter()
|
|
.any(|item| matches!(item, DisplayItem::Rule { .. }))
|
|
);
|
|
}
|
|
}
|