gfx: Remove FontTemplateData (#32034)

Now that `FontTemplateData` is more or less the same on all platforms,
it can be removed. This is a preparatory change for a full refactor of
the font system on Servo. The major changes here are:

 - Remove `FontTemplateData` and move its members into `FontTemplate`
 - Make `FontTemplate` have full interior mutability instead of only
   the `FontTemplateData` member. This is preparation for having these
   data types `Send` and `Sync` with locking.
 - Remove the strong/weak reference concept for font data. In practice,
   all font data references were strong, so this was never fully
   complete. Instead of using this approach, the new font system will
   use a central font data cache with references associated to layouts.
 - The `CTFont` cache is now a global cache, so `CTFont`s can be shared
   between threads. The cache is cleared when clearing font caches.

A benefit of this change (apart from `CTFont` sharing) is that font data
loading is platform-independent now.
This commit is contained in:
Martin Robinson 2024-04-16 19:50:50 +02:00 committed by GitHub
parent 689c144714
commit 6b2fa91357
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 394 additions and 600 deletions

View file

@ -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.
@ -21,12 +20,12 @@ use core_text::font_descriptor::{
use log::debug;
use style::values::computed::font::{FontStretch, FontStyle, FontWeight};
use super::font_template::CoreTextFontTemplateMethods;
use crate::font::{
FontHandleMethods, FontMetrics, FontTableMethods, FontTableTag, FractionalPixel, GPOS, GSUB,
KERN,
};
use crate::font_cache_thread::FontIdentifier;
use crate::platform::font_template::FontTemplateData;
use crate::font_template::FontTemplateRef;
use crate::text::glyph::GlyphId;
const KERN_PAIR_LEN: usize = 6;
@ -54,7 +53,7 @@ impl FontTableMethods for FontTable {
#[derive(Debug)]
pub struct FontHandle {
font_data: Arc<FontTemplateData>,
font_template: FontTemplateRef,
ctfont: CTFont,
h_kern_subtable: Option<CachedKernTable>,
can_do_fast_shaping: bool,
@ -157,34 +156,33 @@ impl fmt::Debug for CachedKernTable {
impl FontHandleMethods for FontHandle {
fn new_from_template(
template: Arc<FontTemplateData>,
font_template: FontTemplateRef,
pt_size: Option<Au>,
) -> Result<FontHandle, &'static str> {
let size = match pt_size {
Some(s) => s.to_f64_px(),
None => 0.0,
};
match template.ctfont(size) {
Some(ref ctfont) => {
let mut handle = FontHandle {
font_data: template.clone(),
ctfont: ctfont.clone_with_font_size(size),
h_kern_subtable: None,
can_do_fast_shaping: false,
};
handle.h_kern_subtable = handle.find_h_kern_subtable();
// TODO (#11310): Implement basic support for GPOS and GSUB.
handle.can_do_fast_shaping = handle.h_kern_subtable.is_some() &&
handle.table_for_tag(GPOS).is_none() &&
handle.table_for_tag(GSUB).is_none();
Ok(handle)
},
None => Err("Could not generate CTFont for FontTemplateData"),
}
let Some(core_text_font) = font_template.core_text_font(size) else {
return Err("Could not generate CTFont for FontTemplateData");
};
let mut handle = FontHandle {
font_template,
ctfont: core_text_font.clone_with_font_size(size),
h_kern_subtable: None,
can_do_fast_shaping: false,
};
handle.h_kern_subtable = handle.find_h_kern_subtable();
// TODO (#11310): Implement basic support for GPOS and GSUB.
handle.can_do_fast_shaping = handle.h_kern_subtable.is_some() &&
handle.table_for_tag(GPOS).is_none() &&
handle.table_for_tag(GSUB).is_none();
Ok(handle)
}
fn template(&self) -> Arc<FontTemplateData> {
self.font_data.clone()
fn template(&self) -> FontTemplateRef {
self.font_template.clone()
}
fn family_name(&self) -> Option<String> {
@ -317,8 +315,4 @@ impl FontHandleMethods for FontHandle {
let result: Option<CFData> = self.ctfont.get_font_table(tag);
result.map(FontTable::wrap)
}
fn identifier(&self) -> &FontIdentifier {
&self.font_data.identifier
}
}

View file

@ -2,10 +2,15 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::fs::File;
use std::io::Read;
use std::path::Path;
use log::debug;
use serde::{Deserialize, Serialize};
use style::Atom;
use ucd::{Codepoint, UnicodeBlock};
use webrender_api::NativeFontHandle;
use crate::text::util::unicode_plane;
@ -18,6 +23,24 @@ pub struct LocalFontIdentifier {
pub path: Atom,
}
impl LocalFontIdentifier {
pub(crate) fn native_font_handle(&self) -> Option<NativeFontHandle> {
Some(NativeFontHandle {
name: self.postscript_name.to_string(),
path: self.path.to_string(),
})
}
pub(crate) fn read_data_from_file(&self) -> Vec<u8> {
let mut bytes = Vec::new();
File::open(Path::new(&*self.path))
.expect("Couldn't open font file!")
.read_to_end(&mut bytes)
.unwrap();
bytes
}
}
pub fn for_each_available_family<F>(mut callback: F)
where
F: FnMut(String),

View file

@ -2,182 +2,73 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt;
use std::fs::File;
use std::io::{Error as IoError, Read};
use std::ops::Deref;
use std::path::Path;
use std::sync::{Arc, Mutex, RwLock};
use std::sync::OnceLock;
use app_units::Au;
use core_graphics::data_provider::CGDataProvider;
use core_graphics::font::CGFont;
use core_text::font::CTFont;
use serde::de::{Error, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use webrender_api::NativeFontHandle;
use parking_lot::RwLock;
use crate::font_cache_thread::FontIdentifier;
use crate::font_template::{FontTemplate, FontTemplateRef, FontTemplateRefMethods};
/// Platform specific font representation for MacOS. CTFont object is cached here for use
/// by the paint functions that create CGFont references.
#[derive(Deserialize, Serialize)]
pub struct FontTemplateData {
// If you add members here, review the Debug impl below
/// The `CTFont` object, if present. This is cached here so that we don't have to keep creating
/// `CTFont` instances over and over. It can always be recreated from the `identifier` and/or
/// `font_data` fields.
///
/// When sending a `FontTemplateData` instance across processes, this will be cleared out on
/// the other side, because `CTFont` instances cannot be sent across processes. This is
/// harmless, however, because it can always be recreated.
ctfont: CachedCTFont,
pub identifier: FontIdentifier,
pub font_data: RwLock<Option<Arc<Vec<u8>>>>,
// A cache of `CTFont` to avoid having to create `CTFont` instances over and over. It is
// always possible to create a `CTFont` using a `FontTemplate` even if it isn't in this
// cache.
static CTFONT_CACHE: OnceLock<RwLock<HashMap<FontIdentifier, CachedCTFont>>> = OnceLock::new();
/// A [`HashMap`] of cached [`CTFont`] for a single [`FontIdentifier`]. There is one [`CTFont`]
/// for each cached font size.
type CachedCTFont = HashMap<Au, CTFont>;
pub(crate) trait CoreTextFontTemplateMethods {
/// Retrieves a [`CTFont`] instance, instantiating it if necessary if it is not
/// stored in the shared Core Text font cache.
fn core_text_font(&self, pt_size: f64) -> Option<CTFont>;
}
impl fmt::Debug for FontTemplateData {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("FontTemplateData")
.field("ctfont", &self.ctfont)
.field("identifier", &self.identifier)
.field(
"font_data",
&self
.font_data
.read()
.unwrap()
.as_ref()
.map(|bytes| format!("[{} bytes]", bytes.len())),
)
.finish()
}
}
unsafe impl Send for FontTemplateData {}
unsafe impl Sync for FontTemplateData {}
impl FontTemplateData {
pub fn new(
identifier: FontIdentifier,
font_data: Option<Vec<u8>>,
) -> Result<FontTemplateData, IoError> {
Ok(FontTemplateData {
ctfont: CachedCTFont(Mutex::new(HashMap::new())),
identifier,
font_data: RwLock::new(font_data.map(Arc::new)),
})
}
/// Retrieves the Core Text font instance, instantiating it if necessary.
pub fn ctfont(&self, pt_size: f64) -> Option<CTFont> {
let mut ctfonts = self.ctfont.lock().unwrap();
let entry = ctfonts.entry(Au::from_f64_px(pt_size));
match entry {
Entry::Occupied(entry) => return Some(entry.get().clone()),
Entry::Vacant(_) => {},
}
// If you pass a zero font size to one of the Core Text APIs, it'll replace it with
// 12.0. We don't want that! (Issue #10492.)
impl CoreTextFontTemplateMethods for FontTemplateRef {
fn core_text_font(&self, pt_size: f64) -> Option<CTFont> {
//// If you pass a zero font size to one of the Core Text APIs, it'll replace it with
//// 12.0. We don't want that! (Issue #10492.)
let clamped_pt_size = pt_size.max(0.01);
let au_size = Au::from_f64_px(clamped_pt_size);
let provider = CGDataProvider::from_buffer(self.bytes());
let cgfont = CGFont::from_data_provider(provider).ok()?;
let ctfont = core_text::font::new_from_CGFont(&cgfont, clamped_pt_size);
// Cache the newly created CTFont font.
entry.or_insert(ctfont.clone());
Some(ctfont)
}
/// Returns a reference to the data in this font. This may be a hugely expensive
/// operation (depending on the platform) which performs synchronous disk I/O
/// and should never be done lightly.
pub fn bytes(&self) -> Arc<Vec<u8>> {
self.font_data
.write()
.unwrap()
.get_or_insert_with(|| {
let path = match &self.identifier {
FontIdentifier::Local(local) => local.path.clone(),
FontIdentifier::Web(_) => unreachable!("Web fonts should always have data."),
};
let mut bytes = Vec::new();
File::open(Path::new(&*path))
.expect("Couldn't open font file!")
.read_to_end(&mut bytes)
.unwrap();
Arc::new(bytes)
})
.clone()
}
/// Returns a reference to the bytes in this font if they are in memory.
/// This function never performs disk I/O.
pub fn bytes_if_in_memory(&self) -> Option<Arc<Vec<u8>>> {
self.font_data.read().unwrap().as_ref().cloned()
}
/// Returns the native font that underlies this font template, if applicable.
pub fn native_font(&self) -> Option<NativeFontHandle> {
let local_identifier = match &self.identifier {
FontIdentifier::Local(local_identifier) => local_identifier,
FontIdentifier::Web(_) => return None,
};
Some(NativeFontHandle {
name: local_identifier.postscript_name.to_string(),
path: local_identifier.path.to_string(),
})
}
}
#[derive(Debug)]
pub struct CachedCTFont(Mutex<HashMap<Au, CTFont>>);
impl Deref for CachedCTFont {
type Target = Mutex<HashMap<Au, CTFont>>;
fn deref(&self) -> &Mutex<HashMap<Au, CTFont>> {
&self.0
}
}
impl Serialize for CachedCTFont {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_none()
}
}
impl<'de> Deserialize<'de> for CachedCTFont {
fn deserialize<D>(deserializer: D) -> Result<CachedCTFont, D::Error>
where
D: Deserializer<'de>,
{
struct NoneOptionVisitor;
impl<'de> Visitor<'de> for NoneOptionVisitor {
type Value = CachedCTFont;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "none")
}
#[inline]
fn visit_none<E>(self) -> Result<CachedCTFont, E>
where
E: Error,
let cache = CTFONT_CACHE.get_or_init(Default::default);
let identifier = self.borrow().identifier.clone();
{
let cache = cache.read();
if let Some(core_text_font) = cache
.get(&identifier)
.and_then(|identifier_cache| identifier_cache.get(&au_size))
{
Ok(CachedCTFont(Mutex::new(HashMap::new())))
return Some(core_text_font.clone());
}
}
deserializer.deserialize_option(NoneOptionVisitor)
let mut cache = cache.write();
let identifier_cache = cache.entry(identifier).or_insert_with(Default::default);
// It could be that between the time of the cache miss above and now, after the write lock
// on the cache has been acquired, the cache was populated with the data that we need. Thus
// check again and return the CTFont if it is is already cached.
if let Some(core_text_font) = identifier_cache.get(&au_size) {
return Some(core_text_font.clone());
}
let provider = CGDataProvider::from_buffer(self.data());
let cgfont = CGFont::from_data_provider(provider).ok()?;
let core_text_font = core_text::font::new_from_CGFont(&cgfont, clamped_pt_size);
identifier_cache.insert(au_size, core_text_font.clone());
Some(core_text_font)
}
}
impl FontTemplate {
pub(crate) fn clear_core_text_font_cache() {
let cache = CTFONT_CACHE.get_or_init(Default::default);
cache.write().clear();
}
}