pub type Order = i64;
pub fn order_unit(order: Order) -> &'static str {
match order {
1 => "fil",
2 => "fill",
3 => "filll",
_ => "pt",
}
}
fn component_unit(unit: &str, order: Order) -> &str {
match order {
0 => unit,
other => order_unit(other),
}
}
pub fn order_of(unit: &str) -> Option<Order> {
match unit {
"fil" => Some(1),
"fill" => Some(2),
"filll" => Some(3),
_ => None,
}
}
pub fn print_glue(
natural: i64,
stretch: i64,
stretch_order: Order,
shrink: i64,
shrink_order: Order,
) -> String {
print_glue_in("pt", natural, stretch, stretch_order, shrink, shrink_order)
}
pub fn print_glue_in(
unit: &str,
natural: i64,
stretch: i64,
stretch_order: Order,
shrink: i64,
shrink_order: Order,
) -> String {
let mut out = format!("{}{unit}", crate::dimen::print_scaled(natural));
if stretch != 0 {
out.push_str(&format!(
" plus {}{}",
crate::dimen::print_scaled(stretch),
component_unit(unit, stretch_order)
));
}
if shrink != 0 {
out.push_str(&format!(
" minus {}{}",
crate::dimen::print_scaled(shrink),
component_unit(unit, shrink_order)
));
}
out
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct Glue {
pub natural: i64,
pub stretch: i64,
pub stretch_order: Order,
pub shrink: i64,
pub shrink_order: Order,
}
fn combine(a: (i64, Order), b: (i64, Order)) -> (i64, Order) {
match a.1.cmp(&b.1) {
std::cmp::Ordering::Greater => a,
std::cmp::Ordering::Less => b,
std::cmp::Ordering::Equal => (a.0 + b.0, a.1),
}
}
impl Glue {
pub fn fixed(natural: i64) -> Glue {
Glue {
natural,
..Glue::default()
}
}
pub fn scale(self, by: i64) -> Glue {
Glue {
natural: self.natural * by,
stretch: self.stretch * by,
shrink: self.shrink * by,
..self
}
}
pub fn divide(self, by: i64, round: impl Fn(i64, i64) -> i64) -> Glue {
Glue {
natural: round(self.natural, by),
stretch: round(self.stretch, by),
shrink: round(self.shrink, by),
..self
}
}
}
impl std::ops::Add for Glue {
type Output = Glue;
fn add(self, other: Glue) -> Glue {
let (stretch, stretch_order) = combine(
(self.stretch, self.stretch_order),
(other.stretch, other.stretch_order),
);
let (shrink, shrink_order) = combine(
(self.shrink, self.shrink_order),
(other.shrink, other.shrink_order),
);
Glue {
natural: self.natural + other.natural,
stretch,
stretch_order,
shrink,
shrink_order,
}
}
}
impl std::ops::Sub for Glue {
type Output = Glue;
fn sub(self, other: Glue) -> Glue {
self + Glue {
natural: -other.natural,
stretch: -other.stretch,
shrink: -other.shrink,
..other
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dimen::UNITY;
#[test]
fn an_infinite_component_beats_a_finite_one() {
let pt = |n: i64| n * UNITY;
let a = Glue {
natural: pt(1),
stretch: pt(2),
stretch_order: 0,
..Glue::default()
};
let b = Glue {
natural: pt(3),
stretch: pt(4),
stretch_order: 1,
..Glue::default()
};
let sum = a + b;
assert_eq!(
print_glue(sum.natural, sum.stretch, sum.stretch_order, 0, 0),
"4.0pt plus 4.0fil"
);
let a = Glue {
natural: pt(1),
stretch: pt(2),
stretch_order: 2,
..Glue::default()
};
let sum = a + b;
assert_eq!(
print_glue(sum.natural, sum.stretch, sum.stretch_order, 0, 0),
"4.0pt plus 2.0fill"
);
let a = Glue {
natural: pt(1),
stretch: pt(2),
stretch_order: 1,
..Glue::default()
};
let sum = a + b;
assert_eq!(
print_glue(sum.natural, sum.stretch, sum.stretch_order, 0, 0),
"4.0pt plus 6.0fil"
);
}
#[test]
fn a_glue_is_written_the_way_tex_writes_one() {
assert_eq!(print_glue(UNITY, 0, 0, 0, 0), "1.0pt");
assert_eq!(print_glue(UNITY, 2 * UNITY, 0, 0, 0), "1.0pt plus 2.0pt");
assert_eq!(
print_glue(UNITY, 2 * UNITY, 0, 3 * UNITY, 0),
"1.0pt plus 2.0pt minus 3.0pt"
);
assert_eq!(print_glue(0, UNITY, 1, 0, 0), "0.0pt plus 1.0fil");
assert_eq!(
print_glue(0, UNITY, 2, 2 * UNITY, 3),
"0.0pt plus 1.0fill minus 2.0filll"
);
}
}