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)]
74#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
75pub enum Weight {
76    Thin,
77    ExtraLight,
78    Light,
79    #[default]
80    Normal,
81    Medium,
82    Semibold,
83    Bold,
84    ExtraBold,
85    Black,
86}
87
88/// The width of some text.
89#[allow(missing_docs)]
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92pub enum Stretch {
93    UltraCondensed,
94    ExtraCondensed,
95    Condensed,
96    SemiCondensed,
97    #[default]
98    Normal,
99    SemiExpanded,
100    Expanded,
101    ExtraExpanded,
102    UltraExpanded,
103}
104
105/// The style of some text.
106#[allow(missing_docs)]
107#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
108#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
109pub enum Style {
110    #[default]
111    Normal,
112    Italic,
113    Oblique,
114}