1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! @page at-rule properties

use crate::values::generics::NonNegative;
use crate::values::specified::length::AbsoluteLength;

/// Page size names.
///
/// https://drafts.csswg.org/css-page-3/#typedef-page-size-page-size
#[derive(
    Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, SpecifiedValueInfo, ToCss, ToShmem,
)]
#[repr(u8)]
pub enum PaperSize {
    /// ISO A5 media
    A5,
    /// ISO A4 media
    A4,
    /// ISO A3 media
    A3,
    /// ISO B5 media
    B5,
    /// ISO B4 media
    B4,
    /// JIS B5 media
    JisB5,
    /// JIS B4 media
    JisB4,
    /// North American Letter size
    Letter,
    /// North American Legal size
    Legal,
    /// North American Ledger size
    Ledger,
}

impl PaperSize {
    /// Gets the long edge length of the paper size
    pub fn long_edge(&self) -> NonNegative<AbsoluteLength> {
        NonNegative(match *self {
            PaperSize::A5 => AbsoluteLength::Mm(210.0),
            PaperSize::A4 => AbsoluteLength::Mm(297.0),
            PaperSize::A3 => AbsoluteLength::Mm(420.0),
            PaperSize::B5 => AbsoluteLength::Mm(250.0),
            PaperSize::B4 => AbsoluteLength::Mm(353.0),
            PaperSize::JisB5 => AbsoluteLength::Mm(257.0),
            PaperSize::JisB4 => AbsoluteLength::Mm(364.0),
            PaperSize::Letter => AbsoluteLength::In(11.0),
            PaperSize::Legal => AbsoluteLength::In(14.0),
            PaperSize::Ledger => AbsoluteLength::In(17.0),
        })
    }
    /// Gets the short edge length of the paper size
    pub fn short_edge(&self) -> NonNegative<AbsoluteLength> {
        NonNegative(match *self {
            PaperSize::A5 => AbsoluteLength::Mm(148.0),
            PaperSize::A4 => AbsoluteLength::Mm(210.0),
            PaperSize::A3 => AbsoluteLength::Mm(297.0),
            PaperSize::B5 => AbsoluteLength::Mm(176.0),
            PaperSize::B4 => AbsoluteLength::Mm(250.0),
            PaperSize::JisB5 => AbsoluteLength::Mm(182.0),
            PaperSize::JisB4 => AbsoluteLength::Mm(257.0),
            PaperSize::Letter => AbsoluteLength::In(8.5),
            PaperSize::Legal => AbsoluteLength::In(8.5),
            PaperSize::Ledger => AbsoluteLength::In(11.0),
        })
    }
}

/// Page orientation names.
///
/// https://drafts.csswg.org/css-page-3/#page-orientation-prop
#[derive(
    Clone,
    Copy,
    Debug,
    Eq,
    MallocSizeOf,
    Parse,
    PartialEq,
    SpecifiedValueInfo,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(u8)]
pub enum PageOrientation {
    /// upright
    Upright,
    /// rotate-left (counter-clockwise)
    RotateLeft,
    /// rotate-right (clockwise)
    RotateRight,
}

/// Paper orientation
///
/// https://drafts.csswg.org/css-page-3/#page-size-prop
#[derive(
    Clone,
    Copy,
    Debug,
    Eq,
    MallocSizeOf,
    Parse,
    PartialEq,
    SpecifiedValueInfo,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(u8)]
pub enum PageSizeOrientation {
    /// Portrait orientation
    Portrait,
    /// Landscape orientation
    Landscape,
}

#[inline]
fn is_portrait(orientation: &PageSizeOrientation) -> bool {
    *orientation == PageSizeOrientation::Portrait
}

/// Page size property
///
/// https://drafts.csswg.org/css-page-3/#page-size-prop
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)]
#[repr(C, u8)]
pub enum GenericPageSize<S> {
    /// `auto` value.
    Auto,
    /// Page dimensions.
    Size(S),
    /// An orientation with no size.
    Orientation(PageSizeOrientation),
    /// Paper size by name
    PaperSize(
        PaperSize,
        #[css(skip_if = "is_portrait")] PageSizeOrientation,
    ),
}

pub use self::GenericPageSize as PageSize;

impl<S> PageSize<S> {
    /// `auto` value.
    #[inline]
    pub fn auto() -> Self {
        PageSize::Auto
    }

    /// Whether this is the `auto` value.
    #[inline]
    pub fn is_auto(&self) -> bool {
        matches!(*self, PageSize::Auto)
    }
}