f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! The segment rule of `GlyphSegments::FromLineStart`: the segments count from the line's
//! first glyph, or from the first glyph after a tab or a list label's gap.

use f_office_units::Length;

use super::steps::{Restart, Segment, SegmentRule};

/// Segments in a whole number from the exact place of the line's first glyph, or of the first
/// glyph after a tab or a list label's gap (`GlyphSegments::FromLineStart`, `EdgeSteps::tab`).
///
/// The glyph whose exact right edge first ends past the end of a segment starts the steps
/// again, and the next segment ends one segment later. Its exact place is the one that the edge
/// check counted in the segment before, at the device's rounded dots per point
/// ([`Device::exact_scale`](super::Device::exact_scale)), scaled from the line's first glyph
/// without a break ([`Segment::counted_place`]), and it hands nothing on. Another gap with a
/// width, such as an inline object, keeps the segment; whether Word starts the steps again there
/// is not known.
pub(super) struct FromLineStart;

impl SegmentRule for FromLineStart {
    fn next(
        &self,
        device: &crate::grid::Device,
        segment: Segment,
        (x, advance): (Length, Length),
        length: Length,
        _: Length,
    ) -> Option<Restart> {
        if x + advance - segment.first <= segment.end {
            return None;
        }
        Some(Restart {
            place: segment.counted_place(device, x),
            segment: Segment {
                end: segment.end + length,
                ..segment
            },
            carried: None,
            rounded_up: false,
        })
    }

    fn after_gap(&self, segment: Option<Segment>) -> Option<Segment> {
        segment
    }
}

#[cfg(test)]
mod tests {
    use f_office_units::Length;

    use crate::grid::steps::tests::{SEGMENT, quarter_numbers, quarter_places};
    use crate::grid::{Device, EdgeSteps};

    #[test]
    fn starts_the_steps_again_after_each_segment() {
        // Calibri's e at 11 pt, 1,019 of 2,048 units, 5.4731 pt. The
        // 47th e ends past 256 pt from the first, so it goes to the nearest quarter point of
        // its exact place, 251.76 pt from the first; the steps alone take it to 252 pt. The 46th
        // e, at 246.29 pt, keeps the steps: 246.5 pt.
        let advance = 139_019;
        let places = quarter_places(advance, 47, SEGMENT);
        assert_eq!(places.get(45), Some(&24_650));
        assert_eq!(places.get(46), Some(&25_175));
        let unsegmented = quarter_places(advance, 47, i64::MAX / 2);
        assert_eq!(unsegmented.get(46), Some(&25_200));
    }

    #[test]
    fn starts_a_segment_after_a_tab_as_a_line_starts() {
        // The line of the test above after five glyphs and a tab, from 470 dots (117.5 pt): the
        // glyph after the tab starts the count of the segments, so the 47 glyphs take the places
        // of that line, and the 47th starts the steps again. After another gap, such as an
        // inline object, the segment still counts from the line's first glyph, as after a plain
        // restart, and an earlier glyph starts the steps again.
        let device = Device::per_inch(288).unwrap();
        let (advance, start) = (
            Length::from_units(139_019),
            Length::from_points(72).unwrap(),
        );
        let text = device.dots(1) * 470;
        let numbers = quarter_numbers(SEGMENT);
        let places = |after: fn(&mut EdgeSteps, &Device)| -> Vec<i64> {
            let mut edge = EdgeSteps::default();
            for k in 0..5 {
                edge.place(&device, numbers, start + advance * k, advance);
            }
            after(&mut edge, &device);
            let places: Vec<i64> = (0..47)
                .map(|k| {
                    edge.place(&device, numbers, text + advance * k, advance)
                        .units()
                })
                .collect();
            places
                .iter()
                .map(|p| (p - places[0]) * 100 / Length::UNITS_PER_POINT)
                .collect()
        };
        let after_tab = places(|edge, _| edge.tab());
        assert_eq!(after_tab, quarter_places(advance.units(), 47, SEGMENT));
        let after_object = places(EdgeSteps::gap);
        assert_eq!(after_object, places(|edge, _| edge.restart()));
        assert_ne!(after_object, after_tab);
    }
}