mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
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.
This commit is contained in:
parent
691f3be24a
commit
f22e5ef3bd
7 changed files with 345 additions and 211 deletions
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue