Skip to main content

gpui_material_theme/
typography.rs

1use crate::generated::{BASELINE_TYPE_STYLES, EMPHASIZED_TYPE_STYLES};
2
3/// The two Material typeface families.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum Typeface {
6    Brand,
7    Plain,
8}
9
10/// Family preference and ordered fallback for one typeface.
11#[derive(Clone, Debug, PartialEq, Eq)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct FontFamilies {
14    pub primary: String,
15    pub fallback: Vec<String>,
16}
17
18impl Default for FontFamilies {
19    fn default() -> Self {
20        Self {
21            primary: "Roboto".into(),
22            fallback: ["Segoe UI", "Microsoft YaHei UI", "Noto Sans CJK SC"]
23                .map(str::to_owned)
24                .into(),
25        }
26    }
27}
28
29/// One of the fifteen Material text roles.
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub enum TypeRole {
32    BodyLarge,
33    BodyMedium,
34    BodySmall,
35    DisplayLarge,
36    DisplayMedium,
37    DisplaySmall,
38    HeadlineLarge,
39    HeadlineMedium,
40    HeadlineSmall,
41    LabelLarge,
42    LabelMedium,
43    LabelSmall,
44    TitleLarge,
45    TitleMedium,
46    TitleSmall,
47}
48
49impl TypeRole {
50    pub const ALL: [Self; 15] = [
51        Self::BodyLarge,
52        Self::BodyMedium,
53        Self::BodySmall,
54        Self::DisplayLarge,
55        Self::DisplayMedium,
56        Self::DisplaySmall,
57        Self::HeadlineLarge,
58        Self::HeadlineMedium,
59        Self::HeadlineSmall,
60        Self::LabelLarge,
61        Self::LabelMedium,
62        Self::LabelSmall,
63        Self::TitleLarge,
64        Self::TitleMedium,
65        Self::TitleSmall,
66    ];
67
68    pub const fn style(self, emphasis: TypographyEmphasis) -> TypeStyle {
69        match emphasis {
70            TypographyEmphasis::Baseline => BASELINE_TYPE_STYLES[self as usize],
71            TypographyEmphasis::Emphasized => EMPHASIZED_TYPE_STYLES[self as usize],
72        }
73    }
74}
75
76#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
77pub enum TypographyEmphasis {
78    #[default]
79    Baseline,
80    Emphasized,
81}
82
83/// Material type metrics in scale-independent units (sp).
84#[derive(Clone, Copy, Debug, PartialEq)]
85pub struct TypeStyle {
86    pub typeface: Typeface,
87    pub size: f32,
88    pub line_height: f32,
89    pub tracking: f32,
90    pub weight: u16,
91}
92
93/// Family names for the Material scale. The host resolves available fonts
94/// and measures the resulting text.
95#[derive(Clone, Debug, Default, PartialEq, Eq)]
96#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
97pub struct Typography {
98    pub brand: FontFamilies,
99    pub plain: FontFamilies,
100}
101
102impl Typography {
103    pub fn families(&self, typeface: Typeface) -> &FontFamilies {
104        match typeface {
105            Typeface::Brand => &self.brand,
106            Typeface::Plain => &self.plain,
107        }
108    }
109}