style: Cleanup (a bit) system font copypasta.

If I had to write that again I would've killed myself :).

This is still not perfect, and the system font code is still quite a mess, but
well, little steps.

Bug: 1455358
Reviewed-by: xidorn
MozReview-Commit-ID: BmrZlCSejo7
This commit is contained in:
Emilio Cobos Álvarez 2018-04-19 22:01:15 +02:00
parent 0a8518b452
commit b2722e965c
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C

View file

@ -26,6 +26,41 @@ use values::generics::font::{KeywordInfo as GenericKeywordInfo, KeywordSize, Var
use values::specified::{AllowQuirks, Angle, Integer, LengthOrPercentage, NoCalcLength, Number, Percentage}; use values::specified::{AllowQuirks, Angle, Integer, LengthOrPercentage, NoCalcLength, Number, Percentage};
use values::specified::length::{FontBaseSize, AU_PER_PT, AU_PER_PX}; use values::specified::length::{FontBaseSize, AU_PER_PT, AU_PER_PX};
// FIXME(emilio): The system font code is copy-pasta, and should be cleaned up.
macro_rules! system_font_methods {
($ty:ident, $field:ident) => {
system_font_methods!($ty);
fn compute_system(&self, _context: &Context) -> <$ty as ToComputedValue>::ComputedValue {
debug_assert!(matches!(*self, $ty::System(..)));
#[cfg(feature = "gecko")]
{
_context.cached_system_font.as_ref().unwrap().$field.clone()
}
#[cfg(feature = "servo")]
{
unreachable!()
}
}
};
($ty:ident) => {
/// Get a specified value that represents a system font.
pub fn system_font(f: SystemFont) -> Self {
$ty::System(f)
}
/// Retreive a SystemFont from the specified value.
pub fn get_system(&self) -> Option<SystemFont> {
if let $ty::System(s) = *self {
Some(s)
} else {
None
}
}
}
}
const DEFAULT_SCRIPT_MIN_SIZE_PT: u32 = 8; const DEFAULT_SCRIPT_MIN_SIZE_PT: u32 = 8;
const DEFAULT_SCRIPT_SIZE_MULTIPLIER: f64 = 0.71; const DEFAULT_SCRIPT_SIZE_MULTIPLIER: f64 = 0.71;
@ -55,6 +90,8 @@ pub enum FontWeight {
} }
impl FontWeight { impl FontWeight {
system_font_methods!(FontWeight, font_weight);
/// `normal` /// `normal`
#[inline] #[inline]
pub fn normal() -> Self { pub fn normal() -> Self {
@ -67,20 +104,6 @@ impl FontWeight {
debug_assert!(kw as f32 <= MAX_FONT_WEIGHT); debug_assert!(kw as f32 <= MAX_FONT_WEIGHT);
FontWeight::Absolute(AbsoluteFontWeight::Weight(Number::new(kw as f32))) FontWeight::Absolute(AbsoluteFontWeight::Weight(Number::new(kw as f32)))
} }
/// Get a specified FontWeight from a SystemFont
pub fn system_font(f: SystemFont) -> Self {
FontWeight::System(f)
}
/// Retreive a SystemFont from FontWeight
pub fn get_system(&self) -> Option<SystemFont> {
if let FontWeight::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl Parse for FontWeight { impl Parse for FontWeight {
@ -116,15 +139,7 @@ impl ToComputedValue for FontWeight {
.get_parent_font() .get_parent_font()
.clone_font_weight() .clone_font_weight()
.lighter(), .lighter(),
#[cfg(feature = "gecko")] FontWeight::System(_) => self.compute_system(context),
FontWeight::System(_) => context
.cached_system_font
.as_ref()
.unwrap()
.font_weight
.clone(),
#[cfg(not(feature = "gecko"))]
FontWeight::System(_) => unreachable!(),
} }
} }
@ -332,19 +347,7 @@ impl FontStyle {
FontStyle::Specified(generics::FontStyle::Normal) FontStyle::Specified(generics::FontStyle::Normal)
} }
/// More system font copy-pasta. system_font_methods!(FontStyle, font_style);
pub fn system_font(f: SystemFont) -> Self {
FontStyle::System(f)
}
/// Retreive a SystemFont from FontStyle.
pub fn get_system(&self) -> Option<SystemFont> {
if let FontStyle::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl ToComputedValue for FontStyle { impl ToComputedValue for FontStyle {
@ -353,15 +356,7 @@ impl ToComputedValue for FontStyle {
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self { match *self {
FontStyle::Specified(ref specified) => specified.to_computed_value(context), FontStyle::Specified(ref specified) => specified.to_computed_value(context),
#[cfg(feature = "gecko")] FontStyle::System(..) => self.compute_system(context),
FontStyle::System(..) => context
.cached_system_font
.as_ref()
.unwrap()
.font_style
.clone(),
#[cfg(not(feature = "gecko"))]
FontStyle::System(_) => unreachable!(),
} }
} }
@ -431,21 +426,7 @@ impl FontStretch {
FontStretch::Keyword(FontStretchKeyword::Normal) FontStretch::Keyword(FontStretchKeyword::Normal)
} }
/// Get a specified FontStretch from a SystemFont. system_font_methods!(FontStretch, font_stretch);
///
/// FIXME(emilio): All this system font stuff is copy-pasta. :(
pub fn system_font(f: SystemFont) -> Self {
FontStretch::System(f)
}
/// Retreive a SystemFont from FontStretch.
pub fn get_system(&self) -> Option<SystemFont> {
if let FontStretch::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl Parse for FontStretch { impl Parse for FontStretch {
@ -476,15 +457,7 @@ impl ToComputedValue for FontStretch {
FontStretch::Keyword(ref kw) => { FontStretch::Keyword(ref kw) => {
NonNegative(kw.compute()) NonNegative(kw.compute())
}, },
#[cfg(feature = "gecko")] FontStretch::System(_) => self.compute_system(context),
FontStretch::System(_) => context
.cached_system_font
.as_ref()
.unwrap()
.font_stretch
.clone(),
#[cfg(not(feature = "gecko"))]
FontStretch::System(_) => unreachable!(),
} }
} }
@ -534,19 +507,7 @@ pub enum FontFamily {
} }
impl FontFamily { impl FontFamily {
/// Get `font-family` with system font system_font_methods!(FontFamily, font_family);
pub fn system_font(f: SystemFont) -> Self {
FontFamily::System(f)
}
/// Get system font
pub fn get_system(&self) -> Option<SystemFont> {
if let FontFamily::System(s) = *self {
Some(s)
} else {
None
}
}
/// Parse a specified font-family value /// Parse a specified font-family value
pub fn parse_specified<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { pub fn parse_specified<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
@ -568,19 +529,10 @@ impl FontFamily {
impl ToComputedValue for FontFamily { impl ToComputedValue for FontFamily {
type ComputedValue = computed::FontFamily; type ComputedValue = computed::FontFamily;
fn to_computed_value(&self, _cx: &Context) -> Self::ComputedValue { fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self { match *self {
FontFamily::Values(ref v) => computed::FontFamily(v.clone()), FontFamily::Values(ref v) => computed::FontFamily(v.clone()),
FontFamily::System(_) => { FontFamily::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
_cx.cached_system_font.as_ref().unwrap().font_family.clone()
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }
@ -650,19 +602,7 @@ impl FontSizeAdjust {
FontSizeAdjust::None FontSizeAdjust::None
} }
/// Get font-size-adjust with SystemFont system_font_methods!(FontSizeAdjust, font_size_adjust);
pub fn system_font(f: SystemFont) -> Self {
FontSizeAdjust::System(f)
}
/// Get SystemFont variant
pub fn get_system(&self) -> Option<SystemFont> {
if let FontSizeAdjust::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl ToComputedValue for FontSizeAdjust { impl ToComputedValue for FontSizeAdjust {
@ -674,20 +614,7 @@ impl ToComputedValue for FontSizeAdjust {
FontSizeAdjust::Number(ref n) => { FontSizeAdjust::Number(ref n) => {
computed::FontSizeAdjust::Number(n.to_computed_value(context)) computed::FontSizeAdjust::Number(n.to_computed_value(context))
}, },
FontSizeAdjust::System(_) => { FontSizeAdjust::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
context
.cached_system_font
.as_ref()
.unwrap()
.font_size_adjust
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }
@ -1011,22 +938,10 @@ impl ToComputedValue for FontSize {
} }
impl FontSize { impl FontSize {
/// Construct a system font value. system_font_methods!(FontSize);
pub fn system_font(f: SystemFont) -> Self {
FontSize::System(f)
}
/// Obtain the system font, if any
pub fn get_system(&self) -> Option<SystemFont> {
if let FontSize::System(s) = *self {
Some(s)
} else {
None
}
}
#[inline]
/// Get initial value for specified font size. /// Get initial value for specified font size.
#[inline]
pub fn medium() -> Self { pub fn medium() -> Self {
FontSize::Keyword(KeywordInfo::medium()) FontSize::Keyword(KeywordInfo::medium())
} }
@ -1191,42 +1106,16 @@ impl FontVariantAlternates {
FontVariantAlternates::Value(VariantAlternatesList(vec![].into_boxed_slice())) FontVariantAlternates::Value(VariantAlternatesList(vec![].into_boxed_slice()))
} }
/// Get FontVariantAlternates with system font system_font_methods!(FontVariantAlternates, font_variant_alternates);
pub fn system_font(f: SystemFont) -> Self {
FontVariantAlternates::System(f)
}
/// Get SystemFont of FontVariantAlternates
pub fn get_system(&self) -> Option<SystemFont> {
if let FontVariantAlternates::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl ToComputedValue for FontVariantAlternates { impl ToComputedValue for FontVariantAlternates {
type ComputedValue = computed::FontVariantAlternates; type ComputedValue = computed::FontVariantAlternates;
fn to_computed_value(&self, _context: &Context) -> computed::FontVariantAlternates { fn to_computed_value(&self, context: &Context) -> computed::FontVariantAlternates {
match *self { match *self {
FontVariantAlternates::Value(ref v) => v.clone(), FontVariantAlternates::Value(ref v) => v.clone(),
FontVariantAlternates::System(_) => { FontVariantAlternates::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
_context
.cached_system_font
.as_ref()
.unwrap()
.font_variant_alternates
.clone()
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }
@ -1468,42 +1357,16 @@ impl FontVariantEastAsian {
FontVariantEastAsian::Value(VariantEastAsian::empty()) FontVariantEastAsian::Value(VariantEastAsian::empty())
} }
/// Get `font-variant-east-asian` with system font system_font_methods!(FontVariantEastAsian, font_variant_east_asian);
pub fn system_font(f: SystemFont) -> Self {
FontVariantEastAsian::System(f)
}
/// Get system font
pub fn get_system(&self) -> Option<SystemFont> {
if let FontVariantEastAsian::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl ToComputedValue for FontVariantEastAsian { impl ToComputedValue for FontVariantEastAsian {
type ComputedValue = computed::FontVariantEastAsian; type ComputedValue = computed::FontVariantEastAsian;
fn to_computed_value(&self, _context: &Context) -> computed::FontVariantEastAsian { fn to_computed_value(&self, context: &Context) -> computed::FontVariantEastAsian {
match *self { match *self {
FontVariantEastAsian::Value(ref v) => v.clone(), FontVariantEastAsian::Value(ref v) => v.clone(),
FontVariantEastAsian::System(_) => { FontVariantEastAsian::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
_context
.cached_system_font
.as_ref()
.unwrap()
.font_variant_east_asian
.clone()
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }
@ -1714,22 +1577,10 @@ pub enum FontVariantLigatures {
} }
impl FontVariantLigatures { impl FontVariantLigatures {
/// Get `font-variant-ligatures` with system font system_font_methods!(FontVariantLigatures, font_variant_ligatures);
pub fn system_font(f: SystemFont) -> Self {
FontVariantLigatures::System(f)
}
/// Get system font
pub fn get_system(&self) -> Option<SystemFont> {
if let FontVariantLigatures::System(s) = *self {
Some(s)
} else {
None
}
}
#[inline]
/// Default value of `font-variant-ligatures` as `empty` /// Default value of `font-variant-ligatures` as `empty`
#[inline]
pub fn empty() -> FontVariantLigatures { pub fn empty() -> FontVariantLigatures {
FontVariantLigatures::Value(VariantLigatures::empty()) FontVariantLigatures::Value(VariantLigatures::empty())
} }
@ -1744,24 +1595,10 @@ impl FontVariantLigatures {
impl ToComputedValue for FontVariantLigatures { impl ToComputedValue for FontVariantLigatures {
type ComputedValue = computed::FontVariantLigatures; type ComputedValue = computed::FontVariantLigatures;
fn to_computed_value(&self, _context: &Context) -> computed::FontVariantLigatures { fn to_computed_value(&self, context: &Context) -> computed::FontVariantLigatures {
match *self { match *self {
FontVariantLigatures::Value(ref v) => v.clone(), FontVariantLigatures::Value(ref v) => v.clone(),
FontVariantLigatures::System(_) => { FontVariantLigatures::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
_context
.cached_system_font
.as_ref()
.unwrap()
.font_variant_ligatures
.clone()
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }
@ -1974,42 +1811,16 @@ impl FontVariantNumeric {
FontVariantNumeric::Value(VariantNumeric::empty()) FontVariantNumeric::Value(VariantNumeric::empty())
} }
/// Get `font-variant-numeric` with system font system_font_methods!(FontVariantNumeric, font_variant_numeric);
pub fn system_font(f: SystemFont) -> Self {
FontVariantNumeric::System(f)
}
/// Get system font
pub fn get_system(&self) -> Option<SystemFont> {
if let FontVariantNumeric::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl ToComputedValue for FontVariantNumeric { impl ToComputedValue for FontVariantNumeric {
type ComputedValue = computed::FontVariantNumeric; type ComputedValue = computed::FontVariantNumeric;
fn to_computed_value(&self, _context: &Context) -> computed::FontVariantNumeric { fn to_computed_value(&self, context: &Context) -> computed::FontVariantNumeric {
match *self { match *self {
FontVariantNumeric::Value(ref v) => v.clone(), FontVariantNumeric::Value(ref v) => v.clone(),
FontVariantNumeric::System(_) => { FontVariantNumeric::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
_context
.cached_system_font
.as_ref()
.unwrap()
.font_variant_numeric
.clone()
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }
@ -2107,19 +1918,7 @@ impl FontFeatureSettings {
FontFeatureSettings::Value(FontSettings::normal()) FontFeatureSettings::Value(FontSettings::normal())
} }
/// Get `font-feature-settings` with system font system_font_methods!(FontFeatureSettings, font_feature_settings);
pub fn system_font(f: SystemFont) -> Self {
FontFeatureSettings::System(f)
}
/// Get system font
pub fn get_system(&self) -> Option<SystemFont> {
if let FontFeatureSettings::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl ToComputedValue for FontFeatureSettings { impl ToComputedValue for FontFeatureSettings {
@ -2128,21 +1927,7 @@ impl ToComputedValue for FontFeatureSettings {
fn to_computed_value(&self, context: &Context) -> computed::FontFeatureSettings { fn to_computed_value(&self, context: &Context) -> computed::FontFeatureSettings {
match *self { match *self {
FontFeatureSettings::Value(ref v) => v.to_computed_value(context), FontFeatureSettings::Value(ref v) => v.to_computed_value(context),
FontFeatureSettings::System(_) => { FontFeatureSettings::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
context
.cached_system_font
.as_ref()
.unwrap()
.font_feature_settings
.clone()
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }
@ -2282,26 +2067,14 @@ impl FontLanguageOverride {
FontLanguageOverride::Normal FontLanguageOverride::Normal
} }
/// Get `font-language-override` with `system font` system_font_methods!(FontLanguageOverride, font_language_override);
pub fn system_font(f: SystemFont) -> Self {
FontLanguageOverride::System(f)
}
/// Get system font
pub fn get_system(&self) -> Option<SystemFont> {
if let FontLanguageOverride::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl ToComputedValue for FontLanguageOverride { impl ToComputedValue for FontLanguageOverride {
type ComputedValue = computed::FontLanguageOverride; type ComputedValue = computed::FontLanguageOverride;
#[inline] #[inline]
fn to_computed_value(&self, _context: &Context) -> computed::FontLanguageOverride { fn to_computed_value(&self, context: &Context) -> computed::FontLanguageOverride {
match *self { match *self {
FontLanguageOverride::Normal => computed::FontLanguageOverride(0), FontLanguageOverride::Normal => computed::FontLanguageOverride(0),
FontLanguageOverride::Override(ref lang) => { FontLanguageOverride::Override(ref lang) => {
@ -2315,20 +2088,7 @@ impl ToComputedValue for FontLanguageOverride {
let bytes = computed_lang.into_bytes(); let bytes = computed_lang.into_bytes();
computed::FontLanguageOverride(BigEndian::read_u32(&bytes)) computed::FontLanguageOverride(BigEndian::read_u32(&bytes))
}, },
FontLanguageOverride::System(_) => { FontLanguageOverride::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
_context
.cached_system_font
.as_ref()
.unwrap()
.font_language_override
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }
#[inline] #[inline]
@ -2389,19 +2149,7 @@ impl FontVariationSettings {
FontVariationSettings::Value(FontSettings::normal()) FontVariationSettings::Value(FontSettings::normal())
} }
/// Get `font-variation-settings` with system font system_font_methods!(FontVariationSettings, font_variation_settings);
pub fn system_font(f: SystemFont) -> Self {
FontVariationSettings::System(f)
}
/// Get system font
pub fn get_system(&self) -> Option<SystemFont> {
if let FontVariationSettings::System(s) = *self {
Some(s)
} else {
None
}
}
} }
impl ToComputedValue for FontVariationSettings { impl ToComputedValue for FontVariationSettings {
@ -2410,21 +2158,7 @@ impl ToComputedValue for FontVariationSettings {
fn to_computed_value(&self, context: &Context) -> computed::FontVariationSettings { fn to_computed_value(&self, context: &Context) -> computed::FontVariationSettings {
match *self { match *self {
FontVariationSettings::Value(ref v) => v.to_computed_value(context), FontVariationSettings::Value(ref v) => v.to_computed_value(context),
FontVariationSettings::System(_) => { FontVariationSettings::System(_) => self.compute_system(context),
#[cfg(feature = "gecko")]
{
context
.cached_system_font
.as_ref()
.unwrap()
.font_variation_settings
.clone()
}
#[cfg(feature = "servo")]
{
unreachable!()
}
},
} }
} }