Allow configuration of default font

This commit is contained in:
Héctor Ramón Jiménez 2020-01-01 17:49:48 +01:00
parent e1062a02d1
commit d96ced8e2d
8 changed files with 39 additions and 14 deletions

View File

@ -4,13 +4,15 @@ use raw_window_handle::HasRawWindowHandle;
/// A renderer that can target windows.
pub trait Windowed: super::Renderer + Sized {
type Settings: Default;
/// The type of target.
type Target: Target<Renderer = Self>;
/// Creates a new [`Windowed`] renderer.
///
/// [`Windowed`]: trait.Windowed.html
fn new() -> Self;
fn new(settings: Self::Settings) -> Self;
/// Performs the drawing operations described in the output on the given
/// target.

View File

@ -151,7 +151,12 @@ pub trait Application: Sized {
Self: 'static,
{
#[cfg(not(target_arch = "wasm32"))]
<Instance<Self> as iced_winit::Application>::run(_settings.into());
<Instance<Self> as iced_winit::Application>::run(
_settings.into(),
iced_wgpu::Settings {
default_font: _settings.default_font,
},
);
#[cfg(target_arch = "wasm32")]
<Instance<Self> as iced_web::Application>::run();

View File

@ -15,6 +15,9 @@ pub struct Settings {
///
/// [`Color`]: ../struct.Color.html
pub background: Color,
// TODO: Add `name` for web compatibility
pub default_font: Option<&'static [u8]>,
}
impl Default for Settings {
@ -22,6 +25,7 @@ impl Default for Settings {
Settings {
window: Window::default(),
background: Color::WHITE,
default_font: None,
}
}
}

View File

@ -31,12 +31,14 @@ mod image;
mod primitive;
mod quad;
mod renderer;
mod settings;
mod text;
mod transformation;
pub use defaults::Defaults;
pub use primitive::Primitive;
pub use renderer::{Renderer, Target};
pub use settings::Settings;
#[doc(no_inline)]
pub use widget::*;

View File

@ -1,5 +1,6 @@
use crate::{
image, quad, text, Defaults, Image, Primitive, Quad, Transformation,
image, quad, text, Defaults, Image, Primitive, Quad, Settings,
Transformation,
};
use iced_native::{
renderer::{Debugger, Windowed},
@ -49,7 +50,7 @@ impl<'a> Layer<'a> {
}
impl Renderer {
fn new() -> Self {
fn new(settings: Settings) -> Self {
let adapter = Adapter::request(&RequestAdapterOptions {
power_preference: PowerPreference::Default,
backends: BackendBit::all(),
@ -63,7 +64,8 @@ impl Renderer {
limits: Limits { max_bind_groups: 2 },
});
let text_pipeline = text::Pipeline::new(&mut device);
let text_pipeline =
text::Pipeline::new(&mut device, settings.default_font);
let quad_pipeline = quad::Pipeline::new(&mut device);
let image_pipeline = image::Pipeline::new(&mut device);
@ -432,10 +434,11 @@ impl iced_native::Renderer for Renderer {
}
impl Windowed for Renderer {
type Settings = Settings;
type Target = Target;
fn new() -> Self {
Self::new()
fn new(settings: Settings) -> Self {
Self::new(settings)
}
fn draw<T: AsRef<str>>(

4
wgpu/src/settings.rs Normal file
View File

@ -0,0 +1,4 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Settings {
pub default_font: Option<&'static [u8]>,
}

View File

@ -22,13 +22,16 @@ pub struct Pipeline {
}
impl Pipeline {
pub fn new(device: &mut wgpu::Device) -> Self {
pub fn new(device: &mut wgpu::Device, default_font: Option<&[u8]>) -> Self {
// TODO: Font customization
let font_source = font::Source::new();
let default_font = font_source
let default_font =
default_font.map(|slice| slice.to_vec()).unwrap_or_else(|| {
font_source
.load(&[font::Family::SansSerif, font::Family::Serif])
.unwrap_or_else(|_| FALLBACK_FONT.to_vec());
.unwrap_or_else(|_| FALLBACK_FONT.to_vec())
});
let load_glyph_brush = |font: Vec<u8>| {
let builder =

View File

@ -81,8 +81,10 @@ pub trait Application: Sized {
/// It should probably be that last thing you call in your `main` function.
///
/// [`Application`]: trait.Application.html
fn run(settings: Settings)
where
fn run(
settings: Settings,
renderer_settings: <Self::Renderer as Windowed>::Settings,
) where
Self: 'static,
{
use winit::{
@ -140,7 +142,7 @@ pub trait Application: Sized {
let mut resized = false;
let clipboard = Clipboard::new(&window);
let mut renderer = Self::Renderer::new();
let mut renderer = Self::Renderer::new(renderer_settings);
let mut target = {
let (width, height) = to_physical(size, dpi);