Fix servo build.

This commit is contained in:
Emilio Cobos Álvarez 2018-04-25 18:50:48 +02:00
parent 16ca8de6df
commit 54b444992d
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
13 changed files with 148 additions and 143 deletions

View file

@ -19,7 +19,7 @@ use std::rc::Rc;
use std::str;
use std::sync::Arc;
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
use style::computed_values::{font_stretch, font_variant_caps, font_weight};
use style::computed_values::{font_stretch, font_style, font_variant_caps, font_weight};
use style::properties::style_structs::Font as FontStyleStruct;
use style::values::computed::font::SingleFontFamily;
use text::Shaper;
@ -47,18 +47,24 @@ static TEXT_SHAPING_PERFORMANCE_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
// resources needed by the graphics layer to draw glyphs.
pub trait FontHandleMethods: Sized {
fn new_from_template(fctx: &FontContextHandle, template: Arc<FontTemplateData>, pt_size: Option<Au>)
-> Result<Self, ()>;
fn new_from_template(
fctx: &FontContextHandle,
template: Arc<FontTemplateData>,
pt_size: Option<Au>,
) -> Result<Self, ()>;
fn template(&self) -> Arc<FontTemplateData>;
fn family_name(&self) -> String;
fn face_name(&self) -> Option<String>;
fn is_italic(&self) -> bool;
fn style(&self) -> font_style::T;
fn boldness(&self) -> font_weight::T;
fn stretchiness(&self) -> font_stretch::T;
fn glyph_index(&self, codepoint: char) -> Option<GlyphId>;
fn glyph_h_advance(&self, GlyphId) -> Option<FractionalPixel>;
fn glyph_h_kerning(&self, glyph0: GlyphId, glyph1: GlyphId) -> FractionalPixel;
/// Can this font do basic horizontal LTR shaping without Harfbuzz?
fn can_do_fast_shaping(&self) -> bool;
fn metrics(&self) -> FontMetrics;

View file

@ -18,14 +18,11 @@ use platform::font_list::system_default_family;
use platform::font_template::FontTemplateData;
use servo_atoms::Atom;
use servo_url::ServoUrl;
use std::{fmt, f32, mem, thread};
use std::borrow::ToOwned;
use std::collections::HashMap;
use std::fmt;
use std::mem;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::thread;
use std::u32;
use style::font_face::{EffectiveSources, Source};
use style::values::computed::font::{SingleFontFamily, FamilyName};
use webrender_api;
@ -63,7 +60,7 @@ impl FontTemplates {
// We didn't find an exact match. Do more expensive fuzzy matching.
// TODO(#190): Do a better job.
let (mut best_template_data, mut best_distance) = (None, u32::MAX);
let (mut best_template_data, mut best_distance) = (None, f32::MAX);
for template in &mut self.templates {
if let Some((template_data, distance)) =
template.data_for_approximate_descriptor(fctx, desc) {

View file

@ -10,7 +10,6 @@ use servo_atoms::Atom;
use std::fmt::{Debug, Error, Formatter};
use std::io::Error as IoError;
use std::sync::{Arc, Weak};
use std::u32;
use style::computed_values::font_stretch::T as FontStretch;
use style::computed_values::font_style::T as FontStyle;
use style::properties::style_structs::Font as FontStyleStruct;
@ -20,24 +19,35 @@ use style::values::computed::font::FontWeight;
/// to be expanded or refactored when we support more of the font styling parameters.
///
/// NB: If you change this, you will need to update `style::properties::compute_font_hash()`.
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
pub struct FontTemplateDescriptor {
pub weight: FontWeight,
pub stretch: FontStretch,
pub italic: bool,
pub style: FontStyle,
}
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(),
}
}
impl FontTemplateDescriptor {
#[inline]
pub fn new(
weight: FontWeight,
stretch: FontStretch,
italic: bool,
style: FontStyle,
) -> Self {
Self {
weight,
stretch,
italic,
style,
}
}
@ -49,30 +59,15 @@ impl FontTemplateDescriptor {
///
/// The policy is to care most about differences in italicness, then weight, then stretch
#[inline]
fn distance_from(&self, other: &FontTemplateDescriptor) -> u32 {
let italic_part = if self.italic == other.italic { 0 } else { 1000 };
fn distance_from(&self, other: &FontTemplateDescriptor) -> f32 {
// 0 <= style_part <= 180, since font-style obliqueness should be
// 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 as i16) - (other.weight.0 as i16)).abs() as u32;
let weight_part = (self.weight.0 - other.weight.0).abs();
// 0 <= stretchPart <= 8
let stretch_part = (self.stretch_number() - other.stretch_number()).abs() as u32;
italic_part + weight_part + stretch_part
}
/// Returns a number between 1 and 9 for the stretch property.
/// 1 is ultra_condensed, 5 is normal, and 9 is ultra_expanded
#[inline]
fn stretch_number(&self) -> i32 {
match self.stretch {
FontStretch::UltraCondensed => 1,
FontStretch::ExtraCondensed => 2,
FontStretch::Condensed => 3,
FontStretch::SemiCondensed => 4,
FontStretch::Normal => 5,
FontStretch::SemiExpanded => 6,
FontStretch::Expanded => 7,
FontStretch::ExtraExpanded => 8,
FontStretch::UltraExpanded => 9,
}
let stretch_part = ((self.stretch.0).0 - (other.stretch.0).0).abs();
style_part + weight_part + stretch_part
}
}
@ -81,17 +76,11 @@ impl<'a> From<&'a FontStyleStruct> for FontTemplateDescriptor {
FontTemplateDescriptor {
weight: style.font_weight,
stretch: style.font_stretch,
italic: style.font_style == FontStyle::Italic || style.font_style == FontStyle::Oblique,
style: style.font_style,
}
}
}
impl PartialEq for FontTemplateDescriptor {
fn eq(&self, other: &FontTemplateDescriptor) -> bool {
self.weight == other.weight && self.stretch == other.stretch && self.italic == other.italic
}
}
/// This describes all the information needed to create
/// font instance handles. It contains a unique
/// FontTemplateData structure that is platform specific.
@ -176,10 +165,11 @@ impl FontTemplate {
/// Returns the font data along with the distance between this font's descriptor and the given
/// descriptor, if the font can be loaded.
pub fn data_for_approximate_descriptor(&mut self,
pub fn data_for_approximate_descriptor(
&mut self,
font_context: &FontContextHandle,
requested_descriptor: &FontTemplateDescriptor)
-> Option<(Arc<FontTemplateData>, u32)> {
requested_descriptor: &FontTemplateDescriptor,
) -> Option<(Arc<FontTemplateData>, f32)> {
self.descriptor(&font_context).and_then(|descriptor| {
self.data().ok().map(|data| {
(data, descriptor.distance_from(requested_descriptor))
@ -198,9 +188,11 @@ impl FontTemplate {
None);
self.is_valid = handle.is_ok();
let handle = handle?;
self.descriptor = Some(FontTemplateDescriptor::new(handle.boldness(),
self.descriptor = Some(FontTemplateDescriptor::new(
handle.boldness(),
handle.stretchiness(),
handle.is_italic()));
handle.style(),
));
Ok(())
}

View file

@ -24,6 +24,7 @@ use std::os::raw::{c_char, c_long};
use std::sync::Arc;
use style::computed_values::font_stretch::T as FontStretch;
use style::computed_values::font_weight::T as FontWeight;
use style::values::computed::font::FontStyle;
use super::c_str_to_string;
use text::glyph::GlyphId;
use text::util::fixed_to_float;
@ -149,8 +150,13 @@ impl FontHandleMethods for FontHandle {
}
}
fn is_italic(&self) -> bool {
unsafe { (*self.face).style_flags & FT_STYLE_FLAG_ITALIC as c_long != 0 }
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
} else {
Normal
}
}
fn boldness(&self) -> FontWeight {
@ -163,22 +169,25 @@ impl FontHandleMethods for FontHandle {
}
fn stretchiness(&self) -> FontStretch {
if let Some(os2) = self.os2_table() {
use style::values::generics::NonNegative;
use style::values::specified::font::FontStretchKeyword;
let percentage = if let Some(os2) = self.os2_table() {
match os2.us_width_class {
1 => FontStretch::UltraCondensed,
2 => FontStretch::ExtraCondensed,
3 => FontStretch::Condensed,
4 => FontStretch::SemiCondensed,
5 => FontStretch::Normal,
6 => FontStretch::SemiExpanded,
7 => FontStretch::Expanded,
8 => FontStretch::ExtraExpanded,
9 => FontStretch::UltraExpanded,
_ => FontStretch::Normal
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,
_ => FontStretchKeyword::Normal
}
} else {
FontStretch::Normal
}
FontStretchKeyword::Normal
}.compute();
NonNegative(percentage)
}
fn glyph_index(&self, codepoint: char) -> Option<GlyphId> {

View file

@ -22,8 +22,7 @@ use servo_atoms::Atom;
use std::{fmt, ptr};
use std::ops::Range;
use std::sync::Arc;
use style::computed_values::font_stretch::T as FontStretch;
use style::computed_values::font_weight::T as FontWeight;
use style::values::computed::font::{FontStretch, FontStyle, FontWeight};
use text::glyph::GlyphId;
const KERN_PAIR_LEN: usize = 6;
@ -204,8 +203,13 @@ impl FontHandleMethods for FontHandle {
Some(self.ctfont.face_name())
}
fn is_italic(&self) -> bool {
self.ctfont.symbolic_traits().is_italic()
fn style(&self) -> FontStyle {
use style::values::generics::font::FontStyle::*;
if self.ctfont.symbolic_traits().is_italic() {
Italic
} else {
Normal
}
}
fn boldness(&self) -> FontWeight {
@ -221,19 +225,11 @@ impl FontHandleMethods for FontHandle {
}
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 = (normalized + 1.0) / 2.0 * 9.0; // [0.0, 9.0]
match normalized {
v if v < 1.0 => FontStretch::UltraCondensed,
v if v < 2.0 => FontStretch::ExtraCondensed,
v if v < 3.0 => FontStretch::Condensed,
v if v < 4.0 => FontStretch::SemiCondensed,
v if v < 5.0 => FontStretch::Normal,
v if v < 6.0 => FontStretch::SemiExpanded,
v if v < 7.0 => FontStretch::Expanded,
v if v < 8.0 => FontStretch::ExtraExpanded,
_ => FontStretch::UltraExpanded,
}
NonNegative(Percentage(normalized as f32 + 1.0))
}
fn glyph_index(&self, codepoint: char) -> Option<GlyphId> {

View file

@ -19,6 +19,10 @@ use servo_atoms::Atom;
use std::sync::Arc;
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::NonNegative;
use style::values::generics::font::FontStyle as GenericFontStyle;
use style::values::specified::font::FontStretchKeyword;
use text::glyph::GlyphId;
use truetype;
@ -98,7 +102,7 @@ struct FontInfo {
face_name: String,
weight: StyleFontWeight,
stretch: StyleFontStretch,
style: FontStyle,
style: StyleFontStyle,
}
impl FontInfo {
@ -159,23 +163,23 @@ impl FontInfo {
let weight = StyleFontWeight(weight_val as f32);
let stretch = match min(9, max(1, width_val)) {
1 => StyleFontStretch::UltraCondensed,
2 => StyleFontStretch::ExtraCondensed,
3 => StyleFontStretch::Condensed,
4 => StyleFontStretch::SemiCondensed,
5 => StyleFontStretch::Normal,
6 => StyleFontStretch::SemiExpanded,
7 => StyleFontStretch::Expanded,
8 => StyleFontStretch::ExtraExpanded,
9 => StyleFontStretch::UltraExpanded,
let stretch = 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 style = if italic_bool {
FontStyle::Italic
GenericFontStyle::Italic
} else {
FontStyle::Normal
GenericFontStyle::Normal
};
Ok(FontInfo {
@ -188,7 +192,11 @@ impl FontInfo {
}
fn new_from_font(font: &Font) -> Result<FontInfo, ()> {
let style = font.style();
let style = match font.style() {
FontStyle::Normal => GenericFontStyle::Normal,
FontStyle::Oblique => GenericFontStyle::Oblique(StyleFontStyle::default_angle()),
FontStyle::Italic => GenericFontStyle::Italic,
};
let weight = StyleFontWeight(match font.weight() {
FontWeight::Thin => 100.,
FontWeight::ExtraLight => 200.,
@ -204,18 +212,18 @@ impl FontInfo {
// slightly blacker black
FontWeight::ExtraBlack => 1000.,
});
let stretch = match font.stretch() {
FontStretch::Undefined => StyleFontStretch::Normal,
FontStretch::UltraCondensed => StyleFontStretch::UltraCondensed,
FontStretch::ExtraCondensed => StyleFontStretch::ExtraCondensed,
FontStretch::Condensed => StyleFontStretch::Condensed,
FontStretch::SemiCondensed => StyleFontStretch::SemiCondensed,
FontStretch::Normal => StyleFontStretch::Normal,
FontStretch::SemiExpanded => StyleFontStretch::SemiExpanded,
FontStretch::Expanded => StyleFontStretch::Expanded,
FontStretch::ExtraExpanded => StyleFontStretch::ExtraExpanded,
FontStretch::UltraExpanded => StyleFontStretch::UltraExpanded,
};
let stretch = 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());
Ok(FontInfo {
family_name: font.family_name(),
@ -294,11 +302,8 @@ impl FontHandleMethods for FontHandle {
Some(self.info.face_name.clone())
}
fn is_italic(&self) -> bool {
match self.info.style {
FontStyle::Normal => false,
FontStyle::Oblique | FontStyle::Italic => true,
}
fn style(&self) -> StyleFontStyle {
self.info.style
}
fn boldness(&self) -> StyleFontWeight {

View file

@ -22,12 +22,13 @@ use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::rc::Rc;
use style::properties::longhands::font_stretch::computed_value::T as FontStretch;
use style::properties::longhands::font_style::computed_value::T as FontStyle;
use style::properties::longhands::font_variant_caps::computed_value::T as FontVariantCaps;
use style::properties::style_structs::Font as FontStyleStruct;
use style::values::computed::Percentage;
use style::values::computed::font::{FamilyName, FamilyNameSyntax, FontFamily, FontFamilyList, FontSize};
use style::values::computed::font::{FontWeight, SingleFontFamily};
use style::values::generics::NonNegative;
use style::values::generics::font::FontStyle;
struct TestFontSource {
handle: FontContextHandle,
@ -108,7 +109,7 @@ fn style() -> FontStyleStruct {
font_variant_caps: FontVariantCaps::Normal,
font_weight: FontWeight::normal(),
font_size: FontSize::medium(),
font_stretch: FontStretch::Normal,
font_stretch: NonNegative(Percentage(1.)),
hash: 0,
};
style.compute_font_hash();

View file

@ -16,8 +16,10 @@ fn test_font_template_descriptor() {
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use style::computed_values::font_stretch::T as FontStretch;
use style::values::computed::Percentage;
use style::values::computed::font::FontWeight;
use style::values::generics::NonNegative;
use style::values::generics::font::FontStyle;
fn descriptor(filename: &str) -> FontTemplateDescriptor {
let mut path: PathBuf = [
@ -43,25 +45,25 @@ fn test_font_template_descriptor() {
assert_eq!(descriptor("DejaVuSans"), FontTemplateDescriptor {
weight: FontWeight::normal(),
stretch: FontStretch::Normal,
italic: false,
stretch: NonNegative(Percentage(1.)),
style: FontStyle::Normal,
});
assert_eq!(descriptor("DejaVuSans-Bold"), FontTemplateDescriptor {
weight: FontWeight::bold(),
stretch: FontStretch::Normal,
italic: false,
stretch: NonNegative(Percentage(1.)),
style: FontStyle::Normal,
});
assert_eq!(descriptor("DejaVuSans-Oblique"), FontTemplateDescriptor {
weight: FontWeight::normal(),
stretch: FontStretch::Normal,
italic: true,
stretch: NonNegative(Percentage(1.)),
style: FontStyle::Italic,
});
assert_eq!(descriptor("DejaVuSansCondensed-BoldOblique"), FontTemplateDescriptor {
weight: FontWeight::bold(),
stretch: FontStretch::SemiCondensed,
italic: true,
stretch: NonNegative(Percentage(0.875)),
style: FontStyle::Italic,
});
}

View file

@ -104,13 +104,13 @@ impl Flow for MulticolFlow {
self.block_flow.fragment.border_box.size.inline - padding_and_borders;
let column_width;
{
let column_style = self.block_flow.fragment.style.get_column();
let column_gap = match column_style.column_gap {
let style = &self.block_flow.fragment.style;
let column_gap = match style.get_position().column_gap {
Either::First(len) => len.0.to_pixel_length(content_inline_size).into(),
Either::Second(_normal) => self.block_flow.fragment.style.get_font().font_size.size(),
};
let column_style = style.get_column();
let mut column_count;
if let Either::First(column_width) = column_style.column_width {
let column_width = Au::from(column_width);

View file

@ -352,7 +352,7 @@ ${helpers.predefined_type("grid-template-areas",
${helpers.predefined_type("column-gap",
"length::NonNegativeLengthOrPercentageOrNormal",
"Either::Second(Normal)",
alias="grid-column-gap",
alias="grid-column-gap" if product == "gecko" else "",
extra_prefixes="moz",
servo_pref="layout.columns.enabled",
spec="https://drafts.csswg.org/css-align-3/#propdef-column-gap",
@ -364,7 +364,7 @@ ${helpers.predefined_type("row-gap",
"length::NonNegativeLengthOrPercentageOrNormal",
"Either::Second(Normal)",
alias="grid-row-gap",
servo_pref="layout.columns.enabled",
products="gecko",
spec="https://drafts.csswg.org/css-align-3/#propdef-row-gap",
animation_value_type="NonNegativeLengthOrPercentageOrNormal",
servo_restyle_damage = "reflow")}

View file

@ -2242,9 +2242,13 @@ pub mod style_structs {
pub fn compute_font_hash(&mut self) {
// Corresponds to the fields in
// `gfx::font_template::FontTemplateDescriptor`.
//
// FIXME(emilio): Where's font-style?
let mut hasher: FnvHasher = Default::default();
self.font_weight.hash(&mut hasher);
self.font_stretch.hash(&mut hasher);
// We hash the floating point number with four decimal
// places.
hasher.write_u64((self.font_weight.0 * 10000.).trunc() as u64);
hasher.write_u64(((self.font_stretch.0).0 * 10000.).trunc() as u64);
self.font_family.hash(&mut hasher);
self.hash = hasher.finish()
}

View file

@ -15,6 +15,7 @@ use gecko_bindings::sugar::refptr::RefPtr;
#[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use std::fmt::{self, Write};
#[cfg(feature = "gecko")]
use std::hash::{Hash, Hasher};
#[cfg(feature = "servo")]
use std::slice;
@ -122,16 +123,6 @@ impl FontWeight {
}
}
impl Hash for FontWeight {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
// We hash the floating point number with four decimal places.
state.write_u32((self.0 * 10000.).trunc() as u32)
}
}
impl FontSize {
/// The actual computed font size.
pub fn size(self) -> Au {
@ -841,6 +832,7 @@ impl ToComputedValue for specified::MozScriptLevel {
/// A wrapper over an `Angle`, that handles clamping to the appropriate range
/// for `font-style` animation.
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
pub struct FontStyleAngle(pub Angle);
impl ToAnimatedValue for FontStyleAngle {
@ -878,7 +870,7 @@ impl FontStyle {
///
/// https://drafts.csswg.org/css-fonts-4/#valdef-font-style-oblique-angle
#[inline]
fn default_angle() -> FontStyleAngle {
pub fn default_angle() -> FontStyleAngle {
FontStyleAngle(Angle::Deg(specified::DEFAULT_FONT_STYLE_OBLIQUE_ANGLE_DEGREES))
}

View file

@ -238,6 +238,7 @@ impl ToCss for KeywordSize {
#[allow(missing_docs)]
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf,
PartialEq, ToAnimatedValue, ToAnimatedZero)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
pub enum FontStyle<Angle> {
#[animation(error)]
Normal,