style/values/computed/
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//! Computed @page at-rule properties and named-page style properties
6
7use crate::values::computed::length::NonNegativeLength;
8use crate::values::computed::{Context, ToComputedValue};
9use crate::values::generics;
10use crate::values::generics::size::Size2D;
11
12use crate::values::specified::page as specified;
13pub use generics::page::GenericPageSize;
14pub use generics::page::PageOrientation;
15pub use generics::page::PageSizeOrientation;
16pub use generics::page::PaperSize;
17pub use specified::PageName;
18
19/// Computed value of the @page size descriptor
20///
21/// The spec says that the computed value should be the same as the specified
22/// value but with all absolute units, but it's not currently possibly observe
23/// the computed value of page-size.
24#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss, ToResolvedValue, ToShmem)]
25#[repr(C, u8)]
26pub enum PageSize {
27    /// Specified size, paper size, or paper size and orientation.
28    Size(Size2D<NonNegativeLength>),
29    /// `landscape` or `portrait` value, no specified size.
30    Orientation(PageSizeOrientation),
31    /// `auto` value
32    Auto,
33}
34
35impl ToComputedValue for specified::PageSize {
36    type ComputedValue = PageSize;
37
38    fn to_computed_value(&self, ctx: &Context) -> Self::ComputedValue {
39        match &*self {
40            Self::Size(s) => PageSize::Size(s.to_computed_value(ctx)),
41            Self::PaperSize(p, PageSizeOrientation::Landscape) => PageSize::Size(Size2D {
42                width: p.long_edge().to_computed_value(ctx),
43                height: p.short_edge().to_computed_value(ctx),
44            }),
45            Self::PaperSize(p, PageSizeOrientation::Portrait) => PageSize::Size(Size2D {
46                width: p.short_edge().to_computed_value(ctx),
47                height: p.long_edge().to_computed_value(ctx),
48            }),
49            Self::Orientation(o) => PageSize::Orientation(*o),
50            Self::Auto => PageSize::Auto,
51        }
52    }
53
54    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
55        match *computed {
56            PageSize::Size(s) => Self::Size(ToComputedValue::from_computed_value(&s)),
57            PageSize::Orientation(o) => Self::Orientation(o),
58            PageSize::Auto => Self::Auto,
59        }
60    }
61}
62
63impl PageSize {
64    /// `auto` value.
65    #[inline]
66    pub fn auto() -> Self {
67        PageSize::Auto
68    }
69
70    /// Whether this is the `auto` value.
71    #[inline]
72    pub fn is_auto(&self) -> bool {
73        matches!(*self, PageSize::Auto)
74    }
75}