iced_core/font.rs
1//! Load and use fonts.
2use std::hash::Hash;
3
4/// A font.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
6pub struct Font {
7 /// The [`Family`] of the [`Font`].
8 pub family: Family,
9 /// The [`Weight`] of the [`Font`].
10 pub weight: Weight,
11 /// The [`Stretch`] of the [`Font`].
12 pub stretch: Stretch,
13 /// The [`Style`] of the [`Font`].
14 pub style: Style,
15}
16
17impl Font {
18 /// A non-monospaced sans-serif font with normal [`Weight`].
19 pub const DEFAULT: Font = Font {
20 family: Family::SansSerif,
21 weight: Weight::Normal,
22 stretch: Stretch::Normal,
23 style: Style::Normal,
24 };
25
26 /// A monospaced font with normal [`Weight`].
27 pub const MONOSPACE: Font = Font {
28 family: Family::Monospace,
29 ..Self::DEFAULT
30 };
31
32 /// Creates a non-monospaced [`Font`] with the given [`Family::Name`] and
33 /// normal [`Weight`].
34 pub const fn with_name(name: &'static str) -> Self {
35 Font {
36 family: Family::Name(name),
37 ..Self::DEFAULT
38 }
39 }
40}
41
42/// A font family.
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
44pub enum Family {
45 /// The name of a font family of choice.
46 Name(&'static str),
47
48 /// Serif fonts represent the formal text style for a script.
49 Serif,
50
51 /// Glyphs in sans-serif fonts, as the term is used in CSS, are generally low
52 /// contrast and have stroke endings that are plain — without any flaring,
53 /// cross stroke, or other ornamentation.
54 #[default]
55 SansSerif,
56
57 /// Glyphs in cursive fonts generally use a more informal script style, and
58 /// the result looks more like handwritten pen or brush writing than printed
59 /// letterwork.
60 Cursive,
61
62 /// Fantasy fonts are primarily decorative or expressive fonts that contain
63 /// decorative or expressive representations of characters.
64 Fantasy,
65
66 /// The sole criterion of a monospace font is that all glyphs have the same
67 /// fixed width.
68 Monospace,
69}
70
71/// The weight of some text.
72#[allow(missing_docs)]
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
74pub enum Weight {
75 Thin,
76 ExtraLight,
77 Light,
78 #[default]
79 Normal,
80 Medium,
81 Semibold,
82 Bold,
83 ExtraBold,
84 Black,
85}
86
87/// The width of some text.
88#[allow(missing_docs)]
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
90pub enum Stretch {
91 UltraCondensed,
92 ExtraCondensed,
93 Condensed,
94 SemiCondensed,
95 #[default]
96 Normal,
97 SemiExpanded,
98 Expanded,
99 ExtraExpanded,
100 UltraExpanded,
101}
102
103/// The style of some text.
104#[allow(missing_docs)]
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
106pub enum Style {
107 #[default]
108 Normal,
109 Italic,
110 Oblique,
111}