#![deny(clippy::float_arithmetic)]
use std::iter::Sum;
use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
pub mod binary32;
mod edges;
pub mod fixed;
pub mod measure;
pub mod scale;
pub use edges::Edges;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Length(i64);
impl Length {
pub const ZERO: Self = Self(0);
pub const UNITS_PER_POINT: i64 = 25_400;
pub const UNITS_PER_TWIP: i64 = 1_270;
pub const UNITS_PER_HALF_POINT: i64 = 12_700;
pub const UNITS_PER_EIGHTH_POINT: i64 = 3_175;
pub const UNITS_PER_EMU: i64 = 2;
pub const UNITS_PER_DOT: i64 = 6_096;
pub const UNITS_PER_INCH: i64 = Self::UNITS_PER_POINT * scale::POINTS_PER_INCH;
pub const FILE_LIMIT: i64 = 1 << 46;
#[must_use]
pub const fn from_units(units: i64) -> Self {
Self(units)
}
#[must_use]
pub const fn units(self) -> i64 {
self.0
}
#[must_use]
pub const fn from_points(points: i64) -> Option<Self> {
Self::scaled(points, Self::UNITS_PER_POINT)
}
#[must_use]
pub const fn from_twips(twips: i64) -> Option<Self> {
Self::scaled(twips, Self::UNITS_PER_TWIP)
}
#[must_use]
pub const fn from_half_points(half_points: i64) -> Option<Self> {
Self::scaled(half_points, Self::UNITS_PER_HALF_POINT)
}
#[must_use]
pub const fn from_eighth_points(eighth_points: i64) -> Option<Self> {
Self::scaled(eighth_points, Self::UNITS_PER_EIGHTH_POINT)
}
#[must_use]
pub const fn from_emus(emus: i64) -> Option<Self> {
Self::scaled(emus, Self::UNITS_PER_EMU)
}
#[must_use]
pub const fn checked_add(self, other: Self) -> Option<Self> {
match self.0.checked_add(other.0) {
Some(units) => Some(Self(units)),
None => None,
}
}
#[must_use]
pub const fn checked_sub(self, other: Self) -> Option<Self> {
match self.0.checked_sub(other.0) {
Some(units) => Some(Self(units)),
None => None,
}
}
#[must_use]
pub const fn saturating_sub(self, other: Self) -> Self {
Self(self.0.saturating_sub(other.0))
}
#[must_use]
pub const fn checked_mul(self, factor: i64) -> Option<Self> {
match self.0.checked_mul(factor) {
Some(units) => Some(Self(units)),
None => None,
}
}
#[must_use]
pub const fn div_toward_zero(self, divisor: i64) -> Self {
Self(self.0 / divisor)
}
#[must_use]
pub const fn up_to_whole(self, units_per_step: i64) -> Self {
Self((self.0 + units_per_step - 1).div_euclid(units_per_step) * units_per_step)
}
#[must_use]
pub const fn down_to_whole(self, units_per_step: i64) -> Self {
Self(self.0.div_euclid(units_per_step) * units_per_step)
}
#[must_use]
pub const fn toward_zero_whole(self, units_per_step: i64) -> Self {
Self(self.0 / units_per_step * units_per_step)
}
#[must_use]
pub const fn nearest_whole(self, units_per_step: i64) -> Self {
Self(div_round(self.0, units_per_step) * units_per_step)
}
#[must_use]
pub const fn div_round(self, divisor: i64) -> Self {
Self(div_round(self.0, divisor))
}
#[must_use]
pub fn checked_div_round(self, divisor: i64) -> Option<Self> {
checked_div_round(self.0, divisor).map(Self)
}
const fn scaled(value: i64, units_per_value: i64) -> Option<Self> {
match value.checked_mul(units_per_value) {
Some(units) if -Self::FILE_LIMIT <= units && units <= Self::FILE_LIMIT => {
Some(Self(units))
}
_ => None,
}
}
}
impl Add for Length {
type Output = Self;
fn add(self, other: Self) -> Self {
Self(self.0 + other.0)
}
}
impl Sub for Length {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self(self.0 - other.0)
}
}
impl AddAssign for Length {
fn add_assign(&mut self, other: Self) {
self.0 += other.0;
}
}
impl SubAssign for Length {
fn sub_assign(&mut self, other: Self) {
self.0 -= other.0;
}
}
impl Neg for Length {
type Output = Self;
fn neg(self) -> Self {
Self(-self.0)
}
}
impl Mul<i64> for Length {
type Output = Self;
fn mul(self, factor: i64) -> Self {
Self(self.0 * factor)
}
}
impl Sum for Length {
fn sum<I: Iterator<Item = Self>>(lengths: I) -> Self {
lengths.fold(Self::ZERO, Add::add)
}
}
#[must_use]
#[inline]
pub const fn div_round(numerator: i64, denominator: i64) -> i64 {
if denominator == 0 {
return 0;
}
(numerator * 2 + denominator).div_euclid(denominator * 2)
}
#[must_use]
#[inline]
pub const fn div_round_i128(numerator: i128, denominator: i128) -> i128 {
if denominator == 0 {
return 0;
}
(numerator * 2 + denominator).div_euclid(denominator * 2)
}
#[must_use]
pub fn saturated(value: i128) -> i64 {
i64::try_from(value).unwrap_or(if value < 0 { i64::MIN } else { i64::MAX })
}
#[must_use]
pub fn lowest_terms(numerator: i128, denominator: i128) -> (i128, i128) {
let divisor = gcd(numerator.unsigned_abs(), denominator.unsigned_abs());
match i128::try_from(divisor) {
Ok(divisor) if divisor > 0 => (numerator / divisor, denominator / divisor),
_ => (numerator, denominator),
}
}
const fn gcd(mut a: u128, mut b: u128) -> u128 {
while b != 0 {
(a, b) = (b, a % b);
}
a
}
#[must_use]
#[inline]
pub fn checked_div_round(numerator: i64, denominator: i64) -> Option<i64> {
numerator
.checked_mul(2)?
.checked_add(denominator)?
.checked_div_euclid(denominator.checked_mul(2)?)
}
#[must_use]
#[inline]
pub fn count_factor(count: usize) -> i64 {
i64::try_from(count).unwrap_or(i64::MAX)
}
#[cfg(test)]
mod tests {
use super::{
Length, checked_div_round, count_factor, div_round, div_round_i128, gcd, lowest_terms,
};
#[test]
fn gives_a_count_as_an_i64() {
assert_eq!(count_factor(0), 0);
assert_eq!(count_factor(12), 12);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn saturates_a_count_beyond_an_i64() {
let largest = usize::try_from(i64::MAX).unwrap();
assert_eq!(count_factor(largest), i64::MAX);
assert_eq!(count_factor(largest + 1), i64::MAX);
assert_eq!(count_factor(usize::MAX), i64::MAX);
}
#[test]
fn divides_toward_zero() {
let length = |units| Length::from_units(units);
assert_eq!(length(5).div_toward_zero(2), length(2));
assert_eq!(length(-5).div_toward_zero(2), length(-2));
assert_eq!(length(6).div_toward_zero(3), length(2));
}
#[test]
fn rounds_up_to_whole_steps() {
let length = |units| Length::from_units(units);
let twip = Length::UNITS_PER_TWIP;
assert_eq!(length(0).up_to_whole(twip), length(0));
assert_eq!(length(1).up_to_whole(twip), length(twip));
assert_eq!(length(twip).up_to_whole(twip), length(twip));
assert_eq!(length(twip + 1).up_to_whole(twip), length(2 * twip));
assert_eq!(length(-1).up_to_whole(twip), length(0));
assert_eq!(length(-twip - 1).up_to_whole(twip), length(-twip));
}
#[test]
fn rounds_down_to_whole_steps() {
let length = |units| Length::from_units(units);
let twip = Length::UNITS_PER_TWIP;
assert_eq!(length(0).down_to_whole(twip), length(0));
assert_eq!(length(twip - 1).down_to_whole(twip), length(0));
assert_eq!(length(twip).down_to_whole(twip), length(twip));
assert_eq!(length(5 * twip / 2).down_to_whole(twip), length(2 * twip));
assert_eq!(length(-1).down_to_whole(twip), length(-twip));
assert_eq!(length(-twip).down_to_whole(twip), length(-twip));
}
#[test]
fn rounds_toward_zero_and_to_the_nearest_whole_step() {
let twip = Length::UNITS_PER_TWIP;
for units in -3 * twip..=3 * twip {
let length = Length::from_units(units);
assert_eq!(
length.toward_zero_whole(twip),
length.div_toward_zero(twip) * twip,
"{units}"
);
assert_eq!(
length.nearest_whole(twip),
length.div_round(twip) * twip,
"{units}"
);
}
}
#[test]
fn rounds_a_part_step_toward_zero_and_a_half_step_up() {
let length = |units| Length::from_units(units);
let twip = Length::UNITS_PER_TWIP;
assert_eq!(length(twip - 1).toward_zero_whole(twip), length(0));
assert_eq!(length(1 - twip).toward_zero_whole(twip), length(0));
assert_eq!(length(twip / 2).nearest_whole(twip), length(twip));
assert_eq!(length(-twip / 2).nearest_whole(twip), length(0));
assert_eq!(length(twip / 2 - 1).nearest_whole(twip), length(0));
}
#[test]
fn rounds_up_as_the_tables_and_the_balanced_columns_did() {
let twip = Length::UNITS_PER_TWIP;
let point = Length::UNITS_PER_POINT;
let tables = |units: i64| (units + twip - 1).div_euclid(twip) * twip;
let columns = |units: i64| (units + point - 1) / point * point;
for units in (-3 * point..=3 * point).chain([1_000_003 * point - 1, 1_000_003 * point]) {
let length = Length::from_units(units);
assert_eq!(length.up_to_whole(twip).units(), tables(units), "{units}");
if units >= 0 {
assert_eq!(length.up_to_whole(point).units(), columns(units), "{units}");
}
}
}
#[test]
fn divides_to_the_nearest_unit_when_it_fits() {
let length = |units| Length::from_units(units);
assert_eq!(length(5).div_round(2), length(3));
assert_eq!(length(-5).div_round(2), length(-2));
assert_eq!(length(5).div_round(0), Length::ZERO);
assert_eq!(length(5).checked_div_round(2), Some(length(3)));
assert_eq!(length(-5).checked_div_round(2), Some(length(-2)));
assert_eq!(length(5).checked_div_round(0), None);
assert_eq!(length(i64::MAX).checked_div_round(2), None);
}
#[test]
fn subtracts_up_to_the_bounds() {
let length = |units| Length::from_units(units);
assert_eq!(length(5).saturating_sub(length(2)), length(3));
assert_eq!(
length(i64::MAX).saturating_sub(length(-1)),
length(i64::MAX)
);
assert_eq!(length(i64::MIN).saturating_sub(length(1)), length(i64::MIN));
}
#[test]
fn rounds_halves_up() {
assert_eq!(div_round(5, 2), 3);
assert_eq!(div_round(4, 2), 2);
assert_eq!(div_round(7, 3), 2);
assert_eq!(div_round(-5, 2), -2);
assert_eq!(div_round(1, 0), 0);
}
#[test]
fn rounds_halves_up_past_64_bits() {
assert_eq!(div_round_i128(-5, 2), -2);
assert_eq!(
div_round_i128(i128::from(i64::MAX) * 3, 2),
13_835_058_055_282_163_711
);
assert_eq!(div_round_i128(1, 0), 0);
}
#[test]
fn finds_the_greatest_common_divisor() {
assert_eq!(gcd(12, 18), 6);
assert_eq!(gcd(18, 12), 6);
assert_eq!(gcd(17, 5), 1);
assert_eq!((gcd(0, 5), gcd(5, 0), gcd(0, 0)), (5, 5, 0));
assert_eq!(gcd(i128::MIN.unsigned_abs(), 1 << 64), 1 << 64);
assert_eq!(gcd(u128::MAX, u128::MAX), u128::MAX);
}
#[test]
fn puts_a_fraction_in_its_lowest_terms_with_its_signs() {
let fractions = [
((6, 4), (3, 2)),
((-6, 4), (-3, 2)),
((6, -4), (3, -2)),
((0, 5), (0, 1)),
((7, 1), (7, 1)),
((i128::MIN, 2), (i128::MIN / 2, 1)),
((0, 0), (0, 0)),
((i128::MIN, 0), (i128::MIN, 0)),
((i128::MIN, i128::MIN), (i128::MIN, i128::MIN)),
];
for ((numerator, denominator), lowest) in fractions {
assert_eq!(lowest_terms(numerator, denominator), lowest);
}
}
#[test]
fn checked_division_refuses_what_does_not_fit() {
assert_eq!(checked_div_round(5, 2), Some(3));
assert_eq!(checked_div_round(-5, 2), Some(-2));
assert_eq!(checked_div_round(1, 0), None);
assert_eq!(checked_div_round(i64::MAX / 2 - 1, 2), Some(i64::MAX / 4));
assert_eq!(checked_div_round(i64::MAX / 2, 2), None);
assert_eq!(checked_div_round(1, i64::MAX / 2 + 1), None);
}
#[test]
fn units_round_trip() {
assert_eq!(Length::from_units(-7).units(), -7);
assert_eq!(Length::ZERO.units(), 0);
}
#[test]
fn each_file_unit_has_its_exact_size() {
assert_eq!(Length::from_points(1), Some(Length::from_units(25_400)));
assert_eq!(Length::from_twips(1), Some(Length::from_units(1_270)));
assert_eq!(
Length::from_half_points(1),
Some(Length::from_units(12_700))
);
assert_eq!(
Length::from_eighth_points(1),
Some(Length::from_units(3_175))
);
assert_eq!(Length::from_emus(1), Some(Length::from_units(2)));
}
#[test]
fn file_units_agree_on_one_point_and_one_inch() {
let point = Length::from_points(1);
assert_eq!(Length::from_twips(20), point);
assert_eq!(Length::from_half_points(2), point);
assert_eq!(Length::from_eighth_points(8), point);
assert_eq!(Length::from_emus(12_700), point);
assert_eq!(Length::from_emus(914_400), Length::from_points(72));
assert_eq!(Length::from_twips(1_440), Length::from_points(72));
}
#[test]
fn negative_values_scale_exactly() {
assert_eq!(Length::from_twips(-3), Some(Length::from_units(-3_810)));
}
#[test]
fn scaling_that_overflows_returns_none() {
assert_eq!(Length::from_points(i64::MAX), None);
assert_eq!(Length::from_twips(i64::MIN), None);
assert_eq!(Length::from_half_points(i64::MAX / 2), None);
assert_eq!(Length::from_eighth_points(i64::MAX / 3), None);
assert_eq!(Length::from_emus(i64::MAX), None);
}
#[test]
fn file_constructors_refuse_lengths_beyond_the_limit() {
let limit = Length::from_units(Length::FILE_LIMIT);
assert_eq!(Length::from_emus(Length::FILE_LIMIT / 2), Some(limit));
assert_eq!(Length::from_emus(-Length::FILE_LIMIT / 2), Some(-limit));
assert_eq!(Length::from_emus(Length::FILE_LIMIT / 2 + 1), None);
assert_eq!(Length::from_emus(-Length::FILE_LIMIT / 2 - 1), None);
assert!(Length::from_emus(27_273_042_316_900).is_some());
assert_eq!(Length::from_twips(i64::MIN / 1_270), None);
}
#[test]
fn operators_act_as_the_arithmetic_of_half_emus() {
let (a, b) = (Length::from_units(40), Length::from_units(15));
assert_eq!(a + b, Length::from_units(55));
assert_eq!(b - a, Length::from_units(-25));
assert_eq!(-a, Length::from_units(-40));
assert_eq!(a * 3, Length::from_units(120));
let mut c = a;
c += b;
c -= a + a;
assert_eq!(c, Length::from_units(-25));
assert_eq!(
[a, b, b].into_iter().sum::<Length>(),
Length::from_units(70)
);
assert_eq!(
Length::from_points(72),
Some(Length::from_units(Length::UNITS_PER_INCH))
);
}
#[test]
fn checked_mul_refuses_products_that_do_not_fit() {
let a = Length::from_units(40);
assert_eq!(a.checked_mul(-2), Some(Length::from_units(-80)));
assert_eq!(Length::from_units(i64::MAX / 2 + 1).checked_mul(2), None);
let most = Length::from_units(Length::FILE_LIMIT) * 131_071;
assert_eq!(
most.checked_add(Length::from_units(Length::FILE_LIMIT)),
None
);
}
#[test]
fn checked_add_and_sub() {
let a = Length::from_units(40);
let b = Length::from_units(15);
assert_eq!(a.checked_add(b), Some(Length::from_units(55)));
assert_eq!(a.checked_sub(b), Some(Length::from_units(25)));
assert_eq!(b.checked_sub(a), Some(Length::from_units(-25)));
assert_eq!(
Length::from_units(i64::MAX).checked_add(Length::from_units(1)),
None
);
assert_eq!(
Length::from_units(i64::MIN).checked_sub(Length::from_units(1)),
None
);
}
}