From 785901c75e4ddf017c4e6cf16c30554409594c1a Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Tue, 24 Oct 2023 12:32:30 +0200 Subject: [PATCH] Load embedded fonts --- crates/gpui3/src/text_system.rs | 4 ++++ crates/storybook2/src/storybook2.rs | 31 ++++++++++++++++------------- crates/ui2/src/story.rs | 2 +- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/crates/gpui3/src/text_system.rs b/crates/gpui3/src/text_system.rs index 3b16bb506c1f89e7316f4918dcc3052021deda25..7c96b1a180c70fb2039995e969890ff5878b3733 100644 --- a/crates/gpui3/src/text_system.rs +++ b/crates/gpui3/src/text_system.rs @@ -55,6 +55,10 @@ impl TextSystem { } } + pub fn add_fonts(&self, fonts: &[Arc>]) -> Result<()> { + self.platform_text_system.add_fonts(fonts) + } + pub fn font_id(&self, font: &Font) -> Result { let font_id = self.font_ids_by_font.read().get(font).copied(); if let Some(font_id) = font_id { diff --git a/crates/storybook2/src/storybook2.rs b/crates/storybook2/src/storybook2.rs index 4beca32ca18377c948d44ac8d84b65801b237a88..f8b8b747e9960474e68c06bd4eb9f05d2b188d01 100644 --- a/crates/storybook2/src/storybook2.rs +++ b/crates/storybook2/src/storybook2.rs @@ -10,8 +10,8 @@ use std::sync::Arc; use clap::Parser; use gpui3::{ - div, px, size, view, AnyView, BorrowAppContext, Bounds, Context, Element, ViewContext, - WindowBounds, WindowOptions, + div, px, size, view, AnyView, AppContext, AssetSource, BorrowAppContext, Bounds, Context, + Element, ViewContext, WindowBounds, WindowOptions, }; use log::LevelFilter; use simplelog::SimpleLogger; @@ -54,6 +54,8 @@ fn main() { let asset_source = Arc::new(Assets); gpui3::App::production(asset_source).run(move |cx| { + load_embedded_fonts(cx); + let selector = story_selector.unwrap_or(StorySelector::Component(ComponentStory::Workspace)); @@ -112,15 +114,16 @@ impl StoryWrapper { } } -// fn load_embedded_fonts(platform: &dyn gpui2::Platform) { -// let font_paths = Assets.list("fonts"); -// let mut embedded_fonts = Vec::new(); -// for font_path in &font_paths { -// if font_path.ends_with(".ttf") { -// let font_path = &*font_path; -// let font_bytes = Assets.load(font_path).unwrap().to_vec(); -// embedded_fonts.push(Arc::from(font_bytes)); -// } -// } -// platform.fonts().add_fonts(&embedded_fonts).unwrap(); -// } +fn load_embedded_fonts(cx: &AppContext) { + let font_paths = Assets.list(&"fonts".into()).unwrap(); + let mut embedded_fonts = Vec::new(); + for font_path in &font_paths { + if font_path.ends_with(".ttf") { + let font_path = &*font_path; + let font_bytes = Assets.load(font_path).unwrap().to_vec(); + embedded_fonts.push(Arc::from(font_bytes)); + } + } + + cx.text_system().add_fonts(&embedded_fonts).unwrap(); +} diff --git a/crates/ui2/src/story.rs b/crates/ui2/src/story.rs index 2a753df4f1ce1884c4c7504461377727f0e5546b..e51ff0f8568b0c7db93425862dd5ac117a4ac03d 100644 --- a/crates/ui2/src/story.rs +++ b/crates/ui2/src/story.rs @@ -14,7 +14,7 @@ impl Story { .flex_col() .pt_2() .px_4() - .font("Zed Mono Extended") + .font("Zed Mono") .bg(color.background) }