Skip to content

Commit 7ca736f

Browse files
committed
Add scale mutability, bug fix on blit range
1 parent bece1ad commit 7ca736f

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

adafruit_display_text/bitmap_label.py

100755100644
Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,21 @@ def __init__(
114114
**kwargs
115115
):
116116

117+
118+
# Scale will be passed to Group using kwargs.
119+
if "scale" in kwargs.keys():
120+
scale = kwargs["scale"]
121+
kwargs.pop("scale") # Do not change scale of self Group, use this value to set scale of local_group
122+
117123
# instance the Group
118-
# this Group will contain just one TileGrid with one contained bitmap
124+
# self Group will contain a single local_group which contains one TileGrid which contains the text bitmap
119125
super().__init__(
120126
max_size=1, x=x, y=y, **kwargs
121127
) # this will include any arguments, including scale
122128

129+
self.local_group=displayio.Group(max_size=1, **kwargs) # local_group holds the tileGrid and sets the scaling
130+
self.append(self.local_group) # the local_group will always stay in the self Group
131+
123132
self._font = font
124133

125134
# Create the two-color palette
@@ -130,8 +139,6 @@ def __init__(
130139
self._anchor_point = anchor_point
131140
self._anchored_position = anchored_position
132141

133-
self._scale = 1 # initialize to the default scale of 1
134-
135142
# call the text updater with all the arguments.
136143
self._reset_text(font=font,
137144
x=x,
@@ -146,6 +153,7 @@ def __init__(
146153
anchor_point=anchor_point,
147154
anchored_position=anchored_position,
148155
save_text=save_text,
156+
scale=scale,
149157
**kwargs,
150158
)
151159

@@ -164,7 +172,8 @@ def _reset_text(
164172
padding_right=None,
165173
anchor_point=None,
166174
anchored_position=None,
167-
save_text=None,
175+
save_text=None,
176+
scale=None,
168177
**kwargs
169178
):
170179

@@ -193,6 +202,8 @@ def _reset_text(
193202
self._anchored_position = anchored_position
194203
if save_text is not None:
195204
self._save_text = save_text
205+
if scale is not None: # Scale will be defined in local_group (Note: self should have scale=1)
206+
self.scale=scale # call the setter
196207

197208
# if text is not provided as a parameter (text is None), use the previous value.
198209
if (text is None) and self._save_text:
@@ -203,10 +214,6 @@ def _reset_text(
203214
else:
204215
self._text = None # save a None value since text string is not saved
205216

206-
# Scale will be passed to Group using kwargs.
207-
if "scale" in kwargs.keys():
208-
self._scale = kwargs["scale"]
209-
210217

211218
# Check for empty string
212219
if (text == "") or (text is None): # If empty string, just create a zero-sized bounding box and that's it.
@@ -280,10 +287,10 @@ def _reset_text(
280287
y=label_position_yoffset - y_offset - self._padding_top,
281288
)
282289

283-
# Clear out any items in the self Group, in case this is an update to the bitmap_label
284-
for item in self:
285-
self.pop(0)
286-
self.append(self.tilegrid) # add the bitmap's tilegrid to the group
290+
# Clear out any items in the local_group Group, in case this is an update to the bitmap_label
291+
for item in self.local_group:
292+
self.local_group.pop(0)
293+
self.local_group.append(self.tilegrid) # add the bitmap's tilegrid to the group
287294

288295
# Update bounding_box values. Note: To be consistent with label.py,
289296
# this is the bounding box for the text only, not including the background.
@@ -461,8 +468,8 @@ def _place_text(
461468
my_glyph.bitmap,
462469
x1=glyph_offset_x,
463470
y1=0,
464-
x2=glyph_offset_x + my_glyph.width - 1,
465-
y2=0 + my_glyph.height - 1,
471+
x2=glyph_offset_x + my_glyph.width,
472+
y2=0 + my_glyph.height,
466473
skip_index=0, # do not copy over any 0 background pixels
467474
)
468475

@@ -502,38 +509,22 @@ def _place_text(
502509

503510
return (left, top, right - left, bottom - top) # bounding_box
504511

505-
506-
507-
508-
509-
510512
@property
511513
def bounding_box(self):
512514
"""An (x, y, w, h) tuple that completely covers all glyphs. The
513515
first two numbers are offset from the x, y origin of this group"""
514516
return self._bounding_box
515517

516-
# @property
517-
# def scale(self):
518-
# return self._scale
519-
520-
# @scale.setter
521-
# #@displayio.Group.scale.setter
522-
# def scale(self, new_scale):
523-
# self._scale=new_scale
524-
# #super(displayio.Group, self).scale.fset(self, new_scale)
525-
# self.anchored_position=self._anchored_position # update the anchored_position
526-
# #displayio.Group.scale.__set__(self, new_scale)
527-
# #displayio.Group.scale=new_scale
528-
# #setattr(super(), "scale", new_scale)
529-
# #setattr(self, "scale", new_scale)
530-
# super(displayio.Group, self).scale.__set__(self, new_scale)
531-
532-
def set_scale(self, new_scale):
518+
@property
519+
def scale(self):
533520
"""Set the scaling of the label"""
534-
self._scale=int(round(new_scale))
535-
self.scale=self._scale
536-
self.anchored_position=self._anchored_position
521+
return self._scale
522+
523+
@scale.setter
524+
def scale(self, new_scale):
525+
self.local_group.scale=new_scale
526+
self._scale=new_scale
527+
self.anchored_position=self._anchored_position # update the anchored_position
537528

538529
@property
539530
def line_spacing(self):
@@ -543,7 +534,10 @@ def line_spacing(self):
543534

544535
@line_spacing.setter
545536
def line_spacing(self, new_line_spacing):
546-
self._reset_text(line_spacing=new_line_spacing)
537+
if self._save_text:
538+
self._reset_text(line_spacing=new_line_spacing)
539+
else:
540+
raise RuntimeError("line_spacing is immutable when save_text is False")
547541

548542

549543
@property
@@ -592,7 +586,10 @@ def font(self):
592586

593587
@font.setter
594588
def font(self, new_font):
595-
self._reset_text(font=new_font)
589+
if self._save_text:
590+
self._reset_text(font=new_font)
591+
else:
592+
raise RuntimeError("font is immutable when save_text is False")
596593

597594
@property
598595
def anchor_point(self):
@@ -617,16 +614,15 @@ def anchored_position(self):
617614
@anchored_position.setter
618615
def anchored_position(self, new_position):
619616
self._anchored_position = new_position
620-
621617
# Set anchored_position
622618
if (self._anchor_point is not None) and (self._anchored_position is not None):
623619
self.x = int(
624620
new_position[0]
625-
- (self._bounding_box[0] * self._scale)
626-
- round(self._anchor_point[0] * (self._bounding_box[2] * self._scale))
621+
- (self._bounding_box[0] * self.scale)
622+
- round(self._anchor_point[0] * (self._bounding_box[2] * self.scale))
627623
)
628624
self.y = int(
629625
new_position[1]
630-
- (self._bounding_box[1] * self._scale)
631-
- round(self._anchor_point[1] * self._bounding_box[3] * self._scale)
626+
- (self._bounding_box[1] * self.scale)
627+
- round(self._anchor_point[1] * self._bounding_box[3] * self.scale)
632628
)

0 commit comments

Comments
 (0)