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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! [`Rotation`]: a rotation by a multiple of 90°, clockwise, as `/Rotate` (ISO 32000-2
//! §7.7.3.3) stores it and a viewer adds it.
use crate::Object;
/// A rotation by a multiple of 90°, clockwise when displayed: a page's `/Rotate`
/// (§7.7.3.3), or a view rotation added to it.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Rotation {
/// No rotation.
#[default]
None,
/// 90° clockwise.
Clockwise90,
/// 180°.
Half,
/// 270° clockwise (90° counter-clockwise).
Clockwise270,
}
impl Rotation {
/// The rotation in degrees: 0, 90, 180 or 270.
///
/// Added in 0.2.0.
#[must_use]
pub const fn degrees(self) -> u16 {
match self {
Self::None => 0,
Self::Clockwise90 => 90,
Self::Half => 180,
Self::Clockwise270 => 270,
}
}
/// The rotation for `degrees` (any multiple of 90, negative included), else `Option::None`.
///
/// Added in 0.2.0.
#[must_use]
pub const fn from_degrees(degrees: i64) -> Option<Self> {
match degrees.rem_euclid(360) {
0 => Some(Self::None),
90 => Some(Self::Clockwise90),
180 => Some(Self::Half),
270 => Some(Self::Clockwise270),
_ => None,
}
}
/// The rotation for `degrees` written the canonical way, 0, 90, 180 or 270, else
/// `Option::None`: the check for values callers pass in (op JSON, the bindings), where
/// `-90` or `450` is a mistake rather than a file to tolerate.
///
/// Added in 0.2.0.
#[must_use]
pub const fn from_canonical_degrees(degrees: u16) -> Option<Self> {
match degrees {
0 => Some(Self::None),
90 => Some(Self::Clockwise90),
180 => Some(Self::Half),
270 => Some(Self::Clockwise270),
_ => None,
}
}
/// The rotation a resolved `/Rotate` value states: an integer or an integral real that is
/// a multiple of 90 (`90` and `90.0` alike), else `Option::None`.
///
/// Added in 0.2.0.
#[must_use]
pub fn from_object(value: &Object) -> Option<Self> {
let degrees = match *value {
Object::Integer(i) => i,
#[expect(
clippy::cast_possible_truncation,
reason = "only integral values in i64 range get here"
)]
Object::Real(r) if r.fract() == 0.0 && r.abs() < 1e15 => r as i64,
_ => return None,
};
Self::from_degrees(degrees)
}
/// Whether the rotation swaps width and height.
///
/// Added in 0.2.0.
#[must_use]
pub const fn is_quarter_turn(self) -> bool {
matches!(self, Self::Clockwise90 | Self::Clockwise270)
}
}
#[cfg(test)]
mod tests {
use super::Rotation;
use crate::Object;
#[test]
fn from_degrees_should_accept_every_multiple_of_90() {
let read: Vec<_> = [0, 90, -90, 450, 45].into_iter().map(Rotation::from_degrees).collect();
assert_eq!(
read,
[
Some(Rotation::None),
Some(Rotation::Clockwise90),
Some(Rotation::Clockwise270),
Some(Rotation::Clockwise90),
None
]
);
}
#[test]
fn from_object_should_read_integers_and_integral_reals_only() {
let values = [
Object::Integer(90),
Object::Real(90.0),
Object::Real(-180.0),
Object::Real(90.5),
Object::Real(1e300),
Object::Name("Rotate".into()),
];
let read: Vec<_> = values.iter().map(Rotation::from_object).collect();
assert_eq!(
read,
[
Some(Rotation::Clockwise90),
Some(Rotation::Clockwise90),
Some(Rotation::Half),
None,
None,
None
]
);
}
#[test]
fn from_canonical_degrees_should_refuse_other_spellings() {
let read: Vec<_> = [270, 450, 45]
.into_iter()
.map(Rotation::from_canonical_degrees)
.collect();
assert_eq!(read, [Some(Rotation::Clockwise270), None, None]);
}
#[test]
fn degrees_should_round_trip_through_from_degrees() {
let all = [
Rotation::None,
Rotation::Clockwise90,
Rotation::Half,
Rotation::Clockwise270,
];
let back: Vec<_> = all
.iter()
.map(|r| Rotation::from_degrees(i64::from(r.degrees())))
.collect();
assert_eq!(back, all.map(Some));
}
}