Skip to content

extmod/modframebuf: FrameBuffer text scaling #6263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/library/framebuf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,15 @@ The following methods draw shapes onto the FrameBuffer.
Drawing text
------------

.. method:: FrameBuffer.text(s, x, y[, c])
.. method:: FrameBuffer.text(s, x, y[, c[, size]])

Write text to the FrameBuffer using the the coordinates as the upper-left
corner of the text. The color of the text can be defined by the optional
argument but is otherwise a default value of 1. All characters have
``c`` argument but is otherwise a default value of 1. Characters have base
dimensions of 8x8 pixels and there is currently no way to change the font.
The optional ``size`` argument (which defaults to ``8``) allows a font size
to be specified. The standard 8x8 font will be crudely scaled, so integer
multiples of 8 (e.g., ``16``) will work best.


Other methods
Expand Down
19 changes: 13 additions & 6 deletions extmod/modframebuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,10 @@ static mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
if (n_args >= 5) {
col = mp_obj_get_int(args_in[4]);
}
mp_int_t size = 8;
if (n_args >= 6) {
size = mp_obj_get_int(args_in[5]);
}

// loop over chars
for (; *str; ++str) {
Expand All @@ -823,13 +827,16 @@ static mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
// get char data
const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
// loop over char data
for (int j = 0; j < 8; j++, x0++) {
for (int j = 0; j < size; j++, x0++) {
if (0 <= x0 && x0 < self->width) { // clip x
uint vline_data = chr_data[j]; // each byte is a column of 8 pixels, LSB at top
for (int y = y0; vline_data; vline_data >>= 1, y++) { // scan over vertical column
if (vline_data & 1) { // only draw if pixel set
uint vline_data = chr_data[j * 8 / size]; // each byte is a column of 8 pixels, LSB at top
if (vline_data) { // skip empty columns
for (int i = 0, y = y0; i < size; i++, y++) {
if (0 <= y && y < self->height) { // clip y
setpixel(self, x0, y, col);
// scan over vertical column
if (vline_data & (1 << (i * 8 / size))) { // only draw if pixel set
setpixel(self, x0, y, col);
}
}
}
}
Expand All @@ -838,7 +845,7 @@ static mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
}
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 5, framebuf_text);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 6, framebuf_text);

#if !MICROPY_ENABLE_DYNRUNTIME
static const mp_rom_map_elem_t framebuf_locals_dict_table[] = {
Expand Down
6 changes: 6 additions & 0 deletions tests/extmod/framebuf1.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@
fbuf.text("hello", 0, 0, 0) # clear
print(buf)

# scaled text
fbuf.text("*", 0, 0, 1, 16)
print(buf)
fbuf.text("*", 0, 0, 0, 16) # clear
print(buf)

# char out of font range set to chr(127)
fbuf.text(str(chr(31)), 0, 0)
print(buf)
Expand Down
6 changes: 6 additions & 0 deletions tests/extmod/framebuf1.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ bytearray(b'\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x7f\x7f\x04\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\xc0\xc0\xcc\xcc\xfc\x00\x00\x0c\x0c\x0f\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\xaaU\xaaU\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

MONO_HLSB
Expand All @@ -40,6 +42,8 @@ bytearray(b'\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'``x````\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x0088\x08\x08\xf8\xf8\x08\x0888\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'P\xa8P\xa8P\xa8P\xa8\x00\x00\x00\x00\x00\x00\x00\x00')

MONO_HMSB
Expand All @@ -62,6 +66,8 @@ bytearray(b'\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x06\x06\x1e\x06\x06\x06\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x1c\x1c\x10\x10\x1f\x1f\x10\x10\x1c\x1c\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\n\x15\n\x15\n\x15\n\x15\x00\x00\x00\x00\x00\x00\x00\x00')

ValueError
Expand Down
Loading