Skip to content

Commit 92733f5

Browse files
authored
Merge pull request #102 from jepler/use-ascent-descent
label: Use new, optional 'ascent', 'descent' properties
2 parents 8717242 + bcd7c65 commit 92733f5

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

adafruit_display_text/bitmap_label.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,24 @@ class Label(displayio.Group):
6666
Must include a capital M for measuring character size.
6767
:param str text: Text to display
6868
:param int max_glyphs: Unnecessary parameter (provided only for direct compability
69-
with label.py)
69+
with label.py)
7070
:param int color: Color of all text in RGB hex
7171
:param int background_color: Color of the background, use `None` for transparent
7272
:param double line_spacing: Line spacing of text to display
7373
:param boolean background_tight: Set `True` only if you want background box to tightly
74-
surround text
74+
surround text
7575
:param int padding_top: Additional pixels added to background bounding box at top
7676
:param int padding_bottom: Additional pixels added to background bounding box at bottom
7777
:param int padding_left: Additional pixels added to background bounding box at left
7878
:param int padding_right: Additional pixels added to background bounding box at right
7979
:param (double,double) anchor_point: Point that anchored_position moves relative to.
80-
Tuple with decimal percentage of width and height.
81-
(E.g. (0,0) is top left, (1.0, 0.5): is middle right.)
80+
Tuple with decimal percentage of width and height.
81+
(E.g. (0,0) is top left, (1.0, 0.5): is middle right.)
8282
:param (int,int) anchored_position: Position relative to the anchor_point. Tuple
83-
containing x,y pixel coordinates.
83+
containing x,y pixel coordinates.
8484
:param int scale: Integer value of the pixel scaling
8585
:param bool save_text: Set True to save the text string as a constant in the
86-
label structure. Set False to reduce memory use.
86+
label structure. Set False to reduce memory use.
8787
"""
8888

8989
# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals, too-many-arguments

adafruit_display_text/label.py

+28-22
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,29 @@ 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+
try:
182+
self._font.load_glyphs(glyphs)
183+
except AttributeError:
184+
# Builtin font doesn't have or need load_glyphs
185+
pass
186+
# descender, will depend upon font used
187+
ascender_max = descender_max = 0
188+
for char in glyphs:
189+
this_glyph = self._font.get_glyph(ord(char))
190+
if this_glyph:
191+
ascender_max = max(ascender_max, this_glyph.height + this_glyph.dy)
192+
descender_max = max(descender_max, -this_glyph.dy)
193+
return ascender_max, descender_max
194+
195+
def _get_ascent(self):
196+
return self._get_ascent_descent()[0]
197+
186198
def _update_background_color(self, new_color):
187199

188200
if new_color is None:
@@ -196,7 +208,7 @@ def _update_background_color(self, new_color):
196208
self._background_color = new_color
197209

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

201213
if not self._added_background_tilegrid: # no bitmap is in the self Group
202214
# add bitmap if text is present and bitmap sizes > 0 pixels
@@ -248,13 +260,7 @@ def _update_text(
248260
i = 0
249261
tilegrid_count = i
250262

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)
263+
y_offset = self._get_ascent() // 2
258264

259265
right = top = bottom = 0
260266
left = None

0 commit comments

Comments
 (0)