forked from mirrors/gecko-dev
		
	Bug 1647843 - Add page-size parsing to Servo r=emilio
Differential Revision: https://phabricator.services.mozilla.com/D102360
This commit is contained in:
		
							parent
							
								
									5cbbfdc99f
								
							
						
					
					
						commit
						c4b65cfdda
					
				
					 6 changed files with 183 additions and 0 deletions
				
			
		|  | @ -70,6 +70,7 @@ pub use self::list::MozListReversed; | ||||||
| pub use self::list::Quotes; | pub use self::list::Quotes; | ||||||
| pub use self::motion::{OffsetPath, OffsetRotate}; | pub use self::motion::{OffsetPath, OffsetRotate}; | ||||||
| pub use self::outline::OutlineStyle; | pub use self::outline::OutlineStyle; | ||||||
|  | pub use self::page::{Orientation, PaperSize, PageSize}; | ||||||
| pub use self::percentage::{NonNegativePercentage, Percentage}; | pub use self::percentage::{NonNegativePercentage, Percentage}; | ||||||
| pub use self::position::AspectRatio; | pub use self::position::AspectRatio; | ||||||
| pub use self::position::{ | pub use self::position::{ | ||||||
|  | @ -117,6 +118,7 @@ pub mod length_percentage; | ||||||
| pub mod list; | pub mod list; | ||||||
| pub mod motion; | pub mod motion; | ||||||
| pub mod outline; | pub mod outline; | ||||||
|  | pub mod page; | ||||||
| pub mod percentage; | pub mod percentage; | ||||||
| pub mod position; | pub mod position; | ||||||
| pub mod rect; | pub mod rect; | ||||||
|  |  | ||||||
							
								
								
									
										14
									
								
								servo/components/style/values/computed/page.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								servo/components/style/values/computed/page.rs
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,14 @@ | ||||||
|  | /* 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/. */
 | ||||||
|  | 
 | ||||||
|  | //! Computed @page at-rule properties
 | ||||||
|  | 
 | ||||||
|  | use crate::values::computed::length::NonNegativeLength; | ||||||
|  | use crate::values::generics; | ||||||
|  | use crate::values::generics::size::Size2D; | ||||||
|  | 
 | ||||||
|  | pub use generics::page::Orientation; | ||||||
|  | pub use generics::page::PaperSize; | ||||||
|  | /// Computed value of the @page size descriptor
 | ||||||
|  | pub type PageSize = generics::page::PageSize<Size2D<NonNegativeLength>>; | ||||||
|  | @ -31,6 +31,7 @@ pub mod grid; | ||||||
| pub mod image; | pub mod image; | ||||||
| pub mod length; | pub mod length; | ||||||
| pub mod motion; | pub mod motion; | ||||||
|  | pub mod page; | ||||||
| pub mod position; | pub mod position; | ||||||
| pub mod rect; | pub mod rect; | ||||||
| pub mod size; | pub mod size; | ||||||
|  | @ -292,3 +293,5 @@ impl<L> ClipRectOrAuto<L> { | ||||||
|         matches!(*self, ClipRectOrAuto::Auto) |         matches!(*self, ClipRectOrAuto::Auto) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | pub use page::PageSize; | ||||||
|  |  | ||||||
							
								
								
									
										114
									
								
								servo/components/style/values/generics/page.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										114
									
								
								servo/components/style/values/generics/page.rs
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,114 @@ | ||||||
|  | /* 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
 | ||||||
|  | 
 | ||||||
|  | /// Page size names.
 | ||||||
|  | ///
 | ||||||
|  | /// https://drafts.csswg.org/css-page-3/#typedef-page-size-page-size
 | ||||||
|  | #[derive(
 | ||||||
|  |     Clone, | ||||||
|  |     Copy, | ||||||
|  |     Debug, | ||||||
|  |     Eq, | ||||||
|  |     MallocSizeOf, | ||||||
|  |     Parse, | ||||||
|  |     PartialEq, | ||||||
|  |     SpecifiedValueInfo, | ||||||
|  |     ToComputedValue, | ||||||
|  |     ToCss, | ||||||
|  |     ToResolvedValue, | ||||||
|  |     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, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /// Paper orientation
 | ||||||
|  | ///
 | ||||||
|  | /// https://drafts.csswg.org/css-page-3/#page-size-prop
 | ||||||
|  | #[derive(
 | ||||||
|  |     Clone, | ||||||
|  |     Copy, | ||||||
|  |     Debug, | ||||||
|  |     Eq, | ||||||
|  |     MallocSizeOf, | ||||||
|  |     Parse, | ||||||
|  |     PartialEq, | ||||||
|  |     SpecifiedValueInfo, | ||||||
|  |     ToComputedValue, | ||||||
|  |     ToCss, | ||||||
|  |     ToResolvedValue, | ||||||
|  |     ToShmem, | ||||||
|  | )] | ||||||
|  | #[repr(u8)] | ||||||
|  | pub enum Orientation { | ||||||
|  |     /// Portrait orientation
 | ||||||
|  |     Portrait, | ||||||
|  |     /// Landscape orientation
 | ||||||
|  |     Landscape, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /// Page size property
 | ||||||
|  | ///
 | ||||||
|  | /// https://drafts.csswg.org/css-page-3/#page-size-prop
 | ||||||
|  | #[derive(
 | ||||||
|  |     Clone, | ||||||
|  |     Copy, | ||||||
|  |     Debug, | ||||||
|  |     MallocSizeOf, | ||||||
|  |     PartialEq, | ||||||
|  |     SpecifiedValueInfo, | ||||||
|  |     ToComputedValue, | ||||||
|  |     ToCss, | ||||||
|  |     ToResolvedValue, | ||||||
|  |     ToShmem, | ||||||
|  | )] | ||||||
|  | #[repr(C, u8)] | ||||||
|  | pub enum PageSize<S> { | ||||||
|  |     /// Page dimensions.
 | ||||||
|  |     Size(S), | ||||||
|  |     /// Paper size with no orientation.
 | ||||||
|  |     PaperSize(PaperSize), | ||||||
|  |     /// An orientation with no size.
 | ||||||
|  |     Orientation(Orientation), | ||||||
|  |     /// Paper size by name, with an orientation.
 | ||||||
|  |     PaperSizeAndOrientation(PaperSize, Orientation), | ||||||
|  |     /// `auto` value.
 | ||||||
|  |     Auto, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 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) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -70,6 +70,7 @@ pub use self::list::MozListReversed; | ||||||
| pub use self::list::Quotes; | pub use self::list::Quotes; | ||||||
| pub use self::motion::{OffsetPath, OffsetRotate}; | pub use self::motion::{OffsetPath, OffsetRotate}; | ||||||
| pub use self::outline::OutlineStyle; | pub use self::outline::OutlineStyle; | ||||||
|  | pub use self::page::{Orientation, PaperSize, PageSize}; | ||||||
| pub use self::percentage::Percentage; | pub use self::percentage::Percentage; | ||||||
| pub use self::position::AspectRatio; | pub use self::position::AspectRatio; | ||||||
| pub use self::position::{ | pub use self::position::{ | ||||||
|  | @ -121,6 +122,7 @@ pub mod list; | ||||||
| pub mod motion; | pub mod motion; | ||||||
| pub mod outline; | pub mod outline; | ||||||
| pub mod percentage; | pub mod percentage; | ||||||
|  | pub mod page; | ||||||
| pub mod position; | pub mod position; | ||||||
| pub mod rect; | pub mod rect; | ||||||
| pub mod resolution; | pub mod resolution; | ||||||
|  |  | ||||||
							
								
								
									
										48
									
								
								servo/components/style/values/specified/page.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								servo/components/style/values/specified/page.rs
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,48 @@ | ||||||
|  | /* 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/. */
 | ||||||
|  | 
 | ||||||
|  | //! Specified @page at-rule properties
 | ||||||
|  | 
 | ||||||
|  | use crate::parser::{Parse, ParserContext}; | ||||||
|  | use crate::values::generics; | ||||||
|  | use crate::values::generics::size::Size2D; | ||||||
|  | use crate::values::specified::length::NonNegativeLength; | ||||||
|  | use cssparser::Parser; | ||||||
|  | use style_traits::ParseError; | ||||||
|  | 
 | ||||||
|  | pub use generics::page::Orientation; | ||||||
|  | pub use generics::page::PaperSize; | ||||||
|  | /// Specified value of the @page size descriptor
 | ||||||
|  | pub type PageSize = generics::page::PageSize<Size2D<NonNegativeLength>>; | ||||||
|  | 
 | ||||||
|  | impl Parse for PageSize { | ||||||
|  |     fn parse<'i, 't>( | ||||||
|  |         context: &ParserContext, | ||||||
|  |         input: &mut Parser<'i, 't>, | ||||||
|  |     ) -> Result<Self, ParseError<'i>> { | ||||||
|  |         // Try to parse as <page-size> [ <orientation> ]
 | ||||||
|  |         if let Ok(paper_size) = input.try_parse(PaperSize::parse) { | ||||||
|  |             if let Ok(orientation) = input.try_parse(Orientation::parse) { | ||||||
|  |                 return Ok(PageSize::PaperSizeAndOrientation(paper_size, orientation)); | ||||||
|  |             } | ||||||
|  |             return Ok(PageSize::PaperSize(paper_size)); | ||||||
|  |         } | ||||||
|  |         // Try to parse as <orientation> [ <page-size> ]
 | ||||||
|  |         if let Ok(orientation) = input.try_parse(Orientation::parse) { | ||||||
|  |             if let Ok(paper_size) = input.try_parse(PaperSize::parse) { | ||||||
|  |                 return Ok(PageSize::PaperSizeAndOrientation(paper_size, orientation)); | ||||||
|  |             } | ||||||
|  |             return Ok(PageSize::Orientation(orientation)); | ||||||
|  |         } | ||||||
|  |         // Try to parse dimensions
 | ||||||
|  |         if let Ok(size) = | ||||||
|  |             input.try_parse(|i| Size2D::parse_with(context, i, NonNegativeLength::parse)) | ||||||
|  |         { | ||||||
|  |             return Ok(PageSize::Size(size)); | ||||||
|  |         } | ||||||
|  |         // auto value
 | ||||||
|  |         input.expect_ident_matching("auto")?; | ||||||
|  |         Ok(PageSize::Auto) | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in a new issue
	
	 Emily McDonough
						Emily McDonough