Overhaul Font type to allow font family selection

This commit is contained in:
Héctor Ramón Jiménez 2023-02-04 07:33:33 +01:00
parent a7580e0696
commit b29de28d1f
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
25 changed files with 147 additions and 256 deletions

View file

@ -1,40 +1,29 @@
use std::hash::{Hash, Hasher}; use std::hash::Hash;
/// A font. /// A font.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Font { pub enum Font {
/// The default font. /// The name of a font family of choice.
/// Name(&'static str),
/// This is normally a font configured in a renderer or loaded from the
/// system.
Default,
/// An external font. /// Serif fonts represent the formal text style for a script.
External { Serif,
/// The name of the external font
name: &'static str,
/// The bytes of the external font /// Glyphs in sans-serif fonts, as the term is used in CSS, are generally low
bytes: &'static [u8], /// contrast and have stroke endings that are plain — without any flaring,
}, /// cross stroke, or other ornamentation.
} SansSerif,
impl Default for Font { /// Glyphs in cursive fonts generally use a more informal script style, and
fn default() -> Font { /// the result looks more like handwritten pen or brush writing than printed
Font::Default /// letterwork.
} Cursive,
}
/// Fantasy fonts are primarily decorative or expressive fonts that contain
impl Hash for Font { /// decorative or expressive representations of characters.
fn hash<H: Hasher>(&self, hasher: &mut H) { Fantasy,
match self {
Self::Default => { /// The sole criterion of a monospace font is that all glyphs have the same
0.hash(hasher); /// fixed width.
} Monospace,
Self::External { name, .. } => {
1.hash(hasher);
name.hash(hasher);
}
}
}
} }

Binary file not shown.

View file

@ -466,10 +466,7 @@ fn empty_message(message: &str) -> Element<'_, Message> {
} }
// Fonts // Fonts
const ICONS: Font = Font::External { const ICONS: Font = Font::Name("Iced-Todos-Icons");
name: "Icons",
bytes: include_bytes!("../../todos/fonts/icons.ttf"),
};
fn icon(unicode: char) -> Text<'static> { fn icon(unicode: char) -> Text<'static> {
text(unicode.to_string()) text(unicode.to_string())

Binary file not shown.

View file

@ -1,93 +0,0 @@
Copyright (c) 2010-2014 by tyPoland Lukasz Dziedzic (team@latofonts.com) with Reserved Font Name "Lato"
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

View file

@ -31,6 +31,9 @@ pub trait Text {
/// [`ICON_FONT`]: Self::ICON_FONT /// [`ICON_FONT`]: Self::ICON_FONT
const ARROW_DOWN_ICON: char; const ARROW_DOWN_ICON: char;
/// Returns the default [`Font`].
fn default_font(&self) -> Font;
/// Returns the default size of text. /// Returns the default size of text.
fn default_size(&self) -> f32; fn default_size(&self) -> f32;

View file

@ -63,7 +63,7 @@ impl<'a> Layer<'a> {
), ),
color: Color::new(0.9, 0.9, 0.9, 1.0), color: Color::new(0.9, 0.9, 0.9, 1.0),
size: 20.0, size: 20.0,
font: Font::Default, font: Font::Monospace,
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top, vertical_alignment: alignment::Vertical::Top,
}; };

View file

@ -130,6 +130,10 @@ where
const CHECKMARK_ICON: char = B::CHECKMARK_ICON; const CHECKMARK_ICON: char = B::CHECKMARK_ICON;
const ARROW_DOWN_ICON: char = B::ARROW_DOWN_ICON; const ARROW_DOWN_ICON: char = B::ARROW_DOWN_ICON;
fn default_font(&self) -> Self::Font {
self.backend().default_font()
}
fn default_size(&self) -> f32 { fn default_size(&self) -> f32 {
self.backend().default_size() self.backend().default_size()
} }

View file

@ -34,7 +34,7 @@ impl Default for Text {
position: Point::ORIGIN, position: Point::ORIGIN,
color: Color::BLACK, color: Color::BLACK,
size: 16.0, size: 16.0,
font: Font::Default, font: Font::SansSerif,
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top, vertical_alignment: alignment::Vertical::Top,
} }

View file

