Skip to main content

freya_core/style/
font_weight.rs

1use freya_engine::prelude::Weight as SkWeight;
2
3/// Thickness of a font, from `100` (thin) to `1000`. Defaults to [`FontWeight::NORMAL`] (`400`).
4///
5/// Use one of the named constants. It also implements `From<i32>`.
6///
7/// ```
8/// # use freya::prelude::*;
9/// let weight = FontWeight::BOLD;
10/// ```
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd)]
13pub struct FontWeight(i32);
14
15impl Default for FontWeight {
16    fn default() -> Self {
17        FontWeight::NORMAL
18    }
19}
20
21impl FontWeight {
22    pub const INVISIBLE: Self = Self(0);
23    pub const THIN: Self = Self(100);
24    pub const EXTRA_LIGHT: Self = Self(200);
25    pub const LIGHT: Self = Self(300);
26    pub const NORMAL: Self = Self(400);
27    pub const MEDIUM: Self = Self(500);
28    pub const SEMI_BOLD: Self = Self(600);
29    pub const BOLD: Self = Self(700);
30    pub const EXTRA_BOLD: Self = Self(800);
31    pub const BLACK: Self = Self(900);
32    pub const EXTRA_BLACK: Self = Self(1000);
33}
34
35impl From<i32> for FontWeight {
36    fn from(weight: i32) -> Self {
37        FontWeight(weight)
38    }
39}
40
41impl From<FontWeight> for i32 {
42    fn from(weight: FontWeight) -> i32 {
43        weight.0
44    }
45}
46
47impl From<FontWeight> for f32 {
48    fn from(weight: FontWeight) -> f32 {
49        weight.0 as f32
50    }
51}
52
53impl From<FontWeight> for SkWeight {
54    fn from(value: FontWeight) -> Self {
55        value.0.into()
56    }
57}