tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

page.rs (2727B)


      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 
      7 use crate::derives::*;
      8 use crate::values::computed::length::NonNegativeLength;
      9 use crate::values::computed::{Context, ToComputedValue};
     10 use crate::values::generics;
     11 use crate::values::generics::size::Size2D;
     12 
     13 use crate::values::specified::page as specified;
     14 pub use generics::page::GenericPageSize;
     15 pub use generics::page::PageOrientation;
     16 pub use generics::page::PageSizeOrientation;
     17 pub use generics::page::PaperSize;
     18 pub 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 pub enum PageSize {
     28    /// Specified size, paper size, or paper size and orientation.
     29    Size(Size2D<NonNegativeLength>),
     30    /// `landscape` or `portrait` value, no specified size.
     31    Orientation(PageSizeOrientation),
     32    /// `auto` value
     33    Auto,
     34 }
     35 
     36 impl ToComputedValue for specified::PageSize {
     37    type ComputedValue = PageSize;
     38 
     39    fn to_computed_value(&self, ctx: &Context) -> Self::ComputedValue {
     40        match &*self {
     41            Self::Size(s) => PageSize::Size(s.to_computed_value(ctx)),
     42            Self::PaperSize(p, PageSizeOrientation::Landscape) => PageSize::Size(Size2D {
     43                width: p.long_edge().to_computed_value(ctx),
     44                height: p.short_edge().to_computed_value(ctx),
     45            }),
     46            Self::PaperSize(p, PageSizeOrientation::Portrait) => PageSize::Size(Size2D {
     47                width: p.short_edge().to_computed_value(ctx),
     48                height: p.long_edge().to_computed_value(ctx),
     49            }),
     50            Self::Orientation(o) => PageSize::Orientation(*o),
     51            Self::Auto => PageSize::Auto,
     52        }
     53    }
     54 
     55    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
     56        match *computed {
     57            PageSize::Size(s) => Self::Size(ToComputedValue::from_computed_value(&s)),
     58            PageSize::Orientation(o) => Self::Orientation(o),
     59            PageSize::Auto => Self::Auto,
     60        }
     61    }
     62 }
     63 
     64 impl PageSize {
     65    /// `auto` value.
     66    #[inline]
     67    pub fn auto() -> Self {
     68        PageSize::Auto
     69    }
     70 
     71    /// Whether this is the `auto` value.
     72    #[inline]
     73    pub fn is_auto(&self) -> bool {
     74        matches!(*self, PageSize::Auto)
     75    }
     76 }