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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#![warn(missing_docs)]
#![warn(clippy::all)]
#[cfg(test)]
mod tests;
mod bmfont_parser;
mod parser;
mod sfl_parser;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::fs::File;
use std::io::{Error, ErrorKind, Read};
use std::path::PathBuf;
pub(crate) fn err<T: Into<String>, U>(text: T) -> Result<U, Error> {
Err(Error::new(ErrorKind::Other, text.into()))
}
pub(crate) fn result_or<T: Into<String>, U, N>(res: Result<U, N>, text: T) -> Result<U, Error> {
match res {
Ok(val) => Ok(val),
Err(_) => Err(Error::new(ErrorKind::Other, text.into())),
}
}
#[derive(Debug, Clone)]
pub struct BMCharacter {
pub id: u32,
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
pub xoffset: i32,
pub yoffset: i32,
pub xadvance: i32,
pub page: u32,
pub channel: u32,
}
#[derive(Debug, Clone)]
pub struct InfoDetails {
pub bold: u32,
pub italic: u32,
pub charset: String,
pub unicode: u32,
pub stretch_h: u32,
pub smooth: u32,
pub aa: u32,
pub padding: [u32; 4],
pub spacing: [u32; 2],
pub outline: u32,
}
#[derive(Debug, Clone)]
pub struct CommonDetails {
pub base: u32,
pub scale_w: u32,
pub scale_h: u32,
pub pages_count: u32,
pub packed: u32,
pub alpha_channel: u32,
pub red_channel: u32,
pub green_channel: u32,
pub blue_channel: u32,
}
#[derive(Debug, Clone)]
pub struct BMFont {
pub font_name: String,
pub size: u32,
pub info_details: Option<InfoDetails>,
pub line_height: u32,
pub common_details: Option<CommonDetails>,
pub pages: Vec<Page>,
pub chars: HashMap<u32, BMCharacter>,
}
#[derive(Debug, Clone)]
pub struct Page {
pub id: u32,
pub image_path: PathBuf,
}
pub enum Format {
SFL,
BMFont,
}
impl BMFont {
pub fn from_path<T: Into<PathBuf>>(format: &Format, path: T) -> Result<BMFont, Error> {
let path = path.into();
let mut file = File::open(&path)?;
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
let mut bmfont = match format {
Format::SFL => sfl_parser::load(buffer)?,
Format::BMFont => bmfont_parser::load(buffer)?,
};
if let Some(path) = path.parent() {
for page in bmfont.pages.iter_mut() {
let mut image_path = (*path).to_path_buf();
image_path.push(page.image_path.clone());
page.image_path = image_path;
}
Ok(bmfont)
} else {
err("Unable to retrieve path parent.")
}
}
pub fn from_loaded<T: Into<String>>(
format: &Format,
contents: T,
image_path: &[&str],
) -> Result<BMFont, Error> {
let mut bmfont = match format {
Format::SFL => sfl_parser::load(contents)?,
Format::BMFont => bmfont_parser::load(contents)?,
};
for (idx, page) in bmfont.pages.iter_mut().enumerate() {
let mut pathbuf = PathBuf::new();
let path;
if let Some(p) = image_path.get(idx) {
path = p;
} else {
return err("Wrong amount of image paths given to accompany each page");
};
pathbuf.push(path);
page.image_path = pathbuf;
}
Ok(bmfont)
}
}
impl Display for BMFont {
fn fmt<'a>(&self, f: &mut Formatter<'a>) -> std::fmt::Result {
write!(
f,
"BMFont: {{ name: {}, line_height: {}, size: {}, amount of characters: {}, pages: {:?} }}",
self.font_name,
self.line_height,
self.size,
self.chars.len(),
self.pages
)
}
}