Skip to content

Commit

Permalink
Database::load_font_source returns a list of face IDs now.
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed May 9, 2023
1 parent c07be3a commit ef72752
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -16,6 +16,7 @@ rust-version = "1.49"
log = "0.4"
memmap2 = { version = "0.5", optional = true }
slotmap = { version = "1.0.6", default-features = false }
tinyvec = { version = "1.6.0", features = ["alloc"] }

[dependencies.ttf-parser]
version = "0.18"
Expand Down
23 changes: 17 additions & 6 deletions src/lib.rs
Expand Up @@ -73,6 +73,7 @@ pub use ttf_parser::Language;
pub use ttf_parser::Width as Stretch;

use slotmap::SlotMap;
use tinyvec::TinyVec;

/// A unique per database face ID.
///
Expand All @@ -88,7 +89,7 @@ use slotmap::SlotMap;
/// content of the strings.
///
/// [`KeyData`]: https://docs.rs/slotmap/latest/slotmap/struct.KeyData.html
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)]
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Debug, Default)]
pub struct ID(InnerId);

slotmap::new_key_type! {
Expand Down Expand Up @@ -192,26 +193,36 @@ impl Database {
///
/// Will load all font faces in case of a font collection.
pub fn load_font_data(&mut self, data: Vec<u8>) {
self.load_font_source(Source::Binary(alloc::sync::Arc::new(data)))
self.load_font_source(Source::Binary(alloc::sync::Arc::new(data)));
}

/// Loads a font from the given source into the `Database`.
/// Loads a font from the given source into the `Database` and returns
/// the ID of the loaded font.
///
/// Will load all font faces in case of a font collection.
pub fn load_font_source(&mut self, source: Source) {
source.with_data(|data| {
pub fn load_font_source(&mut self, source: Source) -> TinyVec<[ID; 8]> {
let ids = source.with_data(|data| {
let n = ttf_parser::fonts_in_collection(data).unwrap_or(1);
let mut ids = TinyVec::with_capacity(n as usize);

for index in 0..n {
match parse_face_info(source.clone(), data, index) {
Ok(info) => self.push_face_info(info),
Ok(info) => {
ids.push(info.id);
self.push_face_info(info);
}
Err(e) => log::warn!(
"Failed to load a font face {} from source cause {}.",
index,
e
),
}
}

ids
});

ids.unwrap_or_default()
}

/// Backend function used by load_font_file to load font files.
Expand Down

0 comments on commit ef72752

Please sign in to comment.