Add multiple font support to `print`
This also adds a new font, the API changes are backwards compatible so it should be fine, I will also leave here a font converter from BDF to STM whatever.
use std::{iter, env, error::Error, io};
use itertools::Itertools;
use bdf;
const CHARSET: &[char] = &[' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'];
fn main() -> Result<(), Box<dyn Error>> {
let name = env::args().nth(1).expect("no font name provided");
let font = bdf::read(io::stdin())?;
println!("#include \"fonts.h\"");
println!("const uint8_t {name}_Table[] =", name = name);
println!("{{");
for (&ch, glyph) in CHARSET.iter().map(|ch| (ch, font.glyphs().get(ch).unwrap())) {
println!();
println!("\t// @{codepoint} {ch:?} ({width} pixels wide)",
codepoint = ch as usize,
ch = ch,
width = glyph.width());
for y in 0 .. glyph.height() {
print!("\t");
let mut draw = String::new();
for bits in &iter::repeat(false).take(glyph.bounds().x as usize)
.chain((0 .. glyph.width()).map(|x| glyph.map().get(x, y)))
.chunks(8)
{
let mut bits = bits.collect::<Vec<_>>();
if bits.len() < 8 {
for _ in 0 .. 8 - bits.len() {
bits.push(false);
}
}
let byte = bits.into_iter()
.rev().enumerate()
.fold(0, |acc, (i, b)| acc | ((b as u8) << i));
draw.push_str(&format!("{1:00$b}", glyph.width() as usize, byte));
print!("0x{:02X}, ", byte);
}
println!("//{}", draw.replace("1", "#").replace("0", " "));
}
// Print the right amount of empty lines.
for _ in 0 .. font.bounds().y.abs() + glyph.bounds().y {
for _ in 0 .. (glyph.width() + glyph.bounds().x as u32) / 8 {
print!("0x{:02X}, ", 0);
}
println!();
}
}
println!("}};");
println!();
println!("sFONT {name} = {{ {name}_Table, {width}, {height} }};",
name = name,
width = font.bounds().width,
height = font.bounds().height);
Ok(())
}
Merge request reports
Activity
So this kinda overlaps with !214 (merged) (the part of listing existing fonts) and !259 (closed) (which is a first step towards adding new fonts loaded from the filesystem, but doesn't do that yet)
I talked about this with meh IRL and we agreed that this is the hacky but quick way to add new fonts. It's good for personal usage if anyone wants to merge in new fonts, but otherwise it'd be better to go with the other approaches.
On the other hand, the rust script is definitely interesting and useful!
added 3-Module 6 - Needs Review labels
added 53 commits
-
c3538a07...622d2145 - 52 commits from branch
card10:master
- 2da41e3f - feat: add GohuFont as supported font
-
c3538a07...622d2145 - 52 commits from branch
Sorry, it looks like this Merge Request has some code quality issues!
The pipeline lint has failed - look at its failure output to understand what sort of diffs we'd like you to apply.
You can also use
tools/code-style.sh
to fix files that have issues.Good luck! I will update this comment when I detect you have applied your fixes.
1233 1233 */ 1234 1234 API(API_DISP_PRINT, 1235 1235 int epic_disp_print( 1236 uint16_t posx, 1237 uint16_t posy, 1238 const char *pString, 1239 uint16_t fg, 1240 uint16_t bg) 1241 ); 1236 uint16_t posx, 1237 uint16_t posy, 1238 const char *pString, 1239 uint16_t fg, 1240 uint16_t bg, 1241 uint8_t font)); Changing an existing API call is never allowed, for backwards compatibility. Please either create a new one, or make use of the existing
print_adv
call here.Edited by rahix
43 43 uint32_t posy = mp_obj_get_int(args[2]); 44 44 uint32_t fg = get_color(args[3]); 45 45 uint32_t bg = get_color(args[4]); 46 int res = epic_disp_print(posx, posy, (const char *)print, fg, bg); 46 uint8_t font = mp_obj_get_int(args[5]); 47 int res = 48 epic_disp_print(posx, posy, (const char *)print, fg, bg, font); 47 49 if (res < 0) { 48 50 mp_raise_OSError(-res); 49 51 } 50 52 return mp_const_none; 51 53 } 52 54 static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( 53 display_print_obj, 5, 5, mp_display_print 55 display_print_obj, 6, 6, mp_display_print added 6 - Changes Requested label and removed 6 - Needs Review label
mentioned in merge request !340 (closed)
Took the rust script into !340 (closed), closing this one