1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
7 ================================================================================
11 **Software and Dependencies:**
14 https://github.com/adafruit/Adafruit_Blinka/releases
16 * Author(s): Melissa LeBlanc-Williams
20 from PIL import ImageFont
21 from displayio import Bitmap
23 __version__ = "0.0.0-auto.0"
24 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
28 """Simulate a font built into CircuitPython"""
31 self._font = ImageFont.load_default()
32 self._generate_bitmap(0x20, 0x7E)
34 def _generate_bitmap(self, start_range, end_range):
35 char_width, char_height = self.get_bounding_box()
36 self._bitmap = Bitmap(
37 char_width * (end_range - start_range + 1), char_height, 2
39 for character in range(start_range, end_range + 1):
40 ascii_char = chr(character)
41 ascii_mask = self._font.getmask(ascii_char, mode="1")
42 for y in range(char_height):
43 for x in range(char_width):
44 color = ascii_mask.getpixel((x, y))
45 character_position = character - start_range
46 self._bitmap[character_position * char_width + x, y] = (
50 def get_bounding_box(self):
51 """Returns the maximum bounds of all glyphs in the font in
52 a tuple of two values: width, height.
54 return self._font.getsize("M")
56 def get_glyph(self, codepoint):
57 """Returns a `fontio.Glyph` for the given codepoint or None if no glyph is available."""
58 if 0x20 <= codepoint <= 0x7E:
59 glyph_index = codepoint - 0x20
63 bounding_box = self._font.getsize(chr(codepoint))
64 width, height = bounding_box
67 tile_index=glyph_index,
78 """Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use
79 `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and
80 `terminalio.Terminal`.
85 # pylint: disable=too-few-public-methods, invalid-name, too-many-arguments
87 """Storage of glyph info"""
89 def __init__(self, bitmap, tile_index, width, height, dx, dy, shift_x, shift_y):
95 self.shift_x = shift_x
96 self.shift_y = shift_y
97 self.tile_index = tile_index
100 # pylint: enable=too-few-public-methods, invalid-name, too-many-arguments