mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Change selection of the best font for a style
Previously anything with the wrong stretch/italicness was considered equally bad. Now we consider the wrong weight to be least bad, wrong boldness of intermediate badness, and wrong itallicness to be most bad.
This commit is contained in:
parent
84de6a84d6
commit
6647a2f891
1 changed files with 24 additions and 4 deletions
|
@ -40,13 +40,33 @@ impl FontTemplateDescriptor {
|
||||||
///
|
///
|
||||||
/// The smaller the score, the better the fonts match. 0 indicates an exact match. This must
|
/// The smaller the score, the better the fonts match. 0 indicates an exact match. This must
|
||||||
/// be commutative (distance(A, B) == distance(B, A)).
|
/// be commutative (distance(A, B) == distance(B, A)).
|
||||||
|
///
|
||||||
|
/// The policy is to care most about differences in italicness, then weight, then stretch
|
||||||
#[inline]
|
#[inline]
|
||||||
fn distance_from(&self, other: &FontTemplateDescriptor) -> u32 {
|
fn distance_from(&self, other: &FontTemplateDescriptor) -> u32 {
|
||||||
if self.stretch != other.stretch || self.italic != other.italic {
|
let italic_part = if self.italic == other.italic { 0 } else { 1000 };
|
||||||
// A value higher than all weights.
|
// 0 <= weightPart <= 800
|
||||||
return 1000
|
let weight_part = ((self.weight.0 as i16) - (other.weight.0 as i16)).abs() as u32;
|
||||||
|
// 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 {
|
||||||
|
font_stretch::T::UltraCondensed => 1,
|
||||||
|
font_stretch::T::ExtraCondensed => 2,
|
||||||
|
font_stretch::T::Condensed => 3,
|
||||||
|
font_stretch::T::SemiCondensed => 4,
|
||||||
|
font_stretch::T::Normal => 5,
|
||||||
|
font_stretch::T::SemiExpanded => 6,
|
||||||
|
font_stretch::T::Expanded => 7,
|
||||||
|
font_stretch::T::ExtraExpanded => 8,
|
||||||
|
font_stretch::T::UltraExpanded => 9,
|
||||||
}
|
}
|
||||||
((self.weight.0 as i16) - (other.weight.0 as i16)).abs() as u32
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue