Skip to content

Commit 062b1bc

Browse files
author
Margaret Matocha
committed
Fixed off-by-one error by int() truncation
1 parent 0b524cf commit 062b1bc

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

adafruit_display_text/label.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,17 @@ def _create_background_box(self, lines, y_offset):
164164
box_width = max(0, box_width) # remove any negative values
165165
box_height = max(0, box_height) # remove any negative values
166166

167-
background_bitmap = displayio.Bitmap(box_width, box_height, 1)
168-
tile_grid = displayio.TileGrid(
169-
background_bitmap,
170-
pixel_shader=self._background_palette,
171-
x=left + x_box_offset,
172-
y=y_box_offset,
173-
)
174-
175-
return tile_grid
167+
if box_width > 0 and box_height > 0:
168+
background_bitmap = displayio.Bitmap(box_width, box_height, 1)
169+
tile_grid = displayio.TileGrid(
170+
background_bitmap,
171+
pixel_shader=self._background_palette,
172+
x=left + x_box_offset,
173+
y=y_box_offset,
174+
)
175+
return tile_grid
176+
else:
177+
return None
176178

177179
def _update_background_color(self, new_color):
178180

@@ -270,6 +272,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
270272
self._boundingbox = (left, top, left + right, bottom - top)
271273
self[0] = self._create_background_box(lines, y_offset)
272274

275+
273276
@property
274277
def bounding_box(self):
275278
"""An (x, y, w, h) tuple that completely covers all glyphs. The
@@ -313,8 +316,11 @@ def text(self):
313316
def text(self, new_text):
314317
try:
315318
current_anchored_position = self.anchored_position
319+
print('start anchored_position: {}'.format(self.anchored_position))
320+
print('self.y: {}, self._scale: {}'.format(self.y, self._scale))
316321
self._update_text(str(new_text))
317322
self.anchored_position = current_anchored_position
323+
print('end anchored_position: {}'.format(self.anchored_position))
318324
except RuntimeError:
319325
raise RuntimeError("Text length exceeds max_glyphs")
320326

@@ -351,22 +357,25 @@ def anchored_position(self):
351357
"""Position relative to the anchor_point. Tuple containing x,y
352358
pixel coordinates."""
353359
return (
354-
int(self.x + self._anchor_point[0] * self._boundingbox[2] * self._scale ),
355-
int(self.y + self._anchor_point[1] * self._boundingbox[3] * self._scale
356-
- (self._boundingbox[3] * self._scale)/2 )
357-
)
360+
int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale) ),
361+
int(self.y + (self._anchor_point[1] * self._boundingbox[3] * self._scale)
362+
- round( (self._boundingbox[3] * self._scale)/2.0 ))
363+
)
358364

359365
@anchored_position.setter
360366
def anchored_position(self, new_position):
361367
new_x = int(
362368
new_position[0]
363369
- self._anchor_point[0] * (self._boundingbox[2] * self._scale)
364370
)
365-
new_y = self.y = int(
371+
new_y = int(
366372
new_position[1]
367-
- self._anchor_point[1] * (self._boundingbox[3] * self._scale)
368-
+ (self._boundingbox[3] * self._scale)/2
373+
- ( self._anchor_point[1] * self._boundingbox[3] * self._scale)
374+
+ round( (self._boundingbox[3] * self._scale)/2.0 )
369375
)
376+
377+
print('new_y: {}, new_position[1]: {}, self._anchor_point[1]: {}, self._boundingbox[3]: {}'.format(new_y, new_position[1], self._anchor_point[1], self._boundingbox[3]))
378+
370379
self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3])
371380
self.x = new_x
372381
self.y = new_y

0 commit comments

Comments
 (0)