//! Table layout: column widths, cell boxes, row heights, merged cells, borders and shading, //! table-style conditional formatting. Rows are laid out as free-standing boxes; pagination //! moves whole rows or splits them between lines (`split_row`). use wordcraft_doc::numbering::Counters; use wordcraft_doc::props::{Align, Border, Borders, HeightRule, Rgb, TextDirection, VAlign, VMerge}; use wordcraft_doc::styles::{PartSet, TablePart, TableStyleProps}; use wordcraft_doc::{Block, Blocks, Paragraph, Row, StoryRef, Table}; use wordcraft_geom::Rect; use crate::boxes::BoxContext; use crate::para::{CellText, ClKind, ParaLayout}; use crate::{BoxInput, BoxLayout, Ctx, Placed, layout_box}; /// Largest preferred table width, as a share of the text column, that a file can ask for. const MAX_WIDTH_SHARE: f32 = 10.0; pub struct RowLayout { pub height: f32, /// Items relative to the table's top-left (x) and the row's top (y). pub items: Vec, /// The room the row keeps below its text: the bottom cell margin and border band. Part of a /// row split across pages keeps it too. pub bottom: f32, /// The width of the border below a row that another row follows (it belongs to the next /// row's top band). Word wants it on the page too: the row fits only where its height and /// this border do. pub edge: f32, /// The row's cells: the items each one owns and where its content ends. pub cells: Vec, } /// A cell of a [`RowLayout`]. pub struct CellPart { /// Top cell margin and visible border band retained on a continuation. pub(crate) top: f32, /// Its items in the row's `items` (its fill, text, borders and cell area). pub items: std::ops::Range, /// Where its content ends below the row's top, its last paragraph's space after included /// (what the part of the row left on a page must hold); `None` for turned text and a cell /// covered by a vertical merge. pub end: Option, /// How far a cell aligned to the middle or bottom of its row moved its content down, for /// splitting the row; `None` for a cell at the top. pub(crate) align: Option, } /// The vertical alignment applied to a cell's content (see [`CellPart::align`]). #[derive(Clone)] pub(crate) struct CellAlign { /// The cell's content in the row's `items`. pub content: std::ops::Range, pub valign: VAlign, /// How far the content moved down. pub dy: f32, /// The height it was aligned by: the content and the cell's margins. pub height: f32, } pub struct TableLayout { /// Table x offset within the column. pub x: f32, pub width: f32, pub rows: Vec, } const DEFAULT_MARGINS: [f32; 4] = [0.0, 5.4, 0.0, 5.4]; /// The longest line turned text gets when its row height isn't exact (a Letter page's body /// height): longer text wraps. const TURNED_MAX: f32 = 648.0; /// The longest line turned text ever gets (Word's largest page, 22 inches). const TURNED_LIMIT: f32 = 1584.0; /// A cell whose text is turned, between the two passes of [`layout_table`]. struct Turned { turn: TextDirection, /// Line length it was laid out with, and how far its lines reach across the cell. len: f32, across: f32, /// List counters and equation count before the cell, to lay it out again the same way. snap: (Counters, u32), /// Its table style parts and cell index in the row. region: Region, ci: usize, /// Top margin plus top border band. top: f32, } /// The number of grid columns of `t` ([`Table::cols`]), which walks every row (each row counted /// as a step of [`crate::work`]). fn grid_cols(t: &Table) -> usize { crate::work::steps(t.rows.len()); t.cols() } /// How long the lines laid out in `items` are without the room they didn't use: the length /// turned text needs so that nothing wraps. fn natural_length(items: &[Placed]) -> f32 { let mut len = 0.0f32; for it in items { let Placed::Lines { para, l0, l1, x, .. } = it else { continue }; let indents = para.rp.indent_left.max(0.0) + para.rp.indent_right.max(0.0) + para.rp.indent_first.max(0.0); for l in para.lines.get(*l0..*l1).unwrap_or(&[]) { let (mut lo, mut hi) = (f32::MAX, f32::MIN); for v in &l.vis { lo = lo.min(v.x); hi = hi.max(v.x + v.w); } if l.vis.is_empty() && let (Some(a), Some(b)) = (l.xs.first(), l.xs.last()) { (lo, hi) = (*a, *b); } let hy = l.hyphen.map_or(0.0, |h| h.2); if hi >= lo { len = len.max(x.max(0.0) + hi - lo + hy + indents); } } } // A little slack, so laying the text out again at this length doesn't wrap it. len + 0.5 } /// The table's style with its based-on chain merged. fn table_style(ctx: &Ctx, t: &Table) -> Option { ctx.doc.styles.table_style(t.props.style.as_deref()?) } /// The table's default cell margins: its own, else its style's, else Word's. fn default_margins(t: &Table, style: Option<&TableStyleProps>) -> [f32; 4] { t.props.cell_margins.or(style.and_then(|s| s.parts.cell_margins)).unwrap_or(DEFAULT_MARGINS) } /// The margins (top, left, bottom, right; points) of cell `cell` of row `row`: its own, else the /// table's defaults. The layout uses the left and right ones as they are (the app's ruler /// measures a cell's indents from them) but gives every cell in a row the row's largest top and /// bottom margins. pub fn cell_margins(doc: &wordcraft_doc::Document, t: &Table, row: usize, cell: usize) -> [f32; 4] { let style = t.props.style.as_deref().and_then(|id| doc.styles.table_style(id)); let defaults = default_margins(t, style.as_ref()); t.rows.get(row).and_then(|r| r.cells.get(cell)).map_or(defaults, |cell| cell.props.resolved_margins(defaults)) } /// The most paragraphs [`measure_table_columns`] lays out: a huge table is measured by its first /// rows only, so AutoFit stays quick. const MEASURE_BUDGET: usize = 4000; /// The line length paragraphs are measured at: wide enough that nothing real wraps. pub(crate) const MEASURE_WIDE: f32 = 15_840.0; /// What an empty cell's text needs (about a paragraph mark), points. const EMPTY_TEXT: f32 = 12.0; /// What one paragraph needs: the longest run of text without a break opportunity (`min`) and /// its longest line when nothing wraps (`max`), indents included. fn measure_para(p: &wordcraft_doc::Paragraph, env: &crate::para::ParaEnv) -> (f32, f32) { use crate::para::ClKind; let pl = crate::para::layout_para(p, env); let rp = &pl.rp; let indents = rp.indent_left.max(0.0) + rp.indent_right.max(0.0) + rp.indent_first.max(0.0); let (mut word, mut min) = (0.0f32, 0.0f32); for c in &pl.clusters { match c.kind { ClKind::Text | ClKind::Object(_) => word += c.adv.max(0.0), ClKind::Marker => {} _ => { min = min.max(word); word = 0.0; } } if c.break_after { min = min.max(word); word = 0.0; } } min = min.max(word); let mut max = 0.0f32; for l in &pl.lines { let (mut lo, mut hi) = (f32::MAX, f32::MIN); for k in l.c0..l.c1 { let (Some(c), Some(a), Some(b)) = (pl.clusters.get(k), l.cl_left(k), l.cl_right(k)) else { continue }; lo = lo.min(a.min(b)); if !matches!(c.kind, ClKind::Space | ClKind::Marker | ClKind::LineBreak | ClKind::PageBreak | ClKind::ColumnBreak) { hi = hi.max(a.max(b)); } } if hi > lo { max = max.max(hi - lo + l.hyphen.map_or(0.0, |h| h.2)); } } let min = if min.is_finite() { min } else { 0.0 }; let max = if max.is_finite() { max.max(min) } else { min }; (min + indents, max + indents) } /// What each grid column of `t` needs for AutoFit to Contents, as (narrowest, widest) in points /// with cell margins included: the narrowest fits each cell's longest word (text that can't /// wrap), the widest fits every paragraph on one line. A cell spanning several columns spreads /// what its columns lack over them. Paragraphs are laid out with the table style's formatting; /// after [`MEASURE_BUDGET`] paragraphs the remaining rows are skipped. pub fn measure_table_columns(doc: &wordcraft_doc::Document, t: &Table) -> Vec<(f32, f32)> { use wordcraft_doc::Block; let style = t.props.style.as_deref().and_then(|id| doc.styles.table_style(id)); let margins_def = default_margins(t, style.as_ref()); let ncols = grid_cols(t).clamp(1, wordcraft_doc::table::MAX_COLS); let fields = crate::FieldCtx::default(); let mut cols = vec![(0.0f32, 0.0f32); ncols]; // Cells spanning several columns: (first column, span, min, max), applied after the others. let mut spanned: Vec<(usize, usize, f32, f32)> = Vec::new(); let mut budget = MEASURE_BUDGET; 'rows: for (ri, row) in t.rows.iter().enumerate() { let mut g = row.first_grid(); for cell in &row.cells { let span = cell.span(); let margins = cell.props.resolved_margins(margins_def); let side = margins[1].max(0.0) + margins[3].max(0.0); let text = style.as_ref().map(|st| region_text(st, region_of(t, style.as_ref(), ri, g, span, ncols))); let env = crate::para::ParaEnv { doc, width: MEASURE_WIDE, label: None, fields: &fields, show_hidden: false, field_codes: false, hide_deleted: Default::default(), table: text.as_ref(), proofing: None, typing_word: None, // Measurement uses a fixed environment (like show_hidden/proofing above). numeral: crate::NumeralMode::default(), exclusions: &[], eq_number: 0, read_line_spacing: None, grid: crate::para::Grid::default(), fit_objects: false, fitted: &[], joined: false, }; let (mut min, mut max) = (EMPTY_TEXT, EMPTY_TEXT); if cell.props.vmerge != VMerge::Continue && !cell.props.text_direction.is_turned() { for b in &cell.blocks { match &**b { Block::Para(p) => { if budget == 0 { break 'rows; } budget -= 1; let (a, b) = measure_para(p, &env); min = min.max(a); max = max.max(b); } // A nested table keeps its own column widths. Block::Table(inner) => { let w: f32 = inner.grid.iter().filter(|w| w.is_finite()).map(|w| w.clamp(0.0, 1584.0)).sum(); min = min.max(w); max = max.max(w); } } } } let (min, max) = ((min + side).min(1584.0), (max + side).min(1584.0)); if span == 1 { if let Some(c) = cols.get_mut(g) { c.0 = c.0.max(min); c.1 = c.1.max(max); } } else { spanned.push((g, span, min, max)); } g += span; } spanned.extend(row_gaps(row, g).map(|(g, span, w)| (g, span, w, w))); } for (g, span, min, max) in spanned { let Some(cs) = cols.get_mut(g..(g + span).min(ncols)) else { continue }; if cs.is_empty() { continue; } let n = cs.len() as f32; let (have_min, have_max) = cs.iter().fold((0.0, 0.0), |(a, b), c| (a + c.0, b + c.1)); for c in cs.iter_mut() { c.0 += ((min - have_min) / n).max(0.0); c.1 += ((max - have_max) / n).max(0.0); c.1 = c.1.max(c.0); } } // A column no cell starts in still needs room for its margins. let floor = margins_def[1].max(0.0) + margins_def[3].max(0.0) + EMPTY_TEXT; cols.iter_mut().for_each(|c| { c.0 = c.0.max(floor); c.1 = c.1.max(c.0); }); cols } /// Column widths for AutoFit to Contents, the way Word shares the room: every column gets its /// widest content when that all fits in `avail`; otherwise each gets its narrowest plus a share /// of the remaining room in proportion to how much wider its content would like to be. When /// even the narrowest widths don't fit, the columns keep them (the table runs past the margin, /// as in Word). pub fn autofit_widths(cols: &[(f32, f32)], avail: f32) -> Vec { let clean = |v: f32| if v.is_finite() { v.clamp(0.0, 1584.0) } else { 0.0 }; let cols: Vec<(f32, f32)> = cols.iter().map(|&(a, b)| (clean(a), clean(b).max(clean(a)))).collect(); let avail = clean(avail); let (sum_min, sum_max) = cols.iter().fold((0.0f32, 0.0f32), |(a, b), c| (a + c.0, b + c.1)); if sum_max <= avail { return cols.iter().map(|c| c.1).collect(); } if sum_min >= avail || sum_max <= sum_min { return cols.iter().map(|c| c.0).collect(); } let k = (avail - sum_min) / (sum_max - sum_min); cols.iter().map(|c| c.0 + (c.1 - c.0) * k).collect() } /// The first cell's left margin: how far its text sits inside the table's edge. pub(crate) fn first_cell_left_margin(ctx: &Ctx, t: &Table) -> f32 { first_cell_left_margin_in(t, default_margins(t, table_style(ctx, t).as_ref())) } fn first_cell_left_margin_in(t: &Table, def: [f32; 4]) -> f32 { t.rows.first().and_then(|r| r.cells.first()).map_or(def, |cell| cell.props.resolved_margins(def))[1] } /// What a table style gives the cells in one set of parts: shading, vertical alignment and each /// part's cell borders, lowest precedence first. struct RegionLook { fill: Option, v_align: Option, borders: Vec<(TablePart, Borders)>, } fn region_look(st: &TableStyleProps, region: Region) -> RegionLook { let mut out = RegionLook { fill: None, v_align: None, borders: Vec::new() }; for part in region.iter() { let f = st.part(part); out.fill = f.fill.or(out.fill); out.v_align = f.v_align.or(out.v_align); // The whole table's borders are the table borders, applied on their own. if part != TablePart::WholeTable && let Some(b) = f.borders { out.borders.push((part, b)); } } out } /// A cell's borders from the table style's parts it is in ([`wordcraft_doc::styles::part_borders`]). fn region_borders(parts: &[(TablePart, Borders)], rows: (usize, usize), edges: (bool, bool)) -> Borders { wordcraft_doc::styles::part_borders(parts, rows, edges) } struct CellBox { items: Vec, h: f32, x: f32, w: f32, valign: VAlign, fill: Option, borders: Borders, vmerge: VMerge, margins: [f32; 4], g: usize, span: usize, /// Turned text: `items` are still in the turned frame (placed in the second pass). turned: Option, /// Where the content ends below the cell's top (before vertical alignment). end: f32, } struct TableFrame { ncols: usize, colx: Vec, acc: f32, total: f32, margins_def: [f32; 4], tborders: Borders, rtl: bool, x: f32, } /// The table's column widths before autofit: its grid, or equal shares of `avail` without one, /// then the cells' absolute and percentage widths. Nested measurement needs the same preferred /// weights as layout before sharing span minima. fn preferred_grid(t: &Table, avail: f32) -> Vec { let ncols = grid_cols(t).max(1); let mut grid: Vec = if t.grid.len() == ncols { // Grid units can be narrower than a text box, including omitted row-end columns. t.grid .iter() .map(|&w| match w { w if !w.is_finite() => 72.0, w if w <= 0.0 => 4.0, w => w, }) .collect() } else { vec![avail / ncols as f32; ncols] }; apply_cell_widths(t, &mut grid); apply_pct_widths(t, &mut grid, avail); grid } fn table_frame(ctx: &mut Ctx, t: &Table, avail: f32, depth: usize, style: Option<&TableStyleProps>) -> TableFrame { let ncols = grid_cols(t).max(1); let mut grid = preferred_grid(t, avail); let mut total: f32 = grid.iter().sum(); // A top-level table in an inherited header or footer takes its share of the defining // section's width (`Ctx::pct_base`). let pct_base = if depth <= 1 { ctx.pct_base.unwrap_or(avail) } else { avail }; let target = match (t.props.width_pct, t.props.width) { // Word keeps a percentage past 100%; the cap only bounds hostile input. (Some(p), _) if p > 0.0 => Some(pct_base * (p / 100.0).min(MAX_WIDTH_SHARE)), (_, Some(w)) if w > 0.0 => Some(w), _ => None, }; if let Some(tw) = target { if total > 0.0 && total < 1.0 { // Divide first: a tiny positive total can overflow the scale factor. grid.iter_mut().for_each(|w| *w = (*w / total) * tw); } else { let k = tw / total.max(1.0); grid.iter_mut().for_each(|w| *w *= k); } total = tw; } // A table with its own width keeps it, however far it runs past the margins (as in Word). if target.is_none() && total > avail * 1.5 && !t.props.fixed { let k = avail / total.max(1.0); grid.iter_mut().for_each(|w| *w *= k); total = avail; } if !t.props.fixed { // Measuring lays paragraphs out once more; it mustn't count their equations twice. let eq_count = ctx.eq_count; let content = column_content(ctx, t, style, depth, &grid); ctx.eq_count = eq_count; if let Some(content) = content { autofit(&mut grid, &content, t, avail); // Never wider than the room it had, however long an unbreakable word. let fitted: f32 = grid.iter().sum(); let cap = avail.max(total); if fitted > cap { grid.iter_mut().for_each(|w| *w *= cap / fitted); } total = grid.iter().sum(); } } let mut colx = Vec::with_capacity(ncols + 1); let mut acc = 0.0; for w in &grid { colx.push(acc); acc += w; } colx.push(acc); // Word 2013 and later (compatibility mode 15) put the table's border at the margin (plus its // indent); earlier modes line the first cell's text up with it instead. let margins_def = default_margins(t, style); let indent = t.props.indent.unwrap_or(0.0); // The style's borders, overlaid by the table's own side by side. let mut tborders = style.and_then(|s| s.parts.borders).unwrap_or_default(); if let Some(own) = t.props.borders { tborders.overlay(&own); } let first_cell = t.rows.first().and_then(|r| r.cells.first()); // A right-to-left table (`w:bidiVisual`) mirrors: its first column stands at the right, its // start alignment and indent are measured from the right margin, and each cell's start // (left) margin and border are on its right. let rtl = t.props.bidi_visual; // How far in from the start margin the table's edge sits when it is start-aligned. let start_offset = if ctx.doc.settings.compat_mode >= 15 { // The border is centred on the edge, so half of it sits outside: Word moves the table in // by that half. let border = first_cell.and_then(|c| c.props.borders.and_then(|b| b.left)).or(tborders.left); indent + border.filter(Border::is_visible).map_or(0.0, |b| b.width.clamp(0.0, 12.0) / 2.0) } else { indent - first_cell_left_margin_in(t, margins_def) }; let x = match (t.props.align, rtl) { (Some(Align::Center), _) => (avail - total) / 2.0, // End alignment: the right margin, or the left one in a right-to-left table. (Some(Align::Right), false) => avail - total, (Some(Align::Right), true) => 0.0, (_, true) => avail - total - start_offset, (_, false) => start_offset, }; TableFrame { ncols, colx, acc, total, margins_def, tborders, rtl, x } } pub fn layout_table(ctx: &mut Ctx, story: StoryRef, t: &Table, path: &[u32], avail: f32, depth: usize) -> TableLayout { let style = table_style(ctx, t); // Cell text formatting and looks per set of style parts, built once per table. let mut cell_text: Vec<(Region, CellText)> = Vec::new(); let mut looks: Vec<(Region, RegionLook)> = Vec::new(); let TableFrame { ncols, colx, acc, total, margins_def, tborders, rtl, x } = table_frame(ctx, t, avail, depth, style.as_ref()); let nrows = t.rows.len(); // First pass: lay out every cell's content. let mut rows: Vec> = Vec::with_capacity(nrows); let mut heights: Vec = Vec::with_capacity(nrows); let mut bottoms: Vec = Vec::with_capacity(nrows); let mut edges: Vec = Vec::with_capacity(nrows); for (ri, row) in t.rows.iter().enumerate() { let mut g = row.first_grid(); let mut cells = Vec::with_capacity(row.cells.len()); let mut rh = 0.0f32; // The tallest cell margins plus border bands in the row. let mut row_insets = 0.0f32; let mut row_below = 0.0f32; let mut row_edge = 0.0f32; // Every cell an empty one with a hidden end-of-cell mark: the row is only its borders. let mut collapsed = true; // Word gives every cell in a row the row's largest top and bottom margins: a cell without // its own lines up with a neighbour that has 5.65pt ones. let (row_top, row_bottom) = row .cells .iter() .map(|c| c.props.resolved_margins(margins_def)) .fold((0.0f32, 0.0f32), |(top, bottom), m| (top.max(m[0]), bottom.max(m[2]))); for (ci, cell) in row.cells.iter().enumerate() { let span = cell.span(); let x0 = colx.get(g).copied().unwrap_or(acc); let x1 = colx.get((g + span).min(ncols)).copied().unwrap_or(acc); // Right to left: grid columns run from the right edge, and the start (left) margin // is on the cell's right. let (x0, x1) = if rtl { ((acc - x1).max(0.0), (acc - x0).max(0.0)) } else { (x0, x1) }; let m = cell.props.resolved_margins(margins_def); let margins = if rtl { [row_top, m[3], row_bottom, m[1]] } else { [row_top, m[1], row_bottom, m[3]] }; let cw = (x1 - x0 - margins[1] - margins[3]).max(4.0); let region = region_of(t, style.as_ref(), ri, g, span, ncols); // Conditional formatting, later parts winning (ECMA-376 §17.7.6): whole table, column // bands, row bands, first/last column, header/total row, corner cells. if let Some(st) = &style && !cell_text.iter().any(|(r, _)| *r == region) { cell_text.push((region, region_text(st, region))); looks.push((region, region_look(st, region))); } let look = looks.iter().find(|(r, _)| *r == region).map(|(_, l)| l); let fill = cell.props.shading.or(look.and_then(|l| l.fill)); let text = cell_text.iter().find(|(r, _)| *r == region).map(|(_, c)| c); let mut cpath = path.to_vec(); cpath.push(ri as u32); cpath.push(ci as u32); // Effective borders: cell > table (outer vs inside). let tb = tborders; let edge = |own: Option, outer: bool, outer_b: Option, inner_b: Option| own.or(if outer { outer_b } else { inner_b }); let cb = cell.props.borders.unwrap_or_default(); // A row's table property exceptions (`w:tblPrEx`) override the table's borders for its cells. let row_borders = row.props.exceptions.as_ref().and_then(|e| e.borders).unwrap_or_default(); // A row that skips grid columns (`w:gridBefore`/`w:gridAfter`) has its edge at its own // first and last cell. let (first_g, last_g) = (g == row.first_grid(), g + span >= ncols.saturating_sub(row.grid_after())); let rb = look.map(|l| region_borders(&l.borders, (ri, nrows), (first_g, last_g))).unwrap_or_default(); let mut borders = Borders { top: edge(cb.top.or(if ri == 0 { row_borders.top } else { row_borders.between }).or(rb.top), ri == 0, tb.top, tb.between), bottom: edge( cb.bottom.or(if ri + 1 == nrows { row_borders.bottom } else { row_borders.between }).or(rb.bottom), ri + 1 == nrows, tb.bottom, tb.between, ), left: edge( cb.left.or(if first_g { row_borders.left } else { row_borders.inside_v }).or(if first_g { rb.left } else { rb.inside_v }), first_g, tb.left, tb.inside_v, ), right: edge( cb.right.or(if last_g { row_borders.right } else { row_borders.inside_v }).or(if last_g { rb.right } else { rb.inside_v }), last_g, tb.right, tb.inside_v, ), between: None, inside_v: None, }; // The logical start (left) and end (right) borders, drawn mirrored in an RTL table. if rtl { std::mem::swap(&mut borders.left, &mut borders.right); } // Word keeps the cell's text clear of its top border (and the last row's of its bottom // border): the border's width sits on top of the cell margin. let band = |b: Option| b.filter(Border::is_visible).map_or(0.0, |b| b.width.clamp(0.0, 12.0)); let (band_t, band_b) = (band(borders.top), if ri + 1 == nrows { band(borders.bottom) } else { 0.0 }); // A shared edge painted by the row above still clears this cell's text and minimum. let band_t = if band_t > 0.0 || cell.props.vmerge == VMerge::Continue { band_t } else { rows.last() .into_iter() .flatten() .filter(|above| above.g < g + span && g < above.g + above.span) .filter(|above| { above.vmerge == VMerge::None || !t.cell_at_grid(ri, above.g).is_some_and(|ci| { t.grid_col(ri, ci) == above.g && row.cells.get(ci).is_some_and(|c| c.props.vmerge == VMerge::Continue) }) }) .map(|above| band(above.borders.bottom)) .fold(0.0, f32::max) }; let insets = margins[0] + margins[2] + band_t + band_b; let turn = cell.props.text_direction; let mut turned = None; let (items, h) = if cell.props.vmerge == VMerge::Continue { (Vec::new(), 0.0) } else if turn.is_turned() { collapsed = false; // Turned text: its lines run along the cell's height. An exact row height sets // their length; otherwise they get the length the text needs unwrapped and the row // grows to fit (the second pass lays them out again if the row ends up taller). let snap = (ctx.counters.clone(), ctx.eq_count); let len = match (row.props.height, row.props.height_rule) { (Some(h), HeightRule::Exact) if h > 0.0 => (h + row_bottom - insets).clamp(4.0, TURNED_LIMIT), _ => { let probe = layout_box(ctx, BoxInput::cell(story, &cell.blocks, &cpath, TURNED_MAX, text, depth)).items; (ctx.counters, ctx.eq_count) = (snap.0.clone(), snap.1); natural_length(&probe).clamp(4.0, TURNED_MAX) } }; let BoxLayout { items, height, floats_bottom, .. } = layout_box(ctx, BoxInput::cell(story, &cell.blocks, &cpath, len, text, depth)); let across = height.max(floats_bottom); turned = Some(Turned { turn, len, across, snap, region, ci, top: margins[0] + band_t }); (items, len) } else { let BoxLayout { mut items, mut height, floats_bottom, last_top, .. } = layout_box( ctx, BoxInput { context: BoxContext::Cell { text, left: -margins[1], right: x1 - x0 - margins[1], clamp: true }, ..BoxInput::cell(story, &cell.blocks, &cpath, cw, text, depth) }, ); // A hidden end-of-cell mark: an empty last paragraph takes no room (Word for // Windows: a row of empty such cells is only its borders). if cell.props.hide_mark && cell.blocks.last().and_then(|b| b.as_para()).is_some_and(|p| p.text.is_empty()) { height = last_top; } else { collapsed = false; } for it in &mut items { it.translate(x0 + margins[1], margins[0] + band_t); } // The cell grows to hold its floating tables. (items, height.max(floats_bottom)) }; let h = h + insets; let end = h - margins[2] - band_b; if cell.props.vmerge != VMerge::Restart { rh = rh.max(h); } row_insets = row_insets.max(insets); row_below = row_below.max(margins[2] + band_b); if ri + 1 < nrows { row_edge = row_edge.max(band(borders.bottom)); } cells.push(CellBox { items, h, x: x0, w: x1 - x0, // The style's vertical alignment unless the cell has its own. // The cell's own alignment, even Top, beats the table style's. valign: cell.props.valign.or_else(|| look.and_then(|l| l.v_align)).unwrap_or_default(), fill, borders, vmerge: cell.props.vmerge, margins, g, span, turned, end, }); g += span; } // Word adds the cells' top and bottom margins and border bands to an at-least row height: // a 30pt row with 5pt margins and 0.5pt borders is 40.5pt tall even when its text needs less. // An exact height gains only the bottom margin: it runs from the top border to the bottom // of the text area (a 35pt row with 4pt margins steps 39pt). let rh = match (row.props.height, row.props.height_rule) { (Some(h), HeightRule::Exact) if h > 0.0 => h + row_bottom, (Some(h), _) if h > 0.0 => rh.max(h + row_insets), _ => rh, }; bottoms.push(row_below); edges.push(row_edge); heights.push(if collapsed && !cells.is_empty() { rh.max(0.0) } else { rh.max(4.0) }); rows.push(cells); } grow_vertical_merges(&rows, &mut heights); finish_table_rows(ctx, TableRows { story, table: t, path, depth, cell_text, rows, heights, bottoms, edges, x, total }) } fn grow_vertical_merges(rows: &[Vec], heights: &mut [f32]) { // Vertically merged cells: grow the last row of the merge if the content is taller. for ri in 0..rows.len() { let restarts: Vec<(usize, f32, usize)> = rows.get(ri).map(|r| r.iter().filter(|c| c.vmerge == VMerge::Restart).map(|c| (c.g, c.h, c.span)).collect()).unwrap_or_default(); for (g, h, _) in restarts { let mut end = ri; while rows.get(end + 1).is_some_and(|r| r.iter().any(|c| c.g == g && c.vmerge == VMerge::Continue)) { end += 1; } let have: f32 = heights.get(ri..=end).map(|s| s.iter().sum()).unwrap_or(0.0); if h > have && let Some(last) = heights.get_mut(end) { *last += h - have; } } } } struct TableRows<'a> { story: StoryRef, table: &'a Table, path: &'a [u32], depth: usize, cell_text: Vec<(Region, CellText)>, rows: Vec>, heights: Vec, bottoms: Vec, edges: Vec, x: f32, total: f32, } /// Place the laid-out cells of every row: vertical alignment, turned text, borders and the cell /// rectangles, clipped where the row's exact height or a fixed table's column is too small for /// the cell's text. fn finish_table_rows(ctx: &mut Ctx, input: TableRows<'_>) -> TableLayout { let TableRows { story, table: t, path, depth, cell_text, rows, heights, bottoms, edges, x, total } = input; // Grid columns holding a vertical-merge continuation, per row. let cont: Vec> = t .rows .iter() .map(|r| { let mut gx = r.first_grid(); r.cells .iter() .filter_map(|x| { let g = gx; gx += x.span(); (x.props.vmerge == VMerge::Continue).then_some(g) }) .collect() }) .collect(); // Second pass: place cells with their row height, fills and borders. let mut out = Vec::with_capacity(rows.len()); for (ri, cells) in rows.into_iter().enumerate() { let rh = heights.get(ri).copied().unwrap_or(0.0); let exact = t.rows.get(ri).is_some_and(|r| r.props.height_rule == HeightRule::Exact && r.props.height.is_some_and(|h| h > 0.0)); let mut items = Vec::new(); let mut parts = Vec::with_capacity(cells.len()); for (ci, c) in cells.into_iter().enumerate() { let first = items.len(); // A restart cell spans several rows: its height is their sum. let mut span_h = rh; if c.vmerge == VMerge::Restart { let mut k = ri + 1; while cont.get(k).is_some_and(|v| v.contains(&c.g)) { span_h += heights.get(k).copied().unwrap_or(0.0); k += 1; } } let rect = Rect::new(c.x, 0.0, c.w, span_h); if c.vmerge != VMerge::Continue && let Some(f) = c.fill { items.push(Placed::Fill { rect, color: f }); } // Its background picture, over the shading and under the text, cut off at its edges. if c.vmerge != VMerge::Continue && let Some(pic) = t.rows.get(ri).and_then(|r| r.cells.get(ci)).and_then(|cell| cell.props.picture.as_deref()) { let images = cell_picture(pic, rect, story, path); if !images.is_empty() { items.push(Placed::Clip { rect, spin: wordcraft_geom::Spin::default(), items: images }); } } // Behind-text frames count towards vertical alignment even without inline content. // They do not increase the row's flow height. let align_h = c .items .iter() .filter_map(|item| match item { Placed::Object { rect, wrap: wordcraft_doc::para::Wrap::BehindText, .. } => Some(rect.bottom()), _ => None, }) .fold(c.h, f32::max); let mut dy = match c.valign { VAlign::Top => 0.0, VAlign::Center => ((span_h - align_h) / 2.0).max(0.0), VAlign::Bottom => (span_h - align_h).max(0.0), }; let end = (c.vmerge != VMerge::Continue && c.turned.is_none()).then_some(c.end + dy); let mut cell_items = c.items; if let Some(tn) = c.turned { dy = 0.0; let m = c.margins; // The lines run the cell's whole height (laid out again if the row grew). let len = (span_h - (c.h - tn.len)).clamp(4.0, TURNED_LIMIT); let mut across = tn.across; if (len - tn.len).abs() > 0.01 && let Some(cell) = t.rows.get(ri).and_then(|r| r.cells.get(tn.ci)) { let mut cpath = path.to_vec(); cpath.extend([ri as u32, tn.ci as u32]); let text = cell_text.iter().find(|(r, _)| *r == tn.region).map(|(_, c)| c); // Same list numbers as the first layout: lay it out from the same counters. let now = (std::mem::replace(&mut ctx.counters, tn.snap.0), std::mem::replace(&mut ctx.eq_count, tn.snap.1)); let b = layout_box(ctx, BoxInput::cell(story, &cell.blocks, &cpath, len, text, depth)); (cell_items, across) = (b.items, b.height.max(b.floats_bottom)); (ctx.counters, ctx.eq_count) = now; } // Lines stack across the cell from its start edge (right for top-to-bottom text, // left for bottom-to-top); the vertical alignment moves them across it. let cw = (c.w - m[1] - m[3]).max(0.0); let off = match c.valign { VAlign::Top => 0.0, VAlign::Center => ((cw - across) / 2.0).max(0.0), VAlign::Bottom => (cw - across).max(0.0), }; let (ox, oy) = match tn.turn { TextDirection::Up => (c.x + m[1] + off, tn.top + len), _ => (c.x + m[1] + cw - off, tn.top), }; for it in &mut cell_items { it.turn(tn.turn, ox, oy); } } let content = items.len(); for mut it in cell_items { it.translate(0.0, dy); items.push(it); } let align = (dy > 0.0).then_some(CellAlign { content: content..items.len(), valign: c.valign, dy, height: align_h }); let b = c.borders; let (x0, x1, y0, y1) = (c.x, c.x + c.w, 0.0, rh); // Horizontal edges run on over half of each visible vertical edge, so the corners // close the way Word draws them instead of leaving a notch at thick borders. let half = |o: Option| o.filter(Border::is_visible).map_or(0.0, |e| crate::display::rule_width(&e) / 2.0); let (hx0, hx1) = (x0 - half(b.left), x1 + half(b.right)); let top_vis = c.vmerge != VMerge::Continue; if top_vis && let Some(e) = b.top.filter(Border::is_visible) { items.push(Placed::Rule { x0: hx0, y0, x1: hx1, y1: y0, border: e }); } let last_of_merge = c.vmerge == VMerge::None || !cont.get(ri + 1).is_some_and(|v| v.contains(&c.g)); if last_of_merge && let Some(e) = b.bottom.filter(Border::is_visible) { items.push(Placed::Rule { x0: hx0, y0: y1, x1: hx1, y1, border: e }); } if let Some(e) = b.left.filter(Border::is_visible) { items.push(Placed::Rule { x0, y0, x1: x0, y1, border: e }); } if let Some(e) = b.right.filter(Border::is_visible) { items.push(Placed::Rule { x0: x1, y0, x1, y1, border: e }); } items.push(Placed::Cell { rect: Rect::new(c.x, 0.0, c.w, if c.vmerge == VMerge::Continue { rh } else { span_h }), covered: c.vmerge == VMerge::Continue, table: wordcraft_doc::Path(path.to_vec()), row: ri, cell: ci, story, // A fixed table's cell too narrow for any text (its lines are laid out 4 pt wide) // clips them, as Word does, rather than letting them run past its edges. clip: (exact && c.h > span_h + 0.01) || (t.props.fixed && c.w - c.margins[1] - c.margins[3] < 4.0), }); let top = c.margins[0] + c.borders.top.filter(Border::is_visible).map_or(0.0, |b| b.width.clamp(0.0, 12.0)); parts.push(CellPart { top, items: first..items.len(), end, align }); } out.push(RowLayout { height: rh, items, bottom: bottoms.get(ri).copied().unwrap_or(0.0), edge: edges.get(ri).copied().unwrap_or(0.0), cells: parts, }); } TableLayout { x, width: total, rows: out } } /// Split a row at `cut` (points below the row's top) so the part above fits on this page. /// Paragraph lines break between lines; fills, vertical rules and cell areas are cut; the /// rest moves up to the top of the continuation. `None` when no line fits above the cut. /// /// Like Word, the part left on the page keeps the cells' bottom margin and border below its /// text and a cell's last paragraph its space after (a row only that much taller than the room /// left splits rather than moving on whole), and widow control keeps two lines of a paragraph on /// either side of the break. A cell aligned to the middle or bottom of the row is aligned within /// the part left on the page when all its text fits there, and starts at the top of the cell /// otherwise. Each cell's text goes on at the top of the continuation, and the /// continuation is as tall as the cell needing the most room, and at least the rest of the row /// (a minimum height holds over the pages). pub fn split_row(row: &RowLayout, cut: f32) -> Option<(RowLayout, RowLayout)> { split_row_with(row, cut, false) } /// [`split_row`] for the first part of a row on a page that already holds something: `None` /// also when a cell would be left empty on this page because its first line, starting above the /// cut, runs below it (#4272). A line is never split, and an inline picture is one line, so a /// cell holding only a picture taller than the room keeps nothing there; rather than leave the /// other cells' text beside an empty cell, the row moves on whole (see `docs/layout-parity.md`, /// Tables). Content of a cell aligned below the cut doesn't count. pub fn split_row_keeping_cells(row: &RowLayout, cut: f32) -> Option<(RowLayout, RowLayout)> { split_row_with(row, cut, true) } fn split_row_with(row: &RowLayout, cut: f32, whole_cells: bool) -> Option<(RowLayout, RowLayout)> { // Also rejects a NaN cut. if !(cut > 0.0 && cut < row.height) { return None; } let limit = cut - row.bottom.clamp(0.0, cut); let fits = |bottom: f32| bottom <= limit + 0.01; // A cell's background picture (a clip as big as the cell's area): split with the area like // shading, not moved on like a picture in the text. let areas: Vec = row.items.iter().filter_map(|it| if let Placed::Cell { rect, covered: false, .. } = it { Some(*rect) } else { None }).collect(); let background = |it: &Placed| matches!(it, Placed::Clip { rect, .. } if areas.contains(rect)); // Each cell's items: every item of a row is in one of its cells. let (mut cells, mut ends, realign) = cells_from_top(row, limit); // A nested table's cell is deeper than the row's own. let own_depth = row.items.iter().filter_map(|it| if let Placed::Cell { table, .. } = it { Some(table.0.len()) } else { None }).min(); // The first line of each paragraph that moves on (`l1` when all of them stay), by cell and // item. // The top of a cell's text on this page decides which paragraph starts the cell's part of the // page. Only text in the flow counts: a floating picture or shape anchored in the cell can sit // above it without the text being any less the first. let content_top = |it: &Placed| -> Option { if background(it) { return None; } if let Some((_, _, _, y)) = text_lines(it) { return Some(y); } it.turned_bounds().map(|r| r.y) }; let splits: Vec> = cells .iter() .zip(&ends) .map(|(items, end)| { // Where the cell's text ends: the paragraph ending lowest is the cell's last. let lowest = items.iter().filter_map(|it| text_lines(it).and_then(|(p, l0, l1, y)| crate::item_bottom(y, p, l0, l1))).fold(f32::MIN, f32::max); // The top of what the cell holds on this page: a paragraph there starts the cell's // part of the page, so a row that doesn't start below other content has nowhere to // move it to (see `line_split`). let cell_top = items.iter().filter_map(&content_top).fold(f32::MAX, f32::min); items .iter() .map(|it| { let Some((para, l0, l1, y)) = text_lines(it) else { return 0 }; // The cell's last paragraph ends with the cell's content (its space after). let last = l1 == para.lines.len() && crate::item_bottom(y, para, l0, l1).is_some_and(|b| b >= lowest - 0.01); let first = !whole_cells && y <= cell_top + 0.01; line_split(para, (l0, l1), y, limit, end.filter(|_| last), first) }) .collect() }) .collect(); // A cell that widow/orphan control leaves with nothing on this page, though its first line // would fit there, takes the whole row on with it: Word doesn't leave the other cells behind // beside an empty one (observed with synthetic documents). Trailing space alone can move // the last paragraph without deferring the whole row. for (items, splits) in cells.iter().zip(&splits) { let first = items.iter().zip(splits).find_map(|(it, &split)| text_lines(it).map(|t| (t, split))); if let Some(((para, l0, l1, y), split)) = first && split == l0 && para.rp.widow_control && l1.saturating_sub(l0) > 1 && crate::item_bottom(y, para, l0, l1).is_some_and(|bottom| !fits(bottom)) && para.lines.get(l0).is_some_and(|l| fits(y + l.top - para.lines.get(l0).map_or(0.0, |f| f.top) + l.height)) { return None; } } align_in_part(row, &mut cells, &splits, &realign, &mut ends, cut); // The top of the first thing that moves on in each cell. let mut moved = vec![f32::MAX; cells.len()]; // Whether each cell keeps anything on this page, and whether a line or picture of it that // moves on starts above the cut but runs below it (its space after aside). let mut kept_cells = vec![false; cells.len()]; let mut cut_through = vec![false; cells.len()]; for ((((items, splits), m), kept), through) in cells.iter().zip(&splits).zip(&mut moved).zip(&mut kept_cells).zip(&mut cut_through) { for (it, &split) in items.iter().zip(splits) { if background(it) { continue; } let mut runs_over = |top: f32, bottom: f32| *through |= top < limit - 0.01 && bottom > limit + 0.01; let top = if let Some(r) = it.turned_bounds() { // Turned text moves as a whole, like a picture. if fits(r.bottom()) { *kept = true; None } else { runs_over(r.y, r.bottom()); Some(r.y) } } else { match it { Placed::Lines { para, l0, l1, y, .. } => { *kept |= split > *l0; let base = para.lines.get(*l0).map(|l| l.top).unwrap_or(0.0); (split < *l1).then(|| { let Some(l) = para.lines.get(split) else { return *y }; runs_over(y + l.top - base, y + l.top - base + l.height); y + l.top - base }) } Placed::Image { rect, .. } | Placed::Shape { rect, .. } | Placed::Graphic { rect, .. } | Placed::Clip { rect, .. } => { if fits(rect.bottom()) { *kept = true; None } else { runs_over(rect.y, rect.bottom()); Some(rect.y) } } // A nested table's cell the cut runs through goes on from the cut, so what is // below the cut keeps its size and the text after it stays below it (a // floating table's fixed-height row in an otherwise fitting cell splits the // row there). A row that is just taller than its text (a minimum height) // moves on whole, as in Word. Placed::Cell { rect, table, .. } if Some(table.0.len()) > own_depth && rect.y < cut && rect.bottom() > cut + 0.01 => Some(cut), _ => None, } }; if let Some(top) = top { *m = m.min(top); } } } let kept_any = kept_cells.iter().any(|&k| k); let first_moved = moved.iter().copied().fold(f32::MAX, f32::min); if !kept_any || first_moved == f32::MAX { return None; } // A cell left with nothing here because its first line or picture runs below the cut. if whole_cells && kept_cells.iter().zip(&cut_through).any(|(&kept, &through)| !kept && through) { return None; } // Each cell resumes below its own top margin and border band. let shifts: Vec = moved.iter().zip(&row.cells).map(|(&m, c)| ((if m < f32::MAX { m } else { first_moved }) - c.top).max(0.0)).collect(); // The continuation is as tall as the cell needing the most room (what it moves on and the // bottom margin, the rest of the row for turned text), and at least the rest of the row. let full = (row.height - shifts.iter().copied().fold(f32::MAX, f32::min)).max(4.0); let mut need = row.height - cut; for ((end, &m), &s) in ends.iter().zip(&moved).zip(&shifts) { if m < f32::MAX { need = need.max(end.map_or(row.height, |e| e + row.bottom) - s); } } let height = need.clamp(4.0, full); // What runs to the row's bottom (cell areas, shading, side borders) ends at the // continuation's. let rest = (row.height - height).max(0.0); let geo_shift = |bottom: f32, shift: f32| if bottom >= row.height - 0.01 { rest } else { shift }; let (mut a, mut b) = (Vec::new(), Vec::new()); let mut parts = Vec::with_capacity(cells.len()); for ((((c, items), splits), &shift), end) in row.cells.iter().zip(&cells).zip(&splits).zip(&shifts).zip(&ends) { let start = b.len(); for (it, &split) in items.iter().zip(splits) { // A background picture: the part above the cut stays, and the continuation's area // shows the rest of it, from the cut down. if let Placed::Clip { rect, spin, items: pictures } = it && background(it) { let (above, below) = split_rect(*rect, cut, geo_shift(rect.bottom(), shift)); a.extend(above.map(|rect| Placed::Clip { rect, spin: *spin, items: pictures.clone() })); b.extend(below.map(|r| { let mut pictures = pictures.clone(); for p in &mut pictures { p.translate(0.0, r.y - rect.y.max(cut)); } Placed::Clip { rect: r, spin: *spin, items: pictures } })); continue; } if let Some(r) = it.turned_bounds() { let mut it = it.clone(); if fits(r.bottom()) { a.push(it); } else { it.translate(0.0, -shift); b.push(it); } continue; } match it { Placed::Lines { story, path, para, l0, l1, x, y, turn } => { let base = para.lines.get(*l0).map(|l| l.top).unwrap_or(0.0); if split > *l0 { a.push(Placed::Lines { story: *story, path: path.clone(), para: para.clone(), l0: *l0, l1: split, x: *x, y: *y, turn: *turn, }); } if split < *l1 { let top = para.lines.get(split).map(|l| y + l.top - base).unwrap_or(*y); b.push(Placed::Lines { story: *story, path: path.clone(), para: para.clone(), l0: split, l1: *l1, x: *x, y: top - shift, turn: *turn, }); } } Placed::Image { rect, .. } | Placed::Shape { rect, .. } | Placed::Graphic { rect, .. } | Placed::Clip { rect, .. } | Placed::Object { rect, .. } => { if fits(rect.bottom()) { a.push(it.clone()); } else { let mut it = it.clone(); it.translate(0.0, -shift); b.push(it); } } // Shading and cell areas: the part above the cut stays, the rest moves up with the // continuation (a nested table's cell below the cut moves whole). Placed::Fill { rect, color } => { let (above, below) = split_rect(*rect, cut, geo_shift(rect.bottom(), shift)); a.extend(above.map(|rect| Placed::Fill { rect, color: *color })); b.extend(below.map(|rect| Placed::Fill { rect, color: *color })); } Placed::Cell { rect, covered, table, row: r, cell, story, clip } => { let (above, below) = split_rect(*rect, cut, geo_shift(rect.bottom(), shift)); let cell_at = |rect| Placed::Cell { rect, covered: *covered, table: table.clone(), row: *r, cell: *cell, story: *story, clip: *clip }; a.extend(above.map(cell_at)); b.extend(below.map(cell_at)); } Placed::Rule { x0, y0, x1, y1, border } => { if (y0 - y1).abs() < 0.01 { // Horizontal: the top edge stays, the bottom edge moves; both fragments close. if *y0 <= 0.01 { a.push(it.clone()); b.push(Placed::Rule { x0: *x0, y0: 0.0, x1: *x1, y1: 0.0, border: *border }); } else if *y0 >= row.height - 0.01 { a.push(Placed::Rule { x0: *x0, y0: cut, x1: *x1, y1: cut, border: *border }); b.push(Placed::Rule { x0: *x0, y0: y0 - rest, x1: *x1, y1: y1 - rest, border: *border }); } else if *y0 <= cut { a.push(it.clone()); } else { b.push(Placed::Rule { x0: *x0, y0: y0 - shift, x1: *x1, y1: y1 - shift, border: *border }); } } else { let (t, bt) = (y0.min(*y1), y0.max(*y1)); if t < cut { a.push(Placed::Rule { x0: *x0, y0: t, x1: *x1, y1: bt.min(cut), border: *border }); } if bt > cut { let shift = geo_shift(bt, shift); b.push(Placed::Rule { x0: *x0, y0: (t - shift).max(0.0), x1: *x1, y1: bt - shift, border: *border }); } } } } } parts.push(CellPart { top: c.top, items: start..b.len(), end: end.map(|e| e - shift), align: None }); } // The part left on the page is placed and never split again. Some(( RowLayout { height: cut, items: a, bottom: row.bottom, edge: 0.0, cells: Vec::new() }, RowLayout { height, items: b, bottom: row.bottom, edge: row.edge, cells: parts }, )) } type CellItems<'a> = Vec>; /// The items of each cell of a row being split, with each cell's content end, and the cells /// aligned to the middle or bottom that fit above `limit`. Those cells go back to the top first: /// Word runs such a cell from its top when its text doesn't all fit above the cut, and lines it /// up within the part left on the page (in [`align_in_part`]) when it does. fn cells_from_top(row: &RowLayout, limit: f32) -> (CellItems<'_>, Vec>, Vec>) { let mut ends = Vec::with_capacity(row.cells.len()); let mut realign = Vec::with_capacity(row.cells.len()); let cells = row .cells .iter() .map(|c| { let items = row.items.get(c.items.clone()).unwrap_or(&[]); let Some(al) = c.align.as_ref() else { ends.push(c.end); realign.push(None); return std::borrow::Cow::Borrowed(items); }; let mut items = std::borrow::Cow::Borrowed(items); shift_content(&mut items, c, al, -al.dy); let end = c.end.map(|e| e - al.dy); ends.push(end); realign.push(end.is_some_and(|e| e <= limit + 0.01).then_some(al)); items }) .collect(); (cells, ends, realign) } /// Line up the middle and bottom cells marked in `realign` within the part of the row left on the /// page: as tall as the other cells' text kept there (by `splits`) and the bottom margin. fn align_in_part( row: &RowLayout, cells: &mut [std::borrow::Cow<[Placed]>], splits: &[Vec], realign: &[Option<&CellAlign>], ends: &mut [Option], cut: f32, ) { if realign.iter().all(Option::is_none) { return; } let limit = cut - row.bottom.clamp(0.0, cut); let mut kept = f32::MIN; for (((items, splits), _), end) in cells.iter().zip(splits).zip(realign).zip(ends.iter()).filter(|((_, al), _)| al.is_none()) { // No further than the cell's content: its last paragraph may have dropped its space after. let end = end.unwrap_or(f32::MAX); for (it, &split) in items.iter().zip(splits) { let bottom = match (it.turned_bounds(), it) { (Some(r), _) => Some(r.bottom()), // A paragraph kept whole ends with its space after. (None, Placed::Lines { para, l0, y, .. }) if split > *l0 => crate::item_bottom(*y, para, *l0, split) .filter(|&b| b <= limit + 0.01) .map(|b| if split == para.lines.len() { (b + para.rp.space_after).min(end.max(b)).min(limit) } else { b }), (None, Placed::Image { rect, .. } | Placed::Shape { rect, .. } | Placed::Graphic { rect, .. } | Placed::Clip { rect, .. }) => { Some(rect.bottom()) } _ => None, }; if let Some(b) = bottom.filter(|&b| b <= limit + 0.01) { kept = kept.max(b); } } } let part = if kept > f32::MIN { kept + row.bottom } else { cut }; for (((items, al), c), end) in cells.iter_mut().zip(realign).zip(&row.cells).zip(ends) { let (Some(al), Some(e)) = (al, *end) else { continue }; let room = (part - al.height).max(0.0); let dy = if al.valign == VAlign::Center { room / 2.0 } else { room }; let dy = dy.min((limit - e).max(0.0)); shift_content(items, c, al, dy); *end = Some(e + dy); } } /// Move the content of cell `c` (its items `items`) down by `dy`. fn shift_content(items: &mut std::borrow::Cow<[Placed]>, c: &CellPart, al: &CellAlign, dy: f32) { for k in al.content.clone() { if let Some(it) = k.checked_sub(c.items.start).and_then(|k| items.to_mut().get_mut(k)) { it.translate(0.0, dy); } } } /// A cell paragraph's placed lines (not turned text): the paragraph, its lines `l0..l1` and the /// top of line `l0`. fn text_lines(it: &Placed) -> Option<(&ParaLayout, usize, usize, f32)> { match it { Placed::Lines { para, l0, l1, y, .. } if it.turned_bounds().is_none() => Some((para, *l0, *l1, *y)), _ => None, } } /// The first of lines `l0..l1` of `para` (placed with line `l0`'s top at `y`) that does not fit /// above `limit`: the last line of a cell's last paragraph needs room down to the cell's content /// `end`, its space after. Widow control keeps two lines on either side of the break, or moves /// the paragraph on whole. /// /// `first`: the paragraph is the topmost content of its cell on a page the row doesn't start /// below other content on (the row continues from the page before, or starts the page). Moving /// it whole would gain nothing, and Word then splits it where the lines fall (measured with /// synthetic documents, Word for Windows 16.0, compatibility mode 15): a lone first line stays, /// and a three-line paragraph with room for two splits two and one, not leaving the first line /// alone (a continuation's lone line stays too). A paragraph of four lines or more still takes /// one more line along with its widow. /// Without this, nothing was kept and the row ran off the bottom of the page (#6823). fn line_split(para: &ParaLayout, (l0, l1): (usize, usize), y: f32, limit: f32, end: Option, first: bool) -> usize { let n = para.lines.len(); let mut k = l0; while k < l1 { let Some(bottom) = crate::item_bottom(y, para, l0, k + 1) else { break }; let bottom = if k + 1 == n { end.map_or(bottom, |e| e.max(bottom)) } else { bottom }; if bottom > limit + 0.01 { break; } k += 1; } // The body's rule differs: there a widow takes a line along only when three or more lines are // on the page (`place_para_in_flow`). if para.rp.widow_control && k > l0 && k < l1 { // A widow: take one more line along, unless that leaves nothing of a paragraph that // starts the page on it, or its first line alone. if n - k == 1 { let taken = k - 1; if !(first && (taken <= l0 || (l0 == 0 && taken == 1))) { k = taken; } } // An orphan (the first line alone): the paragraph moves on whole. if l0 == 0 && k == 1 && !first { k = 0; } } k } /// Most copies of a tiled cell picture drawn. const MAX_TILES: usize = 400; /// A cell's background picture in `rect` (the cell's area): once at its own size where its /// alignment puts it, tiled from the cell's top left, or stretched over the cell. fn cell_picture(pic: &wordcraft_doc::props::CellPicture, rect: Rect, story: StoryRef, path: &[u32]) -> Vec { use wordcraft_doc::props::PictureRepeat; let [w, h] = pic.size; let image = |r: Rect| Placed::Image { rect: r, media: pic.media.clone(), crop: [0.0; 4], story, path: wordcraft_doc::Path(path.to_vec()), off: usize::MAX, spin: wordcraft_geom::Spin::default(), fx: Default::default(), }; let sized = w.is_finite() && h.is_finite() && w > 0.1 && h > 0.1; match pic.repeat { PictureRepeat::Stretch => vec![image(rect)], PictureRepeat::Tile if sized => { let (cols, rows) = ((rect.w / w).ceil().max(1.0), (rect.h / h).ceil().max(1.0)); // Too many tiles to draw (or a cell of no finite size): one picture stretched over // the cell, as a page's picture is, so the cell is always covered. let tiles = cols * rows; if !tiles.is_finite() || tiles > MAX_TILES as f32 { return vec![image(rect)]; } let (cols, rows) = (cols as usize, rows as usize); (0..rows) .flat_map(|r| (0..cols).map(move |c| (r, c))) .map(|(r, c)| image(Rect::new(rect.x + c as f32 * w, rect.y + r as f32 * h, w, h))) .collect() } _ if sized => { let at = |a: f32| if a.is_finite() { a.clamp(0.0, 1.0) } else { 0.0 }; let x = rect.x + (rect.w - w) * at(pic.align[0]); let y = rect.y + (rect.h - h) * at(pic.align[1]); vec![image(Rect::new(x, y, w, h))] } _ => Vec::new(), } } /// The parts of `r` above `cut` and below it, the latter moved up by `shift` (onto the /// continuation of a row split at `cut`). fn split_rect(r: Rect, cut: f32, shift: f32) -> (Option, Option) { let above = (r.y < cut).then(|| Rect::new(r.x, r.y, r.w, (r.bottom().min(cut) - r.y).max(0.0))); let top = (r.y - shift).max(0.0); let below = (r.bottom() > cut).then(|| Rect::new(r.x, top, r.w, (r.bottom() - shift - top).max(0.0))); (above, below) } /// A cell's table style parts. type Region = PartSet; /// The parts of the cell at grid column `g` spanning `span` of `ncols` columns in row `ri`. fn region_of(t: &Table, style: Option<&TableStyleProps>, ri: usize, g: usize, span: usize, ncols: usize) -> Region { let rows = style.and_then(|s| s.parts.band_size).unwrap_or(0); let cols = style.and_then(|s| s.parts.col_band_size).unwrap_or(0); PartSet::of_cell(&t.props.look, (rows, cols), (ri, t.rows.len()), (g, span, ncols)) } /// The table style's text formatting in `region`: each part's character formatting and paragraph /// alignment over the last, lowest precedence first (ECMA-376 §17.7.6). fn region_text(st: &TableStyleProps, region: Region) -> CellText { let mut chr = wordcraft_doc::CharProps::default(); let mut para = st.para.clone(); for part in region.iter() { let f = st.part(part); chr.overlay(&f.chr); if f.align.is_some() { para.align = f.align; } } CellText { para, chr } } /// Each column's narrowest and widest content, cell margins included: its longest word and its /// longest line. A cell spanning columns shares what they lack between them; nested tables count /// with their own. `None` when every cell is empty. fn column_content(ctx: &mut Ctx, t: &Table, style: Option<&TableStyleProps>, depth: usize, grid: &[f32]) -> Option> { let margins_def = default_margins(t, style); // The grid's width once: counting it walks every row. let ncols = grid_cols(t); let mut cols = vec![(0.0f32, 0.0f32); ncols.max(1)]; let mut spanning = Vec::new(); let mut any = false; for (ri, row) in t.rows.iter().enumerate() { let mut g = row.first_grid(); for cell in &row.cells { let span = cell.span(); if cell.props.vmerge != VMerge::Continue { let text = style.map(|st| region_text(st, region_of(t, style, ri, g, cell.span(), cols.len()))); let (lo, hi) = blocks_content(ctx, &cell.blocks, text.as_ref(), depth); any |= hi > 0.0; let m = cell.props.resolved_margins(margins_def); let need = (lo + m[1] + m[3], hi + m[1] + m[3]); match cols.get_mut(g) { Some(col) if span == 1 => *col = (col.0.max(need.0), col.1.max(need.1)), _ => spanning.push((g, span, need)), } } g += span; } spanning.extend(row_gaps(row, g).map(|(g, span, w)| (g, span, (w, w)))); } for (g, span, need) in spanning { let Some(cs) = cols.get_mut(g..g.saturating_add(span).min(ncols)) else { continue }; let n = cs.len().max(1) as f32; let have_hi: f32 = cs.iter().map(|c| c.1).sum(); let preferred = (!is_automatic(t)).then(|| grid.get(g..g + cs.len())).flatten(); share_span_min(cs, preferred, need.0); for c in cs { c.1 += (need.1 - have_hi).max(0.0) / n; } } any.then_some(cols) } // Share a span's extra minimum in preferred-column proportions. Each column keeps // its own text minimum; automatic tables share the extra equally. fn share_span_min(cols: &mut [(f32, f32)], grid: Option<&[f32]>, need: f32) { let extra = (need - cols.iter().map(|c| c.0).sum::()).max(0.0); let weight = |w: &f32| if w.is_finite() { w.clamp(0.0, UNBOUNDED) } else { 0.0 }; let total = grid.map_or(0.0, |grid| grid.iter().take(cols.len()).map(weight).sum::()); if total > 0.0 { if let Some(grid) = grid { for (c, w) in cols.iter_mut().zip(grid) { c.0 += extra * weight(w) / total; } } return; } let share = extra / cols.len().max(1) as f32; for c in cols { c.0 += share; } } /// The grid columns row `row` leaves empty before its first cell and after its last one (which /// ends at grid column `end`) when it gives their width (`w:wBefore`, `w:wAfter`): (first column, /// columns, width in points). Column widths make room for them like for an empty cell. fn row_gaps(row: &Row, end: usize) -> impl Iterator { let p = &row.props; [(0, row.first_grid(), p.w_before), (end, row.grid_after(), p.w_after)] .into_iter() .filter(|&(_, n, _)| n > 0) .filter_map(|(g, n, w)| w.filter(|w| w.is_finite() && *w > 0.0).map(|w| (g, n, w.min(1584.0)))) } /// The narrowest and widest a block list can be laid out. fn blocks_content(ctx: &mut Ctx, blocks: &Blocks, text: Option<&CellText>, depth: usize) -> (f32, f32) { let (mut lo, mut hi) = (0.0f32, 0.0f32); for b in blocks.iter() { let (l, h) = match &**b { Block::Para(p) => { let label = list_level(ctx, p).map(|l| (String::new(), l)); ctx.para_measured(p, UNBOUNDED, text, label, para_content) } // A nested table: its fixed width, else its columns' but at least its preferred width // (its own, else its grid's when it has preferred widths at all). Block::Table(nt) if depth < 8 => { let grid = if is_automatic(nt) { 0.0 } else { nt.grid.iter().filter(|w| w.is_finite()).map(|w| w.max(0.0)).sum() }; let width = nt.props.width.filter(|w| w.is_finite()).unwrap_or(grid).clamp(0.0, UNBOUNDED); if nt.props.fixed && width > 0.0 { (width, width) } else { let style = table_style(ctx, nt); let weights = preferred_grid(nt, width); let cols = column_content(ctx, nt, style.as_ref(), depth + 1, &weights).unwrap_or_default(); let (l, h) = cols.iter().fold((0.0, 0.0), |(l, h), c| (l + c.0, h + c.1)); (width.max(l), width.max(h)) } } Block::Table(_) => (0.0, 0.0), }; lo = lo.max(l); hi = hi.max(h); } (lo, hi) } /// The list level `p` is in, for its indents (without counting it: measuring isn't laying out). fn list_level(ctx: &Ctx, p: &Paragraph) -> Option { let n = p.props.numbering.or_else(|| ctx.doc.styles.resolve_para(&p.props).numbering).filter(|n| n.num != 0)?; ctx.doc.numbering.level(n.num, n.level).cloned() } /// A width to lay a paragraph out in so that only its own line breaks end lines. const UNBOUNDED: f32 = 100_000.0; /// A paragraph's longest word and longest line (between line breaks), indents included. fn para_content(pl: &ParaLayout) -> (f32, f32) { let indents = pl.rp.indent_left.max(0.0) + pl.rp.indent_right.max(0.0) + pl.rp.indent_first.max(0.0); let (mut word, mut line, mut spaces) = (0.0f32, 0.0f32, 0.0f32); let (mut lo, mut hi) = (0.0f32, 0.0f32); for c in &pl.clusters { let adv = if c.adv.is_finite() { c.adv.clamp(0.0, UNBOUNDED) } else { 0.0 }; match c.kind { ClKind::Space => { lo = lo.max(word); word = 0.0; spaces += adv; } ClKind::LineBreak | ClKind::PageBreak | ClKind::ColumnBreak => { lo = lo.max(word); hi = hi.max(line); (word, line, spaces) = (0.0, 0.0, 0.0); } _ => { word += adv; line += spaces + adv; spaces = 0.0; if c.break_after { lo = lo.max(word); word = 0.0; } } } } (lo.max(word) + indents, hi.max(line) + indents) } /// Whether nothing in `t` asks for a width (its own or any cell's): its columns then follow /// their content alone. fn is_automatic(t: &Table) -> bool { t.props.width.is_none() && t.props.width_pct.is_none() && t.rows.iter().all(|r| r.cells.iter().all(|c| c.props.width.is_none() && c.props.width_pct.is_none())) } /// Consistent cell widths replace a stale grid without changing the table's total width. /// Conflicting widths, spans and percentages need the full shared-grid algorithm. fn apply_cell_widths(t: &Table, grid: &mut [f32]) { let Some(first) = t.rows.first() else { return }; if first.cells.len() != grid.len() { return; } let widths: Option> = first.cells.iter().map(|c| c.props.width.filter(|w| w.is_finite() && *w > 0.0)).collect(); let Some(widths) = widths else { return }; let total: f32 = widths.iter().sum(); if !total.is_finite() || (total - grid.iter().sum::()).abs() > 0.01 { return; } if t.rows.iter().any(|r| { r.cells.len() != grid.len() || r.cells.iter().zip(&widths).any(|(c, w)| c.span() != 1 || c.props.width_pct.is_some() || c.props.width != Some(*w)) }) { return; } grid.copy_from_slice(&widths); } /// Word sizes each column by the percentage its cells ask for, whichever rows ask: cells without a /// preferred width take no part, so a header row of percentages sizes the automatic rows below it. /// The percentages are shares of the table's width, scaled up when they come short of 100%; past /// 100% the columns take their shares left to right until the width runs out /// (`overfull_shares`). A table without a /// width of its own takes `avail` when autofit and its grid's width when fixed. Where rows ask /// different percentages of a column it takes the largest. A merged cell's percentage is shared /// by the columns under it that ask none of their own, in their grid proportions (Word shares it /// by their content), and makes no column narrower than it asks. Measured in Word. Columns no cell /// sizes and absolute widths keep the grid. fn apply_pct_widths(t: &Table, grid: &mut [f32], avail: f32) { let mut pcts: Vec> = vec![None; grid.len()]; let mut merged: Vec<(usize, usize, f32)> = Vec::new(); for row in &t.rows { let mut g = row.first_grid(); for cell in &row.cells { if cell.props.width.is_some() { return; } if let Some(p) = cell.props.width_pct { if !p.is_finite() || p <= 0.0 { return; } if cell.span() == 1 { let Some(slot) = pcts.get_mut(g) else { return }; *slot = Some(slot.map_or(p, |q| q.max(p))); } else { merged.push((g, cell.span(), p)); } } g = g.saturating_add(cell.span()); } } for (g, span, p) in merged { let Some(cols) = pcts.get_mut(g..g.saturating_add(span)) else { return }; let Some(weights) = grid.get(g..g.saturating_add(span)) else { return }; let own: f32 = cols.iter().flatten().sum(); let free: f32 = cols.iter().zip(weights).filter(|(c, _)| c.is_none()).map(|(_, w)| *w).sum(); let rest = (p - own).max(0.0); if free > 0.0 { for (c, w) in cols.iter_mut().zip(weights) { if c.is_none() { *c = Some(rest * w / free); } } } else if own > 0.0 && rest > 0.0 { // Every column asks its own, short of the merged cell's: they grow to it together. cols.iter_mut().flatten().for_each(|c| *c *= p / own); } } let Some(pcts) = pcts.into_iter().collect::>>() else { return }; if pcts.iter().any(|p| *p <= 0.0) { return; } let sum: f32 = pcts.iter().sum(); let own_width = t.props.width_pct.is_some_and(|p| p > 0.0) || t.props.width.is_some_and(|w| w > 0.0); // A table with its own width is scaled to it afterwards; any positive total will do. A fixed // table without one keeps its grid's width. let total = if own_width || t.props.fixed { grid.iter().sum() } else { avail }; if !sum.is_finite() || sum <= 0.0 || !total.is_finite() || total <= 0.0 { return; } if sum <= 100.0 { let shares: Vec = pcts.iter().map(|p| total * (p / sum)).collect(); // A grid Word wrote from these same percentages is within its rounding of them (whole // twips, and percentages in fiftieths): it is kept exactly. Rebuilt, a column could come // out a few hundredths of a point narrower and wrap its text differently (#6575). let rounding = 0.05 + total * 0.0002; if grid.iter().zip(&shares).all(|(g, s)| (g - s).abs() <= rounding) { return; } grid.copy_from_slice(&shares); return; } // A grid too narrow for the floor in every column shares its width out instead, so the table // keeps it. let floor = if t.props.fixed { FIXED_PCT_MIN_COLUMN.min(total / pcts.len() as f32) } else { 0.0 }; grid.copy_from_slice(&overfull_shares(&pcts, total, floor)); } /// Over 100%, as Word lays it out: the columns take their shares left to right until the table's /// width runs out; the column it runs out in takes what is left, and those after it nothing. In an /// autofit table autofit then widens them to their longest words at the others' expense. In a /// fixed table they keep `floor` each, their text clipped, and the shares are of what is left /// once those are set aside. fn overfull_shares(pcts: &[f32], total: f32, floor: f32) -> Vec { let mut starved = 0; loop { let room = (total - floor * starved as f32).max(0.0); let mut used = 0.0; let mut short = 0; let widths: Vec = pcts .iter() .map(|p| { let w = (room * p / 100.0).min(room - used); if w < floor || w <= 0.0 { short += 1; return floor; } used += w; w }) .collect(); // More columns starved than were set aside for: set aside for them and share again. if short <= starved || starved >= pcts.len() { return widths; } starved = short; } } /// The narrowest a fixed table's columns get when the percentages before them fill the table /// (measured in Word: 2 pt, with or without a table width). const FIXED_PCT_MIN_COLUMN: f32 = 2.0; /// Word's autofit for a table whose widths may follow its content (`tblLayout` not fixed). /// - With no preferred width anywhere (the table's and every cell's automatic) the columns come /// from their content alone: each its widest line if they all fit in `avail`, else shared out /// between narrowest and widest in proportion to how much each can give. /// - Otherwise the grid stands, but no column is narrower than its longest word: the columns with /// room to spare give up the difference. fn autofit(grid: &mut [f32], content: &[(f32, f32)], t: &Table, avail: f32) { if grid.len() != content.len() || !avail.is_finite() { return; } if is_automatic(t) { let (lo, hi) = content.iter().fold((0.0f32, 0.0f32), |(l, h), c| (l + c.0, h + c.1.max(c.0))); let k = if hi <= avail { 1.0 } else if lo >= avail || hi - lo < 0.01 { 0.0 } else { (avail - lo) / (hi - lo) }; for (w, c) in grid.iter_mut().zip(content) { *w = (c.0 + (c.1.max(c.0) - c.0) * k).max(4.0); } return; } let short: f32 = grid.iter().zip(content).map(|(w, c)| (c.0 - *w).max(0.0)).sum(); if short < 0.01 { return; } // Donors give equal widths until they reach their own content minimum. let mut rooms: Vec<_> = grid.iter().zip(content).map(|(w, c)| *w - c.0).filter(|room| *room > 0.0).collect(); rooms.sort_by(f32::total_cmp); let mut remaining = short; let mut share = 0.0; for (i, room) in rooms.iter().enumerate() { share = remaining / (rooms.len() - i) as f32; if share <= *room { break; } remaining -= room; } for (w, c) in grid.iter_mut().zip(content) { *w = if *w < c.0 { c.0 } else { *w - (*w - c.0).min(share) }; } } #[cfg(test)] mod width_tests { use super::*; #[test] fn autofit_shares_donor_loss() { crate::test_fonts(); let table = Table::new(1, 4, 260.0); let mut grid = [30.0, 120.0, 20.0, 90.0]; let content = [(28.0, 28.0), (40.0, 120.0), (40.0, 40.0), (30.0, 90.0)]; autofit(&mut grid, &content, &table, 260.0); assert_eq!(grid, [28.0, 111.0, 40.0, 81.0]); } /// #5074 review (and #6575): a grid Word wrote from the cells' percentages is kept exactly, /// while one that is off by more than Word's rounding is rebuilt from them. #[test] fn a_grid_matching_its_percentages_is_kept() { let mut t = Table::new(1, 3, 300.0); t.props.width = Some(300.0); for (cell, p) in t.rows[0].cells.iter_mut().zip([33.32, 33.34, 33.34]) { cell.props.width = None; cell.props.width_pct = Some(p); } let mut grid = [99.95, 100.05, 100.0]; apply_pct_widths(&t, &mut grid, 468.0); assert_eq!(grid, [99.95, 100.05, 100.0], "kept"); let mut stale = [90.0, 110.0, 100.0]; apply_pct_widths(&t, &mut stale, 468.0); assert!(stale.iter().zip([99.96, 100.02, 100.02]).all(|(g, w)| (g - w).abs() < 0.01), "rebuilt: {stale:?}"); } #[test] fn autofit_exhausts_donors() { crate::test_fonts(); let table = Table::new(1, 2, 50.0); let mut grid = [30.0, 20.0]; autofit(&mut grid, &[(25.0, 25.0), (40.0, 40.0)], &table, 50.0); assert_eq!(grid, [25.0, 40.0]); } #[test] fn autofit_keeps_fitting_widths() { crate::test_fonts(); let table = Table::new(1, 3, 120.0); let mut grid = [30.0, 40.0, 50.0]; autofit(&mut grid, &[(10.0, 10.0), (20.0, 20.0), (30.0, 30.0)], &table, 120.0); assert_eq!(grid, [30.0, 40.0, 50.0]); } }