Skip to content

Commit 9ef676e

Browse files
committed
Fix scale bug in label.py, remove kwargs from both and add scale input parameter
1 parent df1c8ea commit 9ef676e

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

adafruit_display_text/bitmap_label.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,19 @@ def __init__(
111111
anchor_point=None,
112112
anchored_position=None,
113113
save_text=True, # can reduce memory use if save_text = False
114-
**kwargs
114+
scale=1,
115115
):
116116

117-
# Scale will be passed to Group using kwargs.
118-
if "scale" in kwargs.keys():
119-
scale = kwargs["scale"]
120-
kwargs.pop(
121-
"scale"
122-
) # Do not change scale of self Group, use this value to set scale of local_group
123-
else:
124-
scale = 1 # Set default scale=1
125-
126117
# instance the Group
127118
# self Group will contain a single local_group which contains one TileGrid which contains
128119
# the text bitmap
129120
super().__init__(
130-
max_size=1, x=x, y=y, **kwargs
121+
max_size=1, x=x, y=y,
131122
) # this will include any arguments, including scale
132123

133124
self.local_group = displayio.Group(
134-
max_size=1, **kwargs
135-
) # local_group holds the tileGrid and
136-
# sets the scaling
125+
max_size=1, scale=scale
126+
) # local_group holds the tileGrid and sets the scaling
137127
self.append(
138128
self.local_group
139129
) # the local_group will always stay in the self Group
@@ -165,7 +155,6 @@ def __init__(
165155
anchored_position=anchored_position,
166156
save_text=save_text,
167157
scale=scale,
168-
**kwargs,
169158
)
170159

171160
def _reset_text(
@@ -184,7 +173,6 @@ def _reset_text(
184173
anchored_position=None,
185174
save_text=None,
186175
scale=None,
187-
**kwargs
188176
):
189177

190178
# Store all the instance variables

adafruit_display_text/label.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,16 @@ def __init__(
7979
padding_right=0,
8080
anchor_point=None,
8181
anchored_position=None,
82-
**kwargs
82+
scale=1,
8383
):
84-
if "scale" in kwargs.keys():
85-
self._scale = kwargs["scale"]
86-
else:
87-
self._scale = 1
8884
if not max_glyphs and not text:
8985
raise RuntimeError("Please provide a max size, or initial text")
9086
if not max_glyphs:
9187
max_glyphs = len(text)
9288
# add one to max_size for the background bitmap tileGrid
93-
super().__init__(max_size=max_glyphs + 1, **kwargs)
89+
super().__init__(max_size=1)
90+
self.local_group=displayio.Group(max_size=max_glyphs + 1, scale=scale)
91+
self.append(self.local_group)
9492

9593
self.width = max_glyphs
9694
self._font = font
@@ -121,12 +119,15 @@ def __init__(
121119
self._padding_bottom = padding_bottom
122120
self._padding_left = padding_left
123121
self._padding_right = padding_right
122+
123+
self._scale=scale
124124

125125
if text is not None:
126126
self._update_text(str(text))
127127
if (anchored_position is not None) and (anchor_point is not None):
128128
self.anchored_position = anchored_position
129129

130+
130131
def _create_background_box(self, lines, y_offset):
131132

132133
left = self._boundingbox[0]
@@ -172,14 +173,16 @@ def _create_background_box(self, lines, y_offset):
172173
y=y_box_offset,
173174
)
174175

176+
177+
175178
return tile_grid
176179

177180
def _update_background_color(self, new_color):
178181

179182
if new_color is None:
180183
self._background_palette.make_transparent(0)
181184
if self._added_background_tilegrid:
182-
self.pop(0)
185+
self.local_group.pop(0)
183186
self._added_background_tilegrid = False
184187
else:
185188
self._background_palette.make_opaque(0)
@@ -200,10 +203,10 @@ def _update_background_color(self, new_color):
200203
self._boundingbox[3] + self._padding_top + self._padding_bottom > 0
201204
)
202205
):
203-
if len(self) > 0:
204-
self.insert(0, self._create_background_box(lines, y_offset))
206+
if len(self.local_group) > 0: # This can be simplified in CP v6.0, when group.append(0) bug is corrected
207+
self.local_group.insert(0, self._create_background_box(lines, y_offset))
205208
else:
206-
self.append(self._create_background_box(lines, y_offset))
209+
self.local_group.append(self._create_background_box(lines, y_offset))
207210
self._added_background_tilegrid = True
208211

209212
else: # a bitmap is present in the self Group
@@ -217,9 +220,9 @@ def _update_background_color(self, new_color):
217220
self._boundingbox[3] + self._padding_top + self._padding_bottom > 0
218221
)
219222
):
220-
self[0] = self._create_background_box(lines, y_offset)
223+
self.local_group[0] = self._create_background_box(lines, y_offset)
221224
else: # delete the existing bitmap
222-
self.pop(0)
225+
self.local_group.pop(0)
223226
self._added_background_tilegrid = False
224227

225228
def _update_text(
@@ -284,10 +287,10 @@ def _update_text(
284287
x=position_x,
285288
y=position_y,
286289
)
287-
if tilegrid_count < len(self):
288-
self[tilegrid_count] = face
290+
if tilegrid_count < len(self.local_group):
291+
self.local_group[tilegrid_count] = face
289292
else:
290-
self.append(face)
293+
self.local_group.append(face)
291294
tilegrid_count += 1
292295
x += glyph.shift_x
293296
i += 1
@@ -296,8 +299,8 @@ def _update_text(
296299
if left is None:
297300
left = 0
298301

299-
while len(self) > tilegrid_count: # i:
300-
self.pop()
302+
while len(self.local_group) > tilegrid_count: # i:
303+
self.local_group.pop()
301304
self._text = new_text
302305
self._boundingbox = (left, top, right - left, bottom - top)
303306

@@ -319,6 +322,7 @@ def line_spacing(self):
319322
@line_spacing.setter
320323
def line_spacing(self, spacing):
321324
self._line_spacing = spacing
325+
self.text=self._text # redraw the box
322326

323327
@property
324328
def color(self):
@@ -358,6 +362,19 @@ def text(self, new_text):
358362
except RuntimeError as run_error:
359363
raise RuntimeError("Text length exceeds max_glyphs") from run_error
360364

365+
@property
366+
def scale(self):
367+
return self._scale
368+
369+
@scale.setter
370+
def scale(self, new_scale):
371+
current_anchored_position=self.anchored_position
372+
self._scale=new_scale
373+
self.local_group.scale=new_scale
374+
self.anchored_position=current_anchored_position
375+
376+
377+
361378
@property
362379
def font(self):
363380
"""Font to use for text display."""

0 commit comments

Comments
 (0)