forked from embedded-graphics/bdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont_viewer.rs
More file actions
183 lines (152 loc) · 5.16 KB
/
Copy pathfont_viewer.rs
File metadata and controls
183 lines (152 loc) · 5.16 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
use anyhow::{anyhow, Context, Result};
use eg_bdf::BdfTextStyle;
use eg_font_converter::{FontConverter, Mapping};
use embedded_graphics::{
geometry::AnchorPoint,
mono_font::{ascii::FONT_6X10, MonoTextStyle},
pixelcolor::Rgb888,
prelude::*,
primitives::{Line, PrimitiveStyle},
text::{renderer::TextRenderer, Alignment, Baseline, Text, TextStyleBuilder},
};
use embedded_graphics_simulator::{
sdl2::Keycode, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
fn main() {
if let Err(e) = try_main() {
eprintln!("Error: {e:#}");
}
}
/// Draws a text and its bounding box.
fn draw_text<S: TextRenderer<Color = Rgb888>>(
display: &mut SimulatorDisplay<Rgb888>,
text: &Text<S>,
) -> Point {
text.bounding_box()
.into_styled(PrimitiveStyle::with_stroke(Rgb888::CSS_DARK_ORANGE, 1))
.draw(display)
.unwrap();
text.draw(display).unwrap()
}
fn draw<S: TextRenderer<Color = Rgb888> + Copy>(
display: &mut SimulatorDisplay<Rgb888>,
style: S,
line_height: u32,
) {
let abc = ('a'..='z').collect::<String>();
let digits = ('0'..='9').collect::<String>();
let string = format!(
"The quick brown fox jumps over the lazy dog\n{}\n{}\n{}",
abc,
abc.to_ascii_uppercase(),
digits
);
let position = Point::new(5, 5 + line_height as i32);
Line::with_delta(position.y_axis(), Point::zero() + display.size().x_axis())
.into_styled(PrimitiveStyle::with_stroke(Rgb888::CSS_DIM_GRAY, 1))
.draw(display)
.unwrap();
let text = Text::new(&string, position, style);
draw_text(display, &text);
let position = display.bounding_box().anchor_point(AnchorPoint::BottomLeft)
+ Point::new(5, -(line_height as i32) * 3 / 2);
Line::with_delta(position.y_axis(), Point::zero() + display.size().x_axis())
.into_styled(PrimitiveStyle::with_stroke(Rgb888::CSS_DIM_GRAY, 1))
.draw(display)
.unwrap();
let p = draw_text(
display,
&Text::with_baseline("Top ", position, style, Baseline::Top),
);
let p = draw_text(
display,
&Text::with_baseline(
"Middle ",
Point::new(p.x, position.y),
style,
Baseline::Middle,
),
);
let p = draw_text(
display,
&Text::with_baseline(
"Bottom ",
Point::new(p.x, position.y),
style,
Baseline::Bottom,
),
);
draw_text(
display,
&Text::with_baseline(
"Alphabetic",
Point::new(p.x, position.y),
style,
Baseline::Alphabetic,
),
);
}
fn try_main() -> Result<()> {
let file = std::env::args()
.nth(1)
.ok_or_else(|| anyhow!("missing filename"))?;
let converter = FontConverter::with_file(&file, "BDF_FILE")
.glyphs(Mapping::Ascii)
.missing_glyph_substitute('?');
let bdf_output = converter
.convert_eg_bdf()
.with_context(|| "couldn't convert font")?;
let bdf_font = bdf_output.as_font();
let mono_output = converter
.convert_mono_font()
.with_context(|| "couldn't convert font")?;
let mono_font = mono_output.as_font();
let hints_style = MonoTextStyle::new(&FONT_6X10, Rgb888::CSS_DIM_GRAY);
let bottom_right = TextStyleBuilder::new()
.baseline(Baseline::Bottom)
.alignment(Alignment::Right)
.build();
let line_height = bdf_font.ascent + bdf_font.descent;
let display_height = line_height * 8;
let display_width = (line_height * 25).max(display_height);
let display_size = Size::new(display_width, display_height);
let mut display = SimulatorDisplay::<Rgb888>::new(display_size);
let scale = (1200 / display_size.width).max(1);
let settings = OutputSettingsBuilder::new().scale(scale).build();
let mut window = Window::new("Font viewer", &settings);
let mut use_mono_font = false;
'main_loop: loop {
window.update(&display);
for event in window.events() {
match event {
SimulatorEvent::KeyDown { keycode, .. } => match keycode {
Keycode::M => {
use_mono_font = !use_mono_font;
}
_ => {}
},
SimulatorEvent::Quit => break 'main_loop,
_ => {}
}
}
let mut hint = "Press M to toggle".to_string();
display.clear(Rgb888::BLACK).unwrap();
if use_mono_font {
let style = MonoTextStyle::new(&mono_font, Rgb888::WHITE);
draw(&mut display, style, line_height);
hint.insert_str(0, "Mono | ");
} else {
let style = BdfTextStyle::new(&bdf_font, Rgb888::WHITE);
draw(&mut display, style, line_height);
hint.insert_str(0, "Bdf | ");
}
let corner = display
.bounding_box()
.offset(-3)
.anchor_point(AnchorPoint::BottomRight);
Text::with_text_style(&hint, corner, hints_style, bottom_right)
.draw(&mut display)
.unwrap();
}
Ok(())
}