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::derives::*;
8use crate::values::generics::NonNegative;
9use crate::values::specified::length::AbsoluteLength;
10
11/// Page size names.
12///
13/// https://drafts.csswg.org/css-page-3/#typedef-page-size-page-size
14#[derive(
15    Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, SpecifiedValueInfo, ToCss, ToShmem,
16)]
17#[repr(u8)]
18pub enum PaperSize {
19    /// ISO A5 media
20    A5,
21    /// ISO A4 media
22    A4,
23    /// ISO A3 media
24    A3,
25    /// ISO B5 media
26    B5,
27    /// ISO B4 media
28    B4,
29    /// JIS B5 media
30    JisB5,
31    /// JIS B4 media
32    JisB4,
33    /// North American Letter size
34    Letter,
35    /// North American Legal size
36    Legal,
37    /// North American Ledger size
38    Ledger,
39}
40
41impl PaperSize {
42    /// Gets the long edge length of the paper size
43    pub fn long_edge(&self) -> NonNegative<AbsoluteLength> {
44        NonNegative(match *self {
45            PaperSize::A5 => AbsoluteLength::Mm(210.0),
46            PaperSize::A4 => AbsoluteLength::Mm(297.0),
47            PaperSize::A3 => AbsoluteLength::Mm(420.0),
48            PaperSize::B5 => AbsoluteLength::Mm(250.0),
49            PaperSize::B4 => AbsoluteLength::Mm(353.0),
50            PaperSize::JisB5 => AbsoluteLength::Mm(257.0),
51            PaperSize::JisB4 => AbsoluteLength::Mm(364.0),
52            PaperSize::Letter => AbsoluteLength::In(11.0),
53            PaperSize::Legal => AbsoluteLength::In(14.0),
54            PaperSize::Ledger => AbsoluteLength::In(17.0),
55        })
56    }
57    /// Gets the short edge length of the paper size
58    pub fn short_edge(&self) -> NonNegative<AbsoluteLength> {
59        NonNegative(match *self {
60            PaperSize::A5 => AbsoluteLength::Mm(148.0),
61            PaperSize::A4 => AbsoluteLength::Mm(210.0),
62            PaperSize::A3 => AbsoluteLength::Mm(297.0),
63            PaperSize::B5 => AbsoluteLength::Mm(176.0),
64            PaperSize::B4 => AbsoluteLength::Mm(250.0),
65            PaperSize::JisB5 => AbsoluteLength::Mm(182.0),
66            PaperSize::JisB4 => AbsoluteLength::Mm(257.0),
67            PaperSize::Letter => AbsoluteLength::In(8.5),
68            PaperSize::Legal => AbsoluteLength::In(8.5),
69            PaperSize::Ledger => AbsoluteLength::In(11.0),
70        })
71    }
72}
73
74/// Page orientation names.
75///
76/// https://drafts.csswg.org/css-page-3/#page-orientation-prop
77#[derive(
78    Clone,
79    Copy,
80    Debug,
81    Eq,
82    MallocSizeOf,
83    Parse,
84    PartialEq,
85    SpecifiedValueInfo,
86    ToComputedValue,
87    ToCss,
88    ToResolvedValue,
89    ToShmem,
90    ToTyped,
91)]
92#[repr(u8)]
93pub enum PageOrientation {
94    /// upright
95    Upright,
96    /// rotate-left (counter-clockwise)
97    RotateLeft,
98    /// rotate-right (clockwise)
99    RotateRight,
100}
101
102/// Paper orientation
103///
104/// https://drafts.csswg.org/css-page-3/#page-size-prop
105#[derive(
106    Clone,
107    Copy,
108    Debug,
109    Eq,
110    MallocSizeOf,
111    Parse,
112    PartialEq,
113    SpecifiedValueInfo,
114    ToCss,
115    ToResolvedValue,
116    ToShmem,
117)]
118#[repr(u8)]
119pub enum PageSizeOrientation {
120    /// Portrait orientation
121    Portrait,
122    /// Landscape orientation
123    Landscape,
124}
125
126#[inline]
127fn is_portrait(orientation: &PageSizeOrientation) -> bool {
128    *orientation == PageSizeOrientation::Portrait
129}
130
131/// Page size property
132///
133/// https://drafts.csswg.org/css-page-3/#page-size-prop
134#[derive(
135    Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem, ToTyped,
136)]
137#[repr(C, u8)]
138pub enum GenericPageSize<S> {
139    /// `auto` value.
140    Auto,
141    /// Page dimensions.
142    Size(S),
143    /// An orientation with no size.
144    Orientation(PageSizeOrientation),
145    /// Paper size by name
146    PaperSize(
147        PaperSize,
148        #[css(skip_if = "is_portrait")] PageSizeOrientation,
149    ),
150}
151
152pub use self::GenericPageSize as PageSize;
153
154impl<S> PageSize<S> {
155    /// `auto` value.
156    #[inline]
157    pub fn auto() -> Self {
158        PageSize::Auto
159    }
160
161    /// Whether this is the `auto` value.
162    #[inline]
163    pub fn is_auto(&self) -> bool {
164        matches!(*self, PageSize::Auto)
165    }
166}