Make KeywordInfo generic

This commit is contained in:
Anthony Ramine 2018-02-15 10:26:36 +01:00
parent aea66a9fb6
commit b17fea1d7f
4 changed files with 131 additions and 109 deletions

View file

@ -2389,7 +2389,7 @@ fn static_assert() {
}
pub fn set_font_size(&mut self, v: FontSize) {
use values::specified::font::KeywordSize;
use values::generics::font::KeywordSize;
self.gecko.mSize = v.size().0;
self.gecko.mScriptUnconstrainedSize = v.size().0;
if let Some(info) = v.keyword_info {
@ -2601,8 +2601,7 @@ fn static_assert() {
}
pub fn clone_font_size(&self) -> FontSize {
use values::computed::font::KeywordInfo;
use values::specified::font::KeywordSize;
use values::generics::font::{KeywordInfo, KeywordSize};
let size = Au(self.gecko.mSize).into();
let kw = match self.gecko.mFontSizeKeyword as u32 {
structs::NS_STYLE_FONT_SIZE_XXSMALL => KeywordSize::XXSmall,

View file

@ -23,7 +23,8 @@ use style_traits::{CssWriter, ParseError, ToCss};
use values::CSSFloat;
use values::animated::{ToAnimatedValue, ToAnimatedZero};
use values::computed::{Context, NonNegativeLength, ToComputedValue, Integer, Number};
use values::generics::font::{FontSettings, FeatureTagValue, VariationValue};
use values::generics::font::{FontSettings, FeatureTagValue};
use values::generics::font::{KeywordInfo as GenericKeywordInfo, VariationValue};
use values::specified::font as specified;
use values::specified::length::{FontBaseSize, NoCalcLength};
@ -49,50 +50,8 @@ pub struct FontSize {
pub keyword_info: Option<KeywordInfo>,
}
#[derive(Animate, ComputeSquaredDistance, MallocSizeOf, ToAnimatedValue, ToAnimatedZero)]
#[derive(Clone, Copy, Debug, PartialEq)]
/// Additional information for keyword-derived font sizes.
pub struct KeywordInfo {
/// The keyword used
pub kw: specified::KeywordSize,
/// A factor to be multiplied by the computed size of the keyword
pub factor: f32,
/// An additional Au offset to add to the kw*factor in the case of calcs
pub offset: NonNegativeLength,
}
impl KeywordInfo {
/// Computes the final size for this font-size keyword, accounting for
/// text-zoom.
pub fn to_computed_value(&self, context: &Context) -> NonNegativeLength {
let base = context.maybe_zoom_text(self.kw.to_computed_value(context));
base.scale_by(self.factor) + context.maybe_zoom_text(self.offset)
}
/// Given a parent keyword info (self), apply an additional factor/offset to it
pub fn compose(self, factor: f32, offset: NonNegativeLength) -> Self {
KeywordInfo {
kw: self.kw,
factor: self.factor * factor,
offset: self.offset.scale_by(factor) + offset,
}
}
/// KeywordInfo value for font-size: medium
pub fn medium() -> Self {
specified::KeywordSize::Medium.into()
}
}
impl From<specified::KeywordSize> for KeywordInfo {
fn from(x: specified::KeywordSize) -> Self {
KeywordInfo {
kw: x,
factor: 1.,
offset: Au(0).into(),
}
}
}
/// Additional information for computed keyword-derived font sizes.
pub type KeywordInfo = GenericKeywordInfo<NonNegativeLength>;
impl FontWeight {
/// Value for normal

View file

@ -4,6 +4,7 @@
//! Generic types for font stuff.
use app_units::Au;
use byteorder::{ReadBytesExt, BigEndian};
use cssparser::Parser;
use num_traits::One;
@ -159,3 +160,103 @@ impl Parse for FontTag {
Ok(FontTag(raw.read_u32::<BigEndian>().unwrap()))
}
}
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf)]
#[derive(PartialEq, ToAnimatedValue, ToAnimatedZero)]
/// Additional information for keyword-derived font sizes.
pub struct KeywordInfo<Length> {
/// The keyword used
pub kw: KeywordSize,
/// A factor to be multiplied by the computed size of the keyword
pub factor: f32,
/// An additional Au offset to add to the kw*factor in the case of calcs
pub offset: Length,
}
impl<L> KeywordInfo<L>
where
Au: Into<L>,
{
/// KeywordInfo value for font-size: medium
pub fn medium() -> Self {
KeywordSize::Medium.into()
}
}
impl<L> From<KeywordSize> for KeywordInfo<L>
where
Au: Into<L>,
{
fn from(x: KeywordSize) -> Self {
KeywordInfo {
kw: x,
factor: 1.,
offset: Au(0).into(),
}
}
}
/// CSS font keywords
#[derive(Animate, ComputeSquaredDistance, MallocSizeOf, ToAnimatedValue, ToAnimatedZero)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(missing_docs)]
pub enum KeywordSize {
XXSmall = 1, // This is to enable the NonZero optimization
// which simplifies the representation of Option<KeywordSize>
// in bindgen
XSmall,
Small,
Medium,
Large,
XLarge,
XXLarge,
// This is not a real font keyword and will not parse
// HTML font-size 7 corresponds to this value
XXXLarge,
}
impl KeywordSize {
/// Convert to an HTML <font size> value
pub fn html_size(&self) -> u8 {
match *self {
KeywordSize::XXSmall => 0,
KeywordSize::XSmall => 1,
KeywordSize::Small => 2,
KeywordSize::Medium => 3,
KeywordSize::Large => 4,
KeywordSize::XLarge => 5,
KeywordSize::XXLarge => 6,
KeywordSize::XXXLarge => 7,
}
}
}
impl Default for KeywordSize {
fn default() -> Self {
KeywordSize::Medium
}
}
impl ToCss for KeywordSize {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
dest.write_str(match *self {
KeywordSize::XXSmall => "xx-small",
KeywordSize::XSmall => "x-small",
KeywordSize::Small => "small",
KeywordSize::Medium => "medium",
KeywordSize::Large => "large",
KeywordSize::XLarge => "x-large",
KeywordSize::XXLarge => "xx-large",
KeywordSize::XXXLarge => {
debug_assert!(
false,
"We should never serialize specified values set via HTML presentation attributes"
);
"-servo-xxx-large"
},
})
}
}

View file

@ -21,7 +21,8 @@ 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, FontTag, VariationValue};
use values::generics::font::{FontSettings, FontTag, FeatureTagValue};
use values::generics::font::{KeywordInfo as GenericKeywordInfo, KeywordSize, VariationValue};
use values::specified::{AllowQuirks, Integer, LengthOrPercentage, NoCalcLength, Number};
use values::specified::length::{AU_PER_PT, AU_PER_PX, FontBaseSize};
@ -134,7 +135,7 @@ pub enum FontSize {
/// go into the ratio, and the remaining units all computed together
/// will go into the offset.
/// See bug 1355707.
Keyword(computed::KeywordInfo),
Keyword(KeywordInfo),
/// font-size: smaller
Smaller,
/// font-size: larger
@ -340,27 +341,29 @@ impl Parse for FontSizeAdjust {
}
}
/// CSS font keywords
#[derive(Animate, ComputeSquaredDistance, MallocSizeOf, ToAnimatedValue, ToAnimatedZero)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(missing_docs)]
pub enum KeywordSize {
XXSmall = 1, // This is to enable the NonZero optimization
// which simplifies the representation of Option<KeywordSize>
// in bindgen
XSmall,
Small,
Medium,
Large,
XLarge,
XXLarge,
// This is not a real font keyword and will not parse
// HTML font-size 7 corresponds to this value
XXXLarge,
/// Additional information for specified keyword-derived font sizes.
pub type KeywordInfo = GenericKeywordInfo<NonNegativeLength>;
impl KeywordInfo {
/// Computes the final size for this font-size keyword, accounting for
/// text-zoom.
pub fn to_computed_value(&self, context: &Context) -> NonNegativeLength {
let base = context.maybe_zoom_text(self.kw.to_computed_value(context));
base.scale_by(self.factor) + context.maybe_zoom_text(self.offset)
}
/// Given a parent keyword info (self), apply an additional factor/offset to it
pub fn compose(self, factor: f32, offset: NonNegativeLength) -> Self {
KeywordInfo {
kw: self.kw,
factor: self.factor * factor,
offset: self.offset.scale_by(factor) + offset,
}
}
}
impl KeywordSize {
/// Parse a keyword size
/// Parses a keyword size.
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
try_match_ident_ignore_ascii_case! { input,
"xx-small" => Ok(KeywordSize::XXSmall),
@ -372,46 +375,6 @@ impl KeywordSize {
"xx-large" => Ok(KeywordSize::XXLarge),
}
}
/// Convert to an HTML <font size> value
pub fn html_size(&self) -> u8 {
match *self {
KeywordSize::XXSmall => 0,
KeywordSize::XSmall => 1,
KeywordSize::Small => 2,
KeywordSize::Medium => 3,
KeywordSize::Large => 4,
KeywordSize::XLarge => 5,
KeywordSize::XXLarge => 6,
KeywordSize::XXXLarge => 7,
}
}
}
impl Default for KeywordSize {
fn default() -> Self {
KeywordSize::Medium
}
}
impl ToCss for KeywordSize {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
dest.write_str(match *self {
KeywordSize::XXSmall => "xx-small",
KeywordSize::XSmall => "x-small",
KeywordSize::Small => "small",
KeywordSize::Medium => "medium",
KeywordSize::Large => "large",
KeywordSize::XLarge => "x-large",
KeywordSize::XXLarge => "xx-large",
KeywordSize::XXXLarge => unreachable!("We should never serialize \
specified values set via
HTML presentation attributes"),
})
}
}
/// This is the ratio applied for font-size: larger
@ -672,7 +635,7 @@ impl FontSize {
#[inline]
/// Get initial value for specified font size.
pub fn medium() -> Self {
FontSize::Keyword(computed::KeywordInfo::medium())
FontSize::Keyword(KeywordInfo::medium())
}
/// Parses a font-size, with quirks.