Skip to main content

easypdf_core/
enums.rs

1//! `easypdf-rust` 中使用的枚举类型。
2
3/// 标准页面尺寸(PDF 点,1 点 = 1/72 英寸)。
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum PageSize {
6    /// A0(2384 x 3370 点)
7    A0,
8    /// A1(1684 x 2384 点)
9    A1,
10    /// A2(1191 x 1684 点)
11    A2,
12    /// A3(842 x 1191 点)
13    A3,
14    /// A4(595 x 842 点)
15    A4,
16    /// A5(420 x 595 点)
17    A5,
18    /// US Letter(612 x 792 点)
19    Letter,
20    /// US Legal(612 x 1008 点)
21    Legal,
22    /// 自定义页面尺寸(宽度、高度,单位为点)。
23    Custom(f64, f64),
24}
25
26impl PageSize {
27    /// 返回此页面尺寸的 `(宽度, 高度)`,单位为 PDF 点。
28    #[must_use]
29    pub const fn dimensions(self) -> (f64, f64) {
30        match self {
31            Self::A0 => (2384.0, 3370.0),
32            Self::A1 => (1684.0, 2384.0),
33            Self::A2 => (1191.0, 1684.0),
34            Self::A3 => (842.0, 1191.0),
35            Self::A4 => (595.0, 842.0),
36            Self::A5 => (420.0, 595.0),
37            Self::Letter => (612.0, 792.0),
38            Self::Legal => (612.0, 1008.0),
39            Self::Custom(w, h) => (w, h),
40        }
41    }
42}
43
44/// 页面方向。
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
46pub enum Orientation {
47    /// 纵向(高度 > 宽度)。
48    #[default]
49    Portrait,
50    /// 横向(宽度 > 高度)。
51    Landscape,
52}
53
54/// 旋转角度(顺时针,单位为度)。
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum Rotation {
57    /// 不旋转。
58    None,
59    /// 顺时针旋转 90 度。
60    Clockwise90,
61    /// 旋转 180 度。
62    Clockwise180,
63    /// 顺时针旋转 270 度(等价于逆时针 90 度)。
64    Clockwise270,
65}
66
67/// 水平文本对齐方式。
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
69pub enum TextAlignment {
70    /// 文本左对齐。
71    #[default]
72    Left,
73    /// 文本水平居中。
74    Center,
75    /// 文本右对齐。
76    Right,
77    /// 文本两端对齐(拉伸以填满行宽)。
78    Justify,
79}
80
81/// 垂直对齐方式。
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
83pub enum VerticalAlignment {
84    /// 顶部对齐。
85    #[default]
86    Top,
87    /// 垂直居中。
88    Middle,
89    /// 底部对齐。
90    Bottom,
91}
92
93/// 支持嵌入 PDF 的图片格式。
94#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95pub enum ImageFormat {
96    /// JPEG 图片。
97    Jpeg,
98    /// PNG 图片。
99    Png,
100}
101
102#[cfg(test)]
103#[allow(clippy::uninlined_format_args, clippy::float_cmp)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn page_size_a4() {
109        let (w, h) = PageSize::A4.dimensions();
110        assert_eq!(w, 595.0);
111        assert_eq!(h, 842.0);
112    }
113
114    #[test]
115    fn page_size_a0() {
116        let (w, _h) = PageSize::A0.dimensions();
117        assert_eq!(w, 2384.0);
118    }
119
120    #[test]
121    fn page_size_letter() {
122        let (w, h) = PageSize::Letter.dimensions();
123        assert_eq!(w, 612.0);
124        assert_eq!(h, 792.0);
125    }
126
127    #[test]
128    fn page_size_legal() {
129        let (_, h) = PageSize::Legal.dimensions();
130        assert_eq!(h, 1008.0);
131    }
132
133    #[test]
134    fn page_size_custom() {
135        let (w, h) = PageSize::Custom(300.0, 400.0).dimensions();
136        assert_eq!(w, 300.0);
137        assert_eq!(h, 400.0);
138    }
139
140    #[test]
141    fn orientation_default_is_portrait() {
142        assert_eq!(Orientation::default(), Orientation::Portrait);
143    }
144
145    #[test]
146    fn orientation_debug() {
147        assert_eq!(format!("{:?}", Orientation::Landscape), "Landscape");
148    }
149
150    #[test]
151    fn text_alignment_default_is_left() {
152        assert_eq!(TextAlignment::default(), TextAlignment::Left);
153    }
154
155    #[test]
156    fn text_alignment_variants() {
157        assert_ne!(TextAlignment::Left, TextAlignment::Center);
158        assert_ne!(TextAlignment::Right, TextAlignment::Justify);
159    }
160
161    #[test]
162    fn vertical_alignment_default_is_top() {
163        assert_eq!(VerticalAlignment::default(), VerticalAlignment::Top);
164    }
165
166    #[test]
167    fn rotation_variants() {
168        assert_ne!(Rotation::None, Rotation::Clockwise90);
169        assert_ne!(Rotation::Clockwise180, Rotation::Clockwise270);
170    }
171
172    #[test]
173    fn image_format_variants() {
174        assert_ne!(ImageFormat::Jpeg, ImageFormat::Png);
175    }
176
177    #[test]
178    fn page_size_clone_copy() {
179        let ps = PageSize::A4;
180        let copied = ps;
181        assert_eq!(ps, copied);
182    }
183
184    #[test]
185    fn page_size_custom_eq() {
186        assert_eq!(PageSize::Custom(1.0, 2.0), PageSize::Custom(1.0, 2.0));
187        assert_ne!(PageSize::Custom(1.0, 2.0), PageSize::Custom(3.0, 4.0));
188    }
189
190    #[test]
191    fn page_size_all_sizes() {
192        let sizes = [
193            PageSize::A0,
194            PageSize::A1,
195            PageSize::A2,
196            PageSize::A3,
197            PageSize::A4,
198            PageSize::A5,
199            PageSize::Letter,
200            PageSize::Legal,
201        ];
202        for s in sizes {
203            let (w, h) = s.dimensions();
204            assert!(w > 0.0);
205            assert!(h > 0.0);
206        }
207    }
208}