]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/commitdiff
Slight optimization and uses CP font as default
authorMelissa LeBlanc-Williams <melissa@adafruit.com>
Wed, 27 Sep 2023 16:35:33 +0000 (09:35 -0700)
committerMelissa LeBlanc-Williams <melissa@adafruit.com>
Wed, 27 Sep 2023 16:35:33 +0000 (09:35 -0700)
displayio/_bitmap.py
displayio/_display.py
displayio/_shape.py
displayio/resources/__init__.py [new file with mode: 0644]
displayio/resources/ter-u12n.pbm [new file with mode: 0644]
displayio/resources/ter-u12n.pbm.license [new file with mode: 0644]
displayio/resources/ter-u12n.pil [new file with mode: 0644]
displayio/resources/ter-u12n.pil.license [new file with mode: 0644]
fontio.py

index 324475f4662dc7fc86c543867e858593aaca7791..07eb7af8f4ddaba8cc31bed289b7051a8da9b030 100644 (file)
@@ -27,13 +27,11 @@ from ._area import Area
 __version__ = "0.0.0+auto.0"
 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
 
-ALIGN_BITS = 8 * struct.calcsize("I")
-
 
 def stride(width: int, bits_per_pixel: int) -> int:
     """Return the number of bytes per row of a bitmap with the given width and bits per pixel."""
     row_width = width * bits_per_pixel
-    return (row_width + (ALIGN_BITS - 1)) // ALIGN_BITS
+    return (row_width + (31)) // 32
 
 
 class Bitmap:
@@ -109,7 +107,7 @@ class Bitmap:
         self._x_shift = 0
 
         power_of_two = 1
-        while power_of_two < ALIGN_BITS // bits_per_value:
+        while power_of_two < 32 // bits_per_value:
             self._x_shift += 1
             power_of_two = power_of_two << 1
 
@@ -142,11 +140,7 @@ class Bitmap:
         if bytes_per_value < 1:
             word = self._data[row_start + (x >> self._x_shift)]
             return (
-                word
-                >> (
-                    struct.calcsize("I") * 8
-                    - ((x & self._x_mask) + 1) * self._bits_per_value
-                )
+                word >> (32 - ((x & self._x_mask) + 1) * self._bits_per_value)
             ) & self._bitmask
         row = memoryview(self._data)[row_start : row_start + self._stride]
         if bytes_per_value == 1:
@@ -190,10 +184,7 @@ class Bitmap:
         row_start = y * self._stride
         bytes_per_value = self._bits_per_value // 8
         if bytes_per_value < 1:
-            bit_position = (
-                struct.calcsize("I") * 8
-                - ((x & self._x_mask) + 1) * self._bits_per_value
-            )
+            bit_position = 32 - ((x & self._x_mask) + 1) * self._bits_per_value
             index = row_start + (x >> self._x_shift)
             word = self._data[index]
             word &= ~(self._bitmask << bit_position)
index d06ccbecdd1456309a1b45b7b3ac9c75c7b8f5af..d7d04cf2389dcf5ba1c3badbb42cd754c6851e1b 100644 (file)
@@ -18,7 +18,6 @@ displayio for Blinka
 """
 
 import time
-import struct
 from array import array
 from typing import Optional
 import digitalio
@@ -338,7 +337,7 @@ class Display:
             return True
 
         rows_per_buffer = clipped.height()
-        pixels_per_word = (struct.calcsize("I") * 8) // self._core.colorspace.depth
+        pixels_per_word = 32 // self._core.colorspace.depth
         pixels_per_buffer = clipped.size()
 
         subrectangles = 1
@@ -366,8 +365,6 @@ class Display:
             buffer_size = pixels_per_buffer // pixels_per_word
             if pixels_per_buffer % pixels_per_word:
                 buffer_size += 1
-
-        # TODO: Optimize with memoryview
         mask_length = (pixels_per_buffer // 8) + 1  # 1 bit per pixel + 1
         remaining_rows = clipped.height()
 
@@ -391,8 +388,8 @@ class Display:
                     8 // self._core.colorspace.depth
                 )
 
-            buffer = bytearray([0] * (buffer_size * struct.calcsize("I")))
-            mask = bytearray([0] * mask_length)
+            buffer = memoryview(bytearray([0] * (buffer_size * 4)))
+            mask = memoryview(bytearray([0] * mask_length))
             self._core.fill_area(subrectangle, mask, buffer)
             self._core.begin_transaction()
             self._send_pixels(buffer[:subrectangle_size_bytes])
@@ -405,13 +402,13 @@ class Display:
             raise ValueError("Display must have a 16 bit colorspace.")
 
         area = Area(0, y, self._core.width, y + 1)
-        pixels_per_word = (struct.calcsize("I") * 8) // self._core.colorspace.depth
+        pixels_per_word = 32 // self._core.colorspace.depth
         buffer_size = self._core.width // pixels_per_word
         pixels_per_buffer = area.size()
         if pixels_per_buffer % pixels_per_word:
             buffer_size += 1
 
-        buffer = bytearray([0] * (buffer_size * struct.calcsize("I")))
+        buffer = bytearray([0] * (buffer_size * 4))
         mask_length = (pixels_per_buffer // 32) + 1
         mask = array("L", [0x00000000] * mask_length)
         self._core.fill_area(area, mask, buffer)
index 81d15a0663cb2002d4f8344670de9f308b022ac4..8545cf14b4847cbc35edf1aef6f6a174daa378cd 100644 (file)
@@ -18,7 +18,6 @@ displayio for Blinka
 
 """
 
