f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
//! The device's decisions on where the last glyph of a justified line, a line number and the
//! right edge of a right-to-left line land on the device's dots.

use f_office_units::Length;

use super::{ColumnEdges, Device};
use crate::quirks::{AlignedEnds, DeviceAdvance, RightToLeftLineEdge, SpacedLastGlyph};

impl Device {
    /// The last visible glyph of a filled justified line, `last`, when the device can move it
    /// from its steps' place: under `AlignedEnds::OnDots` or
    /// `SpacedLastGlyph::EdgeLimit`. `None` when neither rule moves it.
    #[must_use]
    pub fn last_glyph_to_move<T>(self, last: Option<T>) -> Option<T> {
        // quirk: AlignedEnds::OnDots (src/quirks.rs)
        // quirk: SpacedLastGlyph::EdgeLimit (src/quirks.rs)
        let moves = self.quirks.aligned_ends == AlignedEnds::OnDots
            || self.quirks.spaced_last_glyph == SpacedLastGlyph::EdgeLimit;
        last.filter(|_| moves)
    }

    /// How far the device draws the last visible glyph of a filled justified line that a glyph
    /// comes right before, under `AlignedEnds::OnDots`, by the place `at` that
    /// the steps give the space after it, whose exact place is `x` and whose line's right edge
    /// `end` lies past the dot `edge` that the line counts its start from: from that right edge
    /// ([`Self::restarted_end_move`]) when a segment of the steps starts at the glyph
    /// (`started`), else from `x` ([`Self::justified_end_move`]). Zero under the other variant.
    #[must_use]
    pub fn trailing_space_move(
        self,
        at: impl FnOnce() -> Length,
        (x, end, edge): (Length, Length, Length),
        started: bool,
    ) -> Length {
        // quirk: AlignedEnds::OnDots (src/quirks.rs)
        if self.quirks.aligned_ends != AlignedEnds::OnDots {
            return Length::ZERO;
        }
        let at = at();
        if started {
            self.restarted_end_move(at, end, edge)
        } else {
            self.justified_end_move(at, x)
        }
    }

    /// Where a line number starts, `width` wide at the distance `distance` left of a column whose
    /// left edge lies on the dot `left`, and whether its glyphs step alone: under
    /// `DeviceAdvance::ExactAdvances`, at its exact width left of that distance; under
    /// `ExtrasOnTheirOwn`, at the width that its glyphs step on their own left of the column's dot
    /// less the distance rounded to a dot, from `steps`, which gives that dot and that width.
    #[must_use]
    pub fn line_number_start(
        self,
        left: Length,
        (distance, width): (Length, Length),
        steps: impl FnOnce() -> (Length, Length),
    ) -> (Length, bool) {
        // quirk: DeviceAdvance (src/quirks.rs), whose variant the device carries.
        match self.quirks.device_advance {
            DeviceAdvance::ExactAdvances => (left - distance - width, false),
            DeviceAdvance::ExtrasOnTheirOwn => {
                let (column, alone) = steps();
                (column - self.snap(distance) - alone, true)
            }
        }
    }

    /// The dot of the right edge of the space of a start-aligned right-to-left line whose start
    /// at its indent lies on the dot `start`, in a space `width` wide, in a column whose exact
    /// left edge is `column_left`: under `RightToLeftLineEdge::FromColumnRight`, the dot of the
    /// `column`'s right edge less the dot of the space's distance from it; else, and without a
    /// column, the edge of a right-aligned line ([`Self::aligned_edge`]).
    #[must_use]
    pub fn right_to_left_edge(
        self,
        (start, width): (Length, Length),
        column: Option<ColumnEdges>,
        column_left: Length,
    ) -> Length {
        // quirk: RightToLeftLineEdge::FromColumnRight (src/quirks.rs)
        match (self.quirks.right_to_left_line_edge, column) {
            (RightToLeftLineEdge::FromColumnRight, Some(column)) => {
                let right = column_left + column.width;
                self.snap(self.line_start(right)) - self.snap(self.line_start(column.inset))
            }
            _ => self.aligned_edge((start, width), column),
        }
    }
}