From de2fad4a0f9bf70fff4524e92f6ea1b2e8f828e4 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 22 Nov 2026 22:02:53 +0800 Subject: [PATCH] refactor(layout): place cells by contiguous row group Derive absolute row positions and the span limit from the same row groups used for collapsed borders. --- moli-layout/src/table.rs | 21 ++-- moli-layout/src/table/collapsed_borders.rs | 30 ++---- moli-layout/src/table/structure.rs | 112 +++++++++------------ 3 files changed, 61 insertions(+), 102 deletions(-) diff --git a/moli-layout/src/table.rs b/moli-layout/src/table.rs index 250a39d7aa9..4e0c61f34ec 100644 --- a/moli-layout/src/table.rs +++ b/moli-layout/src/table.rs @@ -155,7 +155,7 @@ fn apply_parent_facing_table_inline_constraints( let mut cell_constraints = vec![None; column_count]; let mut cell_spans = Vec::new(); if let Some(first_row) = structure.rows.first() { - for cell in &structure.cells[first_row.cells()] { + for cell in &structure.cells[first_row.cells.clone()] { let constraint = authored_table_cell_inline_constraint( &world.boxes[cell.id.index()].style.taffy, style.writing_mode(), @@ -565,7 +565,7 @@ impl TableContext { self.structure .rows .first() - .map_or(&[][..], |row| &self.structure.cells[row.cells()]) + .map_or(&[][..], |row| &self.structure.cells[row.cells.clone()]) } else { &self.structure.cells }; @@ -1123,11 +1123,8 @@ fn apply_structural_layout( let content_height = row_bounds.end - row_bounds.start; if context.collapsed_borders { // Empty-section tracks occupy space without adding conflict-grid rows. - let mut row_lines: Vec<_> = context - .structure - .rows - .iter() - .map(|row| row_starts[context.grid_row(row.index)]) + let mut row_lines: Vec<_> = (0..context.structure.rows.len()) + .map(|index| row_starts[context.grid_row(index)]) .collect(); if let Some(first) = row_lines.first_mut() { *first = origin.y; @@ -1148,15 +1145,13 @@ fn apply_structural_layout( set_collapsed_border_geometry(world, root, &column_lines, &row_lines); } - for row in &context.structure.rows { - let y = row_starts - .get(context.grid_row(row.index)) - .copied() - .unwrap_or(origin.y); + for (index, row) in context.structure.rows.iter().enumerate() { + let grid_row = context.grid_row(index); + let y = row_starts.get(grid_row).copied().unwrap_or(origin.y); let height = detailed .rows .positions - .get(context.grid_row(row.index)) + .get(grid_row) .map_or(0.0, |track| track.end - track.start); set_table_part_layout(world, row.id, part_x, y, part_width, height); } diff --git a/moli-layout/src/table/collapsed_borders.rs b/moli-layout/src/table/collapsed_borders.rs index 28f0bcda6ec..b8e8558904e 100644 --- a/moli-layout/src/table/collapsed_borders.rs +++ b/moli-layout/src/table/collapsed_borders.rs @@ -10,7 +10,7 @@ use std::{fmt::Debug, hash::Hash}; use taffy::{Rect, ResolveOrZero, style_helpers}; use super::TableStructure; -use super::structure::{TableCell, TableRow}; +use super::structure::{TableCell, row_groups}; use crate::{ LayoutBoxId, LayoutBoxKind, LayoutRect, LayoutWorld, PaintBorderStyle, PaintColor, PaintEdgeSizes, ResolvedLayoutStyle, style::resolve_stylo_calc_value, @@ -392,10 +392,10 @@ pub(super) fn prepare_collapsed_table_borders( borders.mark_spanned_cell_interior(cell); } } - for row in &structure.rows { + for (index, row) in structure.rows.iter().enumerate() { box_order += 1; borders.merge_box( - row.index, + index, 0, 1, structure.column_count, @@ -403,12 +403,15 @@ pub(super) fn prepare_collapsed_table_borders( box_order, ); } - for (group, start, span) in row_groups(&structure.rows) { + for (start, rows) in row_groups(&structure.rows) { + let Some(group) = rows[0].group else { + continue; + }; box_order += 1; borders.merge_box( start, 0, - span, + rows.len(), structure.column_count, &world.boxes[group.index()].style, box_order, @@ -617,23 +620,6 @@ fn half(widths: PaintEdgeSizes) -> PaintEdgeSizes { ) } -fn row_groups(rows: &[TableRow]) -> Vec<(LayoutBoxId, usize, usize)> { - let mut groups = Vec::new(); - for row in rows { - let Some(group) = row.group else { - continue; - }; - if groups.last().is_some_and(|(last, _, _)| *last == group) { - if let Some((_, _, span)) = groups.last_mut() { - *span += 1; - } - } else { - groups.push((group, row.index, 1)); - } - } - groups -} - #[cfg(test)] mod tests { use super::*; diff --git a/moli-layout/src/table/structure.rs b/moli-layout/src/table/structure.rs index 006b946ffa9..0e221d7958f 100644 --- a/moli-layout/src/table/structure.rs +++ b/moli-layout/src/table/structure.rs @@ -17,16 +17,7 @@ pub(super) struct TableCell { pub(super) struct TableRow { pub(super) id: LayoutBoxId, pub(super) group: Option, - pub(super) index: usize, - start_cell_index: usize, - cell_count: usize, - section_end: usize, -} - -impl TableRow { - pub(super) fn cells(&self) -> Range { - self.start_cell_index..self.start_cell_index + self.cell_count - } + pub(super) cells: Range, } #[derive(Debug)] @@ -95,7 +86,6 @@ impl TableStructure { is_body: Some(section) != children.header && Some(section) != children.footer, }); } - index_row_sections(&mut rows); place_table_cells(&mut cells, &rows, &mut column_count); Self { cells, @@ -243,67 +233,57 @@ fn collect_rows( rows.push(TableRow { id: current, group, - index: row_index, - start_cell_index, - cell_count: cells.len() - start_cell_index, - section_end: 0, + cells: start_cell_index..cells.len(), }); } _ => {} } } -/// Cache the end of each contiguous placement group in one reverse pass. -/// Consecutive ungrouped rows share a boundary, just as grouped rows do. -fn index_row_sections(rows: &mut [TableRow]) { - let mut next_group = None; - let mut section_end = rows.len(); - for row in rows.iter_mut().rev() { - if next_group != Some(row.group) { - section_end = row.index + 1; - next_group = Some(row.group); - } - row.section_end = section_end; - } +/// Consecutive ungrouped rows share a placement boundary too. +pub(super) fn row_groups(rows: &[TableRow]) -> impl Iterator { + let mut next_row = 0; + rows.chunk_by(|left, right| left.group == right.group) + .map(move |group| { + let start = next_row; + next_row += group.len(); + (start, group) + }) } fn place_table_cells(cells: &mut [TableCell], rows: &[TableRow], max_columns: &mut usize) { let mut occupied_until = Vec::::new(); - let mut active_group = None; - for row in rows { - if active_group != Some(row.group) { - occupied_until.clear(); - active_group = Some(row.group); - } - let section_end = row.section_end; - let mut cursor = 0usize; - for cell in &mut cells[row.cells()] { - let span = cell.column_span.max(1); - loop { - let end = cursor.saturating_add(span); - if occupied_until.len() < end { - occupied_until.resize(end, 0); - } - if occupied_until[cursor..end] - .iter() - .all(|occupied| *occupied <= row.index) - { - cell.column = cursor; - let row_span = if cell.row_span == 0 { - section_end.saturating_sub(row.index) - } else { - cell.row_span - }; - cell.row_span = row_span.min(section_end.saturating_sub(row.index)).max(1); - for occupied in &mut occupied_until[cursor..end] { - *occupied = row.index.saturating_add(cell.row_span); + for (start, group) in row_groups(rows) { + occupied_until.clear(); + for (offset, row) in group.iter().enumerate() { + let row_index = start + offset; + let remaining_rows = group.len() - offset; + let mut cursor = 0usize; + for cell in &mut cells[row.cells.clone()] { + let span = cell.column_span.max(1); + loop { + let end = cursor.saturating_add(span); + if occupied_until.len() < end { + occupied_until.resize(end, 0); } - cursor = end; - *max_columns = (*max_columns).max(end); - break; + if occupied_until[cursor..end] + .iter() + .all(|occupied| *occupied <= row_index) + { + cell.column = cursor; + cell.row_span = if cell.row_span == 0 { + remaining_rows + } else { + cell.row_span.min(remaining_rows) + }; + occupied_until[cursor..end].fill(row_index + cell.row_span); + cursor = end; + *max_columns = (*max_columns).max(end); + break; + } + cursor += 1; } - cursor += 1; } } } @@ -354,7 +334,7 @@ mod tests { } #[test] - fn indexed_placement_clips_spans_at_group_boundaries_including_ungrouped_rows() { + fn placement_clips_spans_at_group_boundaries_including_ungrouped_rows() { let a = LayoutBoxId::from_index(20); let b = LayoutBoxId::from_index(21); let groups = [Some(a), Some(a), Some(a), None, None, Some(b), None]; @@ -366,10 +346,7 @@ mod tests { rows.push(TableRow { id: LayoutBoxId::from_index(index), group, - index, - start_cell_index: cells.len(), - cell_count, - section_end: 0, + cells: cells.len()..cells.len() + cell_count, }); for _ in 0..cell_count { let (column_span, row_span) = spans[cells.len()]; @@ -382,10 +359,11 @@ mod tests { }); } } - index_row_sections(&mut rows); assert_eq!( - rows.iter().map(|row| row.section_end).collect::>(), - [3, 3, 3, 5, 5, 6, 7] + row_groups(&rows) + .map(|(start, group)| start..start + group.len()) + .collect::>(), + [0..3, 3..5, 5..6, 6..7] ); let mut columns = 0; place_table_cells(&mut cells, &rows, &mut columns);