f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! The one reader of the lengths of WordprocessingML:
//! twips, half points, and the eighths of a point and the points of a border side. Each type
//! takes a whole number and a number with a fraction, twips and half points also a universal
//! measure such as `21cm` or `12pt` (ECMA-376 Part 1, 22.9.2.15), and each says what a value
//! that does not read means. A [`PartReader`] reads them, with a warning for a value that does
//! not read.

use f_office_units::Length;
use f_office_units::measure::{decimal_thousandths, universal_measure};
use f_office_units::scale::{HALF_POINTS_PER_POINT, THOUSANDTHS, TWIPS_PER_INCH, TWIPS_PER_POINT};
use f_office_xml::NodeId;

use crate::params::{MAX_FONT_SIZE, MAX_LINE_MULTIPLE};
use crate::reader::PartReader;
use crate::warnings::Limit;

impl PartReader<'_> {
    /// The length in twips of the attribute `name` of `node`, as [`twips`] reads it. A value
    /// that does not read counts as missing.
    pub(crate) fn twips(&mut self, node: NodeId, name: &str) -> Option<Length> {
        self.or_missing(node, name, twips)
    }

    /// The line spacing of the attribute `name` of `node`, as [`line()`] reads it. A value that
    /// does not read counts as missing.
    pub(crate) fn line(&mut self, node: NodeId, name: &str) -> Option<i64> {
        self.or_missing(node, name, line)
    }

    /// The half points of the attribute `name` of `node`, as [`half_points`] reads them, such
    /// as a run's size, kerning or raise. A value that does not read is 0, as Word for Windows
    /// reads it.
    pub(crate) fn half_points(&mut self, node: NodeId, name: &str) -> Option<i64> {
        self.or_zero(node, name, (half_points, 0))
    }

    /// A run's font size in half points from the attribute `name` of `node`, as
    /// [`Self::half_points`] reads it, at most [`MAX_FONT_SIZE`]: a larger size reads as the
    /// limit, with the warning `limit-reached`.
    pub(crate) fn font_size(&mut self, node: NodeId, name: &str) -> Option<i64> {
        let size = self.half_points(node, name)?;
        Some(self.bounded_font_size(size))
    }

    /// A run's font size of `half_points`, at most [`MAX_FONT_SIZE`]: a larger size reads as
    /// the limit, with the warning `limit-reached`. A size of 0 or below stays, and the run's
    /// look takes Word's default size for it. The rule of every reader of a run's size.
    pub(crate) fn bounded_font_size(&mut self, half_points: i64) -> i64 {
        if half_points > MAX_FONT_SIZE {
            self.limit_reached(Limit::FontSize);
        }
        half_points.min(MAX_FONT_SIZE)
    }

    /// The multiple `n` of automatic line spacing, in 240ths of a line, between the negative of
    /// [`MAX_LINE_MULTIPLE`] and it: a multiple beyond them reads as the nearer one, with the
    /// warning `limit-reached`. The rule of every reader of a line multiple.
    pub(crate) fn bounded_line_multiple(&mut self, n: i64) -> i64 {
        if !(-MAX_LINE_MULTIPLE..=MAX_LINE_MULTIPLE).contains(&n) {
            self.limit_reached(Limit::LineMultiple);
        }
        n.clamp(-MAX_LINE_MULTIPLE, MAX_LINE_MULTIPLE)
    }

    /// The whole number of the attribute `name` of `node`, as [`whole`] reads it, such as a
    /// border side's width in eighths of a point or its space in points. A value that does not
    /// read counts as missing: Word refuses a document with one, so no page of Word decides it.
    pub(crate) fn whole_number(&mut self, node: NodeId, name: &str) -> Option<i64> {
        self.or_missing(node, name, whole)
    }
}

/// Reads a length in twips: a whole number, a number with a fraction with the fraction dropped
/// toward zero, as Word reads it, or a universal measure in whole twips, rounded.
pub(crate) fn twips(value: &str) -> Option<Length> {
    let value = value.trim();
    if let Ok(twips) = value.parse::<i64>() {
        return Length::from_twips(twips);
    }
    let twips = universal_measure(value, TWIPS_PER_INCH)
        .or_else(|| decimal_thousandths(value).map(|milli| milli / THOUSANDTHS))?;
    Length::from_twips(twips)
}

/// Reads a line spacing (`w:line`, ECMA-376 Part 1, 17.3.1.33), in 240ths of a line for the
/// rule `auto` and in twips otherwise: a whole number, a number with its fraction dropped toward
/// zero, as Word reads it (such as `275.9999942779541` -> `275` and `360.9` -> `360`), or a
/// universal measure such as the `12.95pt` of a Strict document, in whole twips.
pub(crate) fn line(value: &str) -> Option<i64> {
    let trimmed = value.trim();
    if let Ok(whole) = trimmed.parse::<i64>() {
        return Some(whole);
    }
    decimal_thousandths(trimmed)
        .map(|milli| milli / THOUSANDTHS)
        .or_else(|| Some(twips(trimmed)?.units() / Length::UNITS_PER_TWIP))
}

/// Reads a value in half points (`w:sz`, `w:szCs`, `w:kern`, `w:position`): a whole number, a
/// number with its fraction dropped towards zero, or a universal measure in whole half points
/// with the fraction of a half point dropped.
pub(crate) fn half_points(value: &str) -> Option<i64> {
    let value = value.trim();
    if let Ok(half_points) = value.parse::<i64>() {
        return Some(half_points);
    }
    if value.ends_with(|c: char| c.is_ascii_alphabetic()) {
        let twips = twips(value)?.units() / Length::UNITS_PER_TWIP;
        return Some(twips * HALF_POINTS_PER_POINT / TWIPS_PER_POINT);
    }
    decimal_thousandths(value).map(|milli| milli / THOUSANDTHS)
}

/// Reads a whole number, or a number with its fraction dropped towards zero, such as a border
/// side's `w:sz` in eighths of a point and its `w:space` in points.
pub(crate) fn whole(value: &str) -> Option<i64> {
    let value = value.trim();
    if let Ok(whole) = value.parse::<i64>() {
        return Some(whole);
    }
    decimal_thousandths(value).map(|milli| milli / THOUSANDTHS)
}

#[cfg(test)]
mod tests;