Skip to content

Commit a3d7aed

Browse files
authored
Merge pull request #41 from jepler/use-bitmaptools-readinto
pcf: Use `bitmaptools.readinto` to load font data if available
2 parents 1f602d9 + c2e71ce commit a3d7aed

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

adafruit_bitmap_font/pcf.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
from fontio import Glyph
3131
from .glyph_cache import GlyphCache
3232

33+
try:
34+
from bitmaptools import readinto as _bitmap_readinto
35+
except ImportError:
36+
_bitmap_readinto = None # pylint: disable=invalid-name
37+
3338
_PCF_PROPERTIES = const(1 << 0)
3439
_PCF_ACCELERATORS = const(1 << 1)
3540
_PCF_METRICS = const(1 << 2)
@@ -380,12 +385,22 @@ def load_glyphs(self, code_points):
380385
height = metrics.character_ascent + metrics.character_descent
381386

382387
bitmap = bitmaps[i]
383-
words_per_row = (width + 31) // 32
384-
buf = bytearray(4 * words_per_row)
385-
start = 0
386-
for _ in range(height):
387-
self.file.readinto(buf)
388-
for k in range(width):
389-
if buf[k // 8] & (128 >> (k % 8)):
390-
bitmap[start + k] = 1
391-
start += width
388+
389+
if _bitmap_readinto:
390+
_bitmap_readinto(
391+
bitmap,
392+
self.file,
393+
bits_per_pixel=1,
394+
element_size=4,
395+
reverse_pixels_in_element=True,
396+
)
397+
else:
398+
words_per_row = (width + 31) // 32
399+
buf = bytearray(4 * words_per_row)
400+
start = 0
401+
for _ in range(height):
402+
self.file.readinto(buf)
403+
for k in range(width):
404+
if buf[k // 8] & (128 >> (k % 8)):
405+
bitmap[start + k] = 1
406+
start += width

0 commit comments

Comments
 (0)