Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#![allow(clippy::needless_question_mark, clippy::question_mark)]
use nanoserde::{DeJson, DeJsonErr, DeJsonState, SerJson, SerJsonState};
pub struct UnicodeChar(char);
impl DeJson for UnicodeChar {
fn de_json(state: &mut DeJsonState, input: &mut std::str::Chars) -> Result<Self, DeJsonErr> {
u32::de_json(state, input).and_then(|a| {
if let Some(a) = char::from_u32(a) {
Ok(Self(a))
} else {
Err(state.err_parse("Not unicode"))
}
})
}
}
impl SerJson for UnicodeChar {
fn ser_json(&self, d: usize, s: &mut SerJsonState) {
let out = self.0 as u32;
out.ser_json(d, s)
}
}
impl From<&char> for UnicodeChar {
fn from(c: &char) -> Self {
Self(*c)
}
}
impl From<&UnicodeChar> for char {
fn from(uc: &UnicodeChar) -> Self {
uc.0
}
}
#[derive(DeJson, Debug, Clone, Copy, PartialEq)]
pub struct Glyph {
#[nserde(proxy = "UnicodeChar")]
pub unicode: char,
pub advance: f32,
#[nserde(rename = "atlasBounds")]
pub atlas_bounds: Option<Rect>,
#[nserde(rename = "planeBounds")]
pub plane_bounds: Option<Rect>,
}
#[derive(DeJson, Default, Clone, Copy, Debug, PartialEq)]
pub struct Rect {
pub left: f32,
pub bottom: f32,
pub right: f32,
pub top: f32,
}
impl Rect {
pub fn width(&self) -> f32 {
self.right - self.left
}
pub fn height(&self) -> f32 {
self.top - self.bottom
}
pub fn size(&self) -> (f32, f32) {
(self.width(), self.height())
}
}