cleaner, more consistent

macos-click-through
exoticus 5 years ago
commit a42c7a3b4e

@ -4,6 +4,7 @@ use font_kit::{
metrics::Metrics, metrics::Metrics,
properties::{Properties, Stretch, Style, Weight}, properties::{Properties, Stretch, Style, Weight},
source::SystemSource, source::SystemSource,
handle::Handle
}; };
use log::{trace, warn}; use log::{trace, warn};
use lru::LruCache; use lru::LruCache;
@ -71,7 +72,19 @@ impl ExtendedFontFamily {
None None
} }
pub fn to_family(self) -> FontFamily { pub fn from_normal_font_family(fonts: &[Handle]) -> ExtendedFontFamily {
let mut family = ExtendedFontFamily::new();
for font in fonts.into_iter() {
if let Ok(font) = font.load() {
family.add_font(SkriboFont::new(font));
}
}
family
}
pub fn to_normal_font_family(self) -> FontFamily {
let mut new_family = FontFamily::new(); let mut new_family = FontFamily::new();
for font in self.fonts { for font in self.fonts {
@ -96,11 +109,7 @@ impl FontLoader {
} }
fn get(&mut self, font_name: &str) -> Option<ExtendedFontFamily> { fn get(&mut self, font_name: &str) -> Option<ExtendedFontFamily> {
if let Some(family) = self.cache.get(&String::from(font_name)) { return self.cache.get(&String::from(font_name)).cloned();
return Some(family.clone());
}
None
} }
#[cfg(feature = "embed-fonts")] #[cfg(feature = "embed-fonts")]
@ -121,14 +130,7 @@ impl FontLoader {
_ => return None, _ => return None,
}; };
let fonts = handle.fonts(); let family = ExtendedFontFamily::from_normal_font_family(handle.fonts());
let mut family = ExtendedFontFamily::new();
for font in fonts.into_iter() {
if let Ok(font) = font.load() {
family.add_font(SkriboFont::new(font));
}
}
self.cache.put(String::from(font_name), family); self.cache.put(String::from(font_name), family);
self.get(font_name) self.get(font_name)
@ -185,13 +187,13 @@ pub fn build_collection_by_font_name(
for font in &[SYSTEM_SYMBOL_FONT, SYSTEM_EMOJI_FONT] { for font in &[SYSTEM_SYMBOL_FONT, SYSTEM_EMOJI_FONT] {
if let Some(family) = loader.get_or_load(font, false) { if let Some(family) = loader.get_or_load(font, false) {
collection.add_family(family.to_family()); collection.add_family(family.to_normal_font_family());
} }
} }
for font in &[EXTRA_SYMBOL_FONT, MISSING_GLYPH_FONT] { for font in &[EXTRA_SYMBOL_FONT, MISSING_GLYPH_FONT] {
if let Some(family) = loader.get_or_load(font, true) { if let Some(family) = loader.get_or_load(font, true) {
collection.add_family(family.to_family()); collection.add_family(family.to_normal_font_family());
} }
} }

Loading…
Cancel
Save