Skip to main content

laminapdf_syntax/
rotation.rs

1//! [`Rotation`]: a rotation by a multiple of 90°, clockwise, as `/Rotate` (ISO 32000-2
2//! §7.7.3.3) stores it and a viewer adds it.
3
4use crate::Object;
5
6/// A rotation by a multiple of 90°, clockwise when displayed: a page's `/Rotate`
7/// (§7.7.3.3), or a view rotation added to it.
8///
9/// Added in 0.2.0.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
11pub enum Rotation {
12    /// No rotation.
13    #[default]
14    None,
15    /// 90° clockwise.
16    Clockwise90,
17    /// 180°.
18    Half,
19    /// 270° clockwise (90° counter-clockwise).
20    Clockwise270,
21}
22
23impl Rotation {
24    /// The rotation in degrees: 0, 90, 180 or 270.
25    ///
26    /// Added in 0.2.0.
27    #[must_use]
28    pub const fn degrees(self) -> u16 {
29        match self {
30            Self::None => 0,
31            Self::Clockwise90 => 90,
32            Self::Half => 180,
33            Self::Clockwise270 => 270,
34        }
35    }
36
37    /// The rotation for `degrees` (any multiple of 90, negative included), else `Option::None`.
38    ///
39    /// Added in 0.2.0.
40    #[must_use]
41    pub const fn from_degrees(degrees: i64) -> Option<Self> {
42        match degrees.rem_euclid(360) {
43            0 => Some(Self::None),
44            90 => Some(Self::Clockwise90),
45            180 => Some(Self::Half),
46            270 => Some(Self::Clockwise270),
47            _ => None,
48        }
49    }
50
51    /// The rotation for `degrees` written the canonical way, 0, 90, 180 or 270, else
52    /// `Option::None`: the check for values callers pass in (op JSON, the bindings), where
53    /// `-90` or `450` is a mistake rather than a file to tolerate.
54    ///
55    /// Added in 0.2.0.
56    #[must_use]
57    pub const fn from_canonical_degrees(degrees: u16) -> Option<Self> {
58        match degrees {
59            0 => Some(Self::None),
60            90 => Some(Self::Clockwise90),
61            180 => Some(Self::Half),
62            270 => Some(Self::Clockwise270),
63            _ => None,
64        }
65    }
66
67    /// The rotation a resolved `/Rotate` value states: an integer or an integral real that is
68    /// a multiple of 90 (`90` and `90.0` alike), else `Option::None`.
69    ///
70    /// Added in 0.2.0.
71    #[must_use]
72    pub fn from_object(value: &Object) -> Option<Self> {
73        let degrees = match *value {
74            Object::Integer(i) => i,
75            #[expect(
76                clippy::cast_possible_truncation,
77                reason = "only integral values in i64 range get here"
78            )]
79            Object::Real(r) if r.fract() == 0.0 && r.abs() < 1e15 => r as i64,
80            _ => return None,
81        };
82        Self::from_degrees(degrees)
83    }
84
85    /// Whether the rotation swaps width and height.
86    ///
87    /// Added in 0.2.0.
88    #[must_use]
89    pub const fn is_quarter_turn(self) -> bool {
90        matches!(self, Self::Clockwise90 | Self::Clockwise270)
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::Rotation;
97    use crate::Object;
98
99    #[test]
100    fn from_degrees_should_accept_every_multiple_of_90() {
101        let read: Vec<_> = [0, 90, -90, 450, 45].into_iter().map(Rotation::from_degrees).collect();
102        assert_eq!(
103            read,
104            [
105                Some(Rotation::None),
106                Some(Rotation::Clockwise90),
107                Some(Rotation::Clockwise270),
108                Some(Rotation::Clockwise90),
109                None
110            ]
111        );
112    }
113
114    #[test]
115    fn from_object_should_read_integers_and_integral_reals_only() {
116        let values = [
117            Object::Integer(90),
118            Object::Real(90.0),
119            Object::Real(-180.0),
120            Object::Real(90.5),
121            Object::Real(1e300),
122            Object::Name("Rotate".into()),
123        ];
124        let read: Vec<_> = values.iter().map(Rotation::from_object).collect();
125        assert_eq!(
126            read,
127            [
128                Some(Rotation::Clockwise90),
129                Some(Rotation::Clockwise90),
130                Some(Rotation::Half),
131                None,
132                None,
133                None
134            ]
135        );
136    }
137
138    #[test]
139    fn from_canonical_degrees_should_refuse_other_spellings() {
140        let read: Vec<_> = [270, 450, 45]
141            .into_iter()
142            .map(Rotation::from_canonical_degrees)
143            .collect();
144        assert_eq!(read, [Some(Rotation::Clockwise270), None, None]);
145    }
146
147    #[test]
148    fn degrees_should_round_trip_through_from_degrees() {
149        let all = [
150            Rotation::None,
151            Rotation::Clockwise90,
152            Rotation::Half,
153            Rotation::Clockwise270,
154        ];
155        let back: Vec<_> = all
156            .iter()
157            .map(|r| Rotation::from_degrees(i64::from(r.degrees())))
158            .collect();
159        assert_eq!(back, all.map(Some));
160    }
161}