Skip to content

Commit 0c25d27

Browse files
authored
Simple font size scaling for framebuf
I have implemented simple font size scaling as `framebuf_text_scaled` usage from python size x 4 `framebuf.text_scaled('Ab', 0, 26, 4)` also posted to official micropython as micropython/micropython#3583
1 parent 5bb7bc2 commit 0c25d27

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

extmod/modframebuf.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,58 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
494494
}
495495
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 5, framebuf_text);
496496

497+
STATIC mp_obj_t framebuf_text_scaled(size_t n_args, const mp_obj_t *args) {
498+
// extract arguments
499+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
500+
const char *str = mp_obj_str_get_str(args[1]);
501+
mp_int_t x0 = mp_obj_get_int(args[2]);
502+
mp_int_t y0 = mp_obj_get_int(args[3]);
503+
504+
mp_int_t scale = 1;
505+
if (n_args >= 5) {
506+
scale = mp_obj_get_int(args[4]);
507+
}
508+
509+
mp_int_t col = 1;
510+
if (n_args >= 6) {
511+
col = mp_obj_get_int(args[5]);
512+
}
513+
514+
// loop over chars
515+
for (; *str; ++str) {
516+
// get char and make sure its in range of font
517+
int chr = *(uint8_t*)str;
518+
if (chr < 32 || chr > 127) {
519+
chr = 127;
520+
}
521+
// get char data
522+
const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
523+
// loop over char data
524+
mp_int_t xs = 0;
525+
mp_int_t ys = 0;
526+
for (int j = 0; j < 8; j++, x0+=scale) {
527+
uint vline_data = chr_data[j]; // each byte is a column of 8 pixels, LSB at top
528+
for (int y = y0; vline_data; vline_data >>= 1, y+=scale) { // scan over vertical column
529+
if (vline_data & 1) { // only draw if pixel set
530+
for (int sx=0; sx < scale; sx++) {
531+
xs=x0 + sx;
532+
if (0 <= xs && xs < self->width) { // clip x
533+
for(int sy=0; sy < scale; sy++) {
534+
ys=y+sy;
535+
if (0 <= ys && ys < self->height) { // clip y
536+
setpixel(self, xs, ys, col);
537+
}
538+
}
539+
}
540+
}
541+
}
542+
}
543+
}
544+
}
545+
return mp_const_none;
546+
}
547+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_scaled_obj, 5, 6, framebuf_text_scaled);
548+
497549
STATIC const mp_rom_map_elem_t framebuf_locals_dict_table[] = {
498550
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&framebuf_fill_obj) },
499551
{ MP_ROM_QSTR(MP_QSTR_fill_rect), MP_ROM_PTR(&framebuf_fill_rect_obj) },
@@ -505,6 +557,7 @@ STATIC const mp_rom_map_elem_t framebuf_locals_dict_table[] = {
505557
{ MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&framebuf_blit_obj) },
506558
{ MP_ROM_QSTR(MP_QSTR_scroll), MP_ROM_PTR(&framebuf_scroll_obj) },
507559
{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&framebuf_text_obj) },
560+
{ MP_ROM_QSTR(MP_QSTR_text_scaled), MP_ROM_PTR(&framebuf_text_scaled_obj) },
508561
};
509562
STATIC MP_DEFINE_CONST_DICT(framebuf_locals_dict, framebuf_locals_dict_table);
510563

0 commit comments

Comments
 (0)