]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blobdiff - displayio/tilegrid.py
Fix link in Readme
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / tilegrid.py
index d5b344680ed6bece88ac30079d566938e3337b18..2c815a4f9280e92558ffab6d70e4373327ab9076 100644 (file)
@@ -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
@@ -198,30 +202,28 @@ class TileGrid:
         return pixel_value
 
     def _apply_palette(self, image):
-        if isinstance(self._pixel_shader, Palette):
-            image.putpalette(
-                self._pixel_shader._get_palette()  # pylint: disable=protected-access
-            )
-        if isinstance(self._pixel_shader, ColorConverter):
-            # This will be needed for eInks, grayscale, and monochrome displays
-            pass
+        image.putpalette(
+            self._pixel_shader._get_palette()  # pylint: disable=protected-access
+        )
 
     def _add_alpha(self, image):
-        if isinstance(self._pixel_shader, Palette):
-            alpha = self._bitmap._image.copy().convert(  # pylint: disable=protected-access
-                "P"
-            )
-            alpha.putpalette(
-                self._pixel_shader._get_alpha_palette()  # pylint: disable=protected-access
-            )
-            image.putalpha(alpha.convert("L"))
+        alpha = self._bitmap._image.copy().convert(  # pylint: disable=protected-access
+            "P"
+        )
+        alpha.putpalette(
+            self._pixel_shader._get_alpha_palette()  # pylint: disable=protected-access
+        )
+        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),
@@ -237,12 +239,15 @@ class TileGrid:
                 tile_index = self._tiles[tile_y * self._width + tile_x]
                 tile_index_x = tile_index % tile_count_x
                 tile_index_y = tile_index // tile_count_x
-                tile_image = self._bitmap._image.copy().convert(  # pylint: disable=protected-access
-                    "P"
-                )
-                self._apply_palette(tile_image)
-                tile_image = tile_image.convert("RGBA")
-                self._add_alpha(tile_image)
+                tile_image = self._bitmap._image  # pylint: disable=protected-access
+                if isinstance(self._pixel_shader, Palette):
+                    tile_image = tile_image.copy().convert("P")
+                    self._apply_palette(tile_image)
+                    tile_image = tile_image.convert("RGBA")
+                    self._add_alpha(tile_image)
+                elif isinstance(self._pixel_shader, ColorConverter):
+                    # This will be needed for eInks, grayscale, and monochrome displays
+                    pass
                 image.alpha_composite(
                     tile_image,
                     dest=(tile_x * self._tile_width, tile_y * self._tile_height),
@@ -271,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):