From 66885686a095c128f5fcb2a18da80682fa6ab01c Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 25 Oct 2022 14:49:44 -0600 Subject: [PATCH] Move fontdb source handling to font --- src/font/font.rs | 19 +++++++++++++++---- src/font/system.rs | 17 +---------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/font/font.rs b/src/font/font.rs index 525d42a..bcdc359 100644 --- a/src/font/font.rs +++ b/src/font/font.rs @@ -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 { + pub fn new(info: &'a fontdb::FaceInfo) -> Option { + 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)?, }) } } diff --git a/src/font/system.rs b/src/font/system.rs index 0809d1f..1261225 100644 --- a/src/font/system.rs +++ b/src/font/system.rs @@ -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);