diff --git a/adafruit_framebuf.py b/adafruit_framebuf.py old mode 100644 new mode 100755 index 6a736bd..55f1c22 --- a/adafruit_framebuf.py +++ b/adafruit_framebuf.py @@ -278,17 +278,26 @@ def scroll(self, delta_x, delta_y): x += dt_x y += dt_y + # pylint: disable=too-many-arguments def text(self, string, x, y, color, *, - font_name="font5x8.bin"): - """text is not yet implemented""" - if not self._font or self._font.font_name != font_name: - # load the font! - self._font = BitmapFont() - w = self._font.font_width - for i, char in enumerate(string): - self._font.draw_char(char, - x + (i * (w + 1)), - y, self, color) + font_name="font5x8.bin", size=1): + """Place text on the screen in variables sizes. Breaks on \n to next line. + + Does not break on line going off screen. + """ + for chunk in string.split('\n'): + if not self._font or self._font.font_name != font_name: + # load the font! + self._font = BitmapFont() + w = self._font.font_width + for i, char in enumerate(chunk): + self._font.draw_char(char, + x + (i * (w + 1))*size, + y, self, color, size=size) + y += self._font.font_height*size + # pylint: enable=too-many-arguments + + def image(self, img): """Set buffer to value of Python Imaging Library image. The image should @@ -352,9 +361,9 @@ def __exit__(self, exception_type, exception_value, traceback): """cleanup on exit""" self.deinit() - def draw_char(self, char, x, y, framebuffer, color): - # pylint: disable=too-many-arguments + def draw_char(self, char, x, y, framebuffer, color, size=1): # pylint: disable=too-many-arguments """Draw one character at position (x,y) to a framebuffer in a given color""" + size = max(size, 1) # Don't draw the character if it will be clipped off the visible area. if x < -self.font_width or x >= framebuffer.width or \ y < -self.font_height or y >= framebuffer.height: @@ -368,7 +377,7 @@ def draw_char(self, char, x, y, framebuffer, color): for char_y in range(self.font_height): # Draw a pixel for each bit that's flipped on. if (line >> char_y) & 0x1: - framebuffer.pixel(x + char_x, y + char_y, color) + framebuffer.fill_rect(x + char_x*size, y + char_y*size, size, size, color) def width(self, text): """Return the pixel width of the specified text message.""" diff --git a/examples/framebuf_simpletest.py b/examples/framebuf_simpletest.py index 203ba08..120a6d2 100644 --- a/examples/framebuf_simpletest.py +++ b/examples/framebuf_simpletest.py @@ -22,6 +22,12 @@ def print_buffer(the_fb): print(".") print("." * (the_fb.width+2)) +# Small function to clear the buffer +def clear_buffer(): + for i, _ in enumerate(buffer): + buffer[i] = 0 + + print("Shapes test: ") fb.pixel(3, 5, True) fb.rect(0, 0, fb.width, fb.height, True) @@ -32,6 +38,12 @@ def print_buffer(the_fb): print("Text test: ") # empty fb.fill_rect(0, 0, WIDTH, HEIGHT, False) + # write some text fb.text("hello", 0, 0, True) print_buffer(fb) +clear_buffer() + +# write some larger text +fb.text("hello", 8, 0, True, size = 2) +print_buffer(fb)