]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - fontio.py
Black formatted again...
[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 displayio import Bitmap
38 from PIL import ImageFont
39
40 __version__ = "0.0.0-auto.0"
41 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
42
43
44 class BuiltinFont:
45     def __init__(self):
46         self._font = ImageFont.load_default()
47         ascii = ""
48         for character in range(0x20, 0x7F):
49             ascii += chr(character)
50         self._font.getmask(ascii)
51         bmp_size = self._font.getsize(ascii)
52         self._bitmap = Bitmap(bmp_size[0], bmp_size[1], 2)
53         ascii_mask = self._font.getmask(ascii, mode="1")
54         for x in range(bmp_size[0]):
55             for y in range(bmp_size[1]):
56                 self._bitmap[x, y] = 1 if ascii_mask.getpixel((x, y)) else 0
57
58     def _get_glyph_index(self, charcode):
59         if 0x20 <= charcode <= 0x7E:
60             return charcode - 0x20
61
62     def get_bounding_box(self):
63         """Returns the maximum bounds of all glyphs in the font in a tuple of two values: width, height."""
64         return self._font.getsize("M")
65
66     def get_glyph(self, codepoint):
67         """Returns a `fontio.Glyph` for the given codepoint or None if no glyph is available."""
68         bounding_box = self._font.getsize(chr(codepoint))
69         return Glyph(
70             bitmap=self._bitmap,
71             tile_index=self._get_glyph_index(codepoint),
72             width=bounding_box[0],
73             height=bounding_box[1],
74             dx=0,
75             dy=0,
76             shift_x=0,
77             shift_y=0,
78         )
79
80     @property
81     def bitmap(self):
82         """Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and `terminalio.Terminal`.
83         """
84         return self._bitmap
85
86
87 class Glyph:
88     def __init__(self, *, bitmap, tile_index, width, height, dx, dy, shift_x, shift_y):
89         self.bitmap = bitmap
90         self.width = width
91         self.height = height
92         self.dx = dx
93         self.dy = dy
94         self.shift_x = shift_x
95         self.shift_y = shift_y
96         self.tile_index = tile_index