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
use crate::utils::IsDefault;
use enum_display::EnumDisplay;
use std::fmt::{
Display,
Formatter,
};
#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone, EnumDisplay)]
enum Align {
#[display("none")]
None,
#[display("xMinYMin")]
XMinYMin,
#[display("xMidYMin")]
XMidYMin,
#[display("xMaxYMin")]
XMaxYMin,
#[display("xMinYMid")]
XMinYMid,
#[display("xMidYMid")]
XMidYMid,
#[display("xMaxYMid")]
XMaxYMid,
#[display("xMinYMax")]
XMinYMax,
#[display("xMidYMax")]
XMidYMax,
#[display("xMaxYMax")]
XMaxYMax,
}
#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone, EnumDisplay)]
enum MeetOrSlice {
#[display("meet")]
Meet,
#[display("slice")]
Slice,
}
/// Controls how a view box is scaled and aligned within its viewport.
///
/// # Reference
///
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/preserveAspectRatio
#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)]
pub struct PreserveAspectRatio(Align, MeetOrSlice);
impl Default for PreserveAspectRatio {
fn default() -> Self {
Self(Align::XMidYMid, MeetOrSlice::Meet)
}
}
impl IsDefault for PreserveAspectRatio {}
impl PreserveAspectRatio {
/// Creates a new [`PreserveAspectRatio`] instance.
///
/// # Returns
/// - [`Self`]
pub fn new() -> Self {
Self::default()
}
/// Disables aspect ratio preservation (non-uniform scaling).
///
/// # Returns
/// - [`Self`]
pub fn align_none(mut self) -> Self {
self.0 = Align::None;
self
}
/// Aligns the view box to the top-left corner of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_min_y_min(mut self) -> Self {
self.0 = Align::XMinYMin;
self
}
/// Aligns the view box to the top-center of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_mid_y_min(mut self) -> Self {
self.0 = Align::XMidYMin;
self
}
/// Aligns the view box to the top-right corner of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_max_y_min(mut self) -> Self {
self.0 = Align::XMaxYMin;
self
}
/// Aligns the view box to the middle-left of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_min_y_mid(mut self) -> Self {
self.0 = Align::XMinYMid;
self
}
/// Aligns the view box to the center of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_mid_y_mid(mut self) -> Self {
self.0 = Align::XMidYMid;
self
}
/// Aligns the view box to the middle-right of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_max_y_mid(mut self) -> Self {
self.0 = Align::XMaxYMid;
self
}
/// Aligns the view box to the bottom-left corner of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_min_y_max(mut self) -> Self {
self.0 = Align::XMinYMax;
self
}
/// Aligns the view box to the bottom-center of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_mid_y_max(mut self) -> Self {
self.0 = Align::XMidYMax;
self
}
/// Aligns the view box to the bottom-right corner of the viewport.
///
/// # Returns
/// - [`Self`]
pub fn x_max_y_max(mut self) -> Self {
self.0 = Align::XMaxYMax;
self
}
/// Scales the view box to fit entirely within the viewport. This preserves
/// the entire content without clipping.
///
/// # Returns
/// - [`Self`]
pub fn meet(mut self) -> Self {
self.1 = MeetOrSlice::Meet;
self
}
/// Scales the view box to fill the viewport completely. This may clip parts
/// of the content.
///
/// # Returns
/// - [`Self`]
pub fn slice(mut self) -> Self {
self.1 = MeetOrSlice::Slice;
self
}
}
impl Display for PreserveAspectRatio {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.0, self.1)
}
}