From 38d3c2acf4cccb9f7e162b92ad7baf1c706c4936 Mon Sep 17 00:00:00 2001 From: Andreas Goetz Date: Sun, 6 Jan 2013 12:18:51 +0100 Subject: [PATCH] x --- adafruit/adafruitgfx.py | 48 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/adafruit/adafruitgfx.py b/adafruit/adafruitgfx.py index 8b4cfe7..a642114 100644 --- a/adafruit/adafruitgfx.py +++ b/adafruit/adafruitgfx.py @@ -143,6 +143,12 @@ def fill_circle_helper(self, x0, y0, r, cornername, delta, color): def draw_pixel(self, x, y, color): pass + + # to be overwritten + def get_pixel(self, x, y): + pass + + # bresenham's algorithm - thx wikpedia def draw_line(self, x0, y0, x1, y1, color): steep = abs(y1 - y0) > abs(x1 - x0) @@ -313,6 +319,12 @@ def fill_triangle (self, x0, y0, x1, y1, x2, y2, color): y+=1 + + def invert_rect(self, x, y, w, h): + for i in range(x, x+w): + for j in range(y, y+h): + self.draw_pixel(i, j, self.get_pixel(i, j) ^ 1) + # def drawBitmap(self, x, y, const *bitmap, w, h, color): # i, j, byteWidth = (w + 7) / 8 # #for(j=0; jfont.end_char): if prev_char != None: - x += font.space_width + prev_width + font.gap_width + x += font.space_width + prev_width + font.gap_width + kerning prev_char = None else: pos = ord(c) - ord(font.start_char) (width,offset) = font.descriptors[pos] if prev_char != None: - x += font.kerning[prev_char][pos] + font.gap_width + x += font.kerning[prev_char][pos] + font.gap_width + kerning prev_char = pos prev_width = width @@ -478,4 +490,32 @@ def draw_text3(self, x, y, string, font): if prev_char != None: x += prev_width - return x \ No newline at end of file + return x + + def text_width(self, string, font=None, kerning=0): + if font == None: + font_cols = self.font.cols + l = len(string) + return(l * font_cols + (l-1) * (1+kerning)) + else: + x = 0 + prev_char = None + + for c in string: + if (cfont.end_char): + if prev_char != None: + x += font.space_width + prev_width + font.gap_width + kerning + prev_char = None + else: + pos = ord(c) - ord(font.start_char) + (width,offset) = font.descriptors[pos] + + if prev_char != None: + x += font.kerning[prev_char][pos] + font.gap_width + kerning + + prev_char = pos + prev_width = width + + if prev_char != None: + x += prev_width + return x