Use shared font face data

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 18:19:12 +00:00
1 parent 624d98186e
commit 0527be0f9a
5 files changed
+18 -32

No files matched your search

Generated
+1 -1
View File
@@ -1008,7 +1008,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=6d620e971e820b517b11a9
[[package]]
name = "craft-font-catalog"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=f25ce3949366a04f63d30b93def0a43b32ffba99#f25ce3949366a04f63d30b93def0a43b32ffba99"
source = "git+https://github.com/storytold/craft-libs?rev=83b240500fb113b484df2812b6b708566a9c72c0#83b240500fb113b484df2812b6b708566a9c72c0"
dependencies = [
"harfrust 0.13.3",
"kurbo 0.13.1",
+1 -1
View File
@@ -10,7 +10,7 @@ workspace = true
[dependencies]
# Pending shared extraction; pin the immutable official PR head until it merges.
craft-font-catalog = { git = "https://github.com/storytold/craft-libs", rev = "f25ce3949366a04f63d30b93def0a43b32ffba99", default-features = false }
craft-font-catalog = { git = "https://github.com/storytold/craft-libs", rev = "83b240500fb113b484df2812b6b708566a9c72c0", default-features = false }
vectorcraft-geom = { workspace = true }
vectorcraft-doc = { workspace = true }
kurbo = { workspace = true }
+7 -11
View File
@@ -1,7 +1,8 @@
use super::face::{FontBytes, FontFace, Variation};
use super::face::{FontFace, Variation};
use super::names::{name, norm};
use super::traits::FaceStyle;
use super::{HashMap, Location, MetadataProvider, NEXT_ID, Ordering, Size, StringId, Tag};
use craft_font_catalog::FaceData;
/// `face` at the axis settings `variations` (user units), as face `id`: its names, kind, italic
/// and file, and its weight the `wght` setting when there is one.
@@ -17,24 +18,20 @@ pub(super) fn instanced(face: &FontFace, variations: Vec<Variation>, id: u32) ->
italic: face.italic,
traits: face.traits,
};
let mut f = make_face(face.bytes.clone(), face.index, spec, face.path.clone())?;
let mut f = make_face(face.data.clone(), spec, face.path.clone())?;
f.id = id;
Some(f)
}
pub(super) fn make_face(bytes: FontBytes, index: u32, spec: FaceStyle, path: Option<std::path::PathBuf>) -> Option<FontFace> {
let data: &[u8] = match &bytes {
FontBytes::Static(b) => b,
FontBytes::Owned(v) => v.as_slice(),
};
let f = skrifa::FontRef::from_index(data, index).ok()?;
pub(super) fn make_face(data: FaceData, spec: FaceStyle, path: Option<std::path::PathBuf>) -> Option<FontFace> {
let f = data.skrifa()?;
let FaceStyle { family, native_family, style, keys, variations, weight, italic, traits } = spec;
let version = name(&f, &[StringId::VERSION_STRING]).unwrap_or_default();
// A named instance: outlines, metrics and advances at its axis settings.
let location = if variations.is_empty() { Location::default() } else { f.axes().location(variations.iter().map(|(t, v)| (Tag::new(t), *v))) };
let m = f.metrics(Size::unscaled(), &location);
let a = f.attributes();
let hb = harfrust::FontRef::from_index(data, index).ok()?;
let hb = data.harfrust()?;
let shaper = harfrust::ShaperData::new(&hb);
let instance =
(!variations.is_empty()).then(|| harfrust::ShaperInstance::from_variations(&hb, variations.iter().map(|(t, v)| (harfrust::Tag::new(t), *v))));
@@ -69,8 +66,7 @@ pub(super) fn make_face(bytes: FontBytes, index: u32, spec: FaceStyle, path: Opt
underline,
strikeout,
shaper,
bytes,
index,
data,
path,
plans: craft_font_catalog::ShapePlanCache::default(),
ideographic_centre: std::sync::OnceLock::new(),
+3 -3
View File
@@ -6,7 +6,7 @@ use super::cache::CatalogFace;
use super::cache::{CanonicalNames, Catalog, GlyphCache, PostScriptNames};
#[cfg(not(target_arch = "wasm32"))]
use super::dirs::confined_font_dirs;
use super::face::{BUNDLED, FontBytes, FontFace, Instances};
use super::face::{BUNDLED, FontFace, Instances};
#[cfg(not(target_arch = "wasm32"))]
use super::names::SysFallback;
use super::names::name;
@@ -45,7 +45,7 @@ impl FontDb {
let craft = crate::craft_fonts::document_fonts().into_iter().map(|f| f.bytes);
for data in BUNDLED.iter().copied().chain(craft) {
for (i, spec) in enumerate_faces(data) {
if let Some(f) = make_face(FontBytes::Static(data), i, spec, None) {
if let Some(f) = make_face(craft_font_catalog::FaceData::from_static(data, i), spec, None) {
faces.push(Arc::new(f));
}
}
@@ -300,7 +300,7 @@ impl FontDb {
if self.read_faces().iter().any(|f| present(f)) || made.iter().any(present) {
continue;
}
if let Some(f) = make_face(FontBytes::Owned(data.clone()), i, spec, path.clone()) {
if let Some(f) = make_face(craft_font_catalog::FaceData::from_owned(data.clone(), i), spec, path.clone()) {
made.push(f);
}
}
+6 -16
View File
@@ -132,12 +132,6 @@ pub(super) type InstanceKey = (u32, Vec<([u8; 4], u32)>);
/// How many instanced faces [`Instances::live`] holds at most.
pub(crate) const MAX_LIVE_INSTANCES: usize = 256;
#[derive(Clone)]
pub(super) enum FontBytes {
Static(&'static [u8]),
Owned(Arc<Vec<u8>>),
}
/// One loaded font face.
pub struct FontFace {
/// Every name the face answers to (normalized), in every language its name table has.
@@ -158,8 +152,7 @@ pub struct FontFace {
/// usWeightClass-style weight (400 = regular).
pub weight: f32,
pub italic: bool,
pub(super) bytes: FontBytes,
pub(super) index: u32,
pub(super) data: craft_font_catalog::FaceData,
/// The file the face was read from (cataloged system fonts); `None` for the bundled fonts and
/// fonts added as bytes.
pub(super) path: Option<std::path::PathBuf>,
@@ -246,16 +239,13 @@ impl std::fmt::Debug for FontFace {
impl FontFace {
pub(crate) fn data(&self) -> &[u8] {
match &self.bytes {
FontBytes::Static(b) => b,
FontBytes::Owned(v) => v.as_slice(),
}
self.data.bytes()
}
pub(crate) fn skrifa(&self) -> Option<skrifa::FontRef<'_>> {
skrifa::FontRef::from_index(self.data(), self.index).ok()
self.data.skrifa()
}
pub(crate) fn hb(&self) -> Option<harfrust::FontRef<'_>> {
harfrust::FontRef::from_index(self.data(), self.index).ok()
self.data.harfrust()
}
/// The shaping plan for a buffer of `direction`, `script` and `language` shaped with
/// `features` by `shaper` (made from this face at [`Self::instance`]): the plan made earlier for
@@ -283,7 +273,7 @@ impl FontFace {
}
/// The face's index in its font file (collections hold several).
pub(crate) fn index(&self) -> u32 {
self.index
self.data.index()
}
/// Where in a variable font's design space the face's outlines and metrics are taken (the
/// default location for a static face).
@@ -676,7 +666,7 @@ impl FontFace {
}
/// The face's index in its font file ([`Self::file_data`]; collections hold several).
pub fn face_index(&self) -> u32 {
self.index
self.data.index()
}
/// The face's PostScript name, as PostScript files name fonts (`SourceSans3-Bold`): name ID 6;
/// for a named instance of a variable font (whose name ID 6 is the default instance's), its