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

Skip to content

Commit 9595afd

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 1548132 commit 9595afd

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
@@ -806,6 +806,10 @@ static mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
806806
if (n_args >= 5) {
807807
col = mp_obj_get_int(args_in[4]);
808808
}
809+
mp_int_t size = 8;
810+
if (n_args >= 6) {
811+
size = mp_obj_get_int(args_in[5]);
812+
}
809813

810814
// loop over chars
811815
for (; *str; ++str) {
@@ -817,13 +821,16 @@ static mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
817821
// get char data
818822
const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
819823
// loop over char data
820-
for (int j = 0; j < 8; j++, x0++) {
824+
for (int j = 0; j < size; j++, x0++) {
821825
if (0 <= x0 && x0 < self->width) { // clip x
822-
uint vline_data = chr_data[j]; // each byte is a column of 8 pixels, LSB at top
823-
for (int y = y0; vline_data; vline_data >>= 1, y++) { // scan over vertical column
824-
if (vline_data & 1) { // only draw if pixel set
826+
uint vline_data = chr_data[j * 8 / size]; // each byte is a column of 8 pixels, LSB at top
827+
if (vline_data) { // skip empty columns
828+
for (int i = 0, y = y0; i < size; i++, y++) {
825829
if (0 <= y && y < self->height) { // clip y
826-
setpixel(self, x0, y, col);
830+
// scan over vertical column
831+
if (vline_data & (1 << (i * 8 / size))) { // only draw if pixel set
832+
setpixel(self, x0, y, col);
833+
}
827834
}
828835
}
829836
}
@@ -832,7 +839,7 @@ static mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
832839
}
833840
return mp_const_none;
834841
}
835-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 5, framebuf_text);
842+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 6, framebuf_text);
836843

837844
#if !MICROPY_ENABLE_DYNRUNTIME
838845
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