gpui/text_system/
font_weight.rs1use derive_more::{Add, FromStr, Sub};
2use serde::{Deserialize, Serialize};
3use std::{
4 fmt::{Debug, Display},
5 hash::{Hash, Hasher},
6};
7
8#[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 pub const THIN: FontWeight = FontWeight(100.0);
57 pub const EXTRA_LIGHT: FontWeight = FontWeight(200.0);
59 pub const LIGHT: FontWeight = FontWeight(300.0);
61 pub const NORMAL: FontWeight = FontWeight(400.0);
63 pub const MEDIUM: FontWeight = FontWeight(500.0);
65 pub const SEMIBOLD: FontWeight = FontWeight(600.0);
67 pub const BOLD: FontWeight = FontWeight(700.0);
69 pub const EXTRA_BOLD: FontWeight = FontWeight(800.0);
71 pub const BLACK: FontWeight = FontWeight(900.0);
73
74 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}