use super::dimensions::Dimension;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum InfiniteUnit {
Fil = 1,
Fill = 2,
Filll = 3,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stretch {
None,
Finite(Dimension),
Infinite { amount: i32, unit: InfiniteUnit },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Glue {
pub width: Dimension,
pub stretch: Stretch,
pub shrink: Dimension,
}
impl Glue {
pub fn natural(width: Dimension) -> Self {
Self {
width,
stretch: Stretch::None,
shrink: Dimension::from_sp(0),
}
}
pub fn zero() -> Self {
Self::natural(Dimension::from_sp(0))
}
pub fn hfill() -> Self {
Self {
width: Dimension::from_sp(0),
stretch: Stretch::Infinite {
amount: 1,
unit: InfiniteUnit::Fil,
},
shrink: Dimension::from_sp(0),
}
}
pub fn hfil() -> Self {
Self::hfill()
}
pub fn parse(s: &str) -> Option<Self> {
let s = s.trim();
if s.is_empty() {
return None;
}
let (width_part, rest) = split_keyword(s, " plus ")?;
let width = Dimension::parse(width_part)?;
let mut stretch = Stretch::None;
let mut shrink = Dimension::from_sp(0);
if let Some(r) = rest {
let (stretch_part, after_stretch) = split_keyword(r, " minus ")?;
stretch = parse_stretch(stretch_part)?;
if let Some(shrink_part) = after_stretch {
shrink = Dimension::parse(shrink_part)?;
}
}
Some(Self {
width,
stretch,
shrink,
})
}
pub fn resolve_finite(&self, ratio: f64) -> Dimension {
let extra = match self.stretch {
Stretch::None => 0,
Stretch::Finite(d) => (d.sp() as f64 * ratio.clamp(0.0, 1.0)).round() as i64,
Stretch::Infinite { .. } => 0,
};
let shrink_amt = (self.shrink.sp() as f64 * (1.0 - ratio).clamp(0.0, 1.0)).round() as i64;
Dimension::from_sp(self.width.sp() + extra - shrink_amt)
}
pub fn is_infinite(&self) -> bool {
matches!(self.stretch, Stretch::Infinite { .. })
}
}
fn split_keyword<'a>(s: &'a str, keyword: &str) -> Option<(&'a str, Option<&'a str>)> {
match s.find(keyword) {
Some(idx) => {
let before = s[..idx].trim();
let after = s[idx + keyword.len()..].trim();
if after.is_empty() {
Some((before, None))
} else {
Some((before, Some(after)))
}
}
None => Some((s.trim(), None)),
}
}
fn parse_stretch(s: &str) -> Option<Stretch> {
let s = s.trim();
if s.is_empty() {
return Some(Stretch::None);
}
if let Some((amount, unit)) = parse_infinite_unit(s) {
return Some(Stretch::Infinite { amount, unit });
}
Dimension::parse(s).map(Stretch::Finite)
}
fn parse_infinite_unit(s: &str) -> Option<(i32, InfiniteUnit)> {
let s = s.trim();
for (suffix, unit) in [
("filll", InfiniteUnit::Filll),
("fill", InfiniteUnit::Fill),
("fil", InfiniteUnit::Fil),
] {
if let Some(num) = s.strip_suffix(suffix) {
let amount: i32 = if num.is_empty() {
1
} else {
num.parse().ok()?
};
return Some((amount, unit));
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tex::dimensions::PT_TO_SP;
#[test]
fn parse_rigid_glue() {
let g = Glue::parse("10pt").unwrap();
assert_eq!(g.width.pt(), 10.0);
assert_eq!(g.stretch, Stretch::None);
assert_eq!(g.shrink.sp(), 0);
}
#[test]
fn parse_plus_minus() {
let g = Glue::parse("10pt plus 2pt minus 1pt").unwrap();
assert_eq!(g.width.pt(), 10.0);
assert!(matches!(g.stretch, Stretch::Finite(d) if d.pt() == 2.0));
assert_eq!(g.shrink.pt(), 1.0);
}
#[test]
fn parse_infinite_fil() {
let g = Glue::parse("0pt plus 1fil").unwrap();
assert!(g.is_infinite());
assert!(matches!(
g.stretch,
Stretch::Infinite {
amount: 1,
unit: InfiniteUnit::Fil
}
));
}
#[test]
fn parse_infinite_fill() {
let g = Glue::parse("5pt plus 2fill minus 1pt").unwrap();
assert!(matches!(
g.stretch,
Stretch::Infinite {
amount: 2,
unit: InfiniteUnit::Fill
}
));
}
#[test]
fn hfill_preset() {
let g = Glue::hfill();
assert!(g.is_infinite());
assert_eq!(g.width.sp(), 0);
}
#[test]
fn resolve_finite_stretch() {
let g = Glue::parse("10pt plus 10pt minus 0pt").unwrap();
let at_half = g.resolve_finite(0.5);
assert_eq!(at_half.pt(), 15.0);
let at_full = g.resolve_finite(1.0);
assert_eq!(at_full.pt(), 20.0);
}
#[test]
fn infinite_unit_ranking() {
assert!(InfiniteUnit::Fil < InfiniteUnit::Fill);
assert!(InfiniteUnit::Fill < InfiniteUnit::Filll);
}
#[test]
fn pt_to_sp_constant() {
assert_eq!(PT_TO_SP, 65536);
}
}