1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Line-breaking algorithm.

/// An item.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Item {
    /// The type.
    pub t: Type,
    /// The width.
    pub w: f32,
    /// The stretchability.
    pub y: f32,
    /// The shrinkability.
    pub z: f32,
    /// The penalty.
    pub p: f32,
    /// The flag.
    pub f: bool,
}

/// An item type.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Type {
    /// A box item.
    Box,
    /// A glue item.
    Glue,
    /// A penalty item.
    Penalty,
}

impl Item {
    /// Create an item.
    #[inline]
    pub fn new(t: Type, w: f32, y: f32, z: f32, p: f32, f: bool) -> Item {
        Item { t: t, w: w, y: y, z: z, p: p, f: f }
    }

    /// Create a box item.
    #[inline]
    pub fn new_box(w: f32) -> Item {
        Item::new(Type::Box, w, 0.0, 0.0, 0.0, false)
    }

    /// Create a glue item.
    #[inline]
    pub fn new_glue(w: f32, y: f32, z: f32) -> Item {
        Item::new(Type::Glue, w, y, z, 0.0, false)
    }

    /// Create a penalty item.
    #[inline]
    pub fn new_penalty(w: f32, p: f32, f: bool) -> Item {
        Item::new(Type::Penalty, w, 0.0, 0.0, p, f)
    }

    /// Check if it is a box item.
    #[inline]
    pub fn is_box(&self) -> bool {
        self.t == Type::Box
    }

    /// Check if it is a glue item.
    #[inline]
    pub fn is_glue(&self) -> bool {
        self.t == Type::Glue
    }

    /// Check if it is a penalty item.
    #[inline]
    pub fn is_penalty(&self) -> bool {
        self.t == Type::Penalty
    }
}