Skip to content

Commit 3e8f35f

Browse files
committed
Added getter/setters for text, line_spacing and temporary fix for set_scale, some performance speedups by deleting duplication in bounding box calculations
1 parent a404b09 commit 3e8f35f

File tree

1 file changed

+52
-74
lines changed

1 file changed

+52
-74
lines changed

adafruit_display_text/bitmap_label.py

Lines changed: 52 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ def __init__(
137137
x=x,
138138
y=y,
139139
text=text,
140-
# color=color,
141-
# background_color=background_color,
142140
line_spacing=line_spacing,
143141
background_tight=background_tight,
144142
padding_top=padding_top,
@@ -170,21 +168,13 @@ def _reset_text(
170168
**kwargs
171169
):
172170

173-
# store the instance variables
174-
175-
print('_reset_text Text string to print: {}'.format(text))
176-
177171
# Store all the instance variables
178172
if font is not None:
179173
self._font = font
180174
if x is not None:
181175
self.x = x
182176
if y is not None:
183177
self.y = y
184-
# if color is not None:
185-
# self.color = color
186-
# if background_color is not None:
187-
# self.background_color = background_color
188178
if line_spacing is not None:
189179
self._line_spacing = line_spacing
190180
if background_tight is not None:
@@ -236,27 +226,27 @@ def _reset_text(
236226

237227
# Calculate the text bounding box
238228

239-
# Calculate tight box to provide bounding box dimensions to match label for
229+
# Calculate both "tight" and "loose" bounding box dimensions to match label for
240230
# anchor_position calculations
241231
(
242-
tight_box_x,
232+
box_x,
243233
tight_box_y,
244-
tight_x_offset,
234+
x_offset,
245235
tight_y_offset,
236+
loose_box_y,
237+
loose_y_offset
246238
) = self._text_bounding_box(
247-
text, self._font, self._line_spacing, background_tight=True,
248-
) # calculate the box size for a tight background
239+
text, self._font, self._line_spacing,
240+
) # calculate the box size for a tight and loose backgrounds
249241

250242
if self._background_tight:
251-
box_x = tight_box_x
252243
box_y = tight_box_y
253244
y_offset = tight_y_offset
254-
x_offset = tight_x_offset
255245

256246
else: # calculate the box size for a loose background
257-
(box_x, box_y, x_offset, y_offset) = self._text_bounding_box(
258-
text, self._font, self._line_spacing, background_tight=self._background_tight,
259-
)
247+
box_y = loose_box_y
248+
y_offset = loose_y_offset
249+
260250
# Calculate the background size including padding
261251
box_x = box_x + self._padding_left + self._padding_right
262252
box_y = box_y + self._padding_top + self._padding_bottom
@@ -300,7 +290,7 @@ def _reset_text(
300290
self._bounding_box = (
301291
self.tilegrid.x,
302292
self.tilegrid.y,
303-
tight_box_x,
293+
box_x,
304294
tight_box_y,
305295
)
306296

@@ -310,11 +300,11 @@ def _reset_text(
310300

311301
@staticmethod
312302
def _line_spacing_ypixels(font, line_spacing):
313-
# Note: Scale is not implemented at this time, any scaling is pushed up to the Group level
303+
# Note: Scaling is provided at the Group level
314304
return_value = int(line_spacing * font.get_bounding_box()[1])
315305
return return_value
316306

317-
def _text_bounding_box(self, text, font, line_spacing, background_tight=False):
307+
def _text_bounding_box(self, text, font, line_spacing):
318308

319309
# This empirical approach checks several glyphs for maximum ascender and descender height
320310
# (consistent with label.py)
@@ -345,8 +335,6 @@ def _text_bounding_box(self, text, font, line_spacing, background_tight=False):
345335
top = bottom = y_start
346336

347337
y_offset_tight = int((font.get_glyph(ord("M")).height) / 2)
348-
# this needs to be reviewed (also in label.py), since it doesn't respond
349-
# properly to the number of newlines.
350338

351339
newline = False
352340

@@ -387,17 +375,19 @@ def _text_bounding_box(self, text, font, line_spacing, background_tight=False):
387375
left = 0
388376

389377
final_box_width = right - left
390-
if background_tight:
391-
final_box_height = bottom - top
392-
final_y_offset = -top + y_offset_tight
393378

394-
else:
395-
final_box_height = (lines - 1) * self._line_spacing_ypixels(
379+
380+
final_box_height_tight = bottom - top
381+
final_y_offset_tight = -top + y_offset_tight
382+
383+
final_box_height_loose = (lines - 1) * self._line_spacing_ypixels(
396384
font, line_spacing
397-
) + (ascender_max + descender_max)
398-
final_y_offset = ascender_max
385+
) + (ascender_max + descender_max)
386+
final_y_offset_loose = ascender_max
387+
388+
# return (final_box_width, final_box_height, left, final_y_offset)
399389

400-
return (final_box_width, final_box_height, left, final_y_offset)
390+
return (final_box_width, final_box_height_tight, left, final_y_offset_tight, final_box_height_loose, final_y_offset_loose)
401391

402392
# pylint: disable=too-many-nested-blocks
403393
def _place_text(
@@ -410,17 +400,10 @@ def _place_text(
410400
yposition,
411401
text_palette_index=1,
412402
background_palette_index=0,
413-
print_only_pixels=True, # print_only_pixels = True: only update the bitmap where the glyph
414-
# pixel color is > 0. This is especially useful for script fonts where glyph
415-
# bounding boxes overlap
416-
# Set `print_only_pixels=False` to write all pixels
417403
):
418404
# placeText - Writes text into a bitmap at the specified location.
419405
#
420-
# Verify paletteIndex is working properly with * operator, especially
421-
# if accommodating multicolored fonts
422-
#
423-
# Note: Scale is not implemented at this time, is pushed up to Group level
406+
# Note: scale is pushed up to Group level
424407

425408
bitmap_width = bitmap.width
426409
bitmap_height = bitmap.height
@@ -465,38 +448,18 @@ def _place_text(
465448
glyph_offset_x = (
466449
my_glyph.tile_index * my_glyph.width
467450
) # for type BuiltinFont, this creates the x-offset in the glyph bitmap.
468-
# for BDF loaded fonts, this should equal 0
469-
470-
for y in range(my_glyph.height):
471-
for x in range(my_glyph.width):
472-
x_placement = x + xposition + my_glyph.dx
473-
y_placement = y + yposition - my_glyph.height - my_glyph.dy
474-
475-
if (bitmap_width > x_placement >= 0) and (
476-
bitmap_height > y_placement >= 0
477-
):
478-
479-
# Allows for remapping the bitmap indexes using paletteIndex
480-
# for background and text.
481-
palette_indexes = (
482-
background_palette_index,
483-
text_palette_index,
484-
)
485-
486-
this_pixel_color = palette_indexes[
487-
my_glyph.bitmap[
488-
y * my_glyph.bitmap.width + x + glyph_offset_x
489-
]
490-
]
491-
492-
if not print_only_pixels or this_pixel_color > 0:
493-
# write all characters if printOnlyPixels = False,
494-
# or if thisPixelColor is > 0
495-
bitmap[
496-
y_placement * bitmap_width + x_placement
497-
] = this_pixel_color
498-
elif y_placement > bitmap_height:
499-
break
451+
# for BDF loaded fonts, this should equal 0
452+
453+
bitmap.blit(
454+
xposition + my_glyph.dx,
455+
yposition - my_glyph.height - my_glyph.dy,
456+
my_glyph.bitmap,
457+
x1=glyph_offset_x,
458+
y1=0,
459+
x2=glyph_offset_x + my_glyph.width - 1,
460+
y2=0 + my_glyph.height - 1,
461+
skip_index=0, # do not copy over any 0 background pixels
462+
)
500463

501464
xposition = xposition + my_glyph.shift_x
502465

@@ -508,6 +471,22 @@ def bounding_box(self):
508471
first two numbers are offset from the x, y origin of this group"""
509472
return self._bounding_box
510473

474+
# @property
475+
# def scale(self):
476+
# return self._scale
477+
478+
# @scale.setter
479+
# def scale(self, new_scale):
480+
# self._scale=new_scale
481+
# #super(displayio.Group, self).scale.fset(self, new_scale)
482+
# anchored_position=self._anchored_position # update the anchored_position
483+
484+
def set_scale(self, new_scale):
485+
"""Set the scaling of the label"""
486+
self._scale=int(round(new_scale))
487+
self.scale=self._scale
488+
self.anchored_position=self._anchored_position
489+
511490
@property
512491
def line_spacing(self):
513492
"""The amount of space between lines of text, in multiples of the font's
@@ -557,7 +536,6 @@ def text(self):
557536
@text.setter # Cannot set color or background color with text setter, use separate setter
558537
def text(self, new_text):
559538
self._reset_text(text=new_text)
560-
print('updated text: {}'.format(new_text))
561539

562540
@property
563541
def font(self):

0 commit comments

Comments
 (0)