Skip to content

Commit 7147935

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 8957386 commit 7147935

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
@@ -574,6 +574,10 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
574574
if (n_args >= 5) {
575575
col = mp_obj_get_int(args[4]);
576576
}
577+
mp_int_t size = 8;
578+
if (n_args >= 6) {
579+
size = mp_obj_get_int(args[5]);
580+
}
577581

578582
// loop over chars
579583
for (; *str; ++str) {
@@ -585,13 +589,16 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
585589
// get char data
586590
const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
587591
// loop over char data
588-
for (int j = 0; j < 8; j++, x0++) {
592+
for (int j = 0; j < size; j++, x0++) {
589593
if (0 <= x0 && x0 < self->width) { // clip x
590-
uint vline_data = chr_data[j]; // each byte is a column of 8 pixels, LSB at top
591-
for (int y = y0; vline_data; vline_data >>= 1, y++) { // scan over vertical column
592-
if (vline_data & 1) { // only draw if pixel set
594+
uint vline_data = chr_data[j * 8 / size]; // each byte is a column of 8 pixels, LSB at top
595+
if (vline_data) { // skip empty columns
596+
for (int i = 0, y = y0; i < size; i++, y++) {
593597
if (0 <= y && y < self->height) { // clip y
594-
setpixel(self, x0, y, col);
598+
// scan over vertical column
599+
if (vline_data & (1 << (i * 8 / size))) { // only draw if pixel set
600+
setpixel(self, x0, y, col);
601+
}
595602
}
596603
}
597604
}
@@ -600,7 +607,7 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
600607
}
601608
return mp_const_none;
602609
}
603-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 5, framebuf_text);
610+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 6, framebuf_text);
604611

605612
#if !MICROPY_ENABLE_DYNRUNTIME
606613
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')
1818
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01')
1919
bytearray(b'\x00\x7f\x7f\x04\x04\x00\x00\x00\x00\x00')
2020
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
21+
bytearray(b'\xc0\xc0\xcc\xcc\xfc\x00\x00\x0c\x0c\x0f')
22+
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
2123
bytearray(b'\xaaU\xaaU\xaa\x00\x00\x00\x00\x00')
2224

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

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

6773
ValueError

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy