style/values/generics/
page.rs

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
7use crate::values::generics::NonNegative;
8use 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)]
17pub 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
40impl PaperSize {
41    /// Gets the long edge length of the paper size
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
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/// Page orientation names.
74///
75/// https://drafts.csswg.org/css-page-3/#page-orientation-prop
76#[derive(
77    Clone,
78    Copy,
79    Debug,
80    Eq,
81    MallocSizeOf,
82    Parse,
83    PartialEq,
84    SpecifiedValueInfo,
85    ToComputedValue,
86    ToCss,
87    ToResolvedValue,
88    ToShmem,
89    ToTyped,
90)]
91#[repr(u8)]
92pub enum PageOrientation {
93    /// upright
94    Upright,
95    /// rotate-left (counter-clockwise)
96    RotateLeft,
97    /// rotate-right (clockwise)
98    RotateRight,
99}
100
101/// Paper orientation
102///
103/// https://drafts.csswg.org/css-page-3/#page-size-prop
104#[derive(
105    Clone,
106    Copy,
107    Debug,
108    Eq,
109    MallocSizeOf,
110    Parse,
111    PartialEq,
112    SpecifiedValueInfo,
113    ToCss,
114    ToResolvedValue,
115    ToShmem,
116)]
117#[repr(u8)]
118pub enum PageSizeOrientation {
119    /// Portrait orientation
120    Portrait,
121    /// Landscape orientation
122    Landscape,
123}
124
125#[inline]
126fn is_portrait(orientation: &PageSizeOrientation) -> bool {
127    *orientation == PageSizeOrientation::Portrait
128}
129
130/// Page size property
131///
132/// https://drafts.csswg.org/css-page-3/#page-size-prop
133#[derive(
134    Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem, ToTyped,
135)]
136#[repr(C, u8)]
137pub enum GenericPageSize<S> {
138    /// `auto` value.
139    Auto,
140    /// Page dimensions.
141    Size(S),
142    /// An orientation with no size.
143    Orientation(PageSizeOrientation),
144    /// Paper size by name
145    PaperSize(
146        PaperSize,
147        #[css(skip_if = "is_portrait")] PageSizeOrientation,
148    ),
149}
150
151pub use self::GenericPageSize as PageSize;
152
153impl<S> PageSize<S> {
154    /// `auto` value.
155    #[inline]
156    pub fn auto() -> Self {
157        PageSize::Auto
158    }
159
160    /// Whether this is the `auto` value.
161    #[inline]
162    pub fn is_auto(&self) -> bool {
163        matches!(*self, PageSize::Auto)
164    }
165}