Introduce support for Font attributes
This commit is contained in:
parent
472fbdf187
commit
707de9d788
15 changed files with 129 additions and 44 deletions
|
|
@ -2,8 +2,44 @@
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
/// A font.
|
/// A font.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||||
pub enum Font {
|
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.
|
/// The name of a font family of choice.
|
||||||
Name(&'static str),
|
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
|
/// 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,
|
/// contrast and have stroke endings that are plain — without any flaring,
|
||||||
/// cross stroke, or other ornamentation.
|
/// cross stroke, or other ornamentation.
|
||||||
|
#[default]
|
||||||
SansSerif,
|
SansSerif,
|
||||||
|
|
||||||
/// Glyphs in cursive fonts generally use a more informal script style, and
|
/// Glyphs in cursive fonts generally use a more informal script style, and
|
||||||
|
|
@ -31,11 +68,12 @@ pub enum Font {
|
||||||
|
|
||||||
/// The weight of some text.
|
/// The weight of some text.
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||||
pub enum Weight {
|
pub enum Weight {
|
||||||
Thin,
|
Thin,
|
||||||
ExtraLight,
|
ExtraLight,
|
||||||
Light,
|
Light,
|
||||||
|
#[default]
|
||||||
Normal,
|
Normal,
|
||||||
Medium,
|
Medium,
|
||||||
Semibold,
|
Semibold,
|
||||||
|
|
|
||||||
|
|
@ -42,12 +42,12 @@ 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::SansSerif;
|
const ICON_FONT: Font = Font::DEFAULT;
|
||||||
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 {
|
fn default_font(&self) -> Self::Font {
|
||||||
Font::SansSerif
|
Font::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_size(&self) -> f32 {
|
fn default_size(&self) -> f32 {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use iced::font::{self, Font};
|
||||||
use iced::widget::{checkbox, column, container};
|
use iced::widget::{checkbox, column, container};
|
||||||
use iced::{Application, Command, Element, Length, Settings, Theme};
|
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 {
|
pub fn main() -> iced::Result {
|
||||||
Example::run(Settings::default())
|
Example::run(Settings::default())
|
||||||
|
|
|
||||||
|
|
@ -392,10 +392,14 @@ impl Task {
|
||||||
|
|
||||||
row![
|
row![
|
||||||
text_input,
|
text_input,
|
||||||
button(row![delete_icon(), "Delete"].spacing(10))
|
button(
|
||||||
.on_press(TaskMessage::Delete)
|
row![delete_icon(), "Delete"]
|
||||||
.padding(10)
|
.spacing(10)
|
||||||
.style(theme::Button::Destructive)
|
.align_items(Alignment::Center)
|
||||||
|
)
|
||||||
|
.on_press(TaskMessage::Delete)
|
||||||
|
.padding(10)
|
||||||
|
.style(theme::Button::Destructive)
|
||||||
]
|
]
|
||||||
.spacing(20)
|
.spacing(20)
|
||||||
.align_items(Alignment::Center)
|
.align_items(Alignment::Center)
|
||||||
|
|
@ -487,14 +491,13 @@ fn empty_message(message: &str) -> Element<'_, Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fonts
|
// Fonts
|
||||||
const ICONS: Font = Font::Name("Iced-Todos-Icons");
|
const ICONS: Font = Font::with_name("Iced-Todos-Icons");
|
||||||
|
|
||||||
fn icon(unicode: char) -> Text<'static> {
|
fn icon(unicode: char) -> Text<'static> {
|
||||||
text(unicode.to_string())
|
text(unicode.to_string())
|
||||||
.font(ICONS)
|
.font(ICONS)
|
||||||
.width(20)
|
.width(20)
|
||||||
.horizontal_alignment(alignment::Horizontal::Center)
|
.horizontal_alignment(alignment::Horizontal::Center)
|
||||||
.size(20)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn edit_icon() -> Text<'static> {
|
fn edit_icon() -> Text<'static> {
|
||||||
|
|
|
||||||
|
|
@ -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::SansSerif,
|
font: Font::default(),
|
||||||
horizontal_alignment: alignment::Horizontal::Left,
|
horizontal_alignment: alignment::Horizontal::Left,
|
||||||
vertical_alignment: alignment::Vertical::Top,
|
vertical_alignment: alignment::Vertical::Top,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ impl iced_graphics::Backend for Backend {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl backend::Text 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 CHECKMARK_ICON: char = '\u{f00c}';
|
||||||
const ARROW_DOWN_ICON: char = '\u{e800}';
|
const ARROW_DOWN_ICON: char = '\u{e800}';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ pub struct Settings {
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
fn default() -> Settings {
|
fn default() -> Settings {
|
||||||
Settings {
|
Settings {
|
||||||
default_font: Font::SansSerif,
|
default_font: Font::default(),
|
||||||
default_text_size: 16.0,
|
default_text_size: 16.0,
|
||||||
antialiasing: None,
|
antialiasing: None,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ where
|
||||||
id: None,
|
id: None,
|
||||||
window: Default::default(),
|
window: Default::default(),
|
||||||
flags: Default::default(),
|
flags: Default::default(),
|
||||||
default_font: Font::SansSerif,
|
default_font: Default::default(),
|
||||||
default_text_size: 16.0,
|
default_text_size: 16.0,
|
||||||
antialiasing: false,
|
antialiasing: false,
|
||||||
exit_on_close_request: true,
|
exit_on_close_request: true,
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ impl Backend {
|
||||||
height: f32::INFINITY,
|
height: f32::INFINITY,
|
||||||
},
|
},
|
||||||
color: Color::BLACK,
|
color: Color::BLACK,
|
||||||
font: Font::Monospace,
|
font: Font::MONOSPACE,
|
||||||
horizontal_alignment: alignment::Horizontal::Left,
|
horizontal_alignment: alignment::Horizontal::Left,
|
||||||
vertical_alignment: alignment::Vertical::Top,
|
vertical_alignment: alignment::Vertical::Top,
|
||||||
},
|
},
|
||||||
|
|
@ -492,7 +492,7 @@ impl iced_graphics::Backend for Backend {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl backend::Text 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 CHECKMARK_ICON: char = '\u{f00c}';
|
||||||
const ARROW_DOWN_ICON: char = '\u{e800}';
|
const ARROW_DOWN_ICON: char = '\u{e800}';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ pub struct Settings {
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
fn default() -> Settings {
|
fn default() -> Settings {
|
||||||
Settings {
|
Settings {
|
||||||
default_font: Font::SansSerif,
|
default_font: Font::default(),
|
||||||
default_text_size: 16.0,
|
default_text_size: 16.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::core::alignment;
|
use crate::core::alignment;
|
||||||
|
use crate::core::font::{self, Font};
|
||||||
use crate::core::text::Hit;
|
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 rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
@ -183,14 +184,28 @@ impl Pipeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_family(font: Font) -> cosmic_text::Family<'static> {
|
fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
|
||||||
match font {
|
match family {
|
||||||
Font::Name(name) => cosmic_text::Family::Name(name),
|
font::Family::Name(name) => cosmic_text::Family::Name(name),
|
||||||
Font::SansSerif => cosmic_text::Family::SansSerif,
|
font::Family::SansSerif => cosmic_text::Family::SansSerif,
|
||||||
Font::Serif => cosmic_text::Family::Serif,
|
font::Family::Serif => cosmic_text::Family::Serif,
|
||||||
Font::Cursive => cosmic_text::Family::Cursive,
|
font::Family::Cursive => cosmic_text::Family::Cursive,
|
||||||
Font::Fantasy => cosmic_text::Family::Fantasy,
|
font::Family::Fantasy => cosmic_text::Family::Fantasy,
|
||||||
Font::Monospace => cosmic_text::Family::Monospace,
|
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,
|
font_system,
|
||||||
key.content,
|
key.content,
|
||||||
cosmic_text::Attrs::new()
|
cosmic_text::Attrs::new()
|
||||||
.family(to_family(key.font))
|
.family(to_family(key.font.family))
|
||||||
.monospaced(matches!(key.font, Font::Monospace)),
|
.weight(to_weight(key.font.weight))
|
||||||
|
.monospaced(
|
||||||
|
key.font.monospaced
|
||||||
|
|| matches!(
|
||||||
|
key.font.family,
|
||||||
|
font::Family::Monospace
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = entry.insert(buffer);
|
let _ = entry.insert(buffer);
|
||||||
|
|
|
||||||
|
|
@ -336,7 +336,7 @@ impl iced_graphics::Backend for Backend {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl backend::Text 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 CHECKMARK_ICON: char = '\u{f00c}';
|
||||||
const ARROW_DOWN_ICON: char = '\u{e800}';
|
const ARROW_DOWN_ICON: char = '\u{e800}';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,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::Monospace,
|
font: Font::MONOSPACE,
|
||||||
horizontal_alignment: alignment::Horizontal::Left,
|
horizontal_alignment: alignment::Horizontal::Left,
|
||||||
vertical_alignment: alignment::Vertical::Top,
|
vertical_alignment: alignment::Vertical::Top,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ 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: Font::SansSerif,
|
default_font: Font::default(),
|
||||||
default_text_size: 16.0,
|
default_text_size: 16.0,
|
||||||
antialiasing: None,
|
antialiasing: None,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::core::alignment;
|
use crate::core::alignment;
|
||||||
|
use crate::core::font::{self, Font};
|
||||||
use crate::core::text::Hit;
|
use crate::core::text::Hit;
|
||||||
use crate::core::{Font, Point, Rectangle, Size};
|
use crate::core::{Point, Rectangle, Size};
|
||||||
use crate::layer::Text;
|
use crate::layer::Text;
|
||||||
|
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
|
@ -262,14 +263,28 @@ impl Pipeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_family(font: Font) -> glyphon::Family<'static> {
|
fn to_family(family: font::Family) -> glyphon::Family<'static> {
|
||||||
match font {
|
match family {
|
||||||
Font::Name(name) => glyphon::Family::Name(name),
|
font::Family::Name(name) => glyphon::Family::Name(name),
|
||||||
Font::SansSerif => glyphon::Family::SansSerif,
|
font::Family::SansSerif => glyphon::Family::SansSerif,
|
||||||
Font::Serif => glyphon::Family::Serif,
|
font::Family::Serif => glyphon::Family::Serif,
|
||||||
Font::Cursive => glyphon::Family::Cursive,
|
font::Family::Cursive => glyphon::Family::Cursive,
|
||||||
Font::Fantasy => glyphon::Family::Fantasy,
|
font::Family::Fantasy => glyphon::Family::Fantasy,
|
||||||
Font::Monospace => glyphon::Family::Monospace,
|
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,
|
font_system,
|
||||||
key.content,
|
key.content,
|
||||||
glyphon::Attrs::new()
|
glyphon::Attrs::new()
|
||||||
.family(to_family(key.font))
|
.family(to_family(key.font.family))
|
||||||
.monospaced(matches!(key.font, Font::Monospace)),
|
.weight(to_weight(key.font.weight))
|
||||||
|
.monospaced(
|
||||||
|
key.font.monospaced
|
||||||
|
|| matches!(
|
||||||
|
key.font.family,
|
||||||
|
font::Family::Monospace
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = entry.insert(buffer);
|
let _ = entry.insert(buffer);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue