Skip to content

Commit 2c2a403

Browse files
authored
Merge pull request #110 from FoamyGuy/bitmap_label_ascent_descent
Bitmap label ascent descent
2 parents 8de4f5c + cfa0a4a commit 2c2a403

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

adafruit_display_text/bitmap_label.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,8 @@ def _reset_text(
260260
self._padding_top + y_offset,
261261
)
262262

263-
label_position_yoffset = int( # To calibrate with label.py positioning
264-
(self._font.get_glyph(ord("M")).height) / 2
265-
)
263+
# To calibrate with label.py positioning
264+
label_position_yoffset = self._get_ascent() // 2
266265

267266
self.tilegrid = displayio.TileGrid(
268267
self.bitmap,
@@ -303,31 +302,37 @@ def _reset_text(
303302
) # set the anchored_position with setter after bitmap is created, sets the
304303
# x,y positions of the label
305304

306-
@staticmethod
307-
def _line_spacing_ypixels(font, line_spacing):
308-
# Note: Scaling is provided at the Group level
309-
return_value = int(line_spacing * font.get_bounding_box()[1])
310-
return return_value
305+
def _get_ascent_descent(self):
306+
if hasattr(self.font, "ascent"):
307+
return self.font.ascent, self.font.descent
311308

312-
def _text_bounding_box(self, text, font, line_spacing):
313-
314-
# This empirical approach checks several glyphs for maximum ascender and descender height
315-
# (consistent with label.py)
309+
# check a few glyphs for maximum ascender and descender height
316310
glyphs = "M j'" # choose glyphs with highest ascender and lowest
317-
# descender, will depend upon font used
318-
319311
try:
320-
font.load_glyphs(text + glyphs)
312+
self._font.load_glyphs(glyphs)
321313
except AttributeError:
322-
# ignore if font does not have load_glyphs
314+
# Builtin font doesn't have or need load_glyphs
323315
pass
324-
316+
# descender, will depend upon font used
325317
ascender_max = descender_max = 0
326318
for char in glyphs:
327-
this_glyph = font.get_glyph(ord(char))
319+
this_glyph = self._font.get_glyph(ord(char))
328320
if this_glyph:
329321
ascender_max = max(ascender_max, this_glyph.height + this_glyph.dy)
330322
descender_max = max(descender_max, -this_glyph.dy)
323+
return ascender_max, descender_max
324+
325+
def _get_ascent(self):
326+
return self._get_ascent_descent()[0]
327+
328+
@staticmethod
329+
def _line_spacing_ypixels(font, line_spacing):
330+
# Note: Scaling is provided at the Group level
331+
return_value = int(line_spacing * font.get_bounding_box()[1])
332+
return return_value
333+
334+
def _text_bounding_box(self, text, font, line_spacing):
335+
ascender_max, descender_max = self._get_ascent_descent()
331336

332337
lines = 1
333338

@@ -339,7 +344,7 @@ def _text_bounding_box(self, text, font, line_spacing):
339344
right = x_start
340345
top = bottom = y_start
341346

342-
y_offset_tight = int((font.get_glyph(ord("M")).height) / 2)
347+
y_offset_tight = self._get_ascent() // 2
343348

344349
newline = False
345350

0 commit comments

Comments
 (0)