Lay out matrix and array environments as grids

This commit is contained in:
gloamxun committed 2026-10-26 00:39:28 +08:00
1 parent 971190df26
commit 59b6ca571b
6 files changed
+283 -62

No files matched your search

+57 -19
View File
@@ -6,7 +6,7 @@
//! wired up.
use crate::cmr10::Cmr10Metrics;
use crate::math::{self, MathAtom};
use crate::math::{self, MathAtom, MathColumnAlign};
use crate::paragraph::ParagraphFont;
use crate::{CellAlignment, Float, FloatPlacement, Halign, HorizontalItem, Page, VerticalItem};
@@ -1026,32 +1026,70 @@ impl Page {
});
}
}
MathAtom::Cases { rows } => {
let layout = math::cases_layout(rows, font);
let base_extent = font.height('{') + font.depth('{');
let scale = if base_extent > 0 {
(layout.brace.1 + layout.brace.2) as f64 / base_extent as f64
} else {
1.0
MathAtom::Matrix {
rows,
align,
left,
right,
} => {
let layout = math::matrix_layout(rows, font, align, *left, *right);
let base_extent = |delimiter: char| font.height(delimiter) + font.depth(delimiter);
let scaled = |metrics: (char, i64, i64, i64)| -> (i64, i64) {
let base = base_extent(metrics.0);
let scale = if base > 0 {
(metrics.2 + metrics.3) as f64 / base as f64
} else {
1.0
};
let glyph_size = (size as f64 * scale) as i64;
let shift = ((glyph_size - size) as f64 * 0.35) as i64;
(glyph_size, shift)
};
let glyph_size = (size as f64 * scale) as i64;
let shift = ((glyph_size - size) as f64 * 0.35) as i64;
list.push(DisplayItem::Text {
font: FontFace::Roman,
x,
y: baseline + shift,
size: glyph_size,
text: "{".to_string(),
});
let start_x = x + layout.brace.0 + 3 * 65_536;
let left_width = layout.left.map(|(_, width, _, _)| width).unwrap_or(0);
if let Some(metrics) = layout.left {
let (glyph_size, shift) = scaled(metrics);
list.push(DisplayItem::Text {
font: FontFace::Roman,
x,
y: baseline + shift,
size: glyph_size,
text: metrics.0.to_string(),
});
}
let body_x = x + left_width + layout.border;
let first_baseline = baseline - layout.height + layout.first_height;
for (row_index, cells) in rows.iter().enumerate() {
let row_baseline = first_baseline + layout.baselines[row_index];
for (column, cell) in cells.iter().enumerate() {
let cell_x = start_x + layout.column_starts[column];
let offset = match align
.get(column)
.copied()
.unwrap_or(MathColumnAlign::Center)
{
MathColumnAlign::Left => 0,
MathColumnAlign::Center => {
(layout.column_widths[column]
- layout.boxes[row_index][column].width)
/ 2
}
MathColumnAlign::Right => {
layout.column_widths[column] - layout.boxes[row_index][column].width
}
};
let cell_x = body_x + layout.column_starts[column] + offset;
Self::paint_math(cell, font, cell_x, row_baseline, size, list);
}
}
if let Some(metrics) = layout.right {
let (glyph_size, shift) = scaled(metrics);
list.push(DisplayItem::Text {
font: FontFace::Roman,
x: body_x + layout.body_width + layout.border,
y: baseline + shift,
size: glyph_size,
text: metrics.0.to_string(),
});
}
}
MathAtom::Accent { accent, base } => {
let base_box = math::layout_math(base, font);