X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka_Displayio.git/blobdiff_plain/e74f23cb01a08dcc664aa6f36527e6d987322dfb..c91e2161b6c7cbb1f96e68a476ce2d9e0f93c03f:/displayio/tilegrid.py diff --git a/displayio/tilegrid.py b/displayio/tilegrid.py index 774dd77..2c815a4 100644 --- a/displayio/tilegrid.py +++ b/displayio/tilegrid.py @@ -99,10 +99,14 @@ class TileGrid: self._flip_y = False self._top_left_x = 0 self._top_left_y = 0 - if tile_width is None: + if tile_width is None or tile_width == 0: tile_width = bitmap_width - if tile_height is None: + if tile_height is None or tile_width == 0: tile_height = bitmap_height + if tile_width < 1: + tile_width = 1 + if tile_height < 1: + tile_height = 1 if bitmap_width % tile_width != 0: raise ValueError("Tile width must exactly divide bitmap width") self._tile_width = tile_width @@ -211,12 +215,15 @@ class TileGrid: ) image.putalpha(alpha.convert("L")) - # pylint: disable=too-many-locals + # pylint: disable=too-many-locals,too-many-branches def _fill_area(self, buffer): """Draw onto the image""" if self._hidden: return + if self._bitmap.width <= 0 or self._bitmap.height <= 0: + return + image = Image.new( "RGBA", (self._width * self._tile_width, self._height * self._tile_height), @@ -269,9 +276,27 @@ class TileGrid: y *= self._absolute_transform.dy x += self._absolute_transform.x y += self._absolute_transform.y - buffer.alpha_composite(image, (x, y)) - # pylint: enable=too-many-locals + source_x = source_y = 0 + if x < 0: + source_x = round(0 - x) + x = 0 + if y < 0: + source_y = round(0 - y) + y = 0 + + x = round(x) + y = round(y) + + if ( + x <= buffer.width + and y <= buffer.height + and source_x <= image.width + and source_y <= image.height + ): + buffer.alpha_composite(image, (x, y), source=(source_x, source_y)) + + # pylint: enable=too-many-locals,too-many-branches @property def hidden(self):