-import struct
 from ._bitmap import Bitmap
 from ._area import Area
 from ._helpers import clamp
@@ -52,7 +51,7 @@ class Shape(Bitmap):
             height //= 2
             height += self._height % 2
         self._half_height = height
-        self._data = bytearray(height * struct.calcsize("HH"))
+        self._data = bytearray(height * 4)
         for i in range(height):
             self._data[2 * i] = 0
             self._data[2 * i + 1] = width
diff --git a/displayio/resources/__init__.py b/displayio/resources/__init__.py
new file mode 100644 (file)
index 0000000..c0b450f
--- /dev/null
@@ -0,0 +1,3 @@
+# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
\ No newline at end of file
diff --git a/displayio/resources/ter-u12n.pbm b/displayio/resources/ter-u12n.pbm
new file mode 100644 (file)
index 0000000..9805237
Binary files /dev/null and b/displayio/resources/ter-u12n.pbm differ
diff --git a/displayio/resources/ter-u12n.pbm.license b/displayio/resources/ter-u12n.pbm.license
new file mode 100644 (file)
index 0000000..9a506cd
--- /dev/null
@@ -0,0 +1,3 @@
+SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams, written for Adafruit Industries
+
+SPDX-License-Identifier: MIT
diff --git a/displayio/resources/ter-u12n.pil b/displayio/resources/ter-u12n.pil
new file mode 100644 (file)
index 0000000..b13bdb0
Binary files /dev/null and b/displayio/resources/ter-u12n.pil differ
diff --git a/displayio/resources/ter-u12n.pil.license b/displayio/resources/ter-u12n.pil.license
new file mode 100644 (file)
index 0000000..9a506cd
--- /dev/null
@@ -0,0 +1,3 @@
+SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams, written for Adafruit Industries
+
+SPDX-License-Identifier: MIT
index a9200c9051e8775671c3983e9edd5eb4fa3a8f80..2eddfa47b21826ece12944951c773cf94ae17d02 100644 (file)
--- a/fontio.py
+++ b/fontio.py
@@ -17,6 +17,7 @@ fontio for Blinka
 
 """
 
+import os
 from typing import Union, Tuple, Optional
 from PIL import ImageFont
 from displayio import Bitmap
@@ -29,6 +30,8 @@ except ImportError:
 __version__ = "0.0.0+auto.0"
 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
 
+DEFAULT_FONT = "displayio/resources/ter-u12n.pil"
+
 
 class FontProtocol(Protocol):
     """A protocol shared by `BuiltinFont` and classes in ``adafruit_bitmap_font``"""
@@ -52,7 +55,7 @@ class BuiltinFont:
     """Simulate a font built into CircuitPython"""
 
     def __init__(self):
-        self._font = ImageFont.load_default()
+        self._font = ImageFont.load(os.path.dirname(__file__) + "/" + DEFAULT_FONT)
         self._generate_bitmap(0x20, 0x7E)
 
     def _generate_bitmap(self, start_range, end_range):