From aea66a9fb6e5e2c367a65cbb7a2160715d0526d1 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 15 Feb 2018 10:00:51 +0100 Subject: [PATCH] Move FontTag to the generic module --- components/style/gecko/rules.rs | 3 +- components/style/properties/gecko.mako.rs | 3 +- .../helpers/animated_properties.mako.rs | 4 +- components/style/values/generics/font.rs | 46 ++++++++++++++++++- components/style/values/specified/font.rs | 46 +------------------ 5 files changed, 49 insertions(+), 53 deletions(-) diff --git a/components/style/gecko/rules.rs b/components/style/gecko/rules.rs index 599cdf81e25d..ce448cddfd79 100644 --- a/components/style/gecko/rules.rs +++ b/components/style/gecko/rules.rs @@ -21,7 +21,8 @@ use std::fmt::{self, Write}; use std::str; use str::CssStringWriter; use values::computed::font::FamilyName; -use values::specified::font::{FontTag, FontVariationSettings, SpecifiedFontFeatureSettings}; +use values::generics::font::FontTag; +use values::specified::font::{FontVariationSettings, SpecifiedFontFeatureSettings}; /// A @font-face rule pub type FontFaceRule = RefPtr; diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index cded662230e3..b33c63b50d2b 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -1452,8 +1452,7 @@ impl Clone for ${style_struct.gecko_struct_name} { } pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { - use values::generics::font::{FontSettings, ${tag_type}}; - use values::specified::font::FontTag; + use values::generics::font::{FontSettings, FontTag, ${tag_type}}; FontSettings( self.gecko.mFont.${gecko_ffi_name}.iter().map(|gecko_font_setting| { diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index 0d6fa24af2c4..f0eb0113c956 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -52,14 +52,12 @@ use values::computed::transform::Translate as ComputedTranslate; use values::computed::transform::Scale as ComputedScale; use values::generics::transform::{self, Rotate, Translate, Scale, Transform, TransformOperation}; use values::distance::{ComputeSquaredDistance, SquaredDistance}; -use values::generics::font::FontSettings as GenericFontSettings; +use values::generics::font::{FontSettings as GenericFontSettings, FontTag, VariationValue}; use values::computed::font::FontVariationSettings; -use values::generics::font::VariationValue; use values::generics::effects::Filter; use values::generics::position as generic_position; use values::generics::svg::{SVGLength, SvgLengthOrPercentageOrNumber, SVGPaint}; use values::generics::svg::{SVGPaintKind, SVGStrokeDashArray, SVGOpacity}; -use values::specified::font::FontTag; use void::{self, Void}; /// diff --git a/components/style/values/generics/font.rs b/components/style/values/generics/font.rs index ddd3fc0ac3c5..9a8374274ea4 100644 --- a/components/style/values/generics/font.rs +++ b/components/style/values/generics/font.rs @@ -4,13 +4,14 @@ //! Generic types for font stuff. +use byteorder::{ReadBytesExt, BigEndian}; use cssparser::Parser; use num_traits::One; use parser::{Parse, ParserContext}; use std::fmt::{self, Write}; -use style_traits::{CssWriter, ParseError, ToCss}; +use std::io::Cursor; +use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss}; use values::distance::{ComputeSquaredDistance, SquaredDistance}; -use values::specified::font::FontTag; /// https://drafts.csswg.org/css-fonts-4/#feature-tag-value #[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToComputedValue)] @@ -117,3 +118,44 @@ impl ToCss for FontSettings { Ok(()) } } + +/// A font four-character tag, represented as a u32 for convenience. +/// +/// See: +/// https://drafts.csswg.org/css-fonts-4/#font-variation-settings-def +/// https://drafts.csswg.org/css-fonts-4/#descdef-font-face-font-feature-settings +/// +#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToComputedValue)] +pub struct FontTag(pub u32); + +impl ToCss for FontTag { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { + use byteorder::{BigEndian, ByteOrder}; + use std::str; + + let mut raw = [0u8; 4]; + BigEndian::write_u32(&mut raw, self.0); + str::from_utf8(&raw).unwrap_or_default().to_css(dest) + } +} + +impl Parse for FontTag { + fn parse<'i, 't>( + _context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { + let location = input.current_source_location(); + let tag = input.expect_string()?; + + // allowed strings of length 4 containing chars: + if tag.len() != 4 || tag.as_bytes().iter().any(|c| *c < b' ' || *c > b'~') { + return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError)) + } + + let mut raw = Cursor::new(tag.as_bytes()); + Ok(FontTag(raw.read_u32::().unwrap())) + } +} diff --git a/components/style/values/specified/font.rs b/components/style/values/specified/font.rs index a9309267ad40..5443bdba2d07 100644 --- a/components/style/values/specified/font.rs +++ b/components/style/values/specified/font.rs @@ -21,7 +21,7 @@ use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss}; use values::CustomIdent; use values::computed::{font as computed, Context, Length, NonNegativeLength, ToComputedValue}; use values::computed::font::{SingleFontFamily, FontFamilyList, FamilyName}; -use values::generics::font::{FontSettings, FeatureTagValue, VariationValue}; +use values::generics::font::{FontSettings, FeatureTagValue, FontTag, VariationValue}; use values::specified::{AllowQuirks, Integer, LengthOrPercentage, NoCalcLength, Number}; use values::specified::length::{AU_PER_PT, AU_PER_PX, FontBaseSize}; @@ -1945,50 +1945,6 @@ impl Parse for FontLanguageOverride { } } -/// A font four-character tag, represented as a u32 for convenience. -/// -/// See: -/// https://drafts.csswg.org/css-fonts-4/#font-variation-settings-def -/// https://drafts.csswg.org/css-fonts-4/#descdef-font-face-font-feature-settings -/// -#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToComputedValue)] -pub struct FontTag(pub u32); - -impl Parse for FontTag { - fn parse<'i, 't>( - _context: &ParserContext, - input: &mut Parser<'i, 't>, - ) -> Result> { - use byteorder::{ReadBytesExt, BigEndian}; - use std::io::Cursor; - - let location = input.current_source_location(); - let tag = input.expect_string()?; - - // allowed strings of length 4 containing chars: - if tag.len() != 4 || tag.as_bytes().iter().any(|c| *c < b' ' || *c > b'~') { - return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError)) - } - - let mut raw = Cursor::new(tag.as_bytes()); - Ok(FontTag(raw.read_u32::().unwrap())) - } -} - -impl ToCss for FontTag { - fn to_css(&self, dest: &mut CssWriter) -> fmt::Result - where - W: Write, - { - use byteorder::{BigEndian, ByteOrder}; - use std::str; - - let mut raw = [0u8; 4]; - BigEndian::write_u32(&mut raw, self.0); - str::from_utf8(&raw).unwrap_or_default().to_css(dest) - } -} - /// This property provides low-level control over OpenType or TrueType font /// variations. pub type FontVariationSettings = FontSettings>;