Skip to content

Commit 48357d4

Browse files
Refactor local_group to a protected variable.
1 parent 3f9a6b6 commit 48357d4

File tree

3 files changed

+49
-73
lines changed

3 files changed

+49
-73
lines changed

adafruit_display_text/__init__.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ def wrap_text_to_pixels(
3737
# pylint: disable=too-many-locals, too-many-branches
3838
if font is None:
3939

40-
def measure(string):
41-
return len(string)
40+
def measure(text):
41+
return len(text)
4242

4343
else:
4444
if hasattr(font, "load_glyphs"):
4545
font.load_glyphs(string)
4646

47-
def measure(string):
48-
return sum(font.get_glyph(ord(c)).shift_x for c in string)
47+
def measure(text):
48+
return sum(font.get_glyph(ord(c)).shift_x for c in text)
4949

5050
lines = []
5151
partial = [indent0]
@@ -237,9 +237,10 @@ def __init__(
237237
self.background_color = background_color
238238

239239
# local group will hold background and text
240-
# the self group scale should always remain at 1, the self.local_group will
240+
# the self group scale should always remain at 1, the self._local_group will
241241
# be used to set the scale of the label
242-
self.local_group = None
242+
self._local_group = Group(scale=scale)
243+
self.append(self._local_group)
243244

244245
self.baseline = -1.0
245246

@@ -250,7 +251,7 @@ def __init__(
250251

251252
def _get_ascent_descent(self) -> Tuple[int, int]:
252253
""" Private function to calculate ascent and descent font values """
253-
if hasattr(self.font, "ascent"):
254+
if hasattr(self.font, "ascent") and hasattr(self.font, "descent"):
254255
return self.font.ascent, self.font.descent
255256

256257
# check a few glyphs for maximum ascender and descender height
@@ -355,11 +356,11 @@ def anchored_position(self, new_position: Tuple[int, int]) -> None:
355356
@property
356357
def scale(self) -> int:
357358
"""Set the scaling of the label, in integer values"""
358-
return self.local_group.scale
359+
return self._local_group.scale
359360

360361
@scale.setter
361362
def scale(self, new_scale: int) -> None:
362-
self.local_group.scale = new_scale
363+
self._local_group.scale = new_scale
363364
self.anchored_position = self._anchored_position # update the anchored_position
364365

365366
def _set_text(self, new_text: str, scale: int) -> None:

adafruit_display_text/bitmap_label.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@ def __init__(self, font, save_text=True, **kwargs) -> None:
8787

8888
super().__init__(font, **kwargs)
8989

90-
self.local_group = displayio.Group(
91-
scale=kwargs.get("scale", 1)
92-
) # local_group holds the tileGrid and sets the scaling
93-
self.append(
94-
self.local_group
95-
) # the local_group will always stay in the self Group
96-
9790
text = kwargs.get("text", "")
9891
self._save_text = save_text
9992
self._text = self._replace_tabs(text)
@@ -145,10 +138,10 @@ def _reset_text(
145138
0, # zero width with text == ""
146139
0, # zero height with text == ""
147140
)
148-
# Clear out any items in the self.local_group Group, in case this is an
141+
# Clear out any items in the self._local_group Group, in case this is an
149142
# update to the bitmap_label
150-
for _ in self.local_group:
151-
self.local_group.pop(0)
143+
for _ in self._local_group:
144+
self._local_group.pop(0)
152145

153146
else: # The text string is not empty, so create the Bitmap and TileGrid and
154147
# append to the self Group
@@ -222,9 +215,9 @@ def _reset_text(
222215

223216
# Clear out any items in the local_group Group, in case this is an update to
224217
# the bitmap_label
225-
for _ in self.local_group:
226-
self.local_group.pop(0)
227-
self.local_group.append(
218+
for _ in self._local_group:
219+
self._local_group.pop(0)
220+
self._local_group.append(
228221
self.tilegrid
229222
) # add the bitmap's tilegrid to the group
230223

@@ -345,8 +338,6 @@ def _place_text(
345338
font,
346339
xposition: int,
347340
yposition: int,
348-
text_palette_index: int = 1,
349-
background_palette_index: int = 0,
350341
skip_index: int = 0, # set to None to write all pixels, other wise skip this palette index
351342
# when copying glyph bitmaps (this is important for slanted text
352343
# where rectangular glyph boxes overlap)
@@ -403,8 +394,8 @@ def _place_text(
403394
# Clip glyph y-direction if outside the font ascent/descent metrics.
404395
# Note: bitmap.blit will automatically clip the bottom of the glyph.
405396
y_clip = 0
406-
if (y_blit_target) < 0:
407-
y_clip = -(y_blit_target) # clip this amount from top of bitmap
397+
if y_blit_target < 0:
398+
y_clip = -y_blit_target # clip this amount from top of bitmap
408399
y_blit_target = 0 # draw the clipped bitmap at y=0
409400

410401
print(
@@ -434,7 +425,8 @@ def _place_text(
434425

435426
xposition = xposition + my_glyph.shift_x
436427

437-
return (left, top, right - left, bottom - top) # bounding_box
428+
# bounding_box
429+
return left, top, right - left, bottom - top
438430

439431
def _blit(
440432
self,

adafruit_display_text/label.py

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
except ImportError:
3232
pass
3333

34-
import displayio
34+
from displayio import Bitmap, Palette, TileGrid
3535

3636
from adafruit_display_text import LabelBase
3737

@@ -81,17 +81,13 @@ class Label(LabelBase):
8181
# This has a lot of getters/setters, maybe it needs cleanup.
8282

8383
def __init__(self, font, **kwargs) -> None:
84-
self._background_palette = displayio.Palette(1)
84+
self._background_palette = Palette(1)
8585
self._added_background_tilegrid = False
8686

8787
super().__init__(font, **kwargs)
8888

8989
text = self._replace_tabs(self._text)
9090

91-
# local_group will set the scale
92-
self.local_group = displayio.Group(scale=kwargs.get("scale", 1))
93-
self.append(self.local_group)
94-
9591
self.width = len(text)
9692
self._font = font
9793
self._text = None
@@ -106,7 +102,7 @@ def __init__(self, font, **kwargs) -> None:
106102
if text is not None:
107103
self._reset_text(str(text))
108104

109-
def _create_background_box(self, lines: int, y_offset: int) -> None:
105+
def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:
110106
"""Private Class function to create a background_box
111107
:param lines: int number of lines
112108
:param y_offset: int y pixel bottom coordinate for the background_box"""
@@ -165,8 +161,8 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
165161
movx = left + x_box_offset
166162
movy = y_box_offset
167163

168-
background_bitmap = displayio.Bitmap(box_width, box_height, 1)
169-
tile_grid = displayio.TileGrid(
164+
background_bitmap = Bitmap(box_width, box_height, 1)
165+
tile_grid = TileGrid(
170166
background_bitmap,
171167
pixel_shader=self._background_palette,
172168
x=movx,
@@ -182,7 +178,7 @@ def _set_background_color(self, new_color: int) -> None:
182178
if new_color is None:
183179
self._background_palette.make_transparent(0)
184180
if self._added_background_tilegrid:
185-
self.local_group.pop(0)
181+
self._local_group.pop(0)
186182
self._added_background_tilegrid = False
187183
else:
188184
self._background_palette.make_opaque(0)
@@ -207,15 +203,9 @@ def _set_background_color(self, new_color: int) -> None:
207203
self._bounding_box[3] + self._padding_top + self._padding_bottom > 0
208204
)
209205
):
210-
# This can be simplified in CP v6.0, when group.append(0) bug is corrected
211-
if len(self.local_group) > 0:
212-
self.local_group.insert(
213-
0, self._create_background_box(lines, y_offset)
214-
)
215-
else:
216-
self.local_group.append(
217-
self._create_background_box(lines, y_offset)
218-
)
206+
self._local_group.insert(
207+
0, self._create_background_box(lines, y_offset)
208+
)
219209
self._added_background_tilegrid = True
220210

221211
else: # a bitmap is present in the self Group
@@ -229,9 +219,11 @@ def _set_background_color(self, new_color: int) -> None:
229219
self._bounding_box[3] + self._padding_top + self._padding_bottom > 0
230220
)
231221
):
232-
self.local_group[0] = self._create_background_box(lines, self._y_offset)
222+
self._local_group[0] = self._create_background_box(
223+
lines, self._y_offset
224+
)
233225
else: # delete the existing bitmap
234-
self.local_group.pop(0)
226+
self._local_group.pop(0)
235227
self._added_background_tilegrid = False
236228

237229
# pylint: disable = too-many-branches, too-many-statements
@@ -269,6 +261,8 @@ def _update_text(
269261
if not glyph:
270262
continue
271263

264+
position_x, position_y = 0, 0
265+
272266
if self._label_direction in ("LTR", "RTL"):
273267
bottom = max(bottom, y - glyph.dy + self._y_offset)
274268
if y == 0: # first line, find the Ascender height
@@ -337,26 +331,15 @@ def _update_text(
337331
position_x = x + glyph.dy - self._y_offset
338332

339333
if glyph.width > 0 and glyph.height > 0:
340-
try:
341-
# pylint: disable=unexpected-keyword-arg
342-
face = displayio.TileGrid(
343-
glyph.bitmap,
344-
pixel_shader=self._palette,
345-
default_tile=glyph.tile_index,
346-
tile_width=glyph.width,
347-
tile_height=glyph.height,
348-
position=(position_x, position_y),
349-
)
350-
except TypeError:
351-
face = displayio.TileGrid(
352-
glyph.bitmap,
353-
pixel_shader=self._palette,
354-
default_tile=glyph.tile_index,
355-
tile_width=glyph.width,
356-
tile_height=glyph.height,
357-
x=position_x,
358-
y=position_y,
359-
)
334+
face = TileGrid(
335+
glyph.bitmap,
336+
pixel_shader=self._palette,
337+
default_tile=glyph.tile_index,
338+
tile_width=glyph.width,
339+
tile_height=glyph.height,
340+
x=position_x,
341+
y=position_y,
342+
)
360343

361344
if self._label_direction == "UPR":
362345
face.transpose_xy = True
@@ -365,10 +348,10 @@ def _update_text(
365348
face.transpose_xy = True
366349
face.flip_y = True
367350

368-
if tilegrid_count < len(self.local_group):
369-
self.local_group[tilegrid_count] = face
351+
if tilegrid_count < len(self._local_group):
352+
self._local_group[tilegrid_count] = face
370353
else:
371-
self.local_group.append(face)
354+
self._local_group.append(face)
372355
tilegrid_count += 1
373356

374357
if self._label_direction == "RTL":
@@ -394,8 +377,8 @@ def _update_text(
394377
if self._label_direction == "TTB" and top is None:
395378
top = 0
396379

397-
while len(self.local_group) > tilegrid_count: # i:
398-
self.local_group.pop()
380+
while len(self._local_group) > tilegrid_count: # i:
381+
self._local_group.pop()
399382
# pylint: disable=invalid-unary-operand-type
400383
if self._label_direction == "RTL":
401384
self._bounding_box = (-left, top, left - right, bottom - top)

0 commit comments

Comments
 (0)