8000 extmod/modframebuf: Add text size support. · micropython/micropython@468ebd4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 468ebd4

Browse files
committed
extmod/modframebuf: Add text size support.
Add a `size` parameter to the `text()` method. This does a fairly simple scaling of the built-in 8 pixel font for the moment, but could use different fonts in the future.
1 parent 91674c4 commit 468ebd4

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

extmod/modframebuf.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,10 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
794794
if (n_args >= 5) {
795795
col = mp_obj_get_int(args_in[4]);
796796
}
797+
mp_int_t size = 8;
798+
if (n_args >= 6) {
799+
size = mp_obj_get_int(args_in[5]);
800+
}
797801

798802
// loop over chars
799803
for (; *str; ++str) {
@@ -805,13 +809,16 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
805809
// get char data
806810
const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
807811
// loop over char data
808-
for (int j = 0; j < 8; j++, x0++) {
812+
for (int j = 0; j < size; j++, x0++) {
809813
if (0 <= x0 && x0 < self->width) { // clip x
810-
uint vline_data = chr_data[j]; // each byte is a column of 8 pixels, LSB at top
811-
for (int y = y0; vline_data; vline_data >>= 1, y++) { // scan over vertical column
812-
if (vline_data & 1) { // only draw if pixel set
814+
uint vline_data = chr_data[j * 8 / size]; // each byte is a column of 8 pixels, LSB at top
815+
if (vline_data) { // skip empty columns
816+
for (int i = 0, y = y0; i < size; i++, y++) {
813817
if (0 <= y && y < self->height) { // clip y
814-
setpixel(self, x0, y, col);
818+
// scan over vertical column
819+
if (vline_data & (1 << (i * 8 / size))) { // only draw if pixel set
820+
setpixel(self, x0, y, col);
821+
}
815822
}
816823
}
817824
}
@@ -820,7 +827,7 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
820827
}
821828
return mp_const_none;
822829
}
823-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 5, framebuf_text);
830+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 6, framebuf_text);
824831

825832
#if !MICROPY_ENABLE_DYNRUNTIME
826833
STATIC const mp_rom_map_elem_t framebuf_locals_dict_table[] = {

tests/extmod/framebuf1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@
9494
fbuf.text("hello", 0, 0, 0) # clear
9595
print(buf)
9696

97+
# scaled text
98+
fbuf.text("*", 0, 0, 1, 16)
99+
print(buf)
100+
fbuf.text("*", 0, 0, 0, 16) # clear
101+
print(buf)
102+
97103
# char out of font range set to chr(127)
98104
fbuf.text(str(chr(31)), 0, 0)
99105
print(buf)

tests/extmod/framebuf1.py.exp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ bytearray(b'\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
1818
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00')
1919
bytearray(b'\x00\x7f\x7f\x04\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
2020
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
21+
bytearray(b'\xc0\xc0\xcc\xcc\xfc\x00\x00\x0c\x0c\x0f\x00\x00\x00\x00\x00\x00')
22+
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
2123
bytearray(b'\xaaU\xaaU\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
2224

2325
MONO_HLSB
@@ -40,6 +42,8 @@ bytearray(b'\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00')
4042
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00')
4143
bytearray(b'``x````\x00\x00\x00\x00\x00\x00\x00\x00\x00')
4244
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
45+
bytearray(b'\x00\x0088\x08\x08\xf8\xf8\x08\x0888\x00\x00\x00\x00')
46+
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
4347
bytearray(b'P\xa8P\xa8P\xa8P\xa8\x00\x00\x00\x00\x00\x00\x00\x00')
4448

4549
MONO_HMSB
@@ -62,6 +66,8 @@ bytearray(b'\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00')
6266
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00')
6367
bytearray(b'\x06\x06\x1e\x06\x06\x06\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00')
6468
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
69+
bytearray(b'\x00\x00\x1c\x1c\x10\x10\x1f\x1f\x10\x10\x1c\x1c\x00\x00\x00\x00')
70+
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
6571
bytearray(b'\n\x15\n\x15\n\x15\n\x15\x00\x00\x00\x00\x00\x00\x00\x00')
6672

6773
ValueError

0 commit comments

Comments
 (0)
0