Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add font loader for Sandbox, Program and Application. #2014

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ 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.


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)
Expand Down
4 changes: 3 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Self::Executor,
crate::renderer::Compositor<Self::Theme>,
>(settings.into(), renderer_settings)?)
>(settings.into(), renderer_settings, custom_fonts)?)
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Configure your application.
use std::borrow::Cow;

use crate::window;
use crate::Font;

Expand Down Expand Up @@ -49,6 +51,13 @@ pub struct Settings<Flags> {
///
/// [`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<Cow<'static, [u8]>>,
}

impl<Flags> Settings<Flags> {
Expand All @@ -66,8 +75,25 @@ impl<Flags> Settings<Flags> {
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<Item = impl Into<Cow<'static, [u8]>>>,
) -> Self {
let mut fonts = fonts
.into_iter()
.map(|f| f.into())
.collect::<Vec<Cow<'static, [u8]>>>();
self.custom_fonts.append(&mut fonts);
self
}
}

impl<Flags> Default for Settings<Flags>
Expand All @@ -83,6 +109,7 @@ where
default_text_size: 16.0,
antialiasing: false,
exit_on_close_request: true,
custom_fonts: Vec::new(),
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -108,6 +109,7 @@ where
pub fn run<A, E, C>(
settings: Settings<A::Flags>,
compositor_settings: C::Settings,
custom_fonts: Vec<Cow<'static, [u8]>>,
) -> Result<(), Error>
where
A: Application + 'static,
Expand Down Expand Up @@ -212,6 +214,7 @@ where
window,
should_be_visible,
settings.exit_on_close_request,
custom_fonts,
);

#[cfg(feature = "trace")]
Expand Down Expand Up @@ -279,6 +282,7 @@ async fn run_instance<A, E, C>(
window: winit::window::Window,
should_be_visible: bool,
exit_on_close_request: bool,
custom_fonts: Vec<Cow<'static, [u8]>>,
) where
A: Application + 'static,
E: Executor + 'static,
Expand Down Expand Up @@ -306,6 +310,7 @@ async fn run_instance<A, E, C>(
window.set_visible(true);
}

load_fonts(&application, &mut renderer, custom_fonts);
run_command(
&application,
&mut compositor,
Expand Down Expand Up @@ -699,6 +704,24 @@ pub fn update<A: Application, C, E: Executor>(
runtime.track(subscription.into_recipes());
}

/// load fonts on init.
///
pub fn load_fonts<A>(
_application: &A,
renderer: &mut A::Renderer,
custom_fonts: Vec<Cow<'static, [u8]>>,
) where
A: Application,
<A::Renderer as core::Renderer>::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<A, C, E>(
application: &A,
Expand Down
Loading