mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Auto merge of #19965 - dan-robertson:freetype2, r=jdm
Improve Freetype font handling and font selection <!-- Please describe your changes on the following line: --> This correctly implements the boldness and stretchiness properties for Freetype fonts. This also modifies the metric used to select fonts so that it takes account of stretchiness and italicness instead of just boldness. Finally there is a test of font selection (and thereby that fonts are having their boldness/italicness correctly read). These were originally part of an old and abandoned pull request. I've rebased the changes and modified them to make them work with some name changes. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [X] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19965) <!-- Reviewable:end -->
This commit is contained in:
commit
13cf813048
2 changed files with 109 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
|
||||
/// be commutative (distance(A, B) == distance(B, A)).
|
||||
///
|
||||
/// The policy is to care most about differences in italicness, then weight, then stretch
|
||||
#[inline]
|
||||
fn distance_from(&self, other: &FontTemplateDescriptor) -> u32 {
|
||||
if self.stretch != other.stretch || self.italic != other.italic {
|
||||
// A value higher than all weights.
|
||||
return 1000
|
||||
let italic_part = if self.italic == other.italic { 0 } else { 1000 };
|
||||
// 0 <= weightPart <= 800
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
85
tests/html/font_selection.html
Normal file
85
tests/html/font_selection.html
Normal file
|
@ -0,0 +1,85 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<!-- for testing font selection (based on font-stretch, font-weight, etc) -->
|
||||
<style>
|
||||
body {
|
||||
font-size: 20pt;
|
||||
font-family: "DejaVu Sans", "Helvetica Neue", sans;
|
||||
}
|
||||
table { border-collapse: collapse; }
|
||||
td {
|
||||
border: 1px solid #bbb;
|
||||
padding: 2pt;
|
||||
text-align: center;
|
||||
}
|
||||
td:nth-child(1) { font-weight: 100; }
|
||||
td:nth-child(2) { font-weight: 200; }
|
||||
td:nth-child(3) { font-weight: 300; }
|
||||
td:nth-child(4) { font-weight: 400; }
|
||||
td:nth-child(5) { font-weight: 500; }
|
||||
td:nth-child(6) { font-weight: 600; }
|
||||
td:nth-child(7) { font-weight: 700; }
|
||||
td:nth-child(8) { font-weight: 800; }
|
||||
td:nth-child(9) { font-weight: 900; }
|
||||
.s1 { font-stretch: ultra-condensed; }
|
||||
.s2 { font-stretch: extra-condensed; }
|
||||
.s3 { font-stretch: semi-condensed; }
|
||||
.s4 { font-stretch: condensed; }
|
||||
.s5 { font-stretch: normal; }
|
||||
.s6 { font-stretch: semi-expanded; }
|
||||
.s7 { font-stretch: expanded; }
|
||||
.s8 { font-stretch: extra-expanded; }
|
||||
.s9 { font-stretch: ultra-expanded; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table>
|
||||
<tr class="s1">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
<tr class="s2">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
<tr class="s3">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
<tr class="s4">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
<tr class="s5">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
<tr class="s6">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
<tr class="s7">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
<tr class="s8">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
<tr class="s9">
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
<td>two words</td><td>two words</td><td>two words</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</htlm>
|
Loading…
Add table
Add a link
Reference in a new issue