1 # The MIT License (MIT)
3 # Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
24 ================================================================================
28 **Software and Dependencies:**
31 https://github.com/adafruit/Adafruit_Blinka/releases
33 * Author(s): Melissa LeBlanc-Williams
37 from PIL import ImageFont
38 from displayio import Bitmap
40 __version__ = "0.0.0-auto.0"
41 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
45 """Simulate a font built into CircuitPython"""
48 self._font = ImageFont.load_default()
50 for character in range(0x20, 0x7F):
51 ascii_chars += chr(character)
52 self._font.getmask(ascii_chars)
53 bmp_size = self._font.getsize(ascii_chars)
54 self._bitmap = Bitmap(bmp_size[0], bmp_size[1], 2)
55 ascii_mask = self._font.getmask(ascii_chars, mode="1")
56 for x in range(bmp_size[0]):
57 for y in range(bmp_size[1]):
58 self._bitmap[x, y] = 1 if ascii_mask.getpixel((x, y)) else 0
60 def _get_glyph_index(self, charcode):
61 if 0x20 <= charcode <= 0x7E:
62 return charcode - 0x20
65 def get_bounding_box(self):
66 """Returns the maximum bounds of all glyphs in the font in
67 a tuple of two values: width, height.
69 return self._font.getsize("M")
71 def get_glyph(self, codepoint):
72 """Returns a `fontio.Glyph` for the given codepoint or None if no glyph is available."""
73 bounding_box = self._font.getsize(chr(codepoint))
76 tile_index=self._get_glyph_index(codepoint),
77 width=bounding_box[0],
78 height=bounding_box[1],
87 """Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use
88 `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and
89 `terminalio.Terminal`.
95 """Storage of glyph info"""
97 def __init__(self, *, bitmap, tile_index, width, height, dx, dy, shift_x, shift_y):
103 self.shift_x = shift_x
104 self.shift_y = shift_y
105 self.tile_index = tile_index