ibm437/
lib.rs

1#![cfg_attr(not(test), no_std)]
2
3extern crate embedded_graphics;
4
5use embedded_graphics::{
6    geometry::Size,
7    image::ImageRaw,
8    mono_font::{DecorationDimensions, MonoFont},
9};
10
11mod char_offset;
12use char_offset::{char_offset_impl, CHARS_PER_ROW};
13
14/// The 8x8 regular
15///
16/// [![8x8 regular font spritemap screenshot](https://raw.githubusercontent.com/sbechet/ibm437/master/doc/ibm437_font_8_8_regular.png)](https://raw.githubusercontent.com/sbechet/ibm437/master/doc/ibm437_font_8_8_regular.png)
17///
18#[cfg(feature = "regular8x8")]
19pub const IBM437_8X8_REGULAR: MonoFont = MonoFont {
20    image: ImageRaw::new(
21        include_bytes!(concat!(
22            core::env!("OUT_DIR"),
23            "/ibm437_font_8_8_regular.raw"
24        )),
25        CHARS_PER_ROW as u32 * 8,
26    ),
27    glyph_mapping: &char_offset_impl,
28    character_size: Size::new(8, 8),
29    character_spacing: 0,
30    baseline: 6,
31    underline: DecorationDimensions::new(8, 1),
32    strikethrough: DecorationDimensions::default_strikethrough(8),
33};
34
35/// The 8x8 bold
36///
37/// [![8x8 bold font spritemap screenshot](https://raw.githubusercontent.com/sbechet/ibm437/master/doc/ibm437_font_8_8_bold.png)](https://raw.githubusercontent.com/sbechet/ibm437/master/doc/ibm437_font_8_8_bold.png)
38///
39#[cfg(feature = "bold8x8")]
40pub const IBM437_8X8_BOLD: MonoFont = MonoFont {
41    image: ImageRaw::new(
42        include_bytes!(concat!(core::env!("OUT_DIR"), "/ibm437_font_8_8_bold.raw")),
43        CHARS_PER_ROW as u32 * 8,
44    ),
45    glyph_mapping: &char_offset_impl,
46    character_size: Size::new(8, 8),
47    character_spacing: 0,
48    baseline: 6,
49    underline: DecorationDimensions::new(8, 1),
50    strikethrough: DecorationDimensions::default_strikethrough(8),
51};
52
53/// The 9x14 regular
54///
55/// [![9x14 regular font spritemap screenshot](https://raw.githubusercontent.com/sbechet/ibm437/master/doc/ibm437_font_9_14_regular.png)](https://raw.githubusercontent.com/sbechet/ibm437/master/doc/ibm437_font_9_14_regular.png)
56///
57#[cfg(feature = "regular9x14")]
58pub const IBM437_9X14_REGULAR: MonoFont = MonoFont {
59    image: ImageRaw::new(
60        include_bytes!(concat!(
61            core::env!("OUT_DIR"),
62            "/ibm437_font_9_14_regular.raw"
63        )),
64        CHARS_PER_ROW as u32 * 9,
65    ),
66    glyph_mapping: &char_offset_impl,
67    character_size: Size::new(9, 14),
68    character_spacing: 0,
69    baseline: 10,
70    underline: DecorationDimensions::new(14, 1),
71    strikethrough: DecorationDimensions::default_strikethrough(14),
72};
73
74#[cfg(test)]
75mod test {
76    use super::*;
77    use embedded_graphics::{
78        mock_display::MockDisplay,
79        mono_font::{MonoTextStyle, MonoTextStyleBuilder},
80        pixelcolor::BinaryColor,
81        prelude::*,
82        text::Text,
83    };
84
85    #[test]
86    fn test_a() {
87        let mut display = MockDisplay::new();
88
89        assert_eq!(char_offset_impl('a'), 'a' as usize);
90
91        let style = MonoTextStyle::new(&IBM437_8X8_REGULAR, BinaryColor::On);
92
93        Text::new("a", Point::new(0, 6), style)
94            .draw(&mut display)
95            .unwrap();
96
97        assert_eq!(
98            display,
99            MockDisplay::from_pattern(&[
100                "            ",
101                "            ",
102                "  ####      ",
103                "      #     ",
104                "  #####     ",
105                " #    #     ",
106                "  ######    ",
107                "            ",
108            ])
109        );
110    }
111
112    #[test]
113    fn test_nbsp() {
114        let mut display = MockDisplay::new();
115
116        assert_eq!(char_offset_impl('\u{A0}'), '\u{A0}' as usize);
117
118        let style = MonoTextStyleBuilder::new()
119            .font(&IBM437_8X8_REGULAR)
120            .text_color(BinaryColor::On)
121            .background_color(BinaryColor::Off)
122            .build();
123
124        Text::new("\u{A0}", Point::new(0, 6), style)
125            .draw(&mut display)
126            .unwrap();
127
128        assert_eq!(
129            display,
130            MockDisplay::from_pattern(&[
131                "........     ",
132                "........     ",
133                "........     ",
134                "........     ",
135                "........     ",
136                "........     ",
137                "........     ",
138                "........     ",
139            ])
140        );
141    }
142
143    #[test]
144    fn test_space() {
145        let mut display = MockDisplay::new();
146
147        assert_eq!(char_offset_impl(' '), ' ' as usize);
148
149        let style = MonoTextStyleBuilder::new()
150            .font(&IBM437_8X8_REGULAR)
151            .text_color(BinaryColor::On)
152            .background_color(BinaryColor::Off)
153            .build();
154
155        Text::new(" ", Point::new(0, 6), style)
156            .draw(&mut display)
157            .unwrap();
158
159        assert_eq!(
160            display,
161            MockDisplay::from_pattern(&[
162                "........     ",
163                "........     ",
164                "........     ",
165                "........     ",
166                "........     ",
167                "........     ",
168                "........     ",
169                "........     ",
170            ])
171        );
172    }
173}