From 3faf4247002651d76163385a9716dc9896a2c910 Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Wed, 9 Aug 2023 11:22:59 -0400 Subject: [PATCH 1/3] Add font loader for Sandbox, Program and Application. --- src/application.rs | 4 +++- src/settings.rs | 27 +++++++++++++++++++++++++++ winit/src/application.rs | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/application.rs b/src/application.rs index 9518b8c59d..91cd3ee9f8 100644 --- a/src/application.rs +++ b/src/application.rs @@ -205,11 +205,13 @@ pub trait Application: Sized { ..crate::renderer::Settings::default() }; + let custom_fonts = settings.custom_fonts.clone(); + Ok(crate::shell::application::run::< Instance, Self::Executor, crate::renderer::Compositor, - >(settings.into(), renderer_settings)?) + >(settings.into(), renderer_settings, custom_fonts)?) } } diff --git a/src/settings.rs b/src/settings.rs index 0dd465849a..7e00f37b92 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,4 +1,6 @@ //! Configure your application. +use std::borrow::Cow; + use crate::window; use crate::Font; @@ -49,6 +51,13 @@ pub struct Settings { /// /// [`Application`]: crate::Application pub exit_on_close_request: bool, + + /// Custom fonts to be loaded when iced initiates. + /// uses the Bytes of loaded fonts. + /// This will be slower than using commands to batch load. + /// + /// By default, it is `Vec::new()`. + pub custom_fonts: Vec>, } impl Settings { @@ -66,8 +75,25 @@ impl Settings { default_text_size: default_settings.default_text_size, antialiasing: default_settings.antialiasing, exit_on_close_request: default_settings.exit_on_close_request, + custom_fonts: default_settings.custom_fonts, } } + + /// Adds Font bytes to the custom_fonts Vec to be loaded on + /// [`Application`] initiation. + /// + /// [`Application`]: crate::Application + pub fn add_custom_fonts( + mut self, + fonts: impl IntoIterator>>, + ) -> Self { + let mut fonts = fonts + .into_iter() + .map(|f| f.into()) + .collect::>>(); + self.custom_fonts.append(&mut fonts); + self + } } impl Default for Settings @@ -83,6 +109,7 @@ where default_text_size: 16.0, antialiasing: false, exit_on_close_request: true, + custom_fonts: Vec::new(), } } } diff --git a/winit/src/application.rs b/winit/src/application.rs index d1689452b0..35ec8d2d9a 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -25,6 +25,7 @@ use crate::{Clipboard, Error, Proxy, Settings}; use futures::channel::mpsc; +use std::borrow::Cow; use std::mem::ManuallyDrop; #[cfg(feature = "trace")] @@ -108,6 +109,7 @@ where pub fn run( settings: Settings, compositor_settings: C::Settings, + custom_fonts: Vec>, ) -> Result<(), Error> where A: Application + 'static, @@ -212,6 +214,7 @@ where window, should_be_visible, settings.exit_on_close_request, + custom_fonts, ); #[cfg(feature = "trace")] @@ -279,6 +282,7 @@ async fn run_instance( window: winit::window::Window, should_be_visible: bool, exit_on_close_request: bool, + custom_fonts: Vec>, ) where A: Application + 'static, E: Executor + 'static, @@ -306,6 +310,7 @@ async fn run_instance( window.set_visible(true); } + load_fonts(&application, &mut renderer, custom_fonts); run_command( &application, &mut compositor, @@ -699,6 +704,24 @@ pub fn update( runtime.track(subscription.into_recipes()); } +/// load fonts on init. +/// +pub fn load_fonts( + _application: &A, + renderer: &mut A::Renderer, + custom_fonts: Vec>, +) where + A: Application, + ::Theme: StyleSheet, +{ + use crate::core::text::Renderer; + + for font in custom_fonts { + // TODO: Error handling (?) + renderer.load_font(font); + } +} + /// Runs the actions of a [`Command`]. pub fn run_command( application: &A, From 2250e8e7a6ea6c8161a80240282ffcd4e5e5bf44 Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Wed, 9 Aug 2023 12:08:22 -0400 Subject: [PATCH 2/3] updated changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e838f8d5b..1a17824024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Custom Font loader for SandBox, Program and Application Via Settings. + ## [0.10.0] - 2023-07-28 ### Added - Text shaping, font fallback, and `iced_wgpu` overhaul. [#1697](https://github.com/iced-rs/iced/pull/1697) From e560b3ee56084b7d3722224dabcc8fc12ee927ea Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Wed, 9 Aug 2023 12:51:49 -0400 Subject: [PATCH 3/3] added Many thanks to part to changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a17824024..68181c57bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Custom Font loader for SandBox, Program and Application Via Settings. + +Many thanks to... + +- @genusistimelord + ## [0.10.0] - 2023-07-28 ### Added - Text shaping, font fallback, and `iced_wgpu` overhaul. [#1697](https://github.com/iced-rs/iced/pull/1697)