]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - fontio.py
Update to pyproject.toml
[hackapet/Adafruit_Blinka_Displayio.git] / fontio.py
1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 `fontio`
7 ================================================================================
8
9 fontio for Blinka
10
11 **Software and Dependencies:**
12
13 * Adafruit Blinka:
14   https://github.com/adafruit/Adafruit_Blinka/releases
15
16 * Author(s): Melissa LeBlanc-Williams
17
18 """
19
20 from typing import Union, Tuple, Optional
21 from PIL import ImageFont
22 from displayio import Bitmap
23
24 try:
25     from typing import Protocol
26 except ImportError:
27     from typing_extensions import Protocol
28
29 __version__ = "0.0.0+auto.0"
30 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
31
32
33 class FontProtocol(Protocol):
34     """A protocol shared by `BuiltinFont` and classes in ``adafruit_bitmap_font``"""
35
36     def get_bounding_box(self) -> Union[Tuple[int, int], Tuple[int, int, int, int]]:
37         """Retrieve the maximum bounding box of any glyph in the font.
38
39         The four element version is ``(width, height, x_offset, y_offset)``.
40         The two element version is ``(width, height)``, in which
41         ``x_offset`` and ``y_offset`` are assumed to be zero.
42         """
43
44     def get_glyph(self, codepoint: int) -> Optional["Glyph"]:
45         """Retrieve the Glyph for a given code point
46
47         If the code point is not present in the font, `None` is returned.
48         """
49
50
51 class BuiltinFont:
52     """Simulate a font built into CircuitPython"""
53
54     def __init__(self):
55         self._font = ImageFont.load_default()
56         self._generate_bitmap(0x20, 0x7E)
57
58     def _generate_bitmap(self, start_range, end_range):
59         char_width, char_height = self.get_bounding_box()
60         self._bitmap = Bitmap(
61             char_width * (end_range - start_range + 1), char_height, 2
62         )
63         for character in range(start_range, end_range + 1):
64             ascii_char = chr(character)
65             ascii_mask = self._font.getmask(ascii_char, mode="1")
66             for y in range(char_height):
67                 for x in range(char_width):
68                     color = ascii_mask.getpixel((x, y))
69                     character_position = character - start_range
70                     self._bitmap[character_position * char_width + x, y] = (
71                         1 if color else 0
72                     )
73
74     def get_bounding_box(self):
75         """Returns the maximum bounds of all glyphs in the font in
76         a tuple of two values: width, height.
77         """
78         return self._font.getsize("M")
79
80     def get_glyph(self, codepoint):
81         """Returns a `fontio.Glyph` for the given codepoint or None if no glyph is available."""
82         if 0x20 <= codepoint <= 0x7E:
83             glyph_index = codepoint - 0x20
84         else:
85             return None
86
87         bounding_box = self._font.getsize(chr(codepoint))
88         width, height = bounding_box
89         return Glyph(
90             bitmap=self._bitmap,
91             tile_index=glyph_index,
92             width=width,
93             height=height,
94             dx=0,
95             dy=0,
96             shift_x=width,
97             shift_y=0,
98         )
99
100     @property
101     def bitmap(self):
102         """Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use
103         `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and
104         `terminalio.Terminal`.
105         """
106         return self._bitmap
107
108
109 # pylint: disable=too-few-public-methods, invalid-name, too-many-arguments
110 class Glyph:
111     """Storage of glyph info"""
112
113     def __init__(self, bitmap, tile_index, width, height, dx, dy, shift_x, shift_y):
114         self.bitmap = bitmap
115         self.width = width
116         self.height = height
117         self.dx = dx
118         self.dy = dy
119         self.shift_x = shift_x
120         self.shift_y = shift_y
121         self.tile_index = tile_index
122
123
124 # pylint: enable=too-few-public-methods, invalid-name, too-many-arguments