diff --git a/Cargo.lock b/Cargo.lock index 97bd54f302..de6cb847da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/crates/text/Cargo.toml b/crates/text/Cargo.toml index 1df3a800fb..e81e33602c 100644 --- a/crates/text/Cargo.toml +++ b/crates/text/Cargo.toml @@ -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 } diff --git a/crates/text/src/fontdb/build.rs b/crates/text/src/fontdb/build.rs index 69ffc97922..e5fb7ed67c 100644 --- a/crates/text/src/fontdb/build.rs +++ b/crates/text/src/fontdb/build.rs @@ -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, 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) -> Option { - 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) -> Option { + 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(), diff --git a/crates/text/src/fontdb/db.rs b/crates/text/src/fontdb/db.rs index 738a158952..d53170800d 100644 --- a/crates/text/src/fontdb/db.rs +++ b/crates/text/src/fontdb/db.rs @@ -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); } } diff --git a/crates/text/src/fontdb/face.rs b/crates/text/src/fontdb/face.rs index a7cc660ccd..e9f35a5f58 100644 --- a/crates/text/src/fontdb/face.rs +++ b/crates/text/src/fontdb/face.rs @@ -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>), -} - /// 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, @@ -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::from_index(self.data(), self.index).ok() + self.data.skrifa() } pub(crate) fn hb(&self) -> Option> { - 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