summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--core/src/font.rs44
-rw-r--r--core/src/renderer/null.rs4
-rw-r--r--examples/checkbox/src/main.rs2
-rw-r--r--examples/todos/src/main.rs15
-rw-r--r--graphics/src/geometry/text.rs2
-rw-r--r--renderer/src/backend.rs2
-rw-r--r--renderer/src/settings.rs2
-rw-r--r--src/settings.rs2
-rw-r--r--tiny_skia/src/backend.rs4
-rw-r--r--tiny_skia/src/settings.rs2
-rw-r--r--tiny_skia/src/text.rs44
-rw-r--r--wgpu/src/backend.rs2
-rw-r--r--wgpu/src/layer.rs2
-rw-r--r--wgpu/src/settings.rs2
-rw-r--r--wgpu/src/text.rs44
15 files changed, 129 insertions, 44 deletions
diff --git a/core/src/font.rs b/core/src/font.rs
index b67c08af..920b3dd5 100644
--- a/core/src/font.rs
+++ b/core/src/font.rs
@@ -2,8 +2,44 @@
use std::hash::Hash;
/// A font.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-pub enum Font {
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
+pub struct Font {
+ /// The [`Family`] of the [`Font`].
+ pub family: Family,
+ /// The [`Weight`] of the [`Font`].
+ pub weight: Weight,
+ /// Whether if the [`Font`] is monospaced or not.
+ pub monospaced: bool,
+}
+
+impl Font {
+ /// A non-monospaced sans-serif font with normal [`Weight`].
+ pub const DEFAULT: Font = Font {
+ family: Family::SansSerif,
+ weight: Weight::Normal,
+ monospaced: false,
+ };
+
+ /// A monospaced font with normal [`Weight`].
+ pub const MONOSPACE: Font = Font {
+ family: Family::Monospace,
+ monospaced: true,
+ ..Self::DEFAULT
+ };
+
+ /// Creates a non-monospaced [`Font`] with the given [`Family::Name`] and
+ /// normal [`Weight`].
+ pub const fn with_name(name: &'static str) -> Self {
+ Font {
+ family: Family::Name(name),
+ ..Self::DEFAULT
+ }
+ }
+}
+
+/// A font family.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
+pub enum Family {
/// The name of a font family of choice.
Name(&'static str),
@@ -13,6 +49,7 @@ pub enum Font {
/// Glyphs in sans-serif fonts, as the term is used in CSS, are generally low
/// contrast and have stroke endings that are plain — without any flaring,
/// cross stroke, or other ornamentation.
+ #[default]
SansSerif,
/// Glyphs in cursive fonts generally use a more informal script style, and
@@ -31,11 +68,12 @@ pub enum Font {
/// The weight of some text.
#[allow(missing_docs)]
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Weight {
Thin,
ExtraLight,
Light,
+ #[default]
Normal,
Medium,
Semibold,
diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs
index d93338ae..88c58825 100644
--- a/core/src/renderer/null.rs
+++ b/core/src/renderer/null.rs
@@ -42,12 +42,12 @@ impl Renderer for Null {
impl text::Renderer for Null {
type Font = Font;
- const ICON_FONT: Font = Font::SansSerif;
+ const ICON_FONT: Font = Font::DEFAULT;
const CHECKMARK_ICON: char = '0';
const ARROW_DOWN_ICON: char = '0';
fn default_font(&self) -> Self::Font {
- Font::SansSerif
+ Font::default()
}
fn default_size(&self) -> f32 {
diff --git a/examples/checkbox/src/main.rs b/examples/checkbox/src/main.rs
index 77111490..c5f3c134 100644
--- a/examples/checkbox/src/main.rs
+++ b/examples/checkbox/src/main.rs
@@ -3,7 +3,7 @@ use iced::font::{self, Font};
use iced::widget::{checkbox, column, container};
use iced::{Application, Command, Element, Length, Settings, Theme};
-const ICON_FONT: Font = Font::Name("icons");
+const ICON_FONT: Font = Font::with_name("icons");
pub fn main() -> iced::Result {
Example::run(Settings::default())
diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs
index 48b4f2f8..ed3684d3 100644
--- a/examples/todos/src/main.rs
+++ b/examples/todos/src/main.rs
@@ -392,10 +392,14 @@ impl Task {
row![
text_input,
- button(row![delete_icon(), "Delete"].spacing(10))
- .on_press(TaskMessage::Delete)
- .padding(10)
- .style(theme::Button::Destructive)
+ button(
+ row![delete_icon(), "Delete"]
+ .spacing(10)
+ .align_items(Alignment::Center)
+ )
+ .on_press(TaskMessage::Delete)
+ .padding(10)
+ .style(theme::Button::Destructive)
]
.spacing(20)
.align_items(Alignment::Center)
@@ -487,14 +491,13 @@ fn empty_message(message: &str) -> Element<'_, Message> {
}
// Fonts
-const ICONS: Font = Font::Name("Iced-Todos-Icons");
+const ICONS: Font = Font::with_name("Iced-Todos-Icons");
fn icon(unicode: char) -> Text<'static> {
text(unicode.to_string())
.font(ICONS)
.width(20)
.horizontal_alignment(alignment::Horizontal::Center)
- .size(20)
}
fn edit_icon() -> Text<'static> {
diff --git a/graphics/src/geometry/text.rs b/graphics/src/geometry/text.rs
index 06e0b4d0..0befd635 100644
--- a/graphics/src/geometry/text.rs
+++ b/graphics/src/geometry/text.rs
@@ -34,7 +34,7 @@ impl Default for Text {
position: Point::ORIGIN,
color: Color::BLACK,
size: 16.0,
- font: Font::SansSerif,
+ font: Font::default(),
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
}
diff --git a/renderer/src/backend.rs b/renderer/src/backend.rs
index e77b708b..120dd644 100644
--- a/renderer/src/backend.rs
+++ b/renderer/src/backend.rs
@@ -30,7 +30,7 @@ impl iced_graphics::Backend for Backend {
}
impl backend::Text for Backend {
- const ICON_FONT: Font = Font::Name("Iced-Icons");
+ const ICON_FONT: Font = Font::with_name("Iced-Icons");
const CHECKMARK_ICON: char = '\u{f00c}';
const ARROW_DOWN_ICON: char = '\u{e800}';
diff --git a/renderer/src/settings.rs b/renderer/src/settings.rs
index d32c87d3..2e51f339 100644
--- a/renderer/src/settings.rs
+++ b/renderer/src/settings.rs
@@ -23,7 +23,7 @@ pub struct Settings {
impl Default for Settings {
fn default() -> Settings {
Settings {
- default_font: Font::SansSerif,
+ default_font: Font::default(),
default_text_size: 16.0,
antialiasing: None,
}
diff --git a/src/settings.rs b/src/settings.rs
index 13b3af7c..0dd46584 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -79,7 +79,7 @@ where
id: None,
window: Default::default(),
flags: Default::default(),
- default_font: Font::SansSerif,
+ default_font: Default::default(),
default_text_size: 16.0,
antialiasing: false,
exit_on_close_request: true,
diff --git a/tiny_skia/src/backend.rs b/tiny_skia/src/backend.rs
index 271d026f..58076b84 100644
--- a/tiny_skia/src/backend.rs
+++ b/tiny_skia/src/backend.rs
@@ -72,7 +72,7 @@ impl Backend {
height: f32::INFINITY,
},
color: Color::BLACK,
- font: Font::Monospace,
+ font: Font::MONOSPACE,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
},
@@ -492,7 +492,7 @@ impl iced_graphics::Backend for Backend {
}
impl backend::Text for Backend {
- const ICON_FONT: Font = Font::Name("Iced-Icons");
+ const ICON_FONT: Font = Font::with_name("Iced-Icons");
const CHECKMARK_ICON: char = '\u{f00c}';
const ARROW_DOWN_ICON: char = '\u{e800}';
diff --git a/tiny_skia/src/settings.rs b/tiny_skia/src/settings.rs
index 9e4be0c4..abffbfe6 100644
--- a/tiny_skia/src/settings.rs
+++ b/tiny_skia/src/settings.rs
@@ -17,7 +17,7 @@ pub struct Settings {
impl Default for Settings {
fn default() -> Settings {
Settings {
- default_font: Font::SansSerif,
+ default_font: Font::default(),
default_text_size: 16.0,
}
}
diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs
index 8391571c..c9bb9873 100644
--- a/tiny_skia/src/text.rs
+++ b/tiny_skia/src/text.rs
@@ -1,6 +1,7 @@
use crate::core::alignment;
+use crate::core::font::{self, Font};
use crate::core::text::Hit;
-use crate::core::{Color, Font, Point, Rectangle, Size};
+use crate::core::{Color, Point, Rectangle, Size};
use rustc_hash::{FxHashMap, FxHashSet};
use std::borrow::Cow;
@@ -183,14 +184,28 @@ impl Pipeline {
}
}
-fn to_family(font: Font) -> cosmic_text::Family<'static> {
- match font {
- Font::Name(name) => cosmic_text::Family::Name(name),
- Font::SansSerif => cosmic_text::Family::SansSerif,
- Font::Serif => cosmic_text::Family::Serif,
- Font::Cursive => cosmic_text::Family::Cursive,
- Font::Fantasy => cosmic_text::Family::Fantasy,
- Font::Monospace => cosmic_text::Family::Monospace,
+fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
+ match family {
+ font::Family::Name(name) => cosmic_text::Family::Name(name),
+ font::Family::SansSerif => cosmic_text::Family::SansSerif,
+ font::Family::Serif => cosmic_text::Family::Serif,
+ font::Family::Cursive => cosmic_text::Family::Cursive,
+ font::Family::Fantasy => cosmic_text::Family::Fantasy,
+ font::Family::Monospace => cosmic_text::Family::Monospace,
+ }
+}
+
+fn to_weight(weight: font::Weight) -> cosmic_text::Weight {
+ match weight {
+ font::Weight::Thin => cosmic_text::Weight::THIN,
+ font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
+ font::Weight::Light => cosmic_text::Weight::LIGHT,
+ font::Weight::Normal => cosmic_text::Weight::NORMAL,
+ font::Weight::Medium => cosmic_text::Weight::MEDIUM,
+ font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
+ font::Weight::Bold => cosmic_text::Weight::BOLD,
+ font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
+ font::Weight::Black => cosmic_text::Weight::BLACK,
}
}
@@ -354,8 +369,15 @@ impl Cache {
font_system,
key.content,
cosmic_text::Attrs::new()
- .family(to_family(key.font))
- .monospaced(matches!(key.font, Font::Monospace)),
+ .family(to_family(key.font.family))
+ .weight(to_weight(key.font.weight))
+ .monospaced(
+ key.font.monospaced
+ || matches!(
+ key.font.family,
+ font::Family::Monospace
+ ),
+ ),
);
let _ = entry.insert(buffer);
diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs
index 88c58554..9772781a 100644
--- a/wgpu/src/backend.rs
+++ b/wgpu/src/backend.rs
@@ -336,7 +336,7 @@ impl iced_graphics::Backend for Backend {
}
impl backend::Text for Backend {
- const ICON_FONT: Font = Font::Name("Iced-Icons");
+ const ICON_FONT: Font = Font::with_name("Iced-Icons");
const CHECKMARK_ICON: char = '\u{f00c}';
const ARROW_DOWN_ICON: char = '\u{e800}';
diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs
index cb9d5e2f..c4723397 100644
--- a/wgpu/src/layer.rs
+++ b/wgpu/src/layer.rs
@@ -61,7 +61,7 @@ impl<'a> Layer<'a> {
),
color: Color::new(0.9, 0.9, 0.9, 1.0),
size: 20.0,
- font: Font::Monospace,
+ font: Font::MONOSPACE,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
};
diff --git a/wgpu/src/settings.rs b/wgpu/src/settings.rs
index 7c0750ef..ff041bdf 100644
--- a/wgpu/src/settings.rs
+++ b/wgpu/src/settings.rs
@@ -58,7 +58,7 @@ impl Default for Settings {
Settings {
present_mode: wgpu::PresentMode::AutoVsync,
internal_backend: wgpu::Backends::all(),
- default_font: Font::SansSerif,
+ default_font: Font::default(),
default_text_size: 16.0,
antialiasing: None,
}
diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs
index ac116f69..b0b7a198 100644
--- a/wgpu/src/text.rs
+++ b/wgpu/src/text.rs
@@ -1,6 +1,7 @@
use crate::core::alignment;
+use crate::core::font::{self, Font};
use crate::core::text::Hit;
-use crate::core::{Font, Point, Rectangle, Size};
+use crate::core::{Point, Rectangle, Size};
use crate::layer::Text;
use rustc_hash::{FxHashMap, FxHashSet};
@@ -262,14 +263,28 @@ impl Pipeline {
}
}
-fn to_family(font: Font) -> glyphon::Family<'static> {
- match font {
- Font::Name(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,
+fn to_family(family: font::Family) -> glyphon::Family<'static> {
+ match family {
+ font::Family::Name(name) => glyphon::Family::Name(name),
+ font::Family::SansSerif => glyphon::Family::SansSerif,
+ font::Family::Serif => glyphon::Family::Serif,
+ font::Family::Cursive => glyphon::Family::Cursive,
+ font::Family::Fantasy => glyphon::Family::Fantasy,
+ font::Family::Monospace => glyphon::Family::Monospace,
+ }
+}
+
+fn to_weight(weight: font::Weight) -> glyphon::Weight {
+ match weight {
+ font::Weight::Thin => glyphon::Weight::THIN,
+ font::Weight::ExtraLight => glyphon::Weight::EXTRA_LIGHT,
+ font::Weight::Light => glyphon::Weight::LIGHT,
+ font::Weight::Normal => glyphon::Weight::NORMAL,
+ font::Weight::Medium => glyphon::Weight::MEDIUM,
+ font::Weight::Semibold => glyphon::Weight::SEMIBOLD,
+ font::Weight::Bold => glyphon::Weight::BOLD,
+ font::Weight::ExtraBold => glyphon::Weight::EXTRA_BOLD,
+ font::Weight::Black => glyphon::Weight::BLACK,
}
}
@@ -328,8 +343,15 @@ impl Cache {
font_system,
key.content,
glyphon::Attrs::new()
- .family(to_family(key.font))
- .monospaced(matches!(key.font, Font::Monospace)),
+ .family(to_family(key.font.family))
+ .weight(to_weight(key.font.weight))
+ .monospaced(
+ key.font.monospaced
+ || matches!(
+ key.font.family,
+ font::Family::Monospace
+ ),
+ ),
);
let _ = entry.insert(buffer);