diff --git a/adafruit_display_text/bitmap_label.py b/adafruit_display_text/bitmap_label.py index 07ab0dc..e50dd8c 100755 --- a/adafruit_display_text/bitmap_label.py +++ b/adafruit_display_text/bitmap_label.py @@ -260,9 +260,8 @@ def _reset_text( self._padding_top + y_offset, ) - label_position_yoffset = int( # To calibrate with label.py positioning - (self._font.get_glyph(ord("M")).height) / 2 - ) + # To calibrate with label.py positioning + label_position_yoffset = self._get_ascent() // 2 self.tilegrid = displayio.TileGrid( self.bitmap, @@ -303,31 +302,37 @@ def _reset_text( ) # set the anchored_position with setter after bitmap is created, sets the # x,y positions of the label - @staticmethod - def _line_spacing_ypixels(font, line_spacing): - # Note: Scaling is provided at the Group level - return_value = int(line_spacing * font.get_bounding_box()[1]) - return return_value + def _get_ascent_descent(self): + if hasattr(self.font, "ascent"): + return self.font.ascent, self.font.descent - def _text_bounding_box(self, text, font, line_spacing): - - # This empirical approach checks several glyphs for maximum ascender and descender height - # (consistent with label.py) + # check a few glyphs for maximum ascender and descender height glyphs = "M j'" # choose glyphs with highest ascender and lowest - # descender, will depend upon font used - try: - font.load_glyphs(text + glyphs) + self._font.load_glyphs(glyphs) except AttributeError: - # ignore if font does not have load_glyphs + # Builtin font doesn't have or need load_glyphs pass - + # descender, will depend upon font used ascender_max = descender_max = 0 for char in glyphs: - this_glyph = font.get_glyph(ord(char)) + 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] + + @staticmethod + def _line_spacing_ypixels(font, line_spacing): + # Note: Scaling is provided at the Group level + return_value = int(line_spacing * font.get_bounding_box()[1]) + return return_value + + def _text_bounding_box(self, text, font, line_spacing): + ascender_max, descender_max = self._get_ascent_descent() lines = 1 @@ -339,7 +344,7 @@ def _text_bounding_box(self, text, font, line_spacing): right = x_start top = bottom = y_start - y_offset_tight = int((font.get_glyph(ord("M")).height) / 2) + y_offset_tight = self._get_ascent() // 2 newline = False