forked from embedded-graphics/bdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmono_font.rs
More file actions
332 lines (282 loc) · 10.7 KB
/
Copy pathmono_font.rs
File metadata and controls
332 lines (282 loc) · 10.7 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use std::{fs, io, ops::RangeInclusive, path::Path};
use anyhow::{bail, Context, Result};
use bdf_parser::{Encoding, Font, Glyph};
use eg_bdf::BdfTextStyle;
use embedded_graphics::{
image::ImageRaw,
mono_font::{mapping::Mapping, DecorationDimensions, MonoFont},
pixelcolor::BinaryColor,
prelude::*,
text::{Baseline, Text},
};
use embedded_graphics_simulator::{OutputSettings, SimulatorDisplay};
use quote::{format_ident, quote};
use crate::{ConvertedFont, EgBdfOutput};
/// Font conversion output for [`MonoFont`].
#[derive(Debug)]
pub struct MonoFontOutput {
font: ConvertedFont,
bitmap: SimulatorDisplay<BinaryColor>,
data: Vec<u8>,
character_size: Size,
character_spacing: u32,
baseline: u32,
underline: DecorationDimensions,
strikethrough: DecorationDimensions,
glyphs: Vec<Glyph>,
mapping: Option<Mapping>,
}
impl MonoFontOutput {
pub(crate) fn new(bdf: EgBdfOutput) -> Result<Self> {
let font = bdf.as_font();
let style = BdfTextStyle::new(&font, BinaryColor::On);
let glyphs_per_row = 16; //TODO: make configurable
let columns = glyphs_per_row; // TODO: allow smaller column count
let rows = font.glyphs.len().div_ceil(glyphs_per_row);
let character_size = bdf.bounding_box().size;
let character_spacing = 0;
let baseline = bdf.font.ascent.saturating_sub(1);
let strikethrough = DecorationDimensions::new(
bdf.font.strikethrough_position,
bdf.font.strikethrough_thickness,
);
let underline =
DecorationDimensions::new(bdf.font.underline_position, bdf.font.underline_thickness);
let mut bitmap = SimulatorDisplay::new(
character_size.component_mul(Size::new(columns as u32, rows as u32)),
);
let mapping = glyphs_to_mapping(&bdf.font.glyphs);
// Rearrange the glyphs in the correct order if a mapping is used,
// because e-g monofont mappings aren't sorted by the Unicode codepoint.
let glyphs = if let Some(mapping) = mapping {
mapping
.glyph_mapping()
.chars()
.map(|c| {
let index = bdf.font.glyph_index(c).unwrap();
bdf.font.glyphs[index].clone()
})
.collect::<Vec<_>>()
} else {
// Filter out all non standard glyphs.
// TODO: add a warning
bdf.font
.glyphs
.iter()
.filter(|g| matches!(g.encoding, Encoding::Standard(_)))
.cloned()
.collect()
};
for (i, glyph) in glyphs.iter().enumerate() {
let x = (i % glyphs_per_row) as i32 * character_size.width as i32;
let y = (i / glyphs_per_row) as i32 * character_size.height as i32;
// TODO: assumes unicode
let c = match glyph.encoding {
Encoding::Standard(index) => char::from_u32(index).unwrap(),
_ => bail!("invalid encoding: '{:?}'", glyph.encoding),
};
Text::with_baseline(&String::from(c), Point::new(x, y), style, Baseline::Top)
.draw(&mut bitmap)
.unwrap();
}
let data = bitmap.to_be_bytes();
Ok(Self {
font: bdf.font,
bitmap,
data,
character_size,
character_spacing,
baseline,
underline,
strikethrough,
glyphs,
mapping,
})
}
/// Returns the rust code.
pub fn rust(&self) -> String {
self.try_rust().unwrap()
}
fn try_rust(&self) -> Result<String> {
let const_name = format_ident!("{}", self.font.name);
let image_data = self.font.data_file().to_string_lossy().to_string();
let image_width = self.bitmap.size().width;
let MonoFont {
character_size:
Size {
width: character_width,
height: character_height,
},
character_spacing,
baseline,
underline:
DecorationDimensions {
offset: underline_offset,
height: underline_height,
},
strikethrough:
DecorationDimensions {
offset: strikethrough_offset,
height: strikethrough_height,
},
..
} = self.as_font();
let glyph_mapping = if let Some(mapping) = self.mapping {
let mime = format_ident!("{}", mapping.mime());
quote!(::embedded_graphics::mono_font::mapping::#mime)
} else {
let str_mapping = glyphs_to_str_mapping(self.glyphs.iter().map(|glyph| {
// TODO: assumes unicode
match glyph.encoding {
Encoding::Standard(index) => char::from_u32(index).unwrap(),
_ => unreachable!(),
}
}));
let replacement = self.font.replacement_character;
quote!(::embedded_graphics::mono_font::mapping::StrGlyphMapping::new(#str_mapping, #replacement))
};
let comments = self.font.comments.iter().map(|comment| {
let comment = format!(" {comment}");
quote!(
#[doc = #comment]
)
});
Ok(prettyplease::unparse(&syn::parse2(quote!(
#( #comments )*
pub const #const_name: ::embedded_graphics::mono_font::MonoFont = ::embedded_graphics::mono_font::MonoFont {
image: ::embedded_graphics::image::ImageRaw::new(include_bytes!(#image_data), #image_width),
glyph_mapping: &#glyph_mapping,
character_size: ::embedded_graphics::geometry::Size::new(#character_width, #character_height),
character_spacing: #character_spacing,
baseline: #baseline,
underline: ::embedded_graphics::mono_font::DecorationDimensions::new(#underline_offset, #underline_height),
strikethrough: ::embedded_graphics::mono_font::DecorationDimensions::new(#strikethrough_offset, #strikethrough_height),
};
))?))
}
/// Returns the bitmap data.
pub fn data(&self) -> &[u8] {
&self.data
}
/// Returns the conversion result as a [`MonoFont`].
pub fn as_font(&self) -> MonoFont<'_> {
let image = ImageRaw::new(self.data(), self.bitmap.size().width);
MonoFont {
image,
character_size: self.character_size,
character_spacing: self.character_spacing,
baseline: self.baseline,
strikethrough: self.strikethrough,
underline: self.underline,
glyph_mapping: &self.font,
}
}
/// Saves the rust code and bitmap data to a directory.
pub fn save<P: AsRef<Path>>(&self, output_directory: P) -> io::Result<()> {
let output_directory = output_directory.as_ref();
fs::write(self.font.rust_file_path(output_directory), self.rust())?;
fs::write(self.font.data_file_path(output_directory), self.data())?;
Ok(())
}
/// Returns the BDF file.
pub fn bdf(&self) -> &Font {
&self.font.bdf
}
/// Saves the generated bitmap as a PNG file.
pub fn save_png<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let path = path.as_ref();
self.bitmap
.to_grayscale_output_image(&OutputSettings::default())
.save_png(path)
.with_context(|| format!("failed to write PNG file to {}", path.display()))
}
}
fn glyphs_to_str_mapping(glyphs: impl Iterator<Item = char>) -> String {
let mut ranges: Vec<RangeInclusive<u32>> = Vec::new();
for c in glyphs.map(|c| c as u32) {
match ranges.last_mut() {
Some(range) if c == *range.end() + 1 => {
*range = *range.start()..=c;
}
_ => ranges.push(c..=c),
}
}
let mut mapping = String::new();
for range in ranges {
assert!(!range.is_empty());
let chars = range.end() - range.start() + 1;
if chars == 1 {
mapping.push(char::from_u32(*range.start()).unwrap());
} else {
if chars > 2 {
mapping.push('\0');
}
mapping.push(char::from_u32(*range.start()).unwrap());
mapping.push(char::from_u32(*range.end()).unwrap());
}
}
mapping
}
// TODO: also check the replacement character
fn glyphs_to_mapping(glyphs: &[Glyph]) -> Option<Mapping> {
for mapping in Mapping::iter() {
let mut chars = mapping.glyph_mapping().chars().collect::<Vec<_>>();
chars.sort();
if glyphs
.iter()
.map(|glyph| {
// TODO: assumes unicode
match glyph.encoding {
Encoding::Standard(index) => char::from_u32(index),
_ => None,
}
})
.eq(chars.iter().map(|c| Some(*c)))
{
return Some(mapping);
}
}
None
}
#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use super::*;
#[test]
fn test_glyphs_to_str_mapping() {
let mut glyphs = BTreeSet::new();
assert_eq!(glyphs_to_str_mapping(glyphs.iter().copied()), "");
glyphs.insert('a');
assert_eq!(glyphs_to_str_mapping(glyphs.iter().copied()), "a");
glyphs.insert('b');
assert_eq!(glyphs_to_str_mapping(glyphs.iter().copied()), "ab");
glyphs.insert('c');
assert_eq!(glyphs_to_str_mapping(glyphs.iter().copied()), "\0ac");
glyphs.insert('d');
assert_eq!(glyphs_to_str_mapping(glyphs.iter().copied()), "\0ad");
glyphs.insert('x');
assert_eq!(glyphs_to_str_mapping(glyphs.iter().copied()), "\0adx");
glyphs.insert('y');
assert_eq!(glyphs_to_str_mapping(glyphs.iter().copied()), "\0adxy");
glyphs.insert('z');
assert_eq!(glyphs_to_str_mapping(glyphs.iter().copied()), "\0ad\0xz");
}
#[test]
fn test_glyphs_to_mapping() {
let glyphs = (' '..='\x7F')
.map(|c| Glyph {
encoding: Encoding::Standard(c as u32),
..Glyph::default()
})
.collect::<Vec<_>>();
assert_eq!(glyphs_to_mapping(&glyphs), Some(Mapping::Ascii));
let glyphs = (' '..='\x7F')
.chain(0xA0 as char..=0xFF as char)
.map(|c| Glyph {
encoding: Encoding::Standard(c as u32),
..Glyph::default()
})
.collect::<Vec<_>>();
assert_eq!(glyphs_to_mapping(&glyphs), Some(Mapping::Iso8859_1));
}
}