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;
|
||||
|
||||
/// 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,
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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> {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}';
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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}';
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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}';
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue