]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - fontio.py
Encapsulate command for fourwire in bytes list
[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 PIL import ImageFont
21 from displayio import Bitmap
22
23 __version__ = "0.0.0-auto.0"
24 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
25
26
27 class BuiltinFont:
28     """Simulate a font built into CircuitPython"""
29
30     def __init__(self):
31         self._font = ImageFont.load_default()
32         self._generate_bitmap(0x20, 0x7E)
33
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
38         )
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] = (
47                         1 if color else 0
48                     )
49
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.
53         """
54         return self._font.getsize("M")
55
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
60         else:
61             return None
62
63         bounding_box = self._font.getsize(chr(codepoint))
64         width, height = bounding_box
65         return Glyph(
66             bitmap=self._bitmap,
67             tile_index=glyph_index,
68             width=width,
69             height=height,
70             dx=0,
71             dy=0,
72             shift_x=width,
73             shift_y=0,
74         )
75
76     @property
77     def bitmap(self):
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`.
81         """
82         return self._bitmap
83
84
85 # pylint: disable=too-few-public-methods, invalid-name, too-many-arguments
86 class Glyph:
87     """Storage of glyph info"""
88
89     def __init__(self, bitmap, tile_index, width, height, dx, dy, shift_x, shift_y):
90         self.bitmap = bitmap
91         self.width = width
92         self.height = height
93         self.dx = dx
94         self.dy = dy
95         self.shift_x = shift_x
96         self.shift_y = shift_y
97         self.tile_index = tile_index
98
99
100 # pylint: enable=too-few-public-methods, invalid-name, too-many-arguments