Skip to content

Commit

Permalink
Move FontTag to the generic module
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Feb 15, 2018
1 parent b9862f7 commit aea66a9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 53 deletions.
3 changes: 2 additions & 1 deletion components/style/gecko/rules.rs
Expand Up @@ -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<nsCSSFontFaceRule>;
Expand Down
3 changes: 1 addition & 2 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -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| {
Expand Down
Expand Up @@ -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};

/// <https://drafts.csswg.org/css-transitions/#animtype-repeatable-list>
Expand Down
46 changes: 44 additions & 2 deletions components/style/values/generics/font.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -117,3 +118,44 @@ impl<T: ToCss> ToCss for FontSettings<T> {
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<W>(&self, dest: &mut CssWriter<W>) -> 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<Self, ParseError<'i>> {
let location = input.current_source_location();
let tag = input.expect_string()?;

// allowed strings of length 4 containing chars: <U+20, U+7E>
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::<BigEndian>().unwrap()))
}
}
46 changes: 1 addition & 45 deletions components/style/values/specified/font.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Self, ParseError<'i>> {
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: <U+20, U+7E>
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::<BigEndian>().unwrap()))
}
}

impl ToCss for FontTag {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> 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<VariationValue<Number>>;
Expand Down

0 comments on commit aea66a9

Please sign in to comment.