aboutsummaryrefslogtreecommitdiffstats
path: root/components/gfx/platform/macos
diff options
context:
space:
mode:
authorMukilan Thiyagarajan <mukilan@igalia.com>2024-04-22 15:08:21 +0530
committerGitHub <noreply@github.com>2024-04-22 09:38:21 +0000
commit821893b2eecfc72918ab8154c3cb61cd45d53857 (patch)
tree035596a6a3af9a3138b0a58974d9745ffa702a26 /components/gfx/platform/macos
parent25b182c372427e798954b814b0f1a0875ab43f98 (diff)
downloadservo-821893b2eecfc72918ab8154c3cb61cd45d53857.tar.gz
servo-821893b2eecfc72918ab8154c3cb61cd45d53857.zip
fonts: Rework platform font initialization (#32127)
This change reworks the way that platform fonts are created and descriptor data is on `FontTemplate` is initialized. The main change here is that platform fonts for local font faces are always initialized using the font data loaded into memory from disk. This means that there is now only a single path for creating platform fonts. In addition, the font list is now responsible for getting the `FontTemplateDescriptor` for local `FontTemplate`s. Before the font had to be loaded into memory to get the weight, style, and width used for the descriptor. This is what fonts lists are for though, so for every platform we have that information before needing to load the font. In the future, hopefully this will allow discarding fonts before needing to load them into memory. Web fonts still get the descriptor from the platform handle, but hopefully that can be done with skrifa in the future. Thsese two fixes together allow properly loading indexed font variations on Linux machines. Before only the first variation could be instantiated. Fixes https://github.com/servo/servo/issues/13317. Fixes https://github.com/servo/servo/issues/24554. Co-authored-by: Martin Robinson <mrobinson@igalia.com> ---- - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #13317 and #24554 - [x] There are tests for these changes --------- Co-authored-by: Martin Robinson <mrobinson@igalia.com>
Diffstat (limited to 'components/gfx/platform/macos')
-rw-r--r--components/gfx/platform/macos/core_text_font_cache.rs106
-rw-r--r--components/gfx/platform/macos/font.rs91
-rw-r--r--components/gfx/platform/macos/font_list.rs27
-rw-r--r--components/gfx/platform/macos/font_template.rs74
4 files changed, 190 insertions, 108 deletions
diff --git a/components/gfx/platform/macos/core_text_font_cache.rs b/components/gfx/platform/macos/core_text_font_cache.rs
new file mode 100644
index 00000000000..4232b3f375d
--- /dev/null
+++ b/components/gfx/platform/macos/core_text_font_cache.rs
@@ -0,0 +1,106 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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::HashMap;
+use std::sync::{Arc, OnceLock};
+
+use app_units::Au;
+use core_foundation::base::TCFType;
+use core_foundation::string::CFString;
+use core_foundation::url::{kCFURLPOSIXPathStyle, CFURL};
+use core_graphics::data_provider::CGDataProvider;
+use core_graphics::display::CFDictionary;
+use core_graphics::font::CGFont;
+use core_text::font::CTFont;
+use core_text::font_descriptor::kCTFontURLAttribute;
+use parking_lot::RwLock;
+
+use crate::font_cache_thread::FontIdentifier;
+
+/// 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.
+pub(crate) struct CoreTextFontCache(OnceLock<RwLock<HashMap<FontIdentifier, CachedCTFont>>>);
+
+/// The global [`CoreTextFontCache`].
+static CACHE: CoreTextFontCache = CoreTextFontCache(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>;
+
+impl CoreTextFontCache {
+ pub(crate) fn core_text_font(
+ font_identifier: FontIdentifier,
+ data: Arc<Vec<u8>>,
+ 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 cache = CACHE.0.get_or_init(Default::default);
+ {
+ let cache = cache.read();
+ if let Some(core_text_font) = cache
+ .get(&font_identifier)
+ .and_then(|identifier_cache| identifier_cache.get(&au_size))
+ {
+ return Some(core_text_font.clone());
+ }
+ }
+
+ let mut cache = cache.write();
+ let identifier_cache = cache
+ .entry(font_identifier.clone())
+ .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 core_text_font = match font_identifier {
+ FontIdentifier::Local(local_font_identifier) => {
+ // Other platforms can instantiate a platform font by loading the data
+ // from a file and passing an index in the case the file is a TTC bundle.
+ // The only way to reliably load the correct font from a TTC bundle on
+ // macOS is to create the font using a descriptor with both the PostScript
+ // name and path.
+ let cf_name = CFString::new(&local_font_identifier.postscript_name);
+ let mut descriptor = core_text::font_descriptor::new_from_postscript_name(&cf_name);
+
+ let cf_path = CFString::new(&local_font_identifier.path);
+ let url_attribute = unsafe { CFString::wrap_under_get_rule(kCTFontURLAttribute) };
+ let attributes = CFDictionary::from_CFType_pairs(&[(
+ url_attribute,
+ CFURL::from_file_system_path(cf_path, kCFURLPOSIXPathStyle, false),
+ )]);
+ if let Ok(descriptor_with_path) =
+ descriptor.create_copy_with_attributes(attributes.to_untyped())
+ {
+ descriptor = descriptor_with_path;
+ }
+
+ core_text::font::new_from_descriptor(&descriptor, clamped_pt_size)
+ },
+ FontIdentifier::Web(_) => {
+ let provider = CGDataProvider::from_buffer(data);
+ let cgfont = CGFont::from_data_provider(provider).ok()?;
+ core_text::font::new_from_CGFont(&cgfont, clamped_pt_size)
+ },
+ };
+
+ identifier_cache.insert(au_size, core_text_font.clone());
+ Some(core_text_font)
+ }
+
+ pub(crate) fn clear_core_text_font_cache() {
+ let cache = CACHE.0.get_or_init(Default::default);
+ cache.write().clear();
+ }
+}
diff --git a/components/gfx/platform/macos/font.rs b/components/gfx/platform/macos/font.rs
index 477ec36e3cd..9fd3934ed07 100644
--- a/components/gfx/platform/macos/font.rs
+++ b/components/gfx/platform/macos/font.rs
@@ -16,17 +16,17 @@ use core_foundation::string::UniChar;
use core_graphics::font::CGGlyph;
use core_text::font::CTFont;
use core_text::font_descriptor::{
- kCTFontDefaultOrientation, SymbolicTraitAccessors, TraitAccessors,
+ kCTFontDefaultOrientation, CTFontTraits, SymbolicTraitAccessors, TraitAccessors,
};
use log::debug;
use style::values::computed::font::{FontStretch, FontStyle, FontWeight};
-use super::font_template::CoreTextFontTemplateMethods;
+use super::core_text_font_cache::CoreTextFontCache;
use crate::font::{
- FontMetrics, FontTableMethods, FontTableTag, FractionalPixel, PlatformFontMethods, GPOS, GSUB,
- KERN,
+ map_platform_values_to_style_values, FontMetrics, FontTableMethods, FontTableTag,
+ FractionalPixel, PlatformFontMethods, GPOS, GSUB, KERN,
};
-use crate::font_template::FontTemplateRef;
+use crate::font_cache_thread::FontIdentifier;
use crate::text::glyph::GlyphId;
const KERN_PAIR_LEN: usize = 6;
@@ -57,7 +57,7 @@ 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: Option<Arc<Vec<u8>>>,
+ _data: Arc<Vec<u8>>,
h_kern_subtable: Option<CachedKernTable>,
can_do_fast_shaping: bool,
}
@@ -158,20 +158,24 @@ impl fmt::Debug for CachedKernTable {
}
impl PlatformFontMethods for PlatformFont {
- fn new_from_template(
- font_template: FontTemplateRef,
+ fn new_from_data(
+ font_identifier: FontIdentifier,
+ data: Arc<Vec<u8>>,
+ _face_index: u32,
pt_size: Option<Au>,
) -> Result<PlatformFont, &'static str> {
let size = match pt_size {
Some(s) => s.to_f64_px(),
None => 0.0,
};
- let Some(core_text_font) = font_template.core_text_font(size) else {
+ let Some(core_text_font) =
+ CoreTextFontCache::core_text_font(font_identifier, data.clone(), size)
+ else {
return Err("Could not generate CTFont for FontTemplateData");
};
let mut handle = PlatformFont {
- _data: font_template.borrow().data_if_in_memory(),
+ _data: data,
ctfont: core_text_font.clone_with_font_size(size),
h_kern_subtable: None,
can_do_fast_shaping: false,
@@ -193,29 +197,15 @@ impl PlatformFontMethods for PlatformFont {
}
fn style(&self) -> FontStyle {
- if self.ctfont.symbolic_traits().is_italic() {
- FontStyle::ITALIC
- } else {
- FontStyle::NORMAL
- }
+ self.ctfont.all_traits().style()
}
fn boldness(&self) -> FontWeight {
- let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]
-
- // TODO(emilio): It may make sense to make this range [.01, 10.0], to
- // align with css-fonts-4's range of [1, 1000].
- let normalized = if normalized <= 0.0 {
- 4.0 + normalized * 3.0 // [1.0, 4.0]
- } else {
- 4.0 + normalized * 5.0 // [4.0, 9.0]
- }; // [1.0, 9.0], centered on 4.0
- FontWeight::from_float(normalized as f32 * 100.)
+ self.ctfont.all_traits().weight()
}
fn stretchiness(&self) -> FontStretch {
- let normalized = self.ctfont.all_traits().normalized_width(); // [-1.0, 1.0]
- FontStretch::from_percentage(normalized as f32 + 1.0)
+ self.ctfont.all_traits().stretch()
}
fn glyph_index(&self, codepoint: char) -> Option<GlyphId> {
@@ -315,3 +305,50 @@ impl PlatformFontMethods for PlatformFont {
result.map(FontTable::wrap)
}
}
+
+pub(super) trait CoreTextFontTraitsMapping {
+ fn weight(&self) -> FontWeight;
+ fn style(&self) -> FontStyle;
+ fn stretch(&self) -> FontStretch;
+}
+
+impl CoreTextFontTraitsMapping for CTFontTraits {
+ fn weight(&self) -> FontWeight {
+ // From https://developer.apple.com/documentation/coretext/kctfontweighttrait?language=objc
+ // > The value returned is a CFNumberRef representing a float value between -1.0 and
+ // > 1.0 for normalized weight. The value of 0.0 corresponds to the regular or
+ // > medium font weight.
+ let mapping = [(-1., 0.), (0., 400.), (1., 1000.)];
+
+ let mapped_weight = map_platform_values_to_style_values(&mapping, self.normalized_weight());
+ FontWeight::from_float(mapped_weight as f32)
+ }
+
+ fn style(&self) -> FontStyle {
+ let slant = self.normalized_slant();
+ if slant == 0. && self.symbolic_traits().is_italic() {
+ return FontStyle::ITALIC;
+ }
+ if slant == 0. {
+ return FontStyle::NORMAL;
+ }
+
+ // From https://developer.apple.com/documentation/coretext/kctfontslanttrait?language=objc
+ // > The value returned is a CFNumberRef object representing a float value
+ // > between -1.0 and 1.0 for normalized slant angle. The value of 0.0
+ // > corresponds to 0 degrees clockwise rotation from the vertical and 1.0
+ // > corresponds to 30 degrees clockwise rotation.
+ let mapping = [(-1., -30.), (0., 0.), (1., 30.)];
+ let mapped_slant = map_platform_values_to_style_values(&mapping, slant);
+ FontStyle::oblique(mapped_slant as f32)
+ }
+
+ fn stretch(&self) -> FontStretch {
+ // From https://developer.apple.com/documentation/coretext/kctfontwidthtrait?language=objc
+ // > This value corresponds to the relative interglyph spacing for a given font.
+ // > The value returned is a CFNumberRef object representing a float between -1.0
+ // > and 1.0. The value of 0.0 corresponds to regular glyph spacing, and negative
+ // > values represent condensed glyph spacing.
+ FontStretch::from_percentage(self.normalized_width() as f32 + 1.0)
+ }
+}
diff --git a/components/gfx/platform/macos/font_list.rs b/components/gfx/platform/macos/font_list.rs
index 8d3dc2a09e8..dddd0b93275 100644
--- a/components/gfx/platform/macos/font_list.rs
+++ b/components/gfx/platform/macos/font_list.rs
@@ -12,6 +12,8 @@ use style::Atom;
use ucd::{Codepoint, UnicodeBlock};
use webrender_api::NativeFontHandle;
+use crate::font_template::{FontTemplate, FontTemplateDescriptor};
+use crate::platform::font::CoreTextFontTraitsMapping;
use crate::text::util::unicode_plane;
/// An identifier for a local font on a MacOS system. These values comes from the CoreText
@@ -24,11 +26,15 @@ pub struct LocalFontIdentifier {
}
impl LocalFontIdentifier {
- pub(crate) fn native_font_handle(&self) -> Option<NativeFontHandle> {
- Some(NativeFontHandle {
+ pub(crate) fn native_font_handle(&self) -> NativeFontHandle {
+ NativeFontHandle {
name: self.postscript_name.to_string(),
path: self.path.to_string(),
- })
+ }
+ }
+
+ pub(crate) fn index(&self) -> u32 {
+ 0
}
pub(crate) fn read_data_from_file(&self) -> Vec<u8> {
@@ -53,10 +59,9 @@ where
pub fn for_each_variation<F>(family_name: &str, mut callback: F)
where
- F: FnMut(LocalFontIdentifier),
+ F: FnMut(FontTemplate),
{
debug!("Looking for faces of family: {}", family_name);
-
let family_collection = core_text::font_collection::create_for_family(family_name);
if let Some(family_collection) = family_collection {
if let Some(family_descriptors) = family_collection.get_descriptors() {
@@ -66,10 +71,18 @@ where
Some(path) => path,
None => continue,
};
- callback(LocalFontIdentifier {
+
+ let traits = family_descriptor.traits();
+ let descriptor = FontTemplateDescriptor {
+ weight: traits.weight(),
+ stretch: traits.stretch(),
+ style: traits.style(),
+ };
+ let identifier = LocalFontIdentifier {
postscript_name: Atom::from(family_descriptor.font_name()),
path: Atom::from(path),
- })
+ };
+ callback(FontTemplate::new_local(identifier, descriptor));
}
}
}
diff --git a/components/gfx/platform/macos/font_template.rs b/components/gfx/platform/macos/font_template.rs
deleted file mode 100644
index f7a4cde5efa..00000000000
--- a/components/gfx/platform/macos/font_template.rs
+++ /dev/null
@@ -1,74 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * 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::HashMap;
-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 parking_lot::RwLock;
-
-use crate::font_cache_thread::FontIdentifier;
-use crate::font_template::{FontTemplate, FontTemplateRef, FontTemplateRefMethods};
-
-// 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 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 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))
- {
- return Some(core_text_font.clone());
- }
- }
-
- 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();
- }
-}