Further changes required by Servo

This commit is contained in:
Oriol Brufau 2023-08-17 01:23:14 +02:00 committed by Martin Robinson
parent dcafbde256
commit 105050d46d
9 changed files with 72 additions and 88 deletions

View file

@ -1412,15 +1412,15 @@ fn load_system_font_from_style(font_style: Option<&FontStyleStruct>) -> Option<F
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let properties = properties let properties = properties
.style(match style.font_style { .style(match style.font_style {
font::FontStyle::Normal => Style::Normal, font::FontStyle::NORMAL => Style::Normal,
font::FontStyle::Italic => Style::Italic, font::FontStyle::ITALIC => Style::Italic,
font::FontStyle::Oblique(..) => { _ => {
// TODO: support oblique angle. // TODO: support oblique angle.
Style::Oblique Style::Oblique
}, },
}) })
.weight(Weight(style.font_weight.0)) .weight(Weight(style.font_weight.value()))
.stretch(Stretch(style.font_stretch.value())); .stretch(Stretch(style.font_stretch.to_percentage().0));
let font_handle = match SystemSource::new().select_best_match(&family_names, &properties) { let font_handle = match SystemSource::new().select_best_match(&family_names, &properties) {
Ok(handle) => handle, Ok(handle) => handle,
Err(e) => { Err(e) => {

View file

@ -34,12 +34,10 @@ pub struct FontTemplateDescriptor {
impl Eq for FontTemplateDescriptor {} impl Eq for FontTemplateDescriptor {}
fn style_to_number(s: &FontStyle) -> f32 { fn style_to_number(s: &FontStyle) -> f32 {
use style::values::generics::font::FontStyle as GenericFontStyle;
match *s { match *s {
GenericFontStyle::Normal => 0., FontStyle::NORMAL => 0.,
GenericFontStyle::Italic => FontStyle::default_angle().0.degrees(), FontStyle::ITALIC => FontStyle::DEFAULT_OBLIQUE_DEGREES as f32,
GenericFontStyle::Oblique(ref angle) => angle.0.degrees(), _ => s.oblique_degrees(),
} }
} }
@ -66,9 +64,9 @@ impl FontTemplateDescriptor {
// between -90 and +90deg. // between -90 and +90deg.
let style_part = (style_to_number(&self.style) - style_to_number(&other.style)).abs(); let style_part = (style_to_number(&self.style) - style_to_number(&other.style)).abs();
// 0 <= weightPart <= 800 // 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 // 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 style_part + weight_part + stretch_part
} }
} }

View file

@ -186,11 +186,10 @@ impl FontHandleMethods for FontHandle {
} }
fn style(&self) -> FontStyle { fn style(&self) -> FontStyle {
use style::values::generics::font::FontStyle::*;
if unsafe { (*self.face).style_flags & FT_STYLE_FLAG_ITALIC as c_long != 0 } { if unsafe { (*self.face).style_flags & FT_STYLE_FLAG_ITALIC as c_long != 0 } {
Italic FontStyle::ITALIC
} else { } else {
Normal FontStyle::NORMAL
} }
} }
@ -200,13 +199,12 @@ impl FontHandleMethods for FontHandle {
Some(os2) => os2, Some(os2) => os2,
}; };
let weight = os2.us_weight_class as f32; let weight = os2.us_weight_class as f32;
FontWeight(weight.max(1.).min(1000.)) FontWeight::from_float(weight)
} }
fn stretchiness(&self) -> FontStretch { fn stretchiness(&self) -> FontStretch {
use style::values::generics::NonNegative;
use style::values::specified::font::FontStretchKeyword; 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 { match os2.us_width_class {
1 => FontStretchKeyword::UltraCondensed, 1 => FontStretchKeyword::UltraCondensed,
2 => FontStretchKeyword::ExtraCondensed, 2 => FontStretchKeyword::ExtraCondensed,
@ -222,8 +220,7 @@ impl FontHandleMethods for FontHandle {
} else { } else {
FontStretchKeyword::Normal FontStretchKeyword::Normal
} }
.compute(); .compute()
FontStretch(NonNegative(percentage))
} }
fn glyph_index(&self, codepoint: char) -> Option<GlyphId> { fn glyph_index(&self, codepoint: char) -> Option<GlyphId> {

View file

@ -207,11 +207,10 @@ impl FontHandleMethods for FontHandle {
} }
fn style(&self) -> FontStyle { fn style(&self) -> FontStyle {
use style::values::generics::font::FontStyle::*;
if self.ctfont.symbolic_traits().is_italic() { if self.ctfont.symbolic_traits().is_italic() {
Italic FontStyle::ITALIC
} else { } else {
Normal FontStyle::NORMAL
} }
} }
@ -225,15 +224,12 @@ impl FontHandleMethods for FontHandle {
} else { } else {
4.0 + normalized * 5.0 // [4.0, 9.0] 4.0 + normalized * 5.0 // [4.0, 9.0]
}; // [1.0, 9.0], centered on 4.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 { 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] 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> { fn glyph_index(&self, codepoint: char) -> Option<GlyphId> {

View file

@ -17,8 +17,6 @@ use servo_atoms::Atom;
use style::computed_values::font_stretch::T as StyleFontStretch; use style::computed_values::font_stretch::T as StyleFontStretch;
use style::computed_values::font_weight::T as StyleFontWeight; use style::computed_values::font_weight::T as StyleFontWeight;
use style::values::computed::font::FontStyle as StyleFontStyle; 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 style::values::specified::font::FontStretchKeyword;
use crate::font::{ use crate::font::{
@ -140,28 +138,26 @@ impl FontInfo {
}, },
}; };
let weight = StyleFontWeight(weight_val as f32); let weight = StyleFontWeight::from_float(weight_val as f32);
let stretch = StyleFontStretch(NonNegative( let stretch = match min(9, max(1, width_val)) {
match min(9, max(1, width_val)) { 1 => FontStretchKeyword::UltraCondensed,
1 => FontStretchKeyword::UltraCondensed, 2 => FontStretchKeyword::ExtraCondensed,
2 => FontStretchKeyword::ExtraCondensed, 3 => FontStretchKeyword::Condensed,
3 => FontStretchKeyword::Condensed, 4 => FontStretchKeyword::SemiCondensed,
4 => FontStretchKeyword::SemiCondensed, 5 => FontStretchKeyword::Normal,
5 => FontStretchKeyword::Normal, 6 => FontStretchKeyword::SemiExpanded,
6 => FontStretchKeyword::SemiExpanded, 7 => FontStretchKeyword::Expanded,
7 => FontStretchKeyword::Expanded, 8 => FontStretchKeyword::ExtraExpanded,
8 => FontStretchKeyword::ExtraExpanded, 9 => FontStretchKeyword::UltraExpanded,
9 => FontStretchKeyword::UltraExpanded, _ => return Err(()),
_ => return Err(()), }
} .compute();
.compute(),
));
let style = if italic_bool { let style = if italic_bool {
GenericFontStyle::Italic StyleFontStyle::ITALIC
} else { } else {
GenericFontStyle::Normal StyleFontStyle::NORMAL
}; };
Ok(FontInfo { Ok(FontInfo {
@ -175,26 +171,24 @@ impl FontInfo {
fn new_from_font(font: &Font) -> Result<FontInfo, ()> { fn new_from_font(font: &Font) -> Result<FontInfo, ()> {
let style = match font.style() { let style = match font.style() {
FontStyle::Normal => GenericFontStyle::Normal, FontStyle::Normal => StyleFontStyle::NORMAL,
FontStyle::Oblique => GenericFontStyle::Oblique(StyleFontStyle::default_angle()), FontStyle::Oblique => StyleFontStyle::OBLIQUE,
FontStyle::Italic => GenericFontStyle::Italic, FontStyle::Italic => StyleFontStyle::ITALIC,
}; };
let weight = StyleFontWeight(font.weight().to_u32() as f32); let weight = StyleFontWeight::from_float(font.weight().to_u32() as f32);
let stretch = StyleFontStretch(NonNegative( let stretch = match font.stretch() {
match font.stretch() { FontStretch::Undefined => FontStretchKeyword::Normal,
FontStretch::Undefined => FontStretchKeyword::Normal, FontStretch::UltraCondensed => FontStretchKeyword::UltraCondensed,
FontStretch::UltraCondensed => FontStretchKeyword::UltraCondensed, FontStretch::ExtraCondensed => FontStretchKeyword::ExtraCondensed,
FontStretch::ExtraCondensed => FontStretchKeyword::ExtraCondensed, FontStretch::Condensed => FontStretchKeyword::Condensed,
FontStretch::Condensed => FontStretchKeyword::Condensed, FontStretch::SemiCondensed => FontStretchKeyword::SemiCondensed,
FontStretch::SemiCondensed => FontStretchKeyword::SemiCondensed, FontStretch::Normal => FontStretchKeyword::Normal,
FontStretch::Normal => FontStretchKeyword::Normal, FontStretch::SemiExpanded => FontStretchKeyword::SemiExpanded,
FontStretch::SemiExpanded => FontStretchKeyword::SemiExpanded, FontStretch::Expanded => FontStretchKeyword::Expanded,
FontStretch::Expanded => FontStretchKeyword::Expanded, FontStretch::ExtraExpanded => FontStretchKeyword::ExtraExpanded,
FontStretch::ExtraExpanded => FontStretchKeyword::ExtraExpanded, FontStretch::UltraExpanded => FontStretchKeyword::UltraExpanded,
FontStretch::UltraExpanded => FontStretchKeyword::UltraExpanded, }
} .compute();
.compute(),
));
Ok(FontInfo { Ok(FontInfo {
family_name: font.family_name(), family_name: font.family_name(),

View file

@ -21,10 +21,9 @@ use servo_atoms::Atom;
use style::properties::longhands::font_variant_caps::computed_value::T as FontVariantCaps; use style::properties::longhands::font_variant_caps::computed_value::T as FontVariantCaps;
use style::properties::style_structs::Font as FontStyleStruct; use style::properties::style_structs::Font as FontStyleStruct;
use style::values::computed::font::{ use style::values::computed::font::{
FamilyName, FontFamily, FontFamilyList, FontFamilyNameSyntax, FontSize, FontStretch, FamilyName, FontFamily, FontFamilyList, FontFamilyNameSyntax, FontSize, FontStretch, FontStyle,
FontWeight, SingleFontFamily, FontWeight, SingleFontFamily,
}; };
use style::values::generics::font::FontStyle;
use webrender_api::{FontInstanceKey, FontKey, IdNamespace}; use webrender_api::{FontInstanceKey, FontKey, IdNamespace};
struct TestFontSource { struct TestFontSource {
@ -95,7 +94,7 @@ impl FontSource for TestFontSource {
fn style() -> FontStyleStruct { fn style() -> FontStyleStruct {
let mut style = FontStyleStruct { let mut style = FontStyleStruct {
font_family: FontFamily::serif(), font_family: FontFamily::serif(),
font_style: FontStyle::Normal, font_style: FontStyle::NORMAL,
font_variant_caps: FontVariantCaps::Normal, font_variant_caps: FontVariantCaps::Normal,
font_weight: FontWeight::normal(), font_weight: FontWeight::normal(),
font_size: FontSize::medium(), font_size: FontSize::medium(),
@ -134,7 +133,7 @@ fn test_font_group_is_cached_by_style() {
let style1 = style(); let style1 = style();
let mut style2 = style(); let mut style2 = style();
style2.set_font_style(FontStyle::Italic); style2.set_font_style(FontStyle::ITALIC);
assert_eq!( assert_eq!(
context.font_group(Arc::new(style1.clone())).as_ptr(), context.font_group(Arc::new(style1.clone())).as_ptr(),
@ -231,7 +230,7 @@ fn test_font_template_is_cached() {
template_descriptor: FontTemplateDescriptor { template_descriptor: FontTemplateDescriptor {
weight: FontWeight::normal(), weight: FontWeight::normal(),
stretch: FontStretch::hundred(), stretch: FontStretch::hundred(),
style: FontStyle::Normal, style: FontStyle::normal(),
}, },
variant: FontVariantCaps::Normal, variant: FontVariantCaps::Normal,
pt_size: Au(10), pt_size: Au(10),

View file

@ -13,10 +13,7 @@ fn test_font_template_descriptor() {
use gfx::font_context::FontContextHandle; use gfx::font_context::FontContextHandle;
use gfx::font_template::{FontTemplate, FontTemplateDescriptor}; use gfx::font_template::{FontTemplate, FontTemplateDescriptor};
use servo_atoms::Atom; use servo_atoms::Atom;
use style::values::computed::font::{FontStretch, FontWeight}; use style::values::computed::font::{FontStretch, FontStyle, FontWeight};
use style::values::computed::Percentage;
use style::values::generics::font::FontStyle;
use style::values::generics::NonNegative;
fn descriptor(filename: &str) -> FontTemplateDescriptor { fn descriptor(filename: &str) -> FontTemplateDescriptor {
let mut path: PathBuf = [ let mut path: PathBuf = [
@ -46,36 +43,36 @@ fn test_font_template_descriptor() {
assert_eq!( assert_eq!(
descriptor("DejaVuSans"), descriptor("DejaVuSans"),
FontTemplateDescriptor { FontTemplateDescriptor {
weight: FontWeight::normal(), weight: FontWeight::NORMAL,
stretch: FontStretch::hundred(), stretch: FontStretch::hundred(),
style: FontStyle::Normal, style: FontStyle::NORMAL,
} }
); );
assert_eq!( assert_eq!(
descriptor("DejaVuSans-Bold"), descriptor("DejaVuSans-Bold"),
FontTemplateDescriptor { FontTemplateDescriptor {
weight: FontWeight::bold(), weight: FontWeight::BOLD,
stretch: FontStretch::hundred(), stretch: FontStretch::hundred(),
style: FontStyle::Normal, style: FontStyle::NORMAL,
} }
); );
assert_eq!( assert_eq!(
descriptor("DejaVuSans-Oblique"), descriptor("DejaVuSans-Oblique"),
FontTemplateDescriptor { FontTemplateDescriptor {
weight: FontWeight::normal(), weight: FontWeight::NORMAL,
stretch: FontStretch::hundred(), stretch: FontStretch::hundred(),
style: FontStyle::Italic, style: FontStyle::ITALIC,
} }
); );
assert_eq!( assert_eq!(
descriptor("DejaVuSansCondensed-BoldOblique"), descriptor("DejaVuSansCondensed-BoldOblique"),
FontTemplateDescriptor { FontTemplateDescriptor {
weight: FontWeight::bold(), weight: FontWeight::BOLD,
stretch: FontStretch(NonNegative(Percentage(0.875))), stretch: FontStretch::from_percentage(0.875),
style: FontStyle::Italic, style: FontStyle::ITALIC,
} }
); );
} }

View file

@ -1779,7 +1779,7 @@ fn serialize_font<W>(style: &Font, dest: &mut W) -> fmt::Result
where where
W: fmt::Write, W: fmt::Write,
{ {
if style.font_style == FontStyle::Italic { if style.font_style == FontStyle::ITALIC {
write!(dest, "{} ", style.font_style.to_css_string())?; write!(dest, "{} ", style.font_style.to_css_string())?;
} }
if style.font_weight.is_bold() { if style.font_weight.is_bold() {

View file

@ -48,8 +48,9 @@ pub use crate::values::specified::Integer as SpecifiedInteger;
/// cbindgen:derive-gte /// cbindgen:derive-gte
#[repr(C)] #[repr(C)]
#[derive( #[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> { pub struct FixedPoint<T, const FRACTION_BITS: u16> {
value: T, value: T,
} }
@ -901,8 +902,9 @@ pub type FontStyleFixedPoint = FixedPoint<i16, FONT_STYLE_FRACTION_BITS>;
/// cbindgen:derive-gt /// cbindgen:derive-gt
/// cbindgen:derive-gte /// cbindgen:derive-gte
#[derive( #[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)] #[repr(C)]
pub struct FontStyle(FontStyleFixedPoint); pub struct FontStyle(FontStyleFixedPoint);
@ -1012,6 +1014,7 @@ pub type FontStretchFixedPoint = FixedPoint<u16, FONT_STRETCH_FRACTION_BITS>;
#[derive( #[derive(
Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue,
)] )]
#[cfg_attr(feature = "servo", derive(Deserialize, Hash, Serialize))]
#[repr(C)] #[repr(C)]
pub struct FontStretch(pub FontStretchFixedPoint); pub struct FontStretch(pub FontStretchFixedPoint);