Skip to content

Bitmap label ascent descent #110

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 2 commits into from
Jan 30, 2021
Merged
Changes from all commits
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
43 changes: 24 additions & 19 deletions adafruit_display_text/bitmap_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down