Skip to content

Commit 815354d

Browse files
authored
Merge pull request #130 from kmatch98/master
clip glyphs that exceed ascent property
2 parents 11ee4d0 + 7620091 commit 815354d

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

adafruit_display_text/bitmap_label.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,37 @@ def _place_text(
418418
) # for type BuiltinFont, this creates the x-offset in the glyph bitmap.
419419
# for BDF loaded fonts, this should equal 0
420420

421+
y_blit_target = yposition - my_glyph.height - my_glyph.dy
422+
423+
# Clip glyph y-direction if outside the font ascent/descent metrics.
424+
# Note: bitmap.blit will automatically clip the bottom of the glyph.
425+
y_clip = 0
426+
if (y_blit_target) < 0:
427+
y_clip = -(y_blit_target) # clip this amount from top of bitmap
428+
y_blit_target = 0 # draw the clipped bitmap at y=0
429+
430+
print(
431+
'Warning: Glyph clipped, exceeds Ascent property: "{}"'.format(
432+
char
433+
)
434+
)
435+
436+
if (y_blit_target + my_glyph.height) > bitmap.height:
437+
print(
438+
'Warning: Glyph clipped, exceeds descent property: "{}"'.format(
439+
char
440+
)
441+
)
442+
421443
self._blit(
422444
bitmap,
423445
xposition + my_glyph.dx,
424-
yposition - my_glyph.height - my_glyph.dy,
446+
y_blit_target,
425447
my_glyph.bitmap,
426448
x_1=glyph_offset_x,
427-
y_1=0,
449+
y_1=y_clip,
428450
x_2=glyph_offset_x + my_glyph.width,
429-
y_2=0 + my_glyph.height,
451+
y_2=my_glyph.height,
430452
skip_index=skip_index, # do not copy over any 0 background pixels
431453
)
432454

0 commit comments

Comments
 (0)