fonts.rs

 1use crate::json::json;
 2pub use font_kit::metrics::Metrics;
 3pub use font_kit::properties::{Properties, Stretch, Style, Weight};
 4
 5use crate::json::ToJson;
 6
 7#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
 8pub struct FontId(pub usize);
 9
10pub type GlyphId = u32;
11
12impl ToJson for Properties {
13    fn to_json(&self) -> crate::json::Value {
14        json!({
15            "style": self.style.to_json(),
16            "weight": self.weight.to_json(),
17            "stretch": self.stretch.to_json(),
18        })
19    }
20}
21
22impl ToJson for Style {
23    fn to_json(&self) -> crate::json::Value {
24        match self {
25            Style::Normal => json!("normal"),
26            Style::Italic => json!("italic"),
27            Style::Oblique => json!("oblique"),
28        }
29    }
30}
31
32impl ToJson for Weight {
33    fn to_json(&self) -> crate::json::Value {
34        if self.0 == Weight::THIN.0 {
35            json!("thin")
36        } else if self.0 == Weight::EXTRA_LIGHT.0 {
37            json!("extra light")
38        } else if self.0 == Weight::LIGHT.0 {
39            json!("light")
40        } else if self.0 == Weight::NORMAL.0 {
41            json!("normal")
42        } else if self.0 == Weight::MEDIUM.0 {
43            json!("medium")
44        } else if self.0 == Weight::SEMIBOLD.0 {
45            json!("semibold")
46        } else if self.0 == Weight::BOLD.0 {
47            json!("bold")
48        } else if self.0 == Weight::EXTRA_BOLD.0 {
49            json!("extra bold")
50        } else if self.0 == Weight::BLACK.0 {
51            json!("black")
52        } else {
53            json!(self.0)
54        }
55    }
56}
57
58impl ToJson for Stretch {
59    fn to_json(&self) -> serde_json::Value {
60        json!(self.0)
61    }
62}