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