diff options
author | Jon Leighton <j@jonathanleighton.com> | 2018-02-10 10:27:54 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2018-02-22 16:36:05 +0100 |
commit | f22e5ef3bdef97daa7b38cf642f24766cab6c488 (patch) | |
tree | a7811c222b0dab2d9364523464fe220ca04656fd /components/gfx/font_template.rs | |
parent | 691f3be24a6fcc90ae7d0b9b0783abf8674e1b0f (diff) | |
download | servo-f22e5ef3bdef97daa7b38cf642f24766cab6c488.tar.gz servo-f22e5ef3bdef97daa7b38cf642f24766cab6c488.zip |
Lazy load fonts in a FontGroup
This is a step towards fixing #17267. To fix that, we need to be able to
try various different fallback fonts in turn, which would become
unweildy with the prior eager-loading strategy.
Prior to this change, FontGroup loaded up all Font instances, including
the fallback font, before any of them were checked for the presence of
the glyphs we're trying to render.
So for the following CSS:
font-family: Helvetica, Arial;
The FontGroup would contain a Font instance for Helvetica, and a Font
instance for Arial, and a Font instance for the fallback font.
It may be that Helvetica contains glyphs for every character in the
document, and therefore Arial and the fallback font are not needed at
all.
This change makes the strategy lazy, so that we'll only create a Font
for Arial if we cannot find a glyph within Helvetica. I've also
substantially refactored the existing code in the process and added
some documentation along the way.
Diffstat (limited to 'components/gfx/font_template.rs')
-rw-r--r-- | components/gfx/font_template.rs | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/components/gfx/font_template.rs b/components/gfx/font_template.rs index b128ee22041..239bfcdd8c9 100644 --- a/components/gfx/font_template.rs +++ b/components/gfx/font_template.rs @@ -11,7 +11,10 @@ use std::fmt::{Debug, Error, Formatter}; use std::io::Error as IoError; use std::sync::{Arc, Weak}; use std::u32; -use style::computed_values::{font_stretch, font_weight}; +use style::computed_values::font_stretch::T as FontStretch; +use style::computed_values::font_style::T as FontStyle; +use style::properties::style_structs::Font as FontStyleStruct; +use style::values::computed::font::FontWeight; /// Describes how to select a font from a given family. This is very basic at the moment and needs /// to be expanded or refactored when we support more of the font styling parameters. @@ -19,14 +22,14 @@ use style::computed_values::{font_stretch, font_weight}; /// NB: If you change this, you will need to update `style::properties::compute_font_hash()`. #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Serialize)] pub struct FontTemplateDescriptor { - pub weight: font_weight::T, - pub stretch: font_stretch::T, + pub weight: FontWeight, + pub stretch: FontStretch, pub italic: bool, } impl FontTemplateDescriptor { #[inline] - pub fn new(weight: font_weight::T, stretch: font_stretch::T, italic: bool) + pub fn new(weight: FontWeight, stretch: FontStretch, italic: bool) -> FontTemplateDescriptor { FontTemplateDescriptor { weight: weight, @@ -57,15 +60,25 @@ impl FontTemplateDescriptor { #[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, + FontStretch::UltraCondensed => 1, + FontStretch::ExtraCondensed => 2, + FontStretch::Condensed => 3, + FontStretch::SemiCondensed => 4, + FontStretch::Normal => 5, + FontStretch::SemiExpanded => 6, + FontStretch::Expanded => 7, + FontStretch::ExtraExpanded => 8, + FontStretch::UltraExpanded => 9, + } + } +} + +impl<'a> From<&'a FontStyleStruct> for FontTemplateDescriptor { + fn from(style: &'a FontStyleStruct) -> Self { + FontTemplateDescriptor { + weight: style.font_weight, + stretch: style.font_stretch, + italic: style.font_style == FontStyle::Italic || style.font_style == FontStyle::Oblique, } } } |