1#![no_std]
2#![doc = include_str!("../README.md")]
3
4use embedded_graphics::mono_font::mapping::GlyphMapping;
5#[cfg(any(
6 feature = "size-7x14",
7 feature = "size-14x28",
8 feature = "size-21x42",
9 feature = "size-28x56"
10))]
11use embedded_graphics::{
12 geometry::Size,
13 image::ImageRaw,
14 mono_font::{mapping::StrGlyphMapping, DecorationDimensions, MonoFont},
15};
16
17include!("codepoints.rs");
18
19#[cfg(any(
20 feature = "size-7x14",
21 feature = "size-14x28",
22 feature = "size-21x42",
23 feature = "size-28x56"
24))]
25const ASCII_MAPPING: StrGlyphMapping<'static> =
26 StrGlyphMapping::new("\0 \u{7e}", '?' as usize - ' ' as usize);
27
28#[cfg(any(
29 feature = "size-7x14",
30 feature = "size-14x28",
31 feature = "size-21x42",
32 feature = "size-28x56"
33))]
34macro_rules! define_font_size {
35 ($full:ident, $ascii:ident, $width:literal, $height:literal, $scale:literal, $raw:literal, $ascii_raw:literal) => {
36 pub const $full: MonoFont<'static> = MonoFont {
38 image: ImageRaw::new(include_bytes!($raw), $width * 8),
39 character_size: Size::new($width, $height),
40 character_spacing: 0,
41 baseline: 11 * $scale,
42 strikethrough: DecorationDimensions::new(7 * $scale, $scale),
43 underline: DecorationDimensions::new(13 * $scale, $scale),
44 glyph_mapping: &DEPARTURE_MONO_MAPPING,
45 };
46
47 pub const $ascii: MonoFont<'static> = MonoFont {
49 image: ImageRaw::new(include_bytes!($ascii_raw), $width * 8),
50 character_size: Size::new($width, $height),
51 character_spacing: 0,
52 baseline: 11 * $scale,
53 strikethrough: DecorationDimensions::new(7 * $scale, $scale),
54 underline: DecorationDimensions::new(13 * $scale, $scale),
55 glyph_mapping: &ASCII_MAPPING,
56 };
57 };
58}
59
60#[cfg(feature = "size-7x14")]
61define_font_size!(
62 FONT_7X14,
63 FONT_7X14_ASCII,
64 7,
65 14,
66 1,
67 "font_7x14.raw",
68 "font_7x14_ascii.raw"
69);
70
71#[cfg(feature = "size-14x28")]
72define_font_size!(
73 FONT_14X28,
74 FONT_14X28_ASCII,
75 14,
76 28,
77 2,
78 "font_14x28.raw",
79 "font_14x28_ascii.raw"
80);
81
82#[cfg(feature = "size-21x42")]
83define_font_size!(
84 FONT_21X42,
85 FONT_21X42_ASCII,
86 21,
87 42,
88 3,
89 "font_21x42.raw",
90 "font_21x42_ascii.raw"
91);
92
93#[cfg(feature = "size-28x56")]
94define_font_size!(
95 FONT_28X56,
96 FONT_28X56_ASCII,
97 28,
98 56,
99 4,
100 "font_28x56.raw",
101 "font_28x56_ascii.raw"
102);
103
104#[derive(Debug, Clone, Copy, Default)]
106pub struct DepartureMonoGlyphMapping;
107
108pub static DEPARTURE_MONO_MAPPING: DepartureMonoGlyphMapping = DepartureMonoGlyphMapping;
110
111impl DepartureMonoGlyphMapping {
112 pub fn contains(&self, character: char) -> bool {
114 glyph_index(character).is_some()
115 }
116}
117
118impl GlyphMapping for DepartureMonoGlyphMapping {
119 fn index(&self, character: char) -> usize {
120 glyph_index(character).unwrap_or(REPLACEMENT_GLYPH_INDEX)
121 }
122}
123
124fn glyph_index(character: char) -> Option<usize> {
125 let codepoint = character as u32;
126 let mut start = 0;
127 let mut end = RANGE_STARTS.len();
128
129 while start < end {
130 let middle = start + (end - start) / 2;
131 if codepoint < RANGE_STARTS[middle] {
132 end = middle;
133 } else if codepoint > RANGE_ENDS[middle] {
134 start = middle + 1;
135 } else {
136 return Some(
137 RANGE_OFFSETS[middle] as usize + (codepoint - RANGE_STARTS[middle]) as usize,
138 );
139 }
140 }
141
142 None
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148 #[cfg(any(
149 feature = "size-7x14",
150 feature = "size-14x28",
151 feature = "size-21x42",
152 feature = "size-28x56"
153 ))]
154 use embedded_graphics::prelude::OriginDimensions;
155 #[cfg(feature = "size-7x14")]
156 use embedded_graphics::{
157 mock_display::MockDisplay,
158 mono_font::MonoTextStyle,
159 pixelcolor::BinaryColor,
160 prelude::Point,
161 text::{Baseline, Text},
162 Drawable,
163 };
164
165 #[cfg(any(
166 feature = "size-7x14",
167 feature = "size-14x28",
168 feature = "size-21x42",
169 feature = "size-28x56"
170 ))]
171 fn assert_font_metrics(
172 full: &MonoFont<'_>,
173 ascii: &MonoFont<'_>,
174 width: u32,
175 height: u32,
176 scale: u32,
177 ) {
178 assert_eq!(full.character_size, Size::new(width, height));
179 assert_eq!(full.image.size(), Size::new(width * 8, height * 135));
180 assert_eq!(full.baseline, 11 * scale);
181 assert_eq!(
182 full.strikethrough,
183 DecorationDimensions::new(7 * scale, scale)
184 );
185 assert_eq!(full.underline, DecorationDimensions::new(13 * scale, scale));
186
187 assert_eq!(ascii.character_size, Size::new(width, height));
188 assert_eq!(ascii.image.size(), Size::new(width * 8, height * 12));
189 assert_eq!(ascii.baseline, 11 * scale);
190 }
191
192 #[cfg(feature = "size-7x14")]
193 #[test]
194 fn size_7x14_metrics() {
195 assert_font_metrics(&FONT_7X14, &FONT_7X14_ASCII, 7, 14, 1);
196 }
197
198 #[cfg(feature = "size-14x28")]
199 #[test]
200 fn size_14x28_metrics() {
201 assert_font_metrics(&FONT_14X28, &FONT_14X28_ASCII, 14, 28, 2);
202 }
203
204 #[cfg(feature = "size-21x42")]
205 #[test]
206 fn size_21x42_metrics() {
207 assert_font_metrics(&FONT_21X42, &FONT_21X42_ASCII, 21, 42, 3);
208 }
209
210 #[cfg(feature = "size-28x56")]
211 #[test]
212 fn size_28x56_metrics() {
213 assert_font_metrics(&FONT_28X56, &FONT_28X56_ASCII, 28, 56, 4);
214 }
215
216 #[test]
217 fn full_mapping_covers_expected_scripts() {
218 assert_eq!(SUPPORTED_GLYPH_COUNT, 1_079);
219 assert!(DEPARTURE_MONO_MAPPING.contains('A'));
220 assert!(DEPARTURE_MONO_MAPPING.contains('Ж'));
221 assert!(DEPARTURE_MONO_MAPPING.contains('Ω'));
222 assert!(DEPARTURE_MONO_MAPPING.contains('─'));
223 assert!(DEPARTURE_MONO_MAPPING.contains('🮂'));
224 assert!(!DEPARTURE_MONO_MAPPING.contains('\n'));
225 assert!(!DEPARTURE_MONO_MAPPING.contains('漢'));
226 }
227
228 #[test]
229 fn generated_ranges_map_every_supported_character() {
230 let mut expected_index = 0;
231
232 for range_index in 0..RANGE_STARTS.len() {
233 assert!(RANGE_STARTS[range_index] <= RANGE_ENDS[range_index]);
234 assert_eq!(RANGE_OFFSETS[range_index] as usize, expected_index);
235
236 for codepoint in RANGE_STARTS[range_index]..=RANGE_ENDS[range_index] {
237 let character = char::from_u32(codepoint).unwrap();
238 assert_eq!(glyph_index(character), Some(expected_index));
239 expected_index += 1;
240 }
241
242 if range_index + 1 < RANGE_STARTS.len() {
243 assert!(RANGE_ENDS[range_index] + 1 < RANGE_STARTS[range_index + 1]);
244 }
245 }
246
247 assert_eq!(expected_index, SUPPORTED_GLYPH_COUNT);
248 }
249
250 #[cfg(feature = "size-7x14")]
251 #[test]
252 fn missing_characters_use_question_mark() {
253 let style = MonoTextStyle::new(&FONT_7X14, BinaryColor::On);
254 let mut missing = MockDisplay::new();
255 let mut replacement = MockDisplay::new();
256
257 Text::with_baseline("漢", Point::zero(), style, Baseline::Top)
258 .draw(&mut missing)
259 .unwrap();
260 Text::with_baseline("?", Point::zero(), style, Baseline::Top)
261 .draw(&mut replacement)
262 .unwrap();
263
264 assert_eq!(missing, replacement);
265 }
266
267 #[cfg(feature = "size-7x14")]
268 #[test]
269 fn ascii_variant_matches_full_font() {
270 let full_style = MonoTextStyle::new(&FONT_7X14, BinaryColor::On);
271 let ascii_style = MonoTextStyle::new(&FONT_7X14_ASCII, BinaryColor::On);
272 let mut full = MockDisplay::new();
273 let mut ascii = MockDisplay::new();
274 full.set_allow_out_of_bounds_drawing(true);
275 ascii.set_allow_out_of_bounds_drawing(true);
276
277 Text::with_baseline("Departure Mono!?", Point::zero(), full_style, Baseline::Top)
278 .draw(&mut full)
279 .unwrap();
280 Text::with_baseline(
281 "Departure Mono!?",
282 Point::zero(),
283 ascii_style,
284 Baseline::Top,
285 )
286 .draw(&mut ascii)
287 .unwrap();
288
289 assert_eq!(full, ascii);
290 }
291
292 #[cfg(feature = "size-7x14")]
293 #[test]
294 fn box_drawing_connects_across_cells() {
295 let style = MonoTextStyle::new(&FONT_7X14, BinaryColor::On);
296 let mut display = MockDisplay::new();
297 display.set_allow_overdraw(true);
298
299 Text::with_baseline("──", Point::zero(), style, Baseline::Top)
300 .draw(&mut display)
301 .unwrap();
302
303 for x in 0..14 {
304 assert_eq!(display.get_pixel(Point::new(x, 7)), Some(BinaryColor::On));
305 }
306 }
307
308 #[cfg(feature = "size-7x14")]
309 #[test]
310 fn capital_a_matches_source_outline() {
311 let style = MonoTextStyle::new(&FONT_7X14, BinaryColor::On);
312 let mut display = MockDisplay::new();
313
314 Text::with_baseline("A", Point::zero(), style, Baseline::Top)
315 .draw(&mut display)
316 .unwrap();
317
318 display.assert_pattern(&[
319 " ", " ", " ", " # ", " # # ", " # # ", " # # ", " ##### ",
320 " # # ", " # # ", " # # ", " ", " ", " ",
321 ]);
322 }
323
324 #[cfg(all(
325 feature = "size-7x14",
326 feature = "size-14x28",
327 feature = "size-21x42",
328 feature = "size-28x56"
329 ))]
330 #[test]
331 fn larger_fonts_are_exact_integer_scales() {
332 let base_style = MonoTextStyle::new(&FONT_7X14, BinaryColor::On);
333 let mut base = MockDisplay::new();
334 Text::with_baseline("A", Point::zero(), base_style, Baseline::Top)
335 .draw(&mut base)
336 .unwrap();
337
338 assert_scaled_glyph(&base, &FONT_14X28, 2);
339 assert_scaled_glyph(&base, &FONT_21X42, 3);
340 assert_scaled_glyph(&base, &FONT_28X56, 4);
341 }
342
343 #[cfg(all(
344 feature = "size-7x14",
345 feature = "size-14x28",
346 feature = "size-21x42",
347 feature = "size-28x56"
348 ))]
349 fn assert_scaled_glyph(base: &MockDisplay<BinaryColor>, font: &MonoFont<'_>, scale: i32) {
350 let style = MonoTextStyle::new(font, BinaryColor::On);
351 let mut scaled = MockDisplay::new();
352 Text::with_baseline("A", Point::zero(), style, Baseline::Top)
353 .draw(&mut scaled)
354 .unwrap();
355
356 for y in 0..14 {
357 for x in 0..7 {
358 let expected = base.get_pixel(Point::new(x, y));
359 for scaled_y in y * scale..(y + 1) * scale {
360 for scaled_x in x * scale..(x + 1) * scale {
361 assert_eq!(scaled.get_pixel(Point::new(scaled_x, scaled_y)), expected);
362 }
363 }
364 }
365 }
366 }
367}