From 8ecbd5e7208ff20719168cae90b6a240b027ce1f Mon Sep 17 00:00:00 2001 From: GloamXun Date: Mon, 26 Oct 2026 20:31:03 +0800 Subject: [PATCH] Centre multirow cells over their rows --- crates/texrra-tex-core/src/lib.rs | 95 +++++++++++++++++++++++++++- crates/texrra-tex-core/src/render.rs | 36 +++++++++++ docs/ROUTE_D_PLAN.md | 2 +- docs/TEX_CORE.md | 2 +- src/route_d.rs | 9 ++- 5 files changed, 138 insertions(+), 6 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 3545eb4..b5c6201 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -172,6 +172,9 @@ pub struct Halign { /// Space reserved at the left and right edges (outer `\tabcolsep` margins). #[serde(default)] pub margin: i64, + /// `(row, cell, span)` triples for `\multirow` cells centred over rows. + #[serde(default)] + pub multirow: Vec<(usize, usize, usize)>, pub width: i64, pub height: i64, pub line_height: i64, @@ -332,6 +335,7 @@ impl Halign { head_len: 0, foot_len: 0, margin: self.margin, + multirow: Vec::new(), width: self.width, height, line_height: self.line_height, @@ -371,6 +375,7 @@ impl Halign { head_len: 0, foot_len: 0, margin: self.margin, + multirow: Vec::new(), width: self.width, height, line_height: self.line_height, @@ -396,6 +401,7 @@ impl Halign { head_len: 0, foot_len: 0, margin: self.margin, + multirow: Vec::new(), width: self.width, height, line_height: self.line_height, @@ -411,6 +417,41 @@ impl Halign { .sum() } + /// Rows a cell covers (`\multirow`), defaulting to one. + pub fn cell_row_span(&self, row_index: usize, cell_index: usize) -> usize { + self.multirow + .iter() + .find(|(row, cell, _)| *row == row_index && *cell == cell_index) + .map(|(_, _, span)| *span) + .unwrap_or(1) + } + + /// Whether a `\multirow` cell above already covers this cell. + fn cell_covered(&self, row_index: usize, cell_index: usize) -> bool { + let Some(row) = self.rows.get(row_index) else { + return false; + }; + let column: usize = row.cells[..cell_index.min(row.cells.len())] + .iter() + .map(|cell| cell.span.max(1)) + .sum(); + for (above, cell, span) in &self.multirow { + if *above >= row_index || above + span <= row_index { + continue; + } + let start: usize = self.rows[*above].cells + [..(*cell).min(self.rows[*above].cells.len())] + .iter() + .map(|cell| cell.span.max(1)) + .sum(); + let width = self.rows[*above].cells[*cell].span.max(1); + if column >= start && column < start + width { + return true; + } + } + false + } + /// The aligned rectangle of a cell: `(x, width)`. pub fn cell_box(&self, row_index: usize, cell_index: usize) -> Option<(i64, i64)> { let (x, width) = self.cell_span(row_index, cell_index)?; @@ -490,7 +531,13 @@ impl Halign { return 26_214; } let lines = (0..row.cells.len()) - .map(|index| self.cell_lines(row_index, index).len()) + .map(|index| { + if self.cell_row_span(row_index, index) > 1 { + 1 + } else { + self.cell_lines(row_index, index).len() + } + }) .max() .unwrap_or(1) .max(1); @@ -1091,6 +1138,7 @@ impl Unit { head_len: 0, foot_len: 0, margin: 0, + multirow: Vec::new(), width, height: 0, line_height, @@ -1151,6 +1199,7 @@ impl Unit { let column_count = alignments.len().max(1); let at_glue = parse_at_glues(template); let mut rows = Vec::new(); + let mut multirow: Vec<(usize, usize, usize)> = Vec::new(); let mut rules: Vec = Vec::new(); let mut row_gaps: Vec = Vec::new(); let mut pending_gap = 0_i64; @@ -1249,6 +1298,19 @@ impl Unit { } } } + if let Some(rest) = text.strip_prefix(r"\texrramultirow") { + let rest = rest.trim_start(); + if let Some((count, rest)) = take_halign_group(rest) { + let rest = rest.trim_start(); + if let Some((body, tail)) = take_halign_group(rest) { + let span = count.trim().parse().unwrap_or(1).max(1); + if span > 1 { + multirow.push((rows.len(), cells.len(), span)); + } + text = format!("{}{}", body.trim(), tail); + } + } + } if alignment.is_none() { if let Some(rest) = text.strip_prefix(r"\span") { span = 2; @@ -1324,6 +1386,10 @@ impl Unit { if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() { halign.rules = rules; halign.vertical_rules = vertical_rules; + halign.multirow = multirow; + halign.height = (0..halign.rows.len()) + .map(|index| halign.row_height(index) + halign.row_gap(index)) + .sum(); halign.first_head_len = first_len.min(halign.rows.len()); halign.head_len = head_len.min(halign.rows.len() - halign.first_head_len); halign.foot_len = @@ -6154,6 +6220,33 @@ mod tests { assert_eq!(layout.pages()[1].insertions.len(), 0); } + #[test] + fn multirow_cells_centre_over_their_rows() { + let source = "\\texrramultirow{3}{Both} & a \\\\ & b \\\\ & c"; + let unit = Unit::halign_from_source_with_template(1, "ll", source, 20); + let VerticalItem::Halign(halign) = &unit.items[0] else { + panic!("expected halign"); + }; + assert_eq!(halign.cell_row_span(0, 0), 3); + let list = halign.display_list(); + let mut y = std::collections::HashMap::new(); + for item in &list.items { + if let crate::render::DisplayItem::Text { + text, y: item_y, .. + } = item + { + y.insert(text.clone(), *item_y); + } + } + let both = *y.get("Both").expect("multirow text"); + let first = *y.get("a").expect("first row"); + let middle = *y.get("b").expect("middle row"); + let last = *y.get("c").expect("last row"); + assert!(both > first, "multirow {both} first {first}"); + assert!(both < last, "multirow {both} last {last}"); + assert!((both - middle).abs() <= halign.line_height / 2); + } + #[test] fn halign_aligns_cells_inside_their_columns() { let rows = vec![ diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index df341ac..4cc41eb 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -445,6 +445,42 @@ impl Halign { + line_index as i64 * self.line_height; for (index, cell) in row.cells.iter().enumerate() { let lines = self.cell_lines(row_index, index); + let row_span = self.cell_row_span(row_index, index); + if row_span > 1 { + if line_index != 0 { + continue; + } + let Some((x, width)) = self.cell_span(row_index, index) else { + continue; + }; + let span_rows = (row_index + row_span).min(self.rows.len()); + let span_height: i64 = (row_index..span_rows) + .map(|index| self.row_height(index) + self.row_gap(index)) + .sum(); + let center = y + span_height / 2; + for (offset_index, line) in lines.iter().enumerate() { + let baseline = center - (lines.len() as i64 - 1) * self.line_height / 2 + + self.line_height * 3 / 10 + + offset_index as i64 * self.line_height; + let text_width = crate::text_width(line); + let offset = match cell.alignment { + CellAlignment::Left => 0, + CellAlignment::Center => (width - text_width).max(0) / 2, + CellAlignment::Right => (width - text_width).max(0), + }; + list.push(DisplayItem::Text { + font: FontFace::Roman, + x: x + offset, + y: baseline, + size: 10 * 65_536, + text: line.clone(), + }); + } + continue; + } + if self.cell_covered(row_index, index) && cell.text.trim().is_empty() { + continue; + } let Some(line) = lines.get(line_index) else { continue; }; diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index c4d2d19..6d504e8 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`, `\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and -PDF opt-in with `?engine=route-d`). 930 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 931 workspace tests are green and no oracle fixture is ignored. Preview JSON also carries a `warnings` array naming environments route-D cannot lay out and control words that survived layout, so unsupported source is reported instead of being silently typeset. diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index c4e3e1d..60a568b 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -367,7 +367,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address; those fallback glyphs keep their Unicode text under PDF extraction, so accented and Greek words round-trip; Type1 faces carry a ToUnicode CMap built from the requested characters, so glyphs whose names are ambiguous in the Adobe Glyph List (capital Omega, which would otherwise extract as U+2126) keep their intended code points. `Engine::edit` preserves the face when reflowing an edited paragraph. -The crate currently has 427 unit tests plus ten route-D integration tests and thirty-eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 930 tests (428 route-D unit, 317 engine binary unit, 10 route-D integration, 39 oracle, 71 v1 integration, 60 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path. +The crate currently has 427 unit tests plus ten route-D integration tests and thirty-eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 931 tests (429 route-D unit, 317 engine binary unit, 10 route-D integration, 39 oracle, 71 v1 integration, 60 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path. ### M0 incremental demo diff --git a/src/route_d.rs b/src/route_d.rs index 966bd8c..1116669 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -1908,7 +1908,10 @@ fn expand_multirow(source: &str) -> String { pieces.push(body.trim().to_string()); cursor = &cursor[consumed..]; } - out.push_str(&pieces[2]); + out.push_str(&format!( + "\\texrramultirow{{{}}}{{{}}}", + pieces[0], pieces[2] + )); rest = cursor; } out.push_str(rest); @@ -11907,8 +11910,8 @@ See \ref{fig:a} and \ref{tab:b}."; let (_, geometry) = geometry_and_body(source); assert_eq!(geometry.tabular.len(), 1); let (_, text, _) = &geometry.tabular[0]; - assert!(text.contains("Both & a"), "{text}"); - assert!(!text.contains("multirow")); + assert!(text.contains("texrramultirow{2}{Both}"), "{text}"); + assert!(!text.contains("\\multirow")); let json = preview_json(source).expect("preview"); assert!(json.contains(">Both")); assert!(!json.contains("multirow"));