Move fontdb source handling to font

This commit is contained in:
Jeremy Soller 2022-10-25 14:49:44 -06:00
parent a3ccbd0481
commit 66885686a0
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
2 changed files with 16 additions and 20 deletions

View file

@ -1,5 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use std::ops::Deref;
pub struct Font<'a> {
pub info: &'a fontdb::FaceInfo,
pub data: &'a [u8],
@ -10,14 +12,23 @@ pub struct Font<'a> {
}
impl<'a> Font<'a> {
pub fn new(info: &'a fontdb::FaceInfo, data: &'a [u8], index: u32) -> Option<Self> {
pub fn new(info: &'a fontdb::FaceInfo) -> Option<Self> {
let data = match &info.source {
fontdb::Source::Binary(data) => data.deref().as_ref(),
fontdb::Source::File(path) => {
log::warn!("Unsupported fontdb Source::File('{}')", path.display());
return None;
}
fontdb::Source::SharedFile(_path, data) => data.deref().as_ref(),
};
Some(Self {
info,
data,
index,
rustybuzz: rustybuzz::Face::from_slice(data, index)?,
index: info.index,
rustybuzz: rustybuzz::Face::from_slice(data, info.index)?,
#[cfg(feature = "swash")]
swash: swash::FontRef::from_index(data, index as usize)?,
swash: swash::FontRef::from_index(data, info.index as usize)?,
})
}
}

View file

@ -1,7 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use std::ops::Deref;
use crate::{Attrs, Font, FontMatches};
/// Access system fonts
@ -51,20 +49,7 @@ impl FontSystem {
continue;
}
let font_opt = Font::new(
face,
match &face.source {
fontdb::Source::Binary(data) => data.deref().as_ref(),
fontdb::Source::File(path) => {
log::warn!("Unsupported fontdb Source::File('{}')", path.display());
continue;
}
fontdb::Source::SharedFile(_path, data) => data.deref().as_ref(),
},
face.index,
);
match font_opt {
match Font::new(face) {
Some(font) => fonts.push(font),
None => {
log::warn!("failed to load font '{}'", face.post_script_name);