Parse halign source spans and omit markers
This commit is contained in:
1 parent
e650177585
commit
abffe4e7bc
2 files changed
+58
No files matched your search
@@ -209,6 +209,50 @@ impl Unit {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a small TeX-like halign body (`&`, `\\`, `\omit`, `\span`,
|
||||
/// `\multispan{n}`) into a laid-out unit.
|
||||
pub fn halign_from_source(
|
||||
id: UnitId,
|
||||
source: &str,
|
||||
column_count: usize,
|
||||
intercolumn: i64,
|
||||
) -> Self {
|
||||
let mut rows = Vec::new();
|
||||
for raw_row in source.split(r"\\") {
|
||||
if raw_row.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut cells = Vec::new();
|
||||
for raw_cell in raw_row.split('&') {
|
||||
let mut text = raw_cell.trim().to_owned();
|
||||
let mut span = 1;
|
||||
if let Some(rest) = text.strip_prefix(r"\span") {
|
||||
span = 2;
|
||||
text = rest.trim().to_owned();
|
||||
}
|
||||
if let Some(rest) = text.strip_prefix(r"\multispan") {
|
||||
let rest = rest.trim_start();
|
||||
if let Some(body) = rest.strip_prefix('{') {
|
||||
if let Some((count, tail)) = body.split_once('}') {
|
||||
span = count.trim().parse().unwrap_or(1).max(1);
|
||||
text = tail.trim().to_owned();
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(rest) = text.strip_prefix(r"\omit") {
|
||||
text = rest.trim().to_owned();
|
||||
}
|
||||
cells.push(HalignCell {
|
||||
text,
|
||||
span,
|
||||
alignment: CellAlignment::Left,
|
||||
});
|
||||
}
|
||||
rows.push(HalignRow { cells, rule: false });
|
||||
}
|
||||
Self::halign(id, rows, column_count, intercolumn)
|
||||
}
|
||||
|
||||
/// M1 paragraph builder: box/glue/penalty item stream with a
|
||||
/// Knuth-Plass-style word-level line breaker.
|
||||
pub fn paragraph(id: UnitId, source: impl Into<String>) -> Self {
|
||||
@@ -1092,6 +1136,17 @@ mod tests {
|
||||
assert_eq!(halign.cell_box(2, 0), Some((30, 110)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn halign_source_parses_omit_span_and_multispan() {
|
||||
let unit = Unit::halign_from_source(1, r"A & B \\ \omit C & \multispan{2} D", 3, 10);
|
||||
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||
panic!("expected halign");
|
||||
};
|
||||
assert_eq!(halign.rows.len(), 2);
|
||||
assert_eq!(halign.rows[1].cells[0].text, "C");
|
||||
assert_eq!(halign.rows[1].cells[0].span, 1);
|
||||
assert_eq!(halign.rows[1].cells[1].span, 2);
|
||||
}
|
||||
#[test]
|
||||
fn halign_computes_natural_column_widths() {
|
||||
let rows = vec![
|
||||
|
||||
@@ -188,3 +188,6 @@ cargo test -p texrra-tex-core --offline
|
||||
The M0 tests cover page splitting, checkpoint serialization and page-local
|
||||
editing. Three M1 tests cover glue-based line breaking, paragraph indentation and
|
||||
empty paragraphs.
|
||||
|
||||
`Unit::halign_from_source` parses `&` rows, `\\` separators, `\omit`,
|
||||
`\span` and `\multispan{n}` into `HalignCell.span` values.
|
||||
Reference in new issue
Block a user