Skip to content
Snippets Groups Projects

Add multiple font support to `print`

Open meh requested to merge meh/firmware:gohu into master
4 unresolved threads

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

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
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
  • Please register or sign in to reply
  • rahix
    rahix @rahix started a thread on the diff
  • 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
  • rahix
    rahix @rahix started a thread on the diff
  • 45 45 lib/mx25lba/
    46 46 lib/sdk/
    47 47 lib/vendor/
    48 lib/gfx/
    • Please remove this change. Code in the lib/gfx/ folder should be formatted just as well. If you want to exclude the font file, please add a /* clang-format off */ at the very top.

    • Please register or sign in to reply
  • rahix
    rahix @rahix started a thread on the diff
  • 84 case 20:
    85 pick = &Font20;
    86 break;
    87
    88 case 24:
    89 pick = &Font24;
    90 break;
    91
    92 case 99:
    93 pick = &GohuFont;
    94 break;
    95 }
    96
    97 gfx_puts(pick, &display_screen, posx, posy, pString, fg, bg);
    98
    99 return 0;
  • added 6 - Changes Requested label and removed 6 - Needs Review label

  • dx mentioned in merge request !340 (closed)

    mentioned in merge request !340 (closed)

  • Maintainer

    Took the rust script into !340 (closed), closing this one

  • dx closed

    closed

  • @dx, the rust script was not the main point of this MR. It was the addition of the Gohufont which I would still like to see.

  • rahix reopened

    reopened

  • Please register or sign in to reply
    Loading