diff options
-rw-r--r-- | components/canvas/canvas_data.rs | 10 | ||||
-rw-r--r-- | components/gfx/font_template.rs | 12 | ||||
-rw-r--r-- | components/gfx/platform/freetype/font.rs | 13 | ||||
-rw-r--r-- | components/gfx/platform/macos/font.rs | 12 | ||||
-rw-r--r-- | components/gfx/platform/windows/font.rs | 74 | ||||
-rw-r--r-- | components/gfx/tests/font_context.rs | 9 | ||||
-rw-r--r-- | components/gfx/tests/font_template.rs | 23 | ||||
-rw-r--r-- | components/script/canvas_state.rs | 2 | ||||
-rw-r--r-- | components/style/values/computed/font.rs | 7 |
9 files changed, 73 insertions, 89 deletions
diff --git a/components/canvas/canvas_data.rs b/components/canvas/canvas_data.rs index d75ef2529f0..778851524ba 100644 --- a/components/canvas/canvas_data.rs +++ b/components/canvas/canvas_data.rs @@ -1412,15 +1412,15 @@ fn load_system_font_from_style(font_style: Option<&FontStyleStruct>) -> Option<F .collect::<Vec<_>>(); let properties = properties .style(match style.font_style { - font::FontStyle::Normal => Style::Normal, - font::FontStyle::Italic => Style::Italic, - font::FontStyle::Oblique(..) => { + font::FontStyle::NORMAL => Style::Normal, + font::FontStyle::ITALIC => Style::Italic, + _ => { // TODO: support oblique angle. Style::Oblique }, }) - .weight(Weight(style.font_weight.0)) - .stretch(Stretch(style.font_stretch.value())); + .weight(Weight(style.font_weight.value())) + .stretch(Stretch(style.font_stretch.to_percentage().0)); let font_handle = match SystemSource::new().select_best_match(&family_names, &properties) { Ok(handle) => handle, Err(e) => { diff --git a/components/gfx/font_template.rs b/components/gfx/font_template.rs index 6de6bb061b3..3ac7026c29a 100644 --- a/components/gfx/font_template.rs +++ b/components/gfx/font_template.rs @@ -34,12 +34,10 @@ pub struct FontTemplateDescriptor { impl Eq for FontTemplateDescriptor {} fn style_to_number(s: &FontStyle) -> f32 { - use style::values::generics::font::FontStyle as GenericFontStyle; - match *s { - GenericFontStyle::Normal => 0., - GenericFontStyle::Italic => FontStyle::default_angle().0.degrees(), - GenericFontStyle::Oblique(ref angle) => angle.0.degrees(), + FontStyle::NORMAL => 0., + FontStyle::ITALIC => FontStyle::DEFAULT_OBLIQUE_DEGREES as f32, + _ => s.oblique_degrees(), } } @@ -66,9 +64,9 @@ impl FontTemplateDescriptor { // between -90 and +90deg. let style_part = (style_to_number(&self.style) - style_to_number(&other.style)).abs(); // 0 <= weightPart <= 800 - let weight_part = (self.weight.0 - other.weight.0).abs(); + let weight_part = (self.weight.value() - other.weight.value()).abs(); // 0 <= stretchPart <= 8 - let stretch_part = (self.stretch.value() - other.stretch.value()).abs(); + let stretch_part = (self.stretch.to_percentage().0 - other.stretch.to_percentage().0).abs(); style_part + weight_part + stretch_part } } diff --git a/components/gfx/platform/freetype/font.rs b/components/gfx/platform/freetype/font.rs index 536b223cf2b..34e98df2cb7 100644 --- a/components/gfx/platform/freetype/font.rs +++ b/components/gfx/platform/freetype/font.rs @@ -186,11 +186,10 @@ impl FontHandleMethods for FontHandle { } fn style(&self) -> FontStyle { - use style::values::generics::font::FontStyle::*; if unsafe { (*self.face).style_flags & FT_STYLE_FLAG_ITALIC as c_long != 0 } { - Italic + FontStyle::ITALIC } else { - Normal + FontStyle::NORMAL } } @@ -200,13 +199,12 @@ impl FontHandleMethods for FontHandle { Some(os2) => os2, }; let weight = os2.us_weight_class as f32; - FontWeight(weight.max(1.).min(1000.)) + FontWeight::from_float(weight) } fn stretchiness(&self) -> FontStretch { - use style::values::generics::NonNegative; use style::values::specified::font::FontStretchKeyword; - let percentage = if let Some(os2) = self.os2_table() { + if let Some(os2) = self.os2_table() { match os2.us_width_class { 1 => FontStretchKeyword::UltraCondensed, 2 => FontStretchKeyword::ExtraCondensed, @@ -222,8 +220,7 @@ impl FontHandleMethods for FontHandle { } else { FontStretchKeyword::Normal } - .compute(); - FontStretch(NonNegative(percentage)) + .compute() } fn glyph_index(&self, codepoint: char) -> Option<GlyphId> { diff --git a/components/gfx/platform/macos/font.rs b/components/gfx/platform/macos/font.rs index eaf8c674c87..552004d98ed 100644 --- a/components/gfx/platform/macos/font.rs +++ b/components/gfx/platform/macos/font.rs @@ -207,11 +207,10 @@ impl FontHandleMethods for FontHandle { } fn style(&self) -> FontStyle { - use style::values::generics::font::FontStyle::*; if self.ctfont.symbolic_traits().is_italic() { - Italic + FontStyle::ITALIC } else { - Normal + FontStyle::NORMAL } } @@ -225,15 +224,12 @@ impl FontHandleMethods for FontHandle { } else { 4.0 + normalized * 5.0 // [4.0, 9.0] }; // [1.0, 9.0], centered on 4.0 - FontWeight(normalized as f32 * 100.) + FontWeight::from_float(normalized as f32 * 100.) } fn stretchiness(&self) -> FontStretch { - use style::values::computed::Percentage; - use style::values::generics::NonNegative; - let normalized = self.ctfont.all_traits().normalized_width(); // [-1.0, 1.0] - FontStretch(NonNegative(Percentage(normalized as f32 + 1.0))) + FontStretch::from_percentage(normalized as f32 + 1.0) } fn glyph_index(&self, codepoint: char) -> Option<GlyphId> { diff --git a/components/gfx/platform/windows/font.rs b/components/gfx/platform/windows/font.rs index 74cfab4ca73..164111ff1e0 100644 --- a/components/gfx/platform/windows/font.rs +++ b/components/gfx/platform/windows/font.rs @@ -17,8 +17,6 @@ use servo_atoms::Atom; use style::computed_values::font_stretch::T as StyleFontStretch; use style::computed_values::font_weight::T as StyleFontWeight; use style::values::computed::font::FontStyle as StyleFontStyle; -use style::values::generics::font::FontStyle as GenericFontStyle; -use style::values::generics::NonNegative; use style::values::specified::font::FontStretchKeyword; use crate::font::{ @@ -140,28 +138,26 @@ impl FontInfo { }, }; - let weight = StyleFontWeight(weight_val as f32); - - let stretch = StyleFontStretch(NonNegative( - match min(9, max(1, width_val)) { - 1 => FontStretchKeyword::UltraCondensed, - 2 => FontStretchKeyword::ExtraCondensed, - 3 => FontStretchKeyword::Condensed, - 4 => FontStretchKeyword::SemiCondensed, - 5 => FontStretchKeyword::Normal, - 6 => FontStretchKeyword::SemiExpanded, - 7 => FontStretchKeyword::Expanded, - 8 => FontStretchKeyword::ExtraExpanded, - 9 => FontStretchKeyword::UltraExpanded, - _ => return Err(()), - } - .compute(), - )); + let weight = StyleFontWeight::from_float(weight_val as f32); + + let stretch = match min(9, max(1, width_val)) { + 1 => FontStretchKeyword::UltraCondensed, + 2 => FontStretchKeyword::ExtraCondensed, + 3 => FontStretchKeyword::Condensed, + 4 => FontStretchKeyword::SemiCondensed, + 5 => FontStretchKeyword::Normal, + 6 => FontStretchKeyword::SemiExpanded, + 7 => FontStretchKeyword::Expanded, + 8 => FontStretchKeyword::ExtraExpanded, + 9 => FontStretchKeyword::UltraExpanded, + _ => return Err(()), + } + .compute(); let style = if italic_bool { - GenericFontStyle::Italic + StyleFontStyle::ITALIC } else { - GenericFontStyle::Normal + StyleFontStyle::NORMAL }; Ok(FontInfo { @@ -175,26 +171,24 @@ impl FontInfo { fn new_from_font(font: &Font) -> Result<FontInfo, ()> { let style = match font.style() { - FontStyle::Normal => GenericFontStyle::Normal, - FontStyle::Oblique => GenericFontStyle::Oblique(StyleFontStyle::default_angle()), - FontStyle::Italic => GenericFontStyle::Italic, + FontStyle::Normal => StyleFontStyle::NORMAL, + FontStyle::Oblique => StyleFontStyle::OBLIQUE, + FontStyle::Italic => StyleFontStyle::ITALIC, }; - let weight = StyleFontWeight(font.weight().to_u32() as f32); - let stretch = StyleFontStretch(NonNegative( - match font.stretch() { - FontStretch::Undefined => FontStretchKeyword::Normal, - FontStretch::UltraCondensed => FontStretchKeyword::UltraCondensed, - FontStretch::ExtraCondensed => FontStretchKeyword::ExtraCondensed, - FontStretch::Condensed => FontStretchKeyword::Condensed, - FontStretch::SemiCondensed => FontStretchKeyword::SemiCondensed, - FontStretch::Normal => FontStretchKeyword::Normal, - FontStretch::SemiExpanded => FontStretchKeyword::SemiExpanded, - FontStretch::Expanded => FontStretchKeyword::Expanded, - FontStretch::ExtraExpanded => FontStretchKeyword::ExtraExpanded, - FontStretch::UltraExpanded => FontStretchKeyword::UltraExpanded, - } - .compute(), - )); + let weight = StyleFontWeight::from_float(font.weight().to_u32() as f32); + let stretch = match font.stretch() { + FontStretch::Undefined => FontStretchKeyword::Normal, + FontStretch::UltraCondensed => FontStretchKeyword::UltraCondensed, + FontStretch::ExtraCondensed => FontStretchKeyword::ExtraCondensed, + FontStretch::Condensed => FontStretchKeyword::Condensed, + FontStretch::SemiCondensed => FontStretchKeyword::SemiCondensed, + FontStretch::Normal => FontStretchKeyword::Normal, + FontStretch::SemiExpanded => FontStretchKeyword::SemiExpanded, + FontStretch::Expanded => FontStretchKeyword::Expanded, + FontStretch::ExtraExpanded => FontStretchKeyword::ExtraExpanded, + FontStretch::UltraExpanded => FontStretchKeyword::UltraExpanded, + } + .compute(); Ok(FontInfo { family_name: font.family_name(), diff --git a/components/gfx/tests/font_context.rs b/components/gfx/tests/font_context.rs index 4669b66141b..035a80ea7c3 100644 --- a/components/gfx/tests/font_context.rs +++ b/components/gfx/tests/font_context.rs @@ -21,10 +21,9 @@ use servo_atoms::Atom; use style::properties::longhands::font_variant_caps::computed_value::T as FontVariantCaps; use style::properties::style_structs::Font as FontStyleStruct; use style::values::computed::font::{ - FamilyName, FontFamily, FontFamilyList, FontFamilyNameSyntax, FontSize, FontStretch, + FamilyName, FontFamily, FontFamilyList, FontFamilyNameSyntax, FontSize, FontStretch, FontStyle, FontWeight, SingleFontFamily, }; -use style::values::generics::font::FontStyle; use webrender_api::{FontInstanceKey, FontKey, IdNamespace}; struct TestFontSource { @@ -95,7 +94,7 @@ impl FontSource for TestFontSource { fn style() -> FontStyleStruct { let mut style = FontStyleStruct { font_family: FontFamily::serif(), - font_style: FontStyle::Normal, + font_style: FontStyle::NORMAL, font_variant_caps: FontVariantCaps::Normal, font_weight: FontWeight::normal(), font_size: FontSize::medium(), @@ -134,7 +133,7 @@ fn test_font_group_is_cached_by_style() { let style1 = style(); let mut style2 = style(); - style2.set_font_style(FontStyle::Italic); + style2.set_font_style(FontStyle::ITALIC); assert_eq!( context.font_group(Arc::new(style1.clone())).as_ptr(), @@ -231,7 +230,7 @@ fn test_font_template_is_cached() { template_descriptor: FontTemplateDescriptor { weight: FontWeight::normal(), stretch: FontStretch::hundred(), - style: FontStyle::Normal, + style: FontStyle::normal(), }, variant: FontVariantCaps::Normal, pt_size: Au(10), diff --git a/components/gfx/tests/font_template.rs b/components/gfx/tests/font_template.rs index 46dbdb88e0d..eecaacbd695 100644 --- a/components/gfx/tests/font_template.rs +++ b/components/gfx/tests/font_template.rs @@ -13,10 +13,7 @@ fn test_font_template_descriptor() { use gfx::font_context::FontContextHandle; use gfx::font_template::{FontTemplate, FontTemplateDescriptor}; use servo_atoms::Atom; - use style::values::computed::font::{FontStretch, FontWeight}; - use style::values::computed::Percentage; - use style::values::generics::font::FontStyle; - use style::values::generics::NonNegative; + use style::values::computed::font::{FontStretch, FontStyle, FontWeight}; fn descriptor(filename: &str) -> FontTemplateDescriptor { let mut path: PathBuf = [ @@ -46,36 +43,36 @@ fn test_font_template_descriptor() { assert_eq!( descriptor("DejaVuSans"), FontTemplateDescriptor { - weight: FontWeight::normal(), + weight: FontWeight::NORMAL, stretch: FontStretch::hundred(), - style: FontStyle::Normal, + style: FontStyle::NORMAL, } ); assert_eq!( descriptor("DejaVuSans-Bold"), FontTemplateDescriptor { - weight: FontWeight::bold(), + weight: FontWeight::BOLD, stretch: FontStretch::hundred(), - style: FontStyle::Normal, + style: FontStyle::NORMAL, } ); assert_eq!( descriptor("DejaVuSans-Oblique"), FontTemplateDescriptor { - weight: FontWeight::normal(), + weight: FontWeight::NORMAL, stretch: FontStretch::hundred(), - style: FontStyle::Italic, + style: FontStyle::ITALIC, } ); assert_eq!( descriptor("DejaVuSansCondensed-BoldOblique"), FontTemplateDescriptor { - weight: FontWeight::bold(), - stretch: FontStretch(NonNegative(Percentage(0.875))), - style: FontStyle::Italic, + weight: FontWeight::BOLD, + stretch: FontStretch::from_percentage(0.875), + style: FontStyle::ITALIC, } ); } diff --git a/components/script/canvas_state.rs b/components/script/canvas_state.rs index a376a56b737..646e8601b1e 100644 --- a/components/script/canvas_state.rs +++ b/components/script/canvas_state.rs @@ -1779,7 +1779,7 @@ fn serialize_font<W>(style: &Font, dest: &mut W) -> fmt::Result where W: fmt::Write, { - if style.font_style == FontStyle::Italic { + if style.font_style == FontStyle::ITALIC { write!(dest, "{} ", style.font_style.to_css_string())?; } if style.font_weight.is_bold() { diff --git a/components/style/values/computed/font.rs b/components/style/values/computed/font.rs index 9f8176d0fc6..20d80ee2e38 100644 --- a/components/style/values/computed/font.rs +++ b/components/style/values/computed/font.rs @@ -48,8 +48,9 @@ pub use crate::values::specified::Integer as SpecifiedInteger; /// cbindgen:derive-gte #[repr(C)] #[derive( - Clone, ComputeSquaredDistance, Copy, Debug, Hash, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue, + Clone, ComputeSquaredDistance, Copy, Debug, Eq, Hash, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue, )] +#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))] pub struct FixedPoint<T, const FRACTION_BITS: u16> { value: T, } @@ -901,8 +902,9 @@ pub type FontStyleFixedPoint = FixedPoint<i16, FONT_STYLE_FRACTION_BITS>; /// cbindgen:derive-gt /// cbindgen:derive-gte #[derive( - Clone, ComputeSquaredDistance, Copy, Debug, Hash, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue, + Clone, ComputeSquaredDistance, Copy, Debug, Eq, Hash, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue, )] +#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))] #[repr(C)] pub struct FontStyle(FontStyleFixedPoint); @@ -1012,6 +1014,7 @@ pub type FontStretchFixedPoint = FixedPoint<u16, FONT_STRETCH_FRACTION_BITS>; #[derive( Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue, )] +#[cfg_attr(feature = "servo", derive(Deserialize, Hash, Serialize))] #[repr(C)] pub struct FontStretch(pub FontStretchFixedPoint); |