Skip to content

Commit

Permalink
Merge 16213a8 into 27767aa
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhogg committed Jul 21, 2020
2 parents 27767aa + 16213a8 commit c072e34
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
7 changes: 5 additions & 2 deletions docs/library/framebuf.rst
Expand Up @@ -87,12 +87,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[, scale]])

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 ``scale`` argument allows an integer scale to be provided,
allowing the standard font to be drawn at larger sizes, e.g., a scale of
2 will double the characters to 16x16 pixels.


Other methods
Expand Down
16 changes: 12 additions & 4 deletions extmod/modframebuf.c
Expand Up @@ -567,6 +567,10 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
if (n_args >= 5) {
col = mp_obj_get_int(args[4]);
}
mp_int_t scale = 1;
if (n_args >= 6) {
scale = mp_obj_get_int(args[5]);
}

// loop over chars
for (; *str; ++str) {
Expand All @@ -578,22 +582,26 @@ STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
// 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 < 8 * scale; 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
uint vline_data = chr_data[j / scale]; // each byte is a column of 8 pixels, LSB at top
for (int i = 0, y = y0; vline_data; i++, y++) {
// scan over vertical column
if (vline_data & 1) { // only draw if pixel set
if (0 <= y && y < self->height) { // clip y
setpixel(self, x0, y, col);
}
}
if ((i % scale) == (scale - 1)) {
vline_data >>= 1;
}
}
}
}
}
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
Expand Up @@ -94,6 +94,12 @@
fbuf.text("hello", 0, 0, 0) # clear
print(buf)

# scaled text
fbuf.text("hello", 0, 0, 1, 2)
print(buf)
fbuf.text("hello", 0, 0, 0, 2) # 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
Expand Up @@ -18,6 +18,8 @@ bytearray(b'\x00\x00@\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01')
bytearray(b'\x00\x7f\x7f\x04\x04\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x00\x00\xff\xff\xff\x00\x00???')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\xaaU\xaaU\xaa\x00\x00\x00\x00\x00')

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

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

ValueError
Expand Down

0 comments on commit c072e34

Please sign in to comment.