@@ -260,9 +260,8 @@ def _reset_text(
260
260
self ._padding_top + y_offset ,
261
261
)
262
262
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
266
265
267
266
self .tilegrid = displayio .TileGrid (
268
267
self .bitmap ,
@@ -303,31 +302,38 @@ def _reset_text(
303
302
) # set the anchored_position with setter after bitmap is created, sets the
304
303
# x,y positions of the label
305
304
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
311
308
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
316
310
glyphs = "M j'" # choose glyphs with highest ascender and lowest
317
- # descender, will depend upon font used
318
-
319
311
try :
320
- font . load_glyphs (text + glyphs )
312
+ self . _font . load_glyphs (glyphs )
321
313
except AttributeError :
322
- # ignore if font does not have load_glyphs
314
+ # Builtin font doesn't have or need load_glyphs
323
315
pass
324
-
316
+ # descender, will depend upon font used
325
317
ascender_max = descender_max = 0
326
318
for char in glyphs :
327
- this_glyph = font .get_glyph (ord (char ))
319
+ this_glyph = self . _font .get_glyph (ord (char ))
328
320
if this_glyph :
329
321
ascender_max = max (ascender_max , this_glyph .height + this_glyph .dy )
330
322
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
+
329
+ @staticmethod
330
+ def _line_spacing_ypixels (font , line_spacing ):
331
+ # Note: Scaling is provided at the Group level
332
+ return_value = int (line_spacing * font .get_bounding_box ()[1 ])
333
+ return return_value
334
+
335
+ def _text_bounding_box (self , text , font , line_spacing ):
336
+ ascender_max , descender_max = self ._get_ascent_descent ()
331
337
332
338
lines = 1
333
339
@@ -339,7 +345,7 @@ def _text_bounding_box(self, text, font, line_spacing):
339
345
right = x_start
340
346
top = bottom = y_start
341
347
342
- y_offset_tight = int (( font . get_glyph ( ord ( "M" )). height ) / 2 )
348
+ y_offset_tight = self . _get_ascent () // 2
343
349
344
350
newline = False
345
351
0 commit comments