Skip to main content

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