1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 //! @page at-rule properties
6 
7 use crate::values::generics::NonNegative;
8 use crate::values::specified::length::AbsoluteLength;
9 
10 /// Page size names.
11 ///
12 /// https://drafts.csswg.org/css-page-3/#typedef-page-size-page-size
13 #[derive(
14     Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, SpecifiedValueInfo, ToCss, ToShmem,
15 )]
16 #[repr(u8)]
17 pub enum PaperSize {
18     /// ISO A5 media
19     A5,
20     /// ISO A4 media
21     A4,
22     /// ISO A3 media
23     A3,
24     /// ISO B5 media
25     B5,
26     /// ISO B4 media
27     B4,
28     /// JIS B5 media
29     JisB5,
30     /// JIS B4 media
31     JisB4,
32     /// North American Letter size
33     Letter,
34     /// North American Legal size
35     Legal,
36     /// North American Ledger size
37     Ledger,
38 }
39 
40 impl PaperSize {
41     /// Gets the long edge length of the paper size
long_edge(&self) -> NonNegative<AbsoluteLength>42     pub fn long_edge(&self) -> NonNegative<AbsoluteLength> {
43         NonNegative(match *self {
44             PaperSize::A5 => AbsoluteLength::Mm(210.0),
45             PaperSize::A4 => AbsoluteLength::Mm(297.0),
46             PaperSize::A3 => AbsoluteLength::Mm(420.0),
47             PaperSize::B5 => AbsoluteLength::Mm(250.0),
48             PaperSize::B4 => AbsoluteLength::Mm(353.0),
49             PaperSize::JisB5 => AbsoluteLength::Mm(257.0),
50             PaperSize::JisB4 => AbsoluteLength::Mm(364.0),
51             PaperSize::Letter => AbsoluteLength::In(11.0),
52             PaperSize::Legal => AbsoluteLength::In(14.0),
53             PaperSize::Ledger => AbsoluteLength::In(17.0),
54         })
55     }
56     /// Gets the short edge length of the paper size
short_edge(&self) -> NonNegative<AbsoluteLength>57     pub fn short_edge(&self) -> NonNegative<AbsoluteLength> {
58         NonNegative(match *self {
59             PaperSize::A5 => AbsoluteLength::Mm(148.0),
60             PaperSize::A4 => AbsoluteLength::Mm(210.0),
61             PaperSize::A3 => AbsoluteLength::Mm(297.0),
62             PaperSize::B5 => AbsoluteLength::Mm(176.0),
63             PaperSize::B4 => AbsoluteLength::Mm(250.0),
64             PaperSize::JisB5 => AbsoluteLength::Mm(182.0),
65             PaperSize::JisB4 => AbsoluteLength::Mm(257.0),
66             PaperSize::Letter => AbsoluteLength::In(8.5),
67             PaperSize::Legal => AbsoluteLength::In(8.5),
68             PaperSize::Ledger => AbsoluteLength::In(11.0),
69         })
70     }
71 }
72 
73 /// Paper orientation
74 ///
75 /// https://drafts.csswg.org/css-page-3/#page-size-prop
76 #[derive(
77     Clone,
78     Copy,
79     Debug,
80     Eq,
81     MallocSizeOf,
82     Parse,
83     PartialEq,
84     SpecifiedValueInfo,
85     ToCss,
86     ToResolvedValue,
87     ToShmem,
88 )]
89 #[repr(u8)]
90 pub enum Orientation {
91     /// Portrait orientation
92     Portrait,
93     /// Landscape orientation
94     Landscape,
95 }
96 
97 /// Page size property
98 ///
99 /// https://drafts.csswg.org/css-page-3/#page-size-prop
100 #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)]
101 #[repr(C, u8)]
102 pub enum GenericPageSize<S> {
103     /// Page dimensions.
104     Size(S),
105     /// Paper size with no orientation.
106     PaperSize(PaperSize),
107     /// An orientation with no size.
108     Orientation(Orientation),
109     /// Paper size by name, with an orientation.
110     PaperSizeAndOrientation(PaperSize, Orientation),
111     /// `auto` value.
112     Auto,
113 }
114 
115 pub use self::GenericPageSize as PageSize;
116 
117 impl<S> PageSize<S> {
118     /// `auto` value.
119     #[inline]
auto() -> Self120     pub fn auto() -> Self {
121         PageSize::Auto
122     }
123 
124     /// Whether this is the `auto` value.
125     #[inline]
is_auto(&self) -> bool126     pub fn is_auto(&self) -> bool {
127         matches!(*self, PageSize::Auto)
128     }
129 }
130