@ -31,7 +31,7 @@ where
width: f32, width: f32,
padding: Padding, padding: Padding,
text_size: Option<f32>, text_size: Option<f32>,
font: Renderer::Font, font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -58,7 +58,7 @@ where
width: 0.0, width: 0.0,
padding: Padding::ZERO, padding: Padding::ZERO,
text_size: None, text_size: None,
font: Default::default(), font: None,
style: Default::default(), style: Default::default(),
} }
} }
@ -82,8 +82,8 @@ where
} }
/// Sets the font of the [`Menu`]. /// Sets the font of the [`Menu`].
pub fn font(mut self, font: Renderer::Font) -> Self { pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = font; self.font = Some(font.into());
self self
} }
@ -311,7 +311,7 @@ where
last_selection: &'a mut Option<T>, last_selection: &'a mut Option<T>,
padding: Padding, padding: Padding,
text_size: Option<f32>, text_size: Option<f32>,
font: Renderer::Font, font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -491,7 +491,7 @@ where
..bounds ..bounds
}, },
size: text_size, size: text_size,
font: self.font.clone(), font: self.font.unwrap_or_else(|| renderer.default_font()),
color: if is_selected { color: if is_selected {
appearance.selected_text_color appearance.selected_text_color
} else { } else {

View file

@ -40,12 +40,16 @@ impl Renderer for Null {
impl text::Renderer for Null { impl text::Renderer for Null {
type Font = Font; type Font = Font;
const ICON_FONT: Font = Font::Default; const ICON_FONT: Font = Font::SansSerif;
const CHECKMARK_ICON: char = '0'; const CHECKMARK_ICON: char = '0';
const ARROW_DOWN_ICON: char = '0'; const ARROW_DOWN_ICON: char = '0';
fn default_font(&self) -> Self::Font {
Font::SansSerif
}
fn default_size(&self) -> f32 { fn default_size(&self) -> f32 {
20.0 16.0
} }
fn measure( fn measure(

View file

@ -57,7 +57,7 @@ impl Hit {
/// A renderer capable of measuring and drawing [`Text`]. /// A renderer capable of measuring and drawing [`Text`].
pub trait Renderer: crate::Renderer { pub trait Renderer: crate::Renderer {
/// The font type used. /// The font type used.
type Font: Default + Clone; type Font: Copy;
/// The icon font of the backend. /// The icon font of the backend.
const ICON_FONT: Self::Font; const ICON_FONT: Self::Font;
@ -72,6 +72,9 @@ pub trait Renderer: crate::Renderer {
/// [`ICON_FONT`]: Self::ICON_FONT /// [`ICON_FONT`]: Self::ICON_FONT
const ARROW_DOWN_ICON: char; const ARROW_DOWN_ICON: char;
/// Returns the default [`Font`].
fn default_font(&self) -> Self::Font;
/// Returns the default size of [`Text`]. /// Returns the default size of [`Text`].
fn default_size(&self) -> f32; fn default_size(&self) -> f32;

View file

@ -55,7 +55,7 @@ where
size: f32, size: f32,
spacing: f32, spacing: f32,
text_size: Option<f32>, text_size: Option<f32>,
font: Renderer::Font, font: Option<Renderer::Font>,
icon: Icon<Renderer::Font>, icon: Icon<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -91,7 +91,7 @@ where
size: Self::DEFAULT_SIZE, size: Self::DEFAULT_SIZE,
spacing: Self::DEFAULT_SPACING, spacing: Self::DEFAULT_SPACING,
text_size: None, text_size: None,
font: Renderer::Font::default(), font: None,
icon: Icon { icon: Icon {
font: Renderer::ICON_FONT, font: Renderer::ICON_FONT,
code_point: Renderer::CHECKMARK_ICON, code_point: Renderer::CHECKMARK_ICON,
@ -128,8 +128,8 @@ where
/// Sets the [`Font`] of the text of the [`Checkbox`]. /// Sets the [`Font`] of the text of the [`Checkbox`].
/// ///
/// [`Font`]: crate::text::Renderer::Font /// [`Font`]: crate::text::Renderer::Font
pub fn font(mut self, font: Renderer::Font) -> Self { pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = font; self.font = Some(font.into());
self self
} }
@ -175,7 +175,7 @@ where
.push(Row::new().width(self.size).height(self.size)) .push(Row::new().width(self.size).height(self.size))
.push( .push(
Text::new(&self.label) Text::new(&self.label)
.font(self.font.clone()) .font(self.font.unwrap_or_else(|| renderer.default_font()))
.width(self.width) .width(self.width)
.size( .size(
self.text_size self.text_size
@ -288,6 +288,7 @@ where
{ {
let label_layout = children.next().unwrap(); let label_layout = children.next().unwrap();
let font = self.font.unwrap_or_else(|| renderer.default_font());
widget::text::draw( widget::text::draw(
renderer, renderer,
@ -295,7 +296,7 @@ where
label_layout, label_layout,
&self.label, &self.label,
self.text_size, self.text_size,
self.font.clone(), font,
widget::text::Appearance { widget::text::Appearance {
color: custom_style.text_color, color: custom_style.text_color,
}, },

View file

@ -35,7 +35,7 @@ where
width: Length, width: Length,
padding: Padding, padding: Padding,
text_size: Option<f32>, text_size: Option<f32>,
font: Renderer::Font, font: Option<Renderer::Font>,
handle: Handle<Renderer::Font>, handle: Handle<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -70,7 +70,7 @@ where
width: Length::Shrink, width: Length::Shrink,
padding: Self::DEFAULT_PADDING, padding: Self::DEFAULT_PADDING,
text_size: None, text_size: None,
font: Default::default(), font: None,
handle: Default::default(), handle: Default::default(),
style: Default::default(), style: Default::default(),
} }
@ -101,8 +101,8 @@ where
} }
/// Sets the font of the [`PickList`]. /// Sets the font of the [`PickList`].
pub fn font(mut self, font: Renderer::Font) -> Self { pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = font; self.font = Some(font.into());
self self
} }
@ -163,7 +163,7 @@ where
self.width, self.width,
self.padding, self.padding,
self.text_size, self.text_size,
&self.font, self.font.unwrap_or_else(|| renderer.default_font()),
self.placeholder.as_deref(), self.placeholder.as_deref(),
&self.options, &self.options,
) )
@ -212,6 +212,7 @@ where
cursor_position: Point, cursor_position: Point,
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
let font = self.font.unwrap_or_else(|| renderer.default_font());
draw( draw(
renderer, renderer,
theme, theme,
@ -219,7 +220,7 @@ where
cursor_position, cursor_position,
self.padding, self.padding,
self.text_size, self.text_size,
&self.font, font,
self.placeholder.as_deref(), self.placeholder.as_deref(),
self.selected.as_ref(), self.selected.as_ref(),
&self.handle, &self.handle,
@ -232,7 +233,7 @@ where
&'b mut self, &'b mut self,
tree: &'b mut Tree, tree: &'b mut Tree,
layout: Layout<'_>, layout: Layout<'_>,
_renderer: &Renderer, renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> { ) -> Option<overlay::Element<'b, Message, Renderer>> {
let state = tree.state.downcast_mut::<State<T>>(); let state = tree.state.downcast_mut::<State<T>>();
@ -241,7 +242,7 @@ where
state, state,
self.padding, self.padding,
self.text_size, self.text_size,
self.font.clone(), self.font.unwrap_or_else(|| renderer.default_font()),
&self.options, &self.options,
self.style.clone(), self.style.clone(),
) )
@ -343,7 +344,7 @@ pub fn layout<Renderer, T>(
width: Length, width: Length,
padding: Padding, padding: Padding,
text_size: Option<f32>, text_size: Option<f32>,
font: &Renderer::Font, font: Renderer::Font,
placeholder: Option<&str>, placeholder: Option<&str>,
options: &[T], options: &[T],
) -> layout::Node ) -> layout::Node
@ -362,7 +363,7 @@ where
let (width, _) = renderer.measure( let (width, _) = renderer.measure(
label, label,
text_size, text_size,
font.clone(), font,
Size::new(f32::INFINITY, f32::INFINITY), Size::new(f32::INFINITY, f32::INFINITY),
); );
@ -560,7 +561,7 @@ pub fn draw<'a, T, Renderer>(
cursor_position: Point, cursor_position: Point,
padding: Padding, padding: Padding,
text_size: Option<f32>, text_size: Option<f32>,
font: &Renderer::Font, font: Renderer::Font,
placeholder: Option<&str>, placeholder: Option<&str>,
selected: Option<&T>, selected: Option<&T>,
handle: &Handle<Renderer::Font>, handle: &Handle<Renderer::Font>,
@ -637,7 +638,7 @@ pub fn draw<'a, T, Renderer>(
renderer.fill_text(Text { renderer.fill_text(Text {
content: label, content: label,
size: text_size, size: text_size,
font: font.clone(), font,
color: if is_selected { color: if is_selected {
style.text_color style.text_color
} else { } else {

View file

@ -53,7 +53,7 @@ where
size: f32, size: f32,
spacing: f32, spacing: f32,
text_size: Option<f32>, text_size: Option<f32>,
font: Renderer::Font, font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -95,7 +95,7 @@ where
size: Self::DEFAULT_SIZE, size: Self::DEFAULT_SIZE,
spacing: Self::DEFAULT_SPACING, //15 spacing: Self::DEFAULT_SPACING, //15
text_size: None, text_size: None,
font: Default::default(), font: None,
style: Default::default(), style: Default::default(),
} }
} }
@ -125,8 +125,8 @@ where
} }
/// Sets the text font of the [`Radio`] button. /// Sets the text font of the [`Radio`] button.
pub fn font(mut self, font: Renderer::Font) -> Self { pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = font; self.font = Some(font.into());
self self
} }
@ -268,6 +268,7 @@ where
{ {
let label_layout = children.next().unwrap(); let label_layout = children.next().unwrap();
let font = self.font.unwrap_or(renderer.default_font());
widget::text::draw( widget::text::draw(
renderer, renderer,
@ -275,7 +276,7 @@ where
label_layout, label_layout,
&self.label, &self.label,
self.text_size, self.text_size,
self.font.clone(), font,
widget::text::Appearance { widget::text::Appearance {
color: custom_style.text_color, color: custom_style.text_color,
}, },

View file

@ -37,7 +37,7 @@ where
height: Length, height: Length,
horizontal_alignment: alignment::Horizontal, horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical, vertical_alignment: alignment::Vertical,
font: Renderer::Font, font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -51,7 +51,7 @@ where
Text { Text {
content: content.into(), content: content.into(),
size: None, size: None,
font: Default::default(), font: None,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
@ -70,7 +70,7 @@ where
/// ///
/// [`Font`]: crate::text::Renderer::Font /// [`Font`]: crate::text::Renderer::Font
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self { pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = font.into(); self.font = Some(font.into());
self self
} }
@ -138,8 +138,12 @@ where
let bounds = limits.max(); let bounds = limits.max();
let (width, height) = let (width, height) = renderer.measure(
renderer.measure(&self.content, size, self.font.clone(), bounds); &self.content,
size,
self.font.unwrap_or_else(|| renderer.default_font()),
bounds,
);
let size = limits.resolve(Size::new(width, height)); let size = limits.resolve(Size::new(width, height));
@ -156,13 +160,15 @@ where
_cursor_position: Point, _cursor_position: Point,
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
let font = self.font.unwrap_or_else(|| renderer.default_font());
draw( draw(
renderer, renderer,
style, style,
layout, layout,
&self.content, &self.content,
self.size, self.size,
self.font.clone(), font,
theme.appearance(self.style), theme.appearance(self.style),
self.horizontal_alignment, self.horizontal_alignment,
self.vertical_alignment, self.vertical_alignment,
@ -242,7 +248,7 @@ where
height: self.height, height: self.height,
horizontal_alignment: self.horizontal_alignment, horizontal_alignment: self.horizontal_alignment,
vertical_alignment: self.vertical_alignment, vertical_alignment: self.vertical_alignment,
font: self.font.clone(), font: self.font,
style: self.style, style: self.style,
} }
} }

View file

@ -61,7 +61,7 @@ where
placeholder: String, placeholder: String,
value: Value, value: Value,
is_secure: bool, is_secure: bool,
font: Renderer::Font, font: Option<Renderer::Font>,
width: Length, width: Length,
padding: Padding, padding: Padding,
size: Option<f32>, size: Option<f32>,
@ -92,7 +92,7 @@ where
placeholder: String::from(placeholder), placeholder: String::from(placeholder),
value: Value::new(value), value: Value::new(value),
is_secure: false, is_secure: false,
font: Default::default(), font: None,
width: Length::Fill, width: Length::Fill,
padding: Padding::new(5.0), padding: Padding::new(5.0),
size: None, size: None,
@ -129,7 +129,7 @@ where
/// ///
/// [`Font`]: text::Renderer::Font /// [`Font`]: text::Renderer::Font
pub fn font(mut self, font: Renderer::Font) -> Self { pub fn font(mut self, font: Renderer::Font) -> Self {
self.font = font; self.font = Some(font);
self self
} }
/// Sets the width of the [`TextInput`]. /// Sets the width of the [`TextInput`].
@ -179,6 +179,8 @@ where
cursor_position: Point, cursor_position: Point,
value: Option<&Value>, value: Option<&Value>,
) { ) {
let font = self.font.unwrap_or(renderer.default_font());
draw( draw(
renderer, renderer,
theme, theme,
@ -188,7 +190,7 @@ where
value.unwrap_or(&self.value), value.unwrap_or(&self.value),
&self.placeholder, &self.placeholder,
self.size, self.size,
&self.font, font,
self.is_secure, self.is_secure,
&self.style, &self.style,
) )
@ -258,7 +260,7 @@ where
shell, shell,
&mut self.value, &mut self.value,
self.size, self.size,
&self.font, self.font.unwrap_or(renderer.default_font()),
self.is_secure, self.is_secure,
self.on_change.as_ref(), self.on_change.as_ref(),
self.on_paste.as_deref(), self.on_paste.as_deref(),
@ -277,6 +279,8 @@ where
cursor_position: Point, cursor_position: Point,
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
let font = self.font.unwrap_or(renderer.default_font());
draw( draw(
renderer, renderer,
theme, theme,
@ -286,7 +290,7 @@ where
&self.value, &self.value,
&self.placeholder, &self.placeholder,
self.size, self.size,
&self.font, font,
self.is_secure, self.is_secure,
&self.style, &self.style,
) )
@ -410,7 +414,7 @@ pub fn update<'a, Message, Renderer>(
shell: &mut Shell<'_, Message>, shell: &mut Shell<'_, Message>,
value: &mut Value, value: &mut Value,
size: Option<f32>, size: Option<f32>,
font: &Renderer::Font, font: Renderer::Font,
is_secure: bool, is_secure: bool,
on_change: &dyn Fn(String) -> Message, on_change: &dyn Fn(String) -> Message,
on_paste: Option<&dyn Fn(String) -> Message>, on_paste: Option<&dyn Fn(String) -> Message>,
@ -459,7 +463,7 @@ where
find_cursor_position( find_cursor_position(
renderer, renderer,
text_layout.bounds(), text_layout.bounds(),
font.clone(), font,
size, size,
&value, &value,
state, state,
@ -487,7 +491,7 @@ where
let position = find_cursor_position( let position = find_cursor_position(
renderer, renderer,
text_layout.bounds(), text_layout.bounds(),
font.clone(), font,
size, size,
value, value,
state, state,
@ -536,7 +540,7 @@ where
let position = find_cursor_position( let position = find_cursor_position(
renderer, renderer,
text_layout.bounds(), text_layout.bounds(),
font.clone(), font,
size, size,
&value, &value,
state, state,
@ -816,7 +820,7 @@ pub fn draw<Renderer>(
value: &Value, value: &Value,
placeholder: &str, placeholder: &str,
size: Option<f32>, size: Option<f32>,
font: &Renderer::Font, font: Renderer::Font,
is_secure: bool, is_secure: bool,
style: &<Renderer::Theme as StyleSheet>::Style, style: &<Renderer::Theme as StyleSheet>::Style,
) where ) where
@ -862,7 +866,7 @@ pub fn draw<Renderer>(
value, value,
size, size,
position, position,
font.clone(), font,
); );
let is_cursor_visible = ((focus.now - focus.updated_at) let is_cursor_visible = ((focus.now - focus.updated_at)
@ -903,7 +907,7 @@ pub fn draw<Renderer>(
value, value,
size, size,
left, left,
font.clone(), font,
); );
let (right_position, right_offset) = let (right_position, right_offset) =
@ -913,7 +917,7 @@ pub fn draw<Renderer>(
value, value,
size, size,
right, right,
font.clone(), font,
); );
let width = right_position - left_position; let width = right_position - left_position;
@ -948,7 +952,7 @@ pub fn draw<Renderer>(
let text_width = renderer.measure_width( let text_width = renderer.measure_width(
if text.is_empty() { placeholder } else { &text }, if text.is_empty() { placeholder } else { &text },
size, size,
font.clone(), font,
); );
let render = |renderer: &mut Renderer| { let render = |renderer: &mut Renderer| {
@ -963,7 +967,7 @@ pub fn draw<Renderer>(
} else { } else {
theme.value_color(style) theme.value_color(style)
}, },
font: font.clone(), font: font,
bounds: Rectangle { bounds: Rectangle {
y: text_bounds.center_y(), y: text_bounds.center_y(),
width: f32::INFINITY, width: f32::INFINITY,
@ -1195,8 +1199,7 @@ where
{ {
let size = size.unwrap_or_else(|| renderer.default_size()); let size = size.unwrap_or_else(|| renderer.default_size());
let offset = let offset = offset(renderer, text_bounds, font, size, value, state);
offset(renderer, text_bounds, font.clone(), size, value, state);
renderer renderer
.hit_test( .hit_test(

View file

@ -42,7 +42,7 @@ where
text_size: Option<f32>, text_size: Option<f32>,
text_alignment: alignment::Horizontal, text_alignment: alignment::Horizontal,
spacing: f32, spacing: f32,
font: Renderer::Font, font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -79,7 +79,7 @@ where
text_size: None, text_size: None,
text_alignment: alignment::Horizontal::Left, text_alignment: alignment::Horizontal::Left,
spacing: 0.0, spacing: 0.0,
font: Renderer::Font::default(), font: None,
style: Default::default(), style: Default::default(),
} }
} }
@ -117,8 +117,8 @@ where
/// Sets the [`Font`] of the text of the [`Toggler`] /// Sets the [`Font`] of the text of the [`Toggler`]
/// ///
/// [`Font`]: crate::text::Renderer::Font /// [`Font`]: crate::text::Renderer::Font
pub fn font(mut self, font: Renderer::Font) -> Self { pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = font; self.font = Some(font.into());
self self
} }
@ -160,7 +160,7 @@ where
row = row.push( row = row.push(
Text::new(label) Text::new(label)
.horizontal_alignment(self.text_alignment) .horizontal_alignment(self.text_alignment)
.font(self.font.clone()) .font(self.font.unwrap_or_else(|| renderer.default_font()))
.width(self.width) .width(self.width)
.size( .size(
self.text_size self.text_size
@ -236,6 +236,7 @@ where
if let Some(label) = &self.label { if let Some(label) = &self.label {
let label_layout = children.next().unwrap(); let label_layout = children.next().unwrap();
let font = self.font.unwrap_or_else(|| renderer.default_font());
crate::widget::text::draw( crate::widget::text::draw(
renderer, renderer,
@ -243,7 +244,7 @@ where
label_layout, label_layout,
label, label,
self.text_size, self.text_size,
self.font.clone(), font,
Default::default(), Default::default(),
self.text_alignment, self.text_alignment,
alignment::Vertical::Center, alignment::Vertical::Center,

View file

@ -197,7 +197,6 @@ pub trait Application: Sized {
let renderer_settings = crate::renderer::Settings { let renderer_settings = crate::renderer::Settings {
default_font: settings.default_font, default_font: settings.default_font,
default_text_size: settings.default_text_size, default_text_size: settings.default_text_size,
text_multithreading: settings.text_multithreading,
antialiasing: if settings.antialiasing { antialiasing: if settings.antialiasing {
Some(crate::renderer::settings::Antialiasing::MSAAx4) Some(crate::renderer::settings::Antialiasing::MSAAx4)
} else { } else {

View file

@ -1,5 +1,6 @@
//! Configure your application. //! Configure your application.
use crate::window; use crate::window;
use crate::Font;
/// The settings of an application. /// The settings of an application.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -20,23 +21,16 @@ pub struct Settings<Flags> {
/// [`Application`]: crate::Application /// [`Application`]: crate::Application
pub flags: Flags, pub flags: Flags,
/// The bytes of the font that will be used by default. /// The default [`Font`] to be used.
/// ///
/// If `None` is provided, a default system font will be chosen. /// By default, it uses [`Font::SansSerif`].
// TODO: Add `name` for web compatibility pub default_font: Font,
pub default_font: Option<&'static [u8]>,
/// The text size that will be used by default. /// The text size that will be used by default.
/// ///
/// The default value is `16.0`. /// The default value is `16.0`.
pub default_text_size: f32, pub default_text_size: f32,
/// If enabled, spread text workload in multiple threads when multiple cores
/// are available.
///
/// By default, it is disabled.
pub text_multithreading: bool,
/// If set to true, the renderer will try to perform antialiasing for some /// If set to true, the renderer will try to perform antialiasing for some
/// primitives. /// primitives.
/// ///
@ -79,7 +73,6 @@ impl<Flags> Settings<Flags> {
window: default_settings.window, window: default_settings.window,
default_font: default_settings.default_font, default_font: default_settings.default_font,
default_text_size: default_settings.default_text_size, default_text_size: default_settings.default_text_size,
text_multithreading: default_settings.text_multithreading,
antialiasing: default_settings.antialiasing, antialiasing: default_settings.antialiasing,
exit_on_close_request: default_settings.exit_on_close_request, exit_on_close_request: default_settings.exit_on_close_request,
try_opengles_first: default_settings.try_opengles_first, try_opengles_first: default_settings.try_opengles_first,
@ -96,9 +89,8 @@ where
id: None, id: None,
window: Default::default(), window: Default::default(),
flags: Default::default(), flags: Default::default(),
default_font: Default::default(), default_font: Font::SansSerif,
default_text_size: 16.0, default_text_size: 16.0,
text_multithreading: false,
antialiasing: false, antialiasing: false,
exit_on_close_request: true, exit_on_close_request: true,
try_opengles_first: false, try_opengles_first: false,

View file

@ -27,6 +27,7 @@ pub struct Backend {
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image", feature = "svg"))]
image_pipeline: image::Pipeline, image_pipeline: image::Pipeline,
default_font: Font,
default_text_size: f32, default_text_size: f32,
} }
@ -38,14 +39,7 @@ impl Backend {
settings: Settings, settings: Settings,
format: wgpu::TextureFormat, format: wgpu::TextureFormat,
) -> Self { ) -> Self {
let text_pipeline = text::Pipeline::new( let text_pipeline = text::Pipeline::new(device, queue, format);
device,
queue,
format,
settings.default_font,
settings.text_multithreading,
);
let quad_pipeline = quad::Pipeline::new(device, format); let quad_pipeline = quad::Pipeline::new(device, format);
let triangle_pipeline = let triangle_pipeline =
triangle::Pipeline::new(device, format, settings.antialiasing); triangle::Pipeline::new(device, format, settings.antialiasing);
@ -61,6 +55,7 @@ impl Backend {
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image", feature = "svg"))]
image_pipeline, image_pipeline,
default_font: settings.default_font,
default_text_size: settings.default_text_size, default_text_size: settings.default_text_size,
} }
} }
@ -199,9 +194,13 @@ impl iced_graphics::Backend for Backend {
} }
impl backend::Text for Backend { impl backend::Text for Backend {
const ICON_FONT: Font = Font::Default; // TODO const ICON_FONT: Font = Font::Name("Iced-Icons");
const CHECKMARK_ICON: char = '✓'; const CHECKMARK_ICON: char = '\u{e800}';
const ARROW_DOWN_ICON: char = '▼'; const ARROW_DOWN_ICON: char = '\u{f00c}';
fn default_font(&self) -> Font {
self.default_font
}
fn default_size(&self) -> f32 { fn default_size(&self) -> f32 {
self.default_text_size self.default_text_size

View file

@ -47,7 +47,9 @@ mod quad;
mod text; mod text;
mod triangle; mod triangle;
pub use iced_graphics::{Antialiasing, Color, Error, Primitive, Viewport}; pub use iced_graphics::{
Antialiasing, Color, Error, Font, Primitive, Viewport,
};
pub use iced_native::Theme; pub use iced_native::Theme;
pub use wgpu; pub use wgpu;

View file

@ -1,12 +1,12 @@
//! Configure a renderer. //! Configure a renderer.
use std::fmt;
pub use crate::Antialiasing; pub use crate::Antialiasing;
use crate::Font;
/// The settings of a [`Backend`]. /// The settings of a [`Backend`].
/// ///
/// [`Backend`]: crate::Backend /// [`Backend`]: crate::Backend
#[derive(Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct Settings { pub struct Settings {
/// The present mode of the [`Backend`]. /// The present mode of the [`Backend`].
/// ///
@ -16,42 +16,20 @@ pub struct Settings {
/// The internal graphics backend to use. /// The internal graphics backend to use.
pub internal_backend: wgpu::Backends, pub internal_backend: wgpu::Backends,
/// The bytes of the font that will be used by default. /// The default [`Font`] to use.
/// pub default_font: Font,
/// If `None` is provided, a default system font will be chosen.
pub default_font: Option<&'static [u8]>,
/// The default size of text. /// The default size of text.
/// ///
/// By default, it will be set to `16.0`. /// By default, it will be set to `16.0`.
pub default_text_size: f32, pub default_text_size: f32,
/// If enabled, spread text workload in multiple threads when multiple cores
/// are available.
///
/// By default, it is disabled.
pub text_multithreading: bool,
/// The antialiasing strategy that will be used for triangle primitives. /// The antialiasing strategy that will be used for triangle primitives.
/// ///
/// By default, it is `None`. /// By default, it is `None`.
pub antialiasing: Option<Antialiasing>, pub antialiasing: Option<Antialiasing>,
} }
impl fmt::Debug for Settings {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Settings")
.field("present_mode", &self.present_mode)
.field("internal_backend", &self.internal_backend)
// Instead of printing the font bytes, we simply show a `bool` indicating if using a default font or not.
.field("default_font", &self.default_font.is_some())
.field("default_text_size", &self.default_text_size)
.field("text_multithreading", &self.text_multithreading)
.field("antialiasing", &self.antialiasing)
.finish()
}
}
impl Settings { impl Settings {
/// Creates new [`Settings`] using environment configuration. /// Creates new [`Settings`] using environment configuration.
/// ///
@ -81,9 +59,8 @@ impl Default for Settings {
Settings { Settings {
present_mode: wgpu::PresentMode::AutoVsync, present_mode: wgpu::PresentMode::AutoVsync,
internal_backend: wgpu::Backends::all(), internal_backend: wgpu::Backends::all(),
default_font: None, default_font: Font::SansSerif,
default_text_size: 20.0, default_text_size: 16.0,
text_multithreading: false,
antialiasing: None, antialiasing: None,
} }
} }

View file

@ -112,8 +112,6 @@ impl Pipeline {
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue, queue: &wgpu::Queue,
format: wgpu::TextureFormat, format: wgpu::TextureFormat,
_default_font: Option<&[u8]>,
_multithreading: bool,
) -> Self { ) -> Self {
Pipeline { Pipeline {
renderers: Vec::new(), renderers: Vec::new(),
@ -316,7 +314,11 @@ impl Pipeline {
fn to_family(font: Font) -> glyphon::Family<'static> { fn to_family(font: Font) -> glyphon::Family<'static> {
match font { match font {
Font::Default => glyphon::Family::SansSerif, Font::Name(name) => glyphon::Family::Name(name),
Font::External { name, .. } => glyphon::Family::Name(name), Font::SansSerif => glyphon::Family::SansSerif,
Font::Serif => glyphon::Family::Serif,
Font::Cursive => glyphon::Family::Cursive,
Font::Fantasy => glyphon::Family::Fantasy,
Font::Monospace => glyphon::Family::Monospace,
} }
} }