]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - fontio.py
Fixed font bitmap generation to remove artifacts
[hackapet/Adafruit_Blinka_Displayio.git] / fontio.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22 """
23 `fontio`
24 ================================================================================
25
26 fontio for Blinka
27
28 **Software and Dependencies:**
29
30 * Adafruit Blinka:
31   https://github.com/adafruit/Adafruit_Blinka/releases
32
33 * Author(s): Melissa LeBlanc-Williams
34
35 """
36
37 from PIL import ImageFont
38 from displayio import Bitmap
39
40 __version__ = "0.0.0-auto.0"
41 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
42
43
44 class BuiltinFont:
45     """Simulate a font built into CircuitPython"""
46
47     def __init__(self):
48         self._font = ImageFont.load_default()
49         self._generate_bitmap(0x20, 0x7E)
50
51     def _generate_bitmap(self, start_range, end_range):
52         char_width, char_height = self.get_bounding_box()
53         self._bitmap = Bitmap(
54             char_width * (end_range - start_range + 1), char_height, 2
55         )
56         for character in range(start_range, end_range + 1):
57             ascii_char = chr(character)
58             ascii_mask = self._font.getmask(ascii_char, mode="1")
59             for y in range(char_height):
60                 for x in range(char_width):
61                     color = ascii_mask.getpixel((x, y))
62                     character_position = character - start_range
63                     self._bitmap[character_position * char_width + x, y] = (
64                         1 if color else 0
65                     )
66
67     def get_bounding_box(self):
68         """Returns the maximum bounds of all glyphs in the font in
69         a tuple of two values: width, height.
70         """
71         return self._font.getsize("M")
72
73     def get_glyph(self, codepoint):
74         """Returns a `fontio.Glyph` for the given codepoint or None if no glyph is available."""
75         if 0x20 <= codepoint <= 0x7E:
76             glyph_index = codepoint - 0x20
77         else:
78             return None
79
80         bounding_box = self._font.getsize(chr(codepoint))
81         width, height = bounding_box
82         return Glyph(
83             bitmap=self._bitmap,
84             tile_index=glyph_index,
85             width=width,
86             height=height,
87             dx=0,
88             dy=0,
89             shift_x=width,
90             shift_y=0,
91         )
92
93     @property
94     def bitmap(self):
95         """Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use
96         `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and
97         `terminalio.Terminal`.
98         """
99         return self._bitmap
100
101
102 # pylint: disable=too-few-public-methods, invalid-name
103 class Glyph:
104     """Storage of glyph info"""
105
106     def __init__(self, *, bitmap, tile_index, width, height, dx, dy, shift_x, shift_y):
107         self.bitmap = bitmap
108         self.width = width
109         self.height = height
110         self.dx = dx
111         self.dy = dy
112         self.shift_x = shift_x
113         self.shift_y = shift_y
114         self.tile_index = tile_index
115
116
117 # pylint: enable=too-few-public-methods, invalid-name