f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
//! The height of a line in whole 1/4096 pt and the dot of a place with a height rest.

use f_office_units::Length;

use super::{ExactPlace, REST_PARTS, REST_PER_PART, times_lines, whole_height};
use crate::grid::Device;
use crate::params::{HEIGHT_PARTS_PER_POINT, MAX_FONT_SIZE, MAX_LINE_MULTIPLE};

#[test]
fn gives_a_length_for_the_largest_multiple_of_the_tallest_single_line() {
    // The tallest single line that a face's metrics can give: three lines of 32,768 units on an
    // em of 16 units, 6,144 em, at the largest size that the reader takes.
    let em_points = MAX_FONT_SIZE / 2;
    let single_points = em_points * 6_144;
    let lines = MAX_LINE_MULTIPLE / 240;
    let single = single_points * HEIGHT_PARTS_PER_POINT;
    let exact = i128::from(single) * i128::from(lines) * i128::from(REST_PER_PART);
    let height = i64::try_from(exact / i128::from(REST_PARTS)).unwrap();
    assert_eq!(
        whole_height(single, 0, MAX_LINE_MULTIPLE),
        (Length::from_units(height), 0)
    );
    // Such a line lies beyond the lengths that the reader takes from a file.
    let single = Length::from_units(single_points * Length::UNITS_PER_POINT);
    assert!(single.units() > Length::FILE_LIMIT);
    assert_eq!(
        times_lines(single, MAX_LINE_MULTIPLE),
        Length::from_units(single.units() * lines)
    );
    assert_eq!(
        times_lines(single, -MAX_LINE_MULTIPLE),
        Length::from_units(-single.units() * lines)
    );
}

#[test]
fn gives_the_end_of_the_range_for_a_multiple_of_a_length_far_beyond_a_page() {
    let longest = Length::from_units(i64::MAX);
    assert_eq!(times_lines(longest, MAX_LINE_MULTIPLE), longest);
    assert_eq!(
        times_lines(longest, -MAX_LINE_MULTIPLE),
        Length::from_units(i64::MIN)
    );
}

#[test]
fn holds_a_height_rest_in_parts_that_make_a_4096th_of_a_point_exact() {
    // 1/4096 pt is 25,400 / 4,096 = 3,175 / 512 half-EMUs.
    assert_eq!((REST_PARTS, REST_PER_PART), (512, 3175));
    assert_eq!(
        Length::UNITS_PER_POINT * REST_PARTS,
        crate::params::HEIGHT_PARTS_PER_POINT * REST_PER_PART
    );
}

#[test]
fn rounds_the_height_to_a_whole_4096th_of_a_point_a_half_up_and_keeps_the_rest() {
    // A face whose line is 2,500 of 2,048 units per em at 10.5 pt: 52,500 parts of 1/4096 pt.
    let single = 2500 * 21;
    // Single spacing keeps the exact line: 325,561.52 half-EMUs.
    assert_eq!(
        whole_height(single, 0, 240),
        (Length::from_units(325_562), -244)
    );
    // At 259 240ths, 56,656.25 parts round down to 56,656: 351,333.59 half-EMUs.
    assert_eq!(
        whole_height(single, 0, 259),
        (Length::from_units(351_334), -208)
    );
    // At 262 240ths, 57,312.5 parts round up to 57,313: 355,407.76 half-EMUs.
    assert_eq!(
        whole_height(single, 0, 262),
        (Length::from_units(355_408), -121)
    );
}

#[test]
fn scales_the_single_line_without_the_part_that_comes_once() {
    // The single line of the face above at 10.5 pt, 52,500 parts, of which 4,000 come once: at
    // 276 240ths the other 48,500 give 55,775 parts, and the line is 59,775 parts, 370,675.05
    // half-EMUs.
    let single = 2500 * 21;
    assert_eq!(
        whole_height(single, 4000, 276),
        (Length::from_units(370_675), 25)
    );
    // In single spacing the part that comes once changes nothing.
    assert_eq!(
        whole_height(single, 4000, 240),
        whole_height(single, 0, 240)
    );
}

#[test]
fn gives_a_place_on_half_a_dot_the_dot_on_the_side_of_its_rest() {
    let device = Device::per_inch(600).unwrap();
    let dot = device.dots(1).units();
    let half = Length::from_units(10 * dot + dot / 2);
    let at = |at, rest| ExactPlace { at, rest }.rested_dot(&device);
    assert_eq!(at(half, 1), Some(Length::from_units(11 * dot)));
    assert_eq!(at(half, -1), Some(Length::from_units(10 * dot)));
    // Without a rest, the rules of ties decide.
    assert_eq!(at(half, 0), None);
    // Off half a dot, the rest does not move the place past it.
    let past = half + Length::from_units(1);
    assert_eq!(
        at(past, -REST_PARTS / 2),
        Some(Length::from_units(11 * dot))
    );
}

#[test]
fn carries_a_rest_into_whole_half_emus() {
    let at = Length::from_units(1_000);
    let place = ExactPlace::carried(at, 3 * REST_PARTS / 2 + 1);
    assert_eq!(place.at, Length::from_units(1_002));
    assert_eq!(place.rest, -REST_PARTS / 2 + 1);
    assert_eq!(ExactPlace::carried(at, -REST_PARTS / 2).at, at);
}