Use integer for specified and computed font-weight

This commit is contained in:
Xidorn Quan 2017-07-06 15:31:24 +10:00
parent 4f0f2fb13e
commit 8b842f5417
11 changed files with 111 additions and 194 deletions

View file

@ -128,7 +128,7 @@ impl FontHandleMethods for FontHandle {
unsafe { (*self.face).style_flags & FT_STYLE_FLAG_ITALIC as c_long != 0 }
}
fn boldness(&self) -> font_weight::T {
let default_weight = font_weight::T::Weight400;
let default_weight = font_weight::T::normal();
if unsafe { (*self.face).style_flags & FT_STYLE_FLAG_BOLD as c_long == 0 } {
default_weight
} else {
@ -136,18 +136,13 @@ impl FontHandleMethods for FontHandle {
let os2 = FT_Get_Sfnt_Table(self.face, FT_Sfnt_Tag::FT_SFNT_OS2) as *mut TT_OS2;
let valid = !os2.is_null() && (*os2).version != 0xffff;
if valid {
let weight =(*os2).usWeightClass;
match weight {
1 | 100...199 => font_weight::T::Weight100,
2 | 200...299 => font_weight::T::Weight200,
3 | 300...399 => font_weight::T::Weight300,
4 | 400...499 => font_weight::T::Weight400,
5 | 500...599 => font_weight::T::Weight500,
6 | 600...699 => font_weight::T::Weight600,
7 | 700...799 => font_weight::T::Weight700,
8 | 800...899 => font_weight::T::Weight800,
9 | 900...999 => font_weight::T::Weight900,
_ => default_weight
let weight =(*os2).usWeightClass as i32;
if weight < 10 {
font_weight::T::from_int(weight * 100).unwrap()
} else if weight >= 100 && weight < 1000 {
font_weight::T::from_int(weight / 100 * 100).unwrap()
} else {
default_weight
}
} else {
default_weight

View file

@ -217,17 +217,7 @@ impl FontHandleMethods for FontHandle {
} else {
4.0 + normalized * 5.0 // [4.0, 9.0]
}; // [1.0, 9.0], centered on 4.0
match normalized.round() as u32 {
1 => font_weight::T::Weight100,
2 => font_weight::T::Weight200,
3 => font_weight::T::Weight300,
4 => font_weight::T::Weight400,
5 => font_weight::T::Weight500,
6 => font_weight::T::Weight600,
7 => font_weight::T::Weight700,
8 => font_weight::T::Weight800,
_ => font_weight::T::Weight900,
}
font_weight::T::from_int(normalized.round() as i32 * 100).unwrap()
}
fn stretchiness(&self) -> font_stretch::T {

View file

@ -155,18 +155,8 @@ impl FontInfo {
},
};
let weight = match min(9, max(1, weight_val / 100)) {
1 => font_weight::T::Weight100,
2 => font_weight::T::Weight200,
3 => font_weight::T::Weight300,
4 => font_weight::T::Weight400,
5 => font_weight::T::Weight500,
6 => font_weight::T::Weight600,
7 => font_weight::T::Weight700,
8 => font_weight::T::Weight800,
9 => font_weight::T::Weight900,
_ => return Err(()),
};
let weight = font_weight::T::
from_int(min(9, max(1, weight_val as i32 / 100)) * 100).unwrap();
let stretch = match min(9, max(1, width_val)) {
1 => font_stretch::T::ultra_condensed,
@ -198,21 +188,21 @@ impl FontInfo {
fn new_from_font(font: &Font) -> Result<FontInfo, ()> {
let style = font.style();
let weight = match font.weight() {
FontWeight::Thin => font_weight::T::Weight100,
FontWeight::ExtraLight => font_weight::T::Weight200,
FontWeight::Light => font_weight::T::Weight300,
let weight = font_weight::T(match font.weight() {
FontWeight::Thin => 100,
FontWeight::ExtraLight => 200,
FontWeight::Light => 300,
// slightly grayer gray
FontWeight::SemiLight => font_weight::T::Weight300,
FontWeight::Regular => font_weight::T::Weight400,
FontWeight::Medium => font_weight::T::Weight500,
FontWeight::SemiBold => font_weight::T::Weight600,
FontWeight::Bold => font_weight::T::Weight700,
FontWeight::ExtraBold => font_weight::T::Weight800,
FontWeight::Black => font_weight::T::Weight900,
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 => font_weight::T::Weight900,
};
FontWeight::ExtraBlack => 900,
});
let stretch = match font.stretch() {
FontStretch::Undefined => font_stretch::T::normal,
FontStretch::UltraCondensed => font_stretch::T::ultra_condensed,