Skip to content

Commit 6ca04e7

Browse files
committed
load system fonts on demand
1 parent 8cdcd6e commit 6ca04e7

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

editor/src/messages/portfolio/portfolio_message_handler.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -372,28 +372,19 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageContext<'_>> for Portfolio
372372
}
373373
PortfolioMessage::LoadFontCatalog => {
374374
if Editor::environment().is_desktop() && preferences.system_fonts {
375-
let mut system_font_collection = Collection::new(CollectionOptions { shared: false, system_fonts: true });
375+
self.cached_data.system_font_collection.0 = Collection::new(CollectionOptions { shared: false, system_fonts: true });
376376
// shove font metadata into cached data catalog
377-
let system_font_family_names: Vec<String> = system_font_collection.family_names().map(|n| n.to_owned()).collect();
377+
let system_font_family_names: Vec<String> = self.cached_data.system_font_collection.0.family_names().map(|n| n.to_owned()).collect();
378378
let mut families_for_catalog = Vec::new();
379379
for name in system_font_family_names {
380-
let family = system_font_collection.family_by_name(&name).unwrap();
380+
let family = self.cached_data.system_font_collection.0.family_by_name(&name).unwrap();
381381
let mut styles = Vec::new();
382382
for font in family.fonts() {
383383
let style = FontCatalogStyle {
384384
weight: (font.weight().value()) as u32,
385385
italic: font.style() == FontStyle::Italic,
386386
url: "_SYSTEM".to_owned(),
387387
};
388-
// simultaneously call font loaded a bunch of times to shove data into font data cache
389-
// need to add logic in font loaded to handle system font loading for it to be able to work without this
390-
let mut font_data_vec = Vec::new();
391-
font_data_vec.extend(font.load(None).unwrap().data());
392-
responses.add(PortfolioMessage::FontLoaded {
393-
font_family: name.clone(),
394-
font_style: style.to_named_style(),
395-
data: font_data_vec,
396-
});
397388
styles.push(style);
398389
}
399390
families_for_catalog.push(FontCatalogFamily { name: name.to_owned(), styles });
@@ -422,7 +413,28 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageContext<'_>> for Portfolio
422413
let font = Font::new(font.font_family, style.to_named_style());
423414

424415
if !self.cached_data.font_cache.loaded_font(&font) {
425-
responses.add(FrontendMessage::TriggerFontDataLoad { font, url: style.url });
416+
if style.url == "_SYSTEM" {
417+
let system_font_family_names: Vec<String> = self.cached_data.system_font_collection.0.family_names().map(|n| n.to_owned()).collect();
418+
for name in system_font_family_names {
419+
if name == font.font_family {
420+
let family = self.cached_data.system_font_collection.0.family_by_name(&name).unwrap();
421+
for system_font_style in family.fonts() {
422+
if (system_font_style.weight().value() as u32 == style.weight) && ((system_font_style.style() == FontStyle::Italic) == style.italic) {
423+
let mut font_data_vec = Vec::new();
424+
font_data_vec.extend(system_font_style.load(None).unwrap().data());
425+
responses.add(PortfolioMessage::FontLoaded {
426+
font_family: name.clone(),
427+
font_style: style.to_named_style(),
428+
data: font_data_vec,
429+
});
430+
return;
431+
}
432+
}
433+
}
434+
}
435+
} else {
436+
responses.add(FrontendMessage::TriggerFontDataLoad { font, url: style.url });
437+
}
426438
}
427439
}
428440
}

editor/src/messages/portfolio/utility_types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
use graphene_std::Color;
22
use graphene_std::raster::Image;
33
use graphene_std::text::{Font, FontCache};
4+
use parley::fontique;
5+
use std::fmt;
46

57
#[derive(Debug, Default)]
68
pub struct CachedData {
79
pub font_cache: FontCache,
810
pub font_catalog: FontCatalog,
11+
pub system_font_collection: FontCollection,
12+
}
13+
14+
#[derive(Default)]
15+
pub struct FontCollection(pub fontique::Collection);
16+
17+
impl fmt::Debug for FontCollection {
18+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19+
write!(f, "Font Collection")
20+
}
921
}
1022

1123
// TODO: Should this be a BTreeMap instead?

0 commit comments

Comments
 (0)