mirror of
https://github.com/servo/servo.git
synced 2025-09-30 08:39:16 +01:00
fonts: Instantiate system fonts using system font loaders (#33747)
System fonts used to be instantiated using the system font loader and this change restores that behavior. In addition, on macOS and FreeType platforms font data for system fonts is loaded using memory mapping. The benefit is that system font loaders typically are able to cache fonts in system memory (using memory mapping, for instance) and we'd like to load them in a the way most compatible with other applications. On my Linux system, this manages to get the overhead of loading a very large font down from 10ms to approximately 1ms. Subsequent runs show even less overhead. We've measured similar gains on macOS systems. Currently, system font data must be loaded into memory manually for canvas and this is unlikely to change even with a switch to `vello`. The use of explicit memmory mapping should help in this case -- though it probably won't be possible to use this properly on macOS and Windows if we ever want to load fonts from TTCs properly. Signed-off-by: Martin Robinson <mrobinson@igalia.com> Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
parent
4564ce2fcc
commit
0553789d48
24 changed files with 771 additions and 810 deletions
|
@ -4,7 +4,6 @@
|
|||
|
||||
use std::cmp::Ordering;
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
use std::{fmt, ptr};
|
||||
|
||||
/// Implementation of Quartz (CoreGraphics) fonts.
|
||||
|
@ -23,8 +22,9 @@ use style::values::computed::font::{FontStretch, FontStyle, FontWeight};
|
|||
use webrender_api::FontInstanceFlags;
|
||||
|
||||
use super::core_text_font_cache::CoreTextFontCache;
|
||||
use super::font_list::LocalFontIdentifier;
|
||||
use crate::{
|
||||
map_platform_values_to_style_values, FontIdentifier, FontMetrics, FontTableMethods,
|
||||
map_platform_values_to_style_values, FontData, FontIdentifier, FontMetrics, FontTableMethods,
|
||||
FontTableTag, FontTemplateDescriptor, FractionalPixel, GlyphId, PlatformFontMethods, CBDT,
|
||||
COLR, KERN, SBIX,
|
||||
};
|
||||
|
@ -55,9 +55,6 @@ impl FontTableMethods for FontTable {
|
|||
#[derive(Debug)]
|
||||
pub struct PlatformFont {
|
||||
ctfont: CTFont,
|
||||
/// A reference to this data used to create this [`PlatformFont`], ensuring the
|
||||
/// data stays alive of the lifetime of this struct.
|
||||
_data: Arc<Vec<u8>>,
|
||||
h_kern_subtable: Option<CachedKernTable>,
|
||||
}
|
||||
|
||||
|
@ -167,31 +164,45 @@ impl fmt::Debug for CachedKernTable {
|
|||
}
|
||||
}
|
||||
|
||||
impl PlatformFontMethods for PlatformFont {
|
||||
fn new_from_data(
|
||||
impl PlatformFont {
|
||||
fn new(
|
||||
font_identifier: FontIdentifier,
|
||||
data: Arc<Vec<u8>>,
|
||||
_face_index: u32,
|
||||
pt_size: Option<Au>,
|
||||
data: Option<&FontData>,
|
||||
requested_size: Option<Au>,
|
||||
) -> Result<PlatformFont, &'static str> {
|
||||
let size = match pt_size {
|
||||
let size = match requested_size {
|
||||
Some(s) => s.to_f64_px(),
|
||||
None => 0.0,
|
||||
};
|
||||
let Some(core_text_font) =
|
||||
CoreTextFontCache::core_text_font(font_identifier, data.clone(), size)
|
||||
let Some(core_text_font) = CoreTextFontCache::core_text_font(font_identifier, data, size)
|
||||
else {
|
||||
return Err("Could not generate CTFont for FontTemplateData");
|
||||
};
|
||||
|
||||
let mut handle = PlatformFont {
|
||||
_data: data,
|
||||
ctfont: core_text_font.clone_with_font_size(size),
|
||||
h_kern_subtable: None,
|
||||
};
|
||||
handle.h_kern_subtable = handle.find_h_kern_subtable();
|
||||
Ok(handle)
|
||||
}
|
||||
}
|
||||
|
||||
impl PlatformFontMethods for PlatformFont {
|
||||
fn new_from_data(
|
||||
font_identifier: FontIdentifier,
|
||||
data: &FontData,
|
||||
requested_size: Option<Au>,
|
||||
) -> Result<PlatformFont, &'static str> {
|
||||
Self::new(font_identifier, Some(data), requested_size)
|
||||
}
|
||||
|
||||
fn new_from_local_font_identifier(
|
||||
font_identifier: LocalFontIdentifier,
|
||||
requested_size: Option<Au>,
|
||||
) -> Result<PlatformFont, &'static str> {
|
||||
Self::new(FontIdentifier::Local(font_identifier), None, requested_size)
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> FontTemplateDescriptor {
|
||||
let traits = self.ctfont.all_traits();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue