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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use png::{ColorType, Decoder};
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use sfl_parser::BMFont;
#[derive(Debug, Clone, PartialEq)]
pub struct CharacterData {
pub(crate) id: i32,
pub(crate) x1: f32,
pub(crate) x2: f32,
pub(crate) y1: f32,
pub(crate) y2: f32,
pub(crate) width: i32,
pub(crate) height: i32,
pub(crate) x_off: i32,
pub(crate) y_off: i32,
}
#[derive(Debug, PartialEq)]
pub struct Font {
pub name: String,
pub(crate) image_buffer: Vec<u8>,
pub(crate) width: u32,
pub(crate) height: u32,
pub line_height: u32,
pub size: u32,
pub(crate) min_offset_y: i32,
pub(crate) characters: HashMap<u16, CharacterData>,
}
impl Font {
pub fn load<T: Into<PathBuf>>(fnt_path: T) -> Font {
let fnt_path = fnt_path.into();
if !fnt_path.exists() {
panic!("Font image or .sfl file missing");
}
let bm_font;
match BMFont::from_path(fnt_path) {
Ok(bmf) => bm_font = bmf,
Err(error) => panic!("Failed to load .sfl file: {}", error),
}
Font::load_with_bmfont_and_image_read(&bm_font, File::open(&bm_font.image_path).unwrap())
}
pub fn load_raw<T: Into<String>, R: Read>(sfl_content: T, image_read: R) -> Font {
let bm_font;
match BMFont::from_loaded(sfl_content.into(), "image.png".to_owned()) {
Ok(bmf) => bm_font = bmf,
Err(error) => panic!("Failed to load .sfl file: {}", error),
}
Font::load_with_bmfont_and_image_read(&bm_font, image_read)
}
fn load_with_bmfont_and_image_read<R: Read>(bm_font: &BMFont, read: R) -> Font {
let decoder = Decoder::new(read);
let (info, mut reader) = decoder.read_info().unwrap();
if info.color_type != ColorType::RGBA {
panic!("Font color type is not RGBA");
}
let mut image_buffer = vec![0; info.buffer_size()];
reader.next_frame(&mut image_buffer).unwrap();
if image_buffer.len() != (info.width * info.height * 4) as usize {
panic!("Font image is deformed");
}
let mut characters = HashMap::<u16, CharacterData>::new();
let width_float = info.width as f32;
let height_float = info.height as f32;
let mut min_off_y = 100_000;
for (key, value) in bm_font.chars.iter() {
let x1 = value.x as f32 / width_float;
let x2 = (value.x as f32 + value.width as f32) / width_float;
let y1 = value.y as f32 / height_float;
let y2 = (value.y as f32 + value.height as f32) / height_float;
if value.yoffset < min_off_y {
min_off_y = value.yoffset;
}
characters.insert(
*key as u16,
CharacterData {
id: value.id,
x1,
x2,
y1,
y2,
width: value.width,
height: value.height,
x_off: value.xoffset,
y_off: value.yoffset,
},
);
}
Font {
name: (&bm_font.font_name).clone(),
image_buffer: image_buffer,
width: info.width,
height: info.height,
line_height: bm_font.line_height,
size: bm_font.size,
min_offset_y: min_off_y,
characters: characters,
}
}
pub fn get_character(&self, character: u16) -> Result<CharacterData, String> {
if let Some(character_data) = self.characters.get(&character) {
Ok(character_data.clone())
} else {
Err(format!("Character not found: '{}'", character))
}
}
}