]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - fontio.py
Remove pillow requirement
[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 import os
21 from dataclasses import dataclass
22 from typing import Union, Tuple, Optional
23 from displayio import Bitmap
24
25 try:
26     from typing import Protocol
27 except ImportError:
28     from typing_extensions import Protocol
29
30 __version__ = "0.0.0+auto.0"
31 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
32
33 DEFAULT_FONT = "displayio/resources/ter-u12n.bdf"
34
35
36 class FontProtocol(Protocol):
37     """A protocol shared by `BuiltinFont` and classes in ``adafruit_bitmap_font``"""
38
39     def get_bounding_box(self) -> Union[Tuple[int, int], Tuple[int, int, int, int]]:
40         """Retrieve the maximum bounding box of any glyph in the font.
41
42         The four element version is ``(width, height, x_offset, y_offset)``.
43         The two element version is ``(width, height)``, in which
44         ``x_offset`` and ``y_offset`` are assumed to be zero.
45         """
46
47     def get_glyph(self, codepoint: int) -> Optional["Glyph"]:
48         """Retrieve the Glyph for a given code point
49
50         If the code point is not present in the font, `None` is returned.
51         """
52
53
54 class BuiltinFont:
55     """Simulate a font built into CircuitPython"""
56
57     def __init__(self):
58         self._width = 0
59         self._height = 0
60
61         # Place import here to avoid circular import
62         from adafruit_bitmap_font import (  # pylint: disable=import-outside-toplevel
63             bitmap_font,
64         )
65
66         self._font = bitmap_font.load_font(
67             os.path.dirname(__file__) + "/" + DEFAULT_FONT
68         )
69
70         self._font.load_glyphs(set(range(0x20, 0x7F)))
71
72     def get_bounding_box(self) -> Union[Tuple[int, int], Tuple[int, int, int, int]]:
73         """Returns the maximum bounds of all glyphs in the font in
74         a tuple of two values: width, height.
75         """
76         return self._font.get_bounding_box()[0:2]
77
78     def get_glyph(self, codepoint: int) -> Optional["Glyph"]:
79         """Returns a `fontio.Glyph` for the given codepoint or None if no glyph is available."""
80         return self._font.get_glyph(codepoint)
81
82     @property
83     def bitmap(self):
84         """Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use
85         `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and
86         `terminalio.Terminal`.
87         """
88         return self._font.bitmap_class
89
90
91 @dataclass
92 class Glyph:
93     # pylint: disable=invalid-name
94     """Storage of glyph info"""
95     bitmap: Bitmap
96     tile_index: int
97     width: int
98     height: int
99     dx: int
100     dy: int
101     shift_x: int
102     shift_y: int