Implement boldness and stretchiness for Freetype

The current implementation for boldness assumes that if Freetype
claims a font is not bold then it must have normal weight. However
fonts with lighter-than-normal weight are not claimed to be bold.
Therefore we get rid of this check.
This commit is contained in:
Dan Robertson 2017-10-26 13:51:38 +01:00
parent 8f24005e17
commit 84de6a84d6

View file

@ -10,7 +10,7 @@ use freetype::freetype::{FT_F26Dot6, FT_Face, FT_FaceRec};
use freetype::freetype::{FT_Get_Char_Index, FT_Get_Postscript_Name};
use freetype::freetype::{FT_Get_Kerning, FT_Get_Sfnt_Table, FT_Load_Sfnt_Table};
use freetype::freetype::{FT_GlyphSlot, FT_Library, FT_Long, FT_ULong};
use freetype::freetype::{FT_Int32, FT_Kerning_Mode, FT_STYLE_FLAG_BOLD, FT_STYLE_FLAG_ITALIC};
use freetype::freetype::{FT_Int32, FT_Kerning_Mode, FT_STYLE_FLAG_ITALIC};
use freetype::freetype::{FT_Load_Glyph, FT_Set_Char_Size};
use freetype::freetype::{FT_SizeRec, FT_Size_Metrics, FT_UInt, FT_Vector};
use freetype::freetype::FT_Sfnt_Tag;
@ -137,30 +137,45 @@ impl FontHandleMethods for FontHandle {
}
fn boldness(&self) -> FontWeight {
let default_weight = FontWeight::normal();
if unsafe { (*self.face).style_flags & FT_STYLE_FLAG_BOLD as c_long == 0 } {
default_weight
} else {
unsafe {
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 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 {
default_weight
}
unsafe {
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 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 {
default_weight
}
} else {
default_weight
}
}
}
fn stretchiness(&self) -> FontStretch {
// TODO(pcwalton): Implement this.
FontStretch::Normal
let default_stretch = FontStretch::Normal;
unsafe {
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 {
match (*os2).usWidthClass {
1 => FontStretch::UltraCondensed,
2 => FontStretch::ExtraCondensed,
3 => FontStretch::Condensed,
4 => FontStretch::SemiCondensed,
5 => FontStretch::Normal, // called medium in TrueType spec
6 => FontStretch::SemiExpanded,
7 => FontStretch::Expanded,
8 => FontStretch::ExtraExpanded,
9 => FontStretch::UltraExpanded,
_ => default_stretch,
}
} else {
default_stretch
}
}
}
fn glyph_index(&self, codepoint: char) -> Option<GlyphId> {