Skip to content

label: Use new, optional 'ascent', 'descent' properties #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,17 @@ def _create_background_box(self, lines, y_offset):
y_box_offset = self._boundingbox[1]

else: # draw a "loose" bounding box to include any ascenders/descenders.

# check a few glyphs for maximum ascender and descender height
# Enhancement: it would be preferred to access the font "FONT_ASCENT" and
# "FONT_DESCENT" in the imported BDF file
glyphs = "M j'" # choose glyphs with highest ascender and lowest
# descender, will depend upon font used
ascender_max = descender_max = 0
for char in glyphs:
this_glyph = self._font.get_glyph(ord(char))
if this_glyph:
ascender_max = max(ascender_max, this_glyph.height + this_glyph.dy)
descender_max = max(descender_max, -this_glyph.dy)
ascent, descent = self._get_ascent_descent()

box_width = self._boundingbox[2] + self._padding_left + self._padding_right
x_box_offset = -self._padding_left
box_height = (
(ascender_max + descender_max)
(ascent + descent)
+ int((lines - 1) * self.height * self._line_spacing)
+ self._padding_top
+ self._padding_bottom
)
y_box_offset = -ascender_max + y_offset - self._padding_top
y_box_offset = -ascent + y_offset - self._padding_top

box_width = max(0, box_width) # remove any negative values
box_height = max(0, box_height) # remove any negative values
Expand All @@ -183,6 +172,25 @@ def _create_background_box(self, lines, y_offset):

return tile_grid

def _get_ascent_descent(self):
if hasattr(self.font, "ascent"):
return self.font.ascent, self.font.descent

# check a few glyphs for maximum ascender and descender height
glyphs = "M j'" # choose glyphs with highest ascender and lowest
self._font.load_glyphs(glyphs)
# descender, will depend upon font used
ascender_max = descender_max = 0
for char in glyphs:
this_glyph = self._font.get_glyph(ord(char))
if this_glyph:
ascender_max = max(ascender_max, this_glyph.height + this_glyph.dy)
descender_max = max(descender_max, -this_glyph.dy)
return ascender_max, descender_max

def _get_ascent(self):
return self._get_ascent_descent()[0]

def _update_background_color(self, new_color):

if new_color is None:
Expand All @@ -196,7 +204,7 @@ def _update_background_color(self, new_color):
self._background_color = new_color

lines = self._text.rstrip("\n").count("\n") + 1
y_offset = int((self._font.get_glyph(ord("M")).height) / 2)
y_offset = self._get_ascent() // 2

if not self._added_background_tilegrid: # no bitmap is in the self Group
# add bitmap if text is present and bitmap sizes > 0 pixels
Expand Down Expand Up @@ -248,13 +256,7 @@ def _update_text(
i = 0
tilegrid_count = i

try:
self._font.load_glyphs(new_text + "M")
except AttributeError:
# ignore if font does not have load_glyphs
pass

y_offset = int((self._font.get_glyph(ord("M")).height) / 2)
y_offset = self._get_ascent() // 2

right = top = bottom = 0
left = None
Expand Down