f-office-docx 0.2.4

DOCX model and Word's layout rules.
Documentation
//! Table rows kept with the next row. A row whose first cell starts with a paragraph that is
//! kept with the next one stays on the page where the next row starts. Such rows form a chain
//! with the row after them and with a chain of kept paragraphs before the table, and the
//! table's last row, when it is kept, stays with the blocks after the table.

use super::merges::Merges;
use super::{Shape, Table};
use crate::body::Block;
use crate::layout::paragraph::starts_page;
use crate::layout::{Column, Layout, Rest};
use f_office_units::Length;

/// What the rows of a table in the body need to know about the blocks around the table.
#[derive(Clone, Copy)]
pub(super) struct Around<'a> {
    /// The text column that the table stands in.
    pub(super) column: &'a Column,
    /// Whether the paragraph right before the table is kept with the next one, so that a
    /// chain of kept rows from the first row goes on from that paragraph's chain.
    pub(super) kept_before: bool,
    /// The blocks after the table.
    pub(super) rest: Rest<'a>,
}

impl Layout<'_> {
    /// Whether row `index` is kept with the next row: the first paragraph of its first cell,
    /// in the order of the markup, is kept with the next one (`w:keepNext`), from the
    /// paragraph or from its style, also when it is empty. The other paragraphs of the row do
    /// not count, and a first cell that starts with a table does not keep its row. From
    /// [`crate::params::HEADER_ROWS_KEPT_MODE`], a header row above another row of its table
    /// is kept too.
    pub(super) fn row_kept(&self, shape: &Shape, index: usize) -> bool {
        // From mode 15 a header row stays on the page where the row below it starts. The last
        // row of a table of header rows alone follows the rule of the other rows below.
        if index < shape.grid.headers
            && index + 1 < shape.grid.table.rows.len()
            && self.settings.mode_rules.keeps_header_rows()
        {
            return true;
        }
        let Some((cell, style)) = shape.grid.first_cell(index) else {
            return false;
        };
        let Some(Block::Paragraph(paragraph)) = cell.blocks.first() else {
            return false;
        };
        self.styles
            .paragraph_properties_without_list_level(&paragraph.props, Some(&style))
            .flags()
            .keep_next
    }

    /// Whether row `index`, about to be laid out at the current position, starts a chain of
    /// kept rows that must start the next page. Only the first row of a chain tests it: a
    /// kept row after a kept row, or the first row after a kept paragraph, whose chain took
    /// the table's rows in already, goes where the rows before it leave it. A chain at the top
    /// of a page stays there.
    pub(super) fn chain_moves(&mut self, shape: &Shape, index: usize, around: Around<'_>) -> bool {
        if self.page_has_no_line() || !self.row_kept(shape, index) {
            return false;
        }
        let chained = match index.checked_sub(1) {
            Some(above) => self.row_kept(shape, above),
            None => around.kept_before,
        };
        !chained && self.row_chain_breaks(shape, (index, self.y), around)
    }

    /// Whether a table with its top at `top` does not start on this page as the chain of kept
    /// paragraphs before it needs, with `bottom` the lowest bottom there. When the table's
    /// first row is kept, the chain goes on through the table's kept rows; otherwise the first
    /// row must start on the page, as the row after kept rows does ([`Self::row_starts`]). A
    /// floating table needs its whole first row.
    pub(in crate::layout) fn table_chain_breaks(
        &mut self,
        table: &Table,
        (top, bottom): (Length, Length),
        rest: Rest<'_>,
    ) -> bool {
        if table.rows.is_empty() {
            return false;
        }
        let style = self.styles.table(table.props.style.as_deref());
        let column = self.column.clone();
        let least = self.least_widths_for(table, &style);
        let shape = Shape::new(table, style, &column, &self.shape_context(), &least);
        if table.props.placement.is_none() {
            if self.row_kept(&shape, 0) {
                let around = Around {
                    column: &column,
                    kept_before: true,
                    rest,
                };
                return self.row_chain_breaks(&shape, (0, top), around);
            }
            return !self.row_starts(&shape, (0, top), &mut Merges::default());
        }
        let mark = self.mark();
        let height = self.row_at(&shape, 0, top, &mut Merges::default(), false);
        self.rewind(&mark);
        top + height + shape.foot_border(0) > bottom
    }

    /// Whether the chain of kept rows from row `index`, with its top at `top`, does not fit on
    /// this page together with the start of the row after it, or, after the table's last row,
    /// with the blocks after the table as a chain of kept paragraphs needs them. Each kept row
    /// must fit whole, as a kept paragraph does. Nothing stays laid out.
    fn row_chain_breaks(
        &mut self,
        shape: &Shape,
        (index, top): (usize, Length),
        around: Around<'_>,
    ) -> bool {
        let mark = self.mark();
        let mut merges = Merges::default();
        let mut y = top;
        let mut at = index;
        let breaks = loop {
            if at >= shape.grid.table.rows.len() {
                let end = y + shape.bottom_border(at.saturating_sub(1));
                break self.after_breaks(end, around);
            }
            if !self.row_kept(shape, at) {
                break !self.row_starts(shape, (at, y), &mut merges);
            }
            y += self.row_at(shape, at, y, &mut merges, false);
            if y + shape.foot_border(at) > self.body_end() {
                break true;
            }
            at += 1;
        };
        self.rewind(&mark);
        breaks
    }

    /// Whether row `index`, the row after a chain of kept rows, with its top at `top`, does
    /// start on this page: it fits whole, or it splits there and keeps a line of each of its
    /// cells that go on on the next page. A row that cannot split, or whose minimum height
    /// does not fit, must fit whole.
    fn row_starts(
        &mut self,
        shape: &Shape,
        (index, top): (usize, Length),
        merges: &mut Merges,
    ) -> bool {
        let mark = self.mark();
        let height = self.row_at(shape, index, top, merges, false);
        let fits = top + height + shape.foot_border(index) <= self.body_end();
        self.rewind(&mark);
        let splits = shape
            .grid
            .table
            .rows
            .get(index)
            .is_some_and(|row| !row.cant_split)
            && self.minimum_fits(shape, index, top);
        fits || (splits && self.splits_with_lines(shape, (index, top), merges))
    }

    /// Whether the blocks after a table whose last row is kept do not fit below `end`, the
    /// bottom of the table, as a chain of kept paragraphs needs them: the first lines of the
    /// paragraph after the table, or the start of a table right after it. A paragraph with
    /// `w:pageBreakBefore` after the table ends the chain and needs no room.
    fn after_breaks(&mut self, end: Length, around: Around<'_>) -> bool {
        let (paragraphs, after) = around.rest.paragraphs();
        self.column = around.column.clone();
        self.move_to(end);
        self.previous = None;
        let resolved: Vec<_> = paragraphs.iter().map(|p| self.resolve(p)).collect();
        if resolved.first().is_some_and(starts_page) {
            return false;
        }
        self.chain_breaks(&paragraphs, &resolved, 0, after)
    }
}