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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use super::simpletypes::{Angle, Coordinate, PositiveCoordinate};
use crate::{
error::MissingAttributeError,
xml::{parse_xml_bool, XmlNode},
};
pub type Result<T> = ::std::result::Result<T, Box<dyn (::std::error::Error)>>;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Point2D {
/// Specifies a coordinate on the x-axis. The origin point for this coordinate shall be specified
/// by the parent XML element.
pub x: Coordinate,
/// Specifies a coordinate on the x-axis. The origin point for this coordinate shall be specified
/// by the parent XML element.
pub y: Coordinate,
}
impl Point2D {
pub fn new(x: Coordinate, y: Coordinate) -> Self {
Self { x, y }
}
pub fn from_xml_element(xml_node: &XmlNode) -> Result<Self> {
let mut x = None;
let mut y = None;
for (attr, value) in &xml_node.attributes {
match attr.as_str() {
"x" => x = Some(value.parse()?),
"y" => y = Some(value.parse()?),
_ => (),
}
}
let x = x.ok_or_else(|| MissingAttributeError::new(xml_node.name.clone(), "x"))?;
let y = y.ok_or_else(|| MissingAttributeError::new(xml_node.name.clone(), "y"))?;
Ok(Self { x, y })
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct PositiveSize2D {
/// Specifies the length of the extents rectangle in EMUs. This rectangle shall dictate the size
/// of the object as displayed (the result of any scaling to the original object).
pub width: PositiveCoordinate,
/// Specifies the width of the extents rectangle in EMUs. This rectangle shall dictate the size
/// of the object as displayed (the result of any scaling to the original object).
pub height: PositiveCoordinate,
}
impl PositiveSize2D {
pub fn new(width: PositiveCoordinate, height: PositiveCoordinate) -> Self {
Self { width, height }
}
pub fn from_xml_element(xml_node: &XmlNode) -> Result<Self> {
let mut opt_width = None;
let mut opt_height = None;
for (attr, value) in &xml_node.attributes {
match attr.as_str() {
"cx" => opt_width = Some(value.parse::<PositiveCoordinate>()?),
"cy" => opt_height = Some(value.parse::<PositiveCoordinate>()?),
_ => (),
}
}
let width = opt_width.ok_or_else(|| MissingAttributeError::new(xml_node.name.clone(), "cx"))?;
let height = opt_height.ok_or_else(|| MissingAttributeError::new(xml_node.name.clone(), "cy"))?;
Ok(Self { width, height })
}
}
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Transform2D {
/// Specifies the rotation of the Graphic Frame. The units for which this attribute is specified
/// in reside within the simple type definition referenced below.
pub rotate_angle: Option<Angle>,
/// Specifies a horizontal flip. When true, this attribute defines that the shape is flipped
/// horizontally about the center of its bounding box.
///
/// Defaults to false
pub flip_horizontal: Option<bool>,
/// Specifies a vertical flip. When true, this attribute defines that the group is flipped
/// vertically about the center of its bounding box.
pub flip_vertical: Option<bool>,
/// This element specifies the location of the bounding box of an object. Effects on an object are not included in this
/// bounding box.
pub offset: Option<Point2D>,
/// This element specifies the size of the bounding box enclosing the referenced object.
pub extents: Option<PositiveSize2D>,
}
impl Transform2D {
pub fn from_xml_element(xml_node: &XmlNode) -> Result<Self> {
xml_node
.attributes
.iter()
.try_fold(Default::default(), |mut instance: Self, (key, value)| {
match key.as_str() {
"rot" => instance.rotate_angle = Some(value.parse()?),
"flipH" => instance.flip_horizontal = Some(parse_xml_bool(value)?),
"flipV" => instance.flip_vertical = Some(parse_xml_bool(value)?),
_ => (),
}
Ok(instance)
})
.and_then(|instance| {
xml_node
.child_nodes
.iter()
.try_fold(instance, |mut instance, child_node| {
match child_node.local_name() {
"off" => instance.offset = Some(Point2D::from_xml_element(child_node)?),
"ext" => instance.extents = Some(PositiveSize2D::from_xml_element(child_node)?),
_ => (),
}
Ok(instance)
})
})
}
}
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct GroupTransform2D {
/// Rotation. Specifies the clockwise rotation of a group in 1/64000 of a degree.
///
/// Defaults to 0
pub rotate_angle: Option<Angle>,
/// Horizontal flip. When true, this attribute defines that the group is flipped horizontally
/// about the center of its bounding box.
///
/// Defaults to false
pub flip_horizontal: Option<bool>,
/// Vertical flip. When true, this attribute defines that the group is flipped vertically about
/// the center of its bounding box.
///
/// Defaults to false
pub flip_vertical: Option<bool>,
/// This element specifies the location of the bounding box of an object. Effects on an object are not included in this
/// bounding box.
pub offset: Option<Point2D>,
/// This element specifies the size of the bounding box enclosing the referenced object.
pub extents: Option<PositiveSize2D>,
/// This element specifies the location of the child extents rectangle and is used for calculations of grouping, scaling,
/// and rotation behavior of shapes placed within a group.
pub child_offset: Option<Point2D>,
/// This element specifies the size dimensions of the child extents rectangle and is used for calculations of grouping,
/// scaling, and rotation behavior of shapes placed within a group.
pub child_extents: Option<PositiveSize2D>,
}
impl GroupTransform2D {
pub fn from_xml_element(xml_node: &XmlNode) -> Result<Self> {
xml_node
.attributes
.iter()
.try_fold(Default::default(), |mut instance: Self, (attr, value)| {
match attr.as_str() {
"rot" => instance.rotate_angle = Some(value.parse()?),
"flipH" => instance.flip_horizontal = Some(parse_xml_bool(value)?),
"flipV" => instance.flip_vertical = Some(parse_xml_bool(value)?),
_ => (),
}
Ok(instance)
})
.and_then(|instance| {
xml_node
.child_nodes
.iter()
.try_fold(instance, |mut instance, child_node| {
match child_node.local_name() {
"off" => instance.offset = Some(Point2D::from_xml_element(child_node)?),
"ext" => instance.extents = Some(PositiveSize2D::from_xml_element(child_node)?),
"chOff" => instance.child_offset = Some(Point2D::from_xml_element(child_node)?),
"chExt" => instance.child_extents = Some(PositiveSize2D::from_xml_element(child_node)?),
_ => (),
}
Ok(instance)
})
})
}
}