Skip to content

Commit eaeed21

Browse files
committed
label: Use new, optional 'ascent', 'descent' properties
Each time new glyphs have to be loaded from a font, it can take a long time (hundreds of milliseconds). In a parallel commit, 'ascent' and 'descent' properties will be added to BDF font objects, are essentially free to compute, and are in any case much quicker than entering load_glyphs. This may change the layout of text slightly. For instance, the height of the "M" glyph in GothamBlack-50.bdf is 35, but the Ascent of the font is 40 (and some characters, such as Å, are taller than the ascent at 45 pixels high)
1 parent 9bc4d2b commit eaeed21

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

adafruit_display_text/label.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,17 @@ def _create_background_box(self, lines, y_offset):
147147
y_box_offset = self._boundingbox[1]
148148

149149
else: # draw a "loose" bounding box to include any ascenders/descenders.
150-
151-
# check a few glyphs for maximum ascender and descender height
152-
# Enhancement: it would be preferred to access the font "FONT_ASCENT" and
153-
# "FONT_DESCENT" in the imported BDF file
154-
glyphs = "M j'" # choose glyphs with highest ascender and lowest
155-
# descender, will depend upon font used
156-
ascender_max = descender_max = 0
157-
for char in glyphs:
158-
this_glyph = self._font.get_glyph(ord(char))
159-
if this_glyph:
160-
ascender_max = max(ascender_max, this_glyph.height + this_glyph.dy)
161-
descender_max = max(descender_max, -this_glyph.dy)
150+
ascent, descent = self._get_ascent_descent()
162151

163152
box_width = self._boundingbox[2] + self._padding_left + self._padding_right
164153
x_box_offset = -self._padding_left
165154
box_height = (
166-
(ascender_max + descender_max)
155+
(ascent + descent)
167156
+ int((lines - 1) * self.height * self._line_spacing)
168157
+ self._padding_top
169158
+ self._padding_bottom
170159
)
171-
y_box_offset = -ascender_max + y_offset - self._padding_top
160+
y_box_offset = -ascent + y_offset - self._padding_top
172161

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

184173
return tile_grid
185174

175+
def _get_ascent_descent(self):
176+
if hasattr(self.font, "ascent"):
177+
return self.font.ascent, self.font.descent
178+
179+
# check a few glyphs for maximum ascender and descender height
180+
glyphs = "M j'" # choose glyphs with highest ascender and lowest
181+
self._font.load_glyphs(glyphs)
182+
# descender, will depend upon font used
183+
ascender_max = descender_max = 0
184+
for char in glyphs:
185+
this_glyph = self._font.get_glyph(ord(char))
186+
if this_glyph:
187+
ascender_max = max(ascender_max, this_glyph.height + this_glyph.dy)
188+
descender_max = max(descender_max, -this_glyph.dy)
189+
return ascender_max, descender_max
190+
191+
def _get_ascent(self):
192+
return self._get_ascent_descent()[0]
193+
186194
def _update_background_color(self, new_color):
187195

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

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

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

251-
try:
252-
self._font.load_glyphs(new_text + "M")
253-
except AttributeError:
254-
# ignore if font does not have load_glyphs
255-
pass
256-
257-
y_offset = int((self._font.get_glyph(ord("M")).height) / 2)
259+
y_offset = self._get_ascent() // 2
258260

259261
right = top = bottom = 0
260262
left = None

0 commit comments

Comments
 (0)