mirror of
https://github.com/servo/servo.git
synced 2025-07-03 05:23:38 +01:00
style: Fixups for css-fonts-4 font-weight.
This commit is contained in:
parent
0f19c25706
commit
48de556f8c
10 changed files with 51 additions and 64 deletions
|
@ -20,7 +20,7 @@ use style::values::computed::font::FontWeight;
|
||||||
/// to be expanded or refactored when we support more of the font styling parameters.
|
/// 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()`.
|
/// NB: If you change this, you will need to update `style::properties::compute_font_hash()`.
|
||||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Serialize)]
|
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
|
||||||
pub struct FontTemplateDescriptor {
|
pub struct FontTemplateDescriptor {
|
||||||
pub weight: FontWeight,
|
pub weight: FontWeight,
|
||||||
pub stretch: FontStretch,
|
pub stretch: FontStretch,
|
||||||
|
@ -29,12 +29,15 @@ pub struct FontTemplateDescriptor {
|
||||||
|
|
||||||
impl FontTemplateDescriptor {
|
impl FontTemplateDescriptor {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(weight: FontWeight, stretch: FontStretch, italic: bool)
|
pub fn new(
|
||||||
-> FontTemplateDescriptor {
|
weight: FontWeight,
|
||||||
FontTemplateDescriptor {
|
stretch: FontStretch,
|
||||||
weight: weight,
|
italic: bool,
|
||||||
stretch: stretch,
|
) -> Self {
|
||||||
italic: italic,
|
Self {
|
||||||
|
weight,
|
||||||
|
stretch,
|
||||||
|
italic,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,19 +154,12 @@ impl FontHandleMethods for FontHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn boldness(&self) -> FontWeight {
|
fn boldness(&self) -> FontWeight {
|
||||||
if let Some(os2) = self.os2_table() {
|
let os2 = match self.os2_table() {
|
||||||
let weight = os2.us_weight_class as i32;
|
None => return FontWeight::normal(),
|
||||||
|
Some(os2) => os2,
|
||||||
if weight < 10 {
|
};
|
||||||
FontWeight::from_int(weight * 100).unwrap()
|
let weight = os2.us_weight_class as f32;
|
||||||
} else if weight >= 100 && weight < 1000 {
|
FontWeight(weight.max(1.).min(1000.))
|
||||||
FontWeight::from_int(weight / 100 * 100).unwrap()
|
|
||||||
} else {
|
|
||||||
FontWeight::normal()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
FontWeight::normal()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stretchiness(&self) -> FontStretch {
|
fn stretchiness(&self) -> FontStretch {
|
||||||
|
|
|
@ -210,12 +210,14 @@ impl FontHandleMethods for FontHandle {
|
||||||
|
|
||||||
fn boldness(&self) -> FontWeight {
|
fn boldness(&self) -> FontWeight {
|
||||||
let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]
|
let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]
|
||||||
|
// TODO(emilio): It may make sense to make this range [.01, 10.0], to
|
||||||
|
// align with css-fonts-4's range of [1, 1000].
|
||||||
let normalized = if normalized <= 0.0 {
|
let normalized = if normalized <= 0.0 {
|
||||||
4.0 + normalized * 3.0 // [1.0, 4.0]
|
4.0 + normalized * 3.0 // [1.0, 4.0]
|
||||||
} 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::from_int(normalized.round() as i32 * 100).unwrap()
|
FontWeight(normalized as f32 * 100.)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stretchiness(&self) -> FontStretch {
|
fn stretchiness(&self) -> FontStretch {
|
||||||
|
|
|
@ -157,10 +157,7 @@ impl FontInfo {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let weight =
|
let weight = StyleFontWeight(weight_val as f32);
|
||||||
StyleFontWeight::from_int(
|
|
||||||
min(9, max(1, weight_val as i32 / 100)) * 100
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
let stretch = match min(9, max(1, width_val)) {
|
let stretch = match min(9, max(1, width_val)) {
|
||||||
1 => StyleFontStretch::UltraCondensed,
|
1 => StyleFontStretch::UltraCondensed,
|
||||||
|
@ -184,28 +181,28 @@ impl FontInfo {
|
||||||
Ok(FontInfo {
|
Ok(FontInfo {
|
||||||
family_name: family,
|
family_name: family,
|
||||||
face_name: face,
|
face_name: face,
|
||||||
weight: weight,
|
weight,
|
||||||
stretch: stretch,
|
stretch,
|
||||||
style: style,
|
style,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_from_font(font: &Font) -> Result<FontInfo, ()> {
|
fn new_from_font(font: &Font) -> Result<FontInfo, ()> {
|
||||||
let style = font.style();
|
let style = font.style();
|
||||||
let weight = StyleFontWeight(match font.weight() {
|
let weight = StyleFontWeight(match font.weight() {
|
||||||
FontWeight::Thin => 100,
|
FontWeight::Thin => 100.,
|
||||||
FontWeight::ExtraLight => 200,
|
FontWeight::ExtraLight => 200.,
|
||||||
FontWeight::Light => 300,
|
FontWeight::Light => 300.,
|
||||||
// slightly grayer gray
|
// slightly grayer gray
|
||||||
FontWeight::SemiLight => 300,
|
FontWeight::SemiLight => 300.,
|
||||||
FontWeight::Regular => 400,
|
FontWeight::Regular => 400.,
|
||||||
FontWeight::Medium => 500,
|
FontWeight::Medium => 500.,
|
||||||
FontWeight::SemiBold => 600,
|
FontWeight::SemiBold => 600.,
|
||||||
FontWeight::Bold => 700,
|
FontWeight::Bold => 700.,
|
||||||
FontWeight::ExtraBold => 800,
|
FontWeight::ExtraBold => 800.,
|
||||||
FontWeight::Black => 900,
|
FontWeight::Black => 900.,
|
||||||
// slightly blacker black
|
// slightly blacker black
|
||||||
FontWeight::ExtraBlack => 900,
|
FontWeight::ExtraBlack => 1000.,
|
||||||
});
|
});
|
||||||
let stretch = match font.stretch() {
|
let stretch = match font.stretch() {
|
||||||
FontStretch::Undefined => StyleFontStretch::Normal,
|
FontStretch::Undefined => StyleFontStretch::Normal,
|
||||||
|
@ -223,9 +220,9 @@ impl FontInfo {
|
||||||
Ok(FontInfo {
|
Ok(FontInfo {
|
||||||
family_name: font.family_name(),
|
family_name: font.family_name(),
|
||||||
face_name: font.face_name(),
|
face_name: font.face_name(),
|
||||||
style: style,
|
style,
|
||||||
weight: weight,
|
weight,
|
||||||
stretch: stretch,
|
stretch,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,8 @@ use style_traits::{StyleParseErrorKind, ToCss};
|
||||||
use style_traits::values::SequenceWriter;
|
use style_traits::values::SequenceWriter;
|
||||||
use values::computed::font::FamilyName;
|
use values::computed::font::FamilyName;
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
use values::specified::font::{AbsoluteFontWeight, SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
|
use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
|
||||||
|
use values::specified::font::AbsoluteFontWeight;
|
||||||
use values::specified::url::SpecifiedUrl;
|
use values::specified::url::SpecifiedUrl;
|
||||||
|
|
||||||
/// A source for a font-face rule.
|
/// A source for a font-face rule.
|
||||||
|
|
|
@ -15,8 +15,8 @@ use properties::longhands::font_language_override;
|
||||||
use std::str;
|
use std::str;
|
||||||
use values::computed::font::FamilyName;
|
use values::computed::font::FamilyName;
|
||||||
use values::generics::font::FontTag;
|
use values::generics::font::FontTag;
|
||||||
use values::specified::font::AbsoluteFontWeight;
|
|
||||||
use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
|
use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
|
||||||
|
use values::specified::font::AbsoluteFontWeight;
|
||||||
|
|
||||||
impl<'a> ToNsCssValue for &'a FamilyName {
|
impl<'a> ToNsCssValue for &'a FamilyName {
|
||||||
fn convert(self, nscssvalue: &mut nsCSSValue) {
|
fn convert(self, nscssvalue: &mut nsCSSValue) {
|
||||||
|
|
|
@ -2243,7 +2243,7 @@ pub mod style_structs {
|
||||||
// Corresponds to the fields in
|
// Corresponds to the fields in
|
||||||
// `gfx::font_template::FontTemplateDescriptor`.
|
// `gfx::font_template::FontTemplateDescriptor`.
|
||||||
let mut hasher: FnvHasher = Default::default();
|
let mut hasher: FnvHasher = Default::default();
|
||||||
hasher.write_u16(self.font_weight.0);
|
self.font_weight.hash(&mut hasher);
|
||||||
self.font_stretch.hash(&mut hasher);
|
self.font_stretch.hash(&mut hasher);
|
||||||
self.font_family.hash(&mut hasher);
|
self.font_family.hash(&mut hasher);
|
||||||
self.hash = hasher.finish()
|
self.hash = hasher.finish()
|
||||||
|
|
|
@ -15,7 +15,6 @@ use gecko_bindings::sugar::refptr::RefPtr;
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
|
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
|
||||||
use std::fmt::{self, Write};
|
use std::fmt::{self, Write};
|
||||||
#[cfg(feature = "gecko")]
|
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
#[cfg(feature = "servo")]
|
#[cfg(feature = "servo")]
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
@ -123,6 +122,16 @@ 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 {
|
impl FontSize {
|
||||||
/// The actual computed font size.
|
/// The actual computed font size.
|
||||||
pub fn size(self) -> Au {
|
pub fn size(self) -> Au {
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
use parsing::parse;
|
|
||||||
use style::properties::longhands::font_weight;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn font_weight_keyword_should_preserve_keyword() {
|
|
||||||
use style::properties::longhands::font_weight::SpecifiedValue;
|
|
||||||
|
|
||||||
let result = parse(font_weight::parse, "normal").unwrap();
|
|
||||||
assert_eq!(result, SpecifiedValue::Normal);
|
|
||||||
|
|
||||||
let result = parse(font_weight::parse, "bold").unwrap();
|
|
||||||
assert_eq!(result, SpecifiedValue::Bold);
|
|
||||||
}
|
|
|
@ -109,7 +109,6 @@ mod border;
|
||||||
mod box_;
|
mod box_;
|
||||||
mod column;
|
mod column;
|
||||||
mod effects;
|
mod effects;
|
||||||
mod font;
|
|
||||||
mod image;
|
mod image;
|
||||||
mod inherited_text;
|
mod inherited_text;
|
||||||
mod length;
|
mod length;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue