Skip to content
This repository was archived by the owner on Mar 23, 2019. It is now read-only.

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jan 6, 2013
1 parent a9fbc3d commit 38d3c2a
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions adafruit/adafruitgfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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; j<h; j+=1)
Expand Down Expand Up @@ -442,21 +454,21 @@ def draw_text(self, x, y, string, size=1, space=1):
return x

# proportional text using fonts module
def draw_text3(self, x, y, string, font):
def draw_text3(self, x, y, string, font, kerning=0):
height = font.char_height
prev_char = None

for c in string:
if (c<font.start_char or c>font.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
Expand All @@ -478,4 +490,32 @@ def draw_text3(self, x, y, string, font):

if prev_char != None:
x += prev_width
return x
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 (c<font.start_char or c>font.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

0 comments on commit 38d3c2a

Please sign in to comment.