aboutsummaryrefslogtreecommitdiffstats
path: root/components/gfx
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <emilio@crisal.io>2018-04-21 20:05:37 +0200
committerEmilio Cobos Álvarez <emilio@crisal.io>2018-04-28 10:26:03 +0200
commit48de556f8caebe1c09b9d2542b074720ff76e8bc (patch)
tree3c32563a41c24bc26c368aecdcdff5d027ccfede /components/gfx
parent0f19c25706872fff5372e726c87f2022aa84877e (diff)
downloadservo-48de556f8caebe1c09b9d2542b074720ff76e8bc.tar.gz
servo-48de556f8caebe1c09b9d2542b074720ff76e8bc.zip
style: Fixups for css-fonts-4 font-weight.
Diffstat (limited to 'components/gfx')
-rw-r--r--components/gfx/font_template.rs17
-rw-r--r--components/gfx/platform/freetype/font.rs19
-rw-r--r--components/gfx/platform/macos/font.rs4
-rw-r--r--components/gfx/platform/windows/font.rs39
4 files changed, 37 insertions, 42 deletions
diff --git a/components/gfx/font_template.rs b/components/gfx/font_template.rs
index 239bfcdd8c9..5b5481a7344 100644
--- a/components/gfx/font_template.rs
+++ b/components/gfx/font_template.rs
@@ -20,7 +20,7 @@ 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, Eq, Hash, Serialize)]
+#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct FontTemplateDescriptor {
pub weight: FontWeight,
pub stretch: FontStretch,
@@ -29,12 +29,15 @@ pub struct FontTemplateDescriptor {
impl FontTemplateDescriptor {
#[inline]
- pub fn new(weight: FontWeight, stretch: FontStretch, italic: bool)
- -> FontTemplateDescriptor {
- FontTemplateDescriptor {
- weight: weight,
- stretch: stretch,
- italic: italic,
+ pub fn new(
+ weight: FontWeight,
+ stretch: FontStretch,
+ italic: bool,
+ ) -> Self {
+ Self {
+ weight,
+ stretch,
+ italic,
}
}
diff --git a/components/gfx/platform/freetype/font.rs b/components/gfx/platform/freetype/font.rs
index 940563d7d29..88a9e04533c 100644
--- a/components/gfx/platform/freetype/font.rs
+++ b/components/gfx/platform/freetype/font.rs
@@ -154,19 +154,12 @@ impl FontHandleMethods for FontHandle {
}
fn boldness(&self) -> FontWeight {
- if let Some(os2) = self.os2_table() {
- let weight = os2.us_weight_class as i32;
-
- if weight < 10 {
- FontWeight::from_int(weight * 100).unwrap()
- } else if weight >= 100 && weight < 1000 {
- FontWeight::from_int(weight / 100 * 100).unwrap()
- } else {
- FontWeight::normal()
- }
- } else {
- FontWeight::normal()
- }
+ let os2 = match self.os2_table() {
+ None => return FontWeight::normal(),
+ Some(os2) => os2,
+ };
+ let weight = os2.us_weight_class as f32;
+ FontWeight(weight.max(1.).min(1000.))
}
fn stretchiness(&self) -> FontStretch {
diff --git a/components/gfx/platform/macos/font.rs b/components/gfx/platform/macos/font.rs
index ca719ddf5b7..6dad917d1fe 100644
--- a/components/gfx/platform/macos/font.rs
+++ b/components/gfx/platform/macos/font.rs
@@ -210,12 +210,14 @@ impl FontHandleMethods for FontHandle {
fn boldness(&self) -> FontWeight {
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 {
4.0 + normalized * 3.0 // [1.0, 4.0]
} else {
4.0 + normalized * 5.0 // [4.0, 9.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 {
diff --git a/components/gfx/platform/windows/font.rs b/components/gfx/platform/windows/font.rs
index 12df6d547d2..8a43494c73e 100644
--- a/components/gfx/platform/windows/font.rs
+++ b/components/gfx/platform/windows/font.rs
@@ -157,10 +157,7 @@ impl FontInfo {
},
};
- let weight =
- StyleFontWeight::from_int(
- min(9, max(1, weight_val as i32 / 100)) * 100
- ).unwrap();
+ let weight = StyleFontWeight(weight_val as f32);
let stretch = match min(9, max(1, width_val)) {
1 => StyleFontStretch::UltraCondensed,
@@ -184,28 +181,28 @@ impl FontInfo {
Ok(FontInfo {
family_name: family,
face_name: face,
- weight: weight,
- stretch: stretch,
- style: style,
+ weight,
+ stretch,
+ style,
})
}
fn new_from_font(font: &Font) -> Result<FontInfo, ()> {
let style = font.style();
let weight = StyleFontWeight(match font.weight() {
- FontWeight::Thin => 100,
- FontWeight::ExtraLight => 200,
- FontWeight::Light => 300,
+ FontWeight::Thin => 100.,
+ FontWeight::ExtraLight => 200.,
+ FontWeight::Light => 300.,
// slightly grayer gray
- FontWeight::SemiLight => 300,
- FontWeight::Regular => 400,
- FontWeight::Medium => 500,
- FontWeight::SemiBold => 600,
- FontWeight::Bold => 700,
- FontWeight::ExtraBold => 800,
- FontWeight::Black => 900,
+ FontWeight::SemiLight => 300.,
+ FontWeight::Regular => 400.,
+ FontWeight::Medium => 500.,
+ FontWeight::SemiBold => 600.,
+ FontWeight::Bold => 700.,
+ FontWeight::ExtraBold => 800.,
+ FontWeight::Black => 900.,
// slightly blacker black
- FontWeight::ExtraBlack => 900,
+ FontWeight::ExtraBlack => 1000.,
});
let stretch = match font.stretch() {
FontStretch::Undefined => StyleFontStretch::Normal,
@@ -223,9 +220,9 @@ impl FontInfo {
Ok(FontInfo {
family_name: font.family_name(),
face_name: font.face_name(),
- style: style,
- weight: weight,
- stretch: stretch,
+ style,
+ weight,
+ stretch,
})
}
}