Skip to main content

gpui/text_system/
font_weight.rs

1use derive_more::{Add, FromStr, Sub};
2use serde::{Deserialize, Serialize};
3use std::{
4    fmt::{Debug, Display},
5    hash::{Hash, Hasher},
6};
7
8/// The weight of a font, from [`FontWeight::THIN`] (100) to [`FontWeight::BLACK`] (900).
9///
10/// Weight selects how thick the strokes of a face are. [`FontWeight::NORMAL`]
11/// (400) is the default, and [`FontWeight::BOLD`] (700) is what [`Font::bold`](crate::Font::bold)
12/// sets. The value is a plain `f32`, so you can use values between the named
13/// constants when the font has faces for them.
14///
15/// # Examples
16///
17/// ```
18/// # use gpui::{FontWeight, font};
19/// let heading = font("Lilex").bold();
20/// assert_eq!(heading.weight, FontWeight::BOLD);
21/// assert_eq!(FontWeight::default(), FontWeight::NORMAL);
22/// ```
23#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Serialize, Deserialize, Add, Sub, FromStr)]
24#[serde(transparent)]
25pub struct FontWeight(pub f32);
26
27impl Display for FontWeight {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        write!(f, "{}", self.0)
30    }
31}
32
33impl From<f32> for FontWeight {
34    fn from(weight: f32) -> Self {
35        FontWeight(weight)
36    }
37}
38
39impl Default for FontWeight {
40    #[inline]
41    fn default() -> FontWeight {
42        FontWeight::NORMAL
43    }
44}
45
46impl Hash for FontWeight {
47    fn hash<H: Hasher>(&self, state: &mut H) {
48        state.write_u32(u32::from_be_bytes(self.0.to_be_bytes()));
49    }
50}
51
52impl Eq for FontWeight {}
53
54impl FontWeight {
55    /// Thin weight (100), the thinnest value.
56    pub const THIN: FontWeight = FontWeight(100.0);
57    /// Extra light weight (200).
58    pub const EXTRA_LIGHT: FontWeight = FontWeight(200.0);
59    /// Light weight (300).
60    pub const LIGHT: FontWeight = FontWeight(300.0);
61    /// Normal (400).
62    pub const NORMAL: FontWeight = FontWeight(400.0);
63    /// Medium weight (500, higher than normal).
64    pub const MEDIUM: FontWeight = FontWeight(500.0);
65    /// Semibold weight (600).
66    pub const SEMIBOLD: FontWeight = FontWeight(600.0);
67    /// Bold weight (700).
68    pub const BOLD: FontWeight = FontWeight(700.0);
69    /// Extra-bold weight (800).
70    pub const EXTRA_BOLD: FontWeight = FontWeight(800.0);
71    /// Black weight (900), the thickest value.
72    pub const BLACK: FontWeight = FontWeight(900.0);
73
74    /// All of the font weights, in order from thinnest to thickest.
75    pub const ALL: [FontWeight; 9] = [
76        Self::THIN,
77        Self::EXTRA_LIGHT,
78        Self::LIGHT,
79        Self::NORMAL,
80        Self::MEDIUM,
81        Self::SEMIBOLD,
82        Self::BOLD,
83        Self::EXTRA_BOLD,
84        Self::BLACK,
85    ];
86}
87
88impl schemars::JsonSchema for FontWeight {
89    fn schema_name() -> std::borrow::Cow<'static, str> {
90        "FontWeight".into()
91    }
92
93    fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
94        use schemars::json_schema;
95        json_schema!({
96            "type": "number",
97            "minimum": Self::THIN,
98            "maximum": Self::BLACK,
99            "default": Self::default(),
100            "description": "Font weight value between 100 (thin) and 900 (black)"
101        })
102    }
103}