From: Melissa LeBlanc-Williams Date: Wed, 27 Sep 2023 16:35:33 +0000 (-0700) Subject: Slight optimization and uses CP font as default X-Git-Tag: 1.0.0^2~13 X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka_Displayio.git/commitdiff_plain/4ce6ccfe5fdee154abaa19bcdbdc1560b8cf43a0 Slight optimization and uses CP font as default --- diff --git a/displayio/_bitmap.py b/displayio/_bitmap.py index 324475f..07eb7af 100644 --- a/displayio/_bitmap.py +++ b/displayio/_bitmap.py @@ -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) diff --git a/displayio/_display.py b/displayio/_display.py index d06ccbe..d7d04cf 100644 --- a/displayio/_display.py +++ b/displayio/_display.py @@ -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) diff --git a/displayio/_shape.py b/displayio/_shape.py index 81d15a0..8545cf1 100644 --- a/displayio/_shape.py +++ b/displayio/_shape.py @@ -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 index 0000000..c0b450f --- /dev/null +++ b/displayio/resources/__init__.py @@ -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 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 index 0000000..9a506cd --- /dev/null +++ b/displayio/resources/ter-u12n.pbm.license @@ -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 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 index 0000000..9a506cd --- /dev/null +++ b/displayio/resources/ter-u12n.pil.license @@ -0,0 +1,3 @@ +SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams, written for Adafruit Industries + +SPDX-License-Identifier: MIT diff --git a/fontio.py b/fontio.py index a9200c9..2eddfa4 100644 --- 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):