Skip to content

Commit e94214f

Browse files
committed
Added Type Annotations
1 parent 94ba4ea commit e94214f

File tree

5 files changed

+60
-36
lines changed

5 files changed

+60
-36
lines changed

adafruit_bitmap_font/bdf.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@
2222
2323
"""
2424

25+
try:
26+
from typing import Union, Optional, Tuple
27+
except ImportError:
28+
pass
29+
2530
import gc
31+
from io import FileIO
2632
from fontio import Glyph
33+
from displayio import Bitmap
2734
from .glyph_cache import GlyphCache
2835

2936
__version__ = "0.0.0-auto.0"
@@ -33,7 +40,7 @@
3340
class BDF(GlyphCache):
3441
"""Loads glyphs from a BDF file in the given bitmap_class."""
3542

36-
def __init__(self, f, bitmap_class):
43+
def __init__(self, f: FileIO, bitmap_class: Bitmap) -> None:
3744
super().__init__()
3845
self.file = f
3946
self.name = f
@@ -50,7 +57,7 @@ def __init__(self, f, bitmap_class):
5057
self._descent = None
5158

5259
@property
53-
def descent(self):
60+
def descent(self) -> Optional[int]:
5461
"""The number of pixels below the baseline of a typical descender"""
5562
if self._descent is None:
5663
self.file.seek(0)
@@ -66,7 +73,7 @@ def descent(self):
6673
return self._descent
6774

6875
@property
69-
def ascent(self):
76+
def ascent(self) -> Optional[int]:
7077
"""The number of pixels above the baseline of a typical ascender"""
7178
if self._ascent is None:
7279
self.file.seek(0)
@@ -81,7 +88,7 @@ def ascent(self):
8188

8289
return self._ascent
8390

84-
def _verify_bounding_box(self):
91+
def _verify_bounding_box(self) -> None:
8592
"""Private function to verify FOUNTBOUNDINGBOX parameter
8693
This function will parse the first 10 lines of the font source
8794
file to verify the value or raise an exception in case is not found
@@ -105,15 +112,15 @@ def _verify_bounding_box(self):
105112
"Source file does not have the FOUNTBOUNDINGBOX parameter"
106113
) from error
107114

108-
def _readline_file(self):
115+
def _readline_file(self) -> str:
109116
line = self.file.readline()
110117
return str(line, "utf-8")
111118

112-
def get_bounding_box(self):
119+
def get_bounding_box(self) -> Tuple[int, int, int, int]:
113120
"""Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
114121
return self._boundingbox
115122

116-
def load_glyphs(self, code_points):
123+
def load_glyphs(self, code_points: Union[int, str, set]) -> None:
117124
# pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals
118125
metadata = True
119126
character = False

adafruit_bitmap_font/bitmap_font.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,34 @@
2222
2323
"""
2424

25+
try:
26+
from typing import Optional, Union
27+
except ImportError:
28+
pass
29+
30+
from displayio import Bitmap
31+
from . import bdf
32+
from . import pcf
33+
from . import ttf
34+
2535
__version__ = "0.0.0-auto.0"
2636
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font.git"
2737

2838

29-
def load_font(filename, bitmap=None):
39+
def load_font(
40+
filename: str, bitmap: Optional[Bitmap] = None
41+
) -> Union[bdf.BDF, pcf.PCF, ttf.TTF, None]:
3042
"""Loads a font file. Returns None if unsupported."""
3143
# pylint: disable=import-outside-toplevel, consider-using-with
3244
if not bitmap:
33-
import displayio
34-
35-
bitmap = displayio.Bitmap
45+
bitmap = Bitmap
3646
font_file = open(filename, "rb")
3747
first_four = font_file.read(4)
3848
if filename.endswith("bdf") and first_four == b"STAR":
39-
from . import bdf
40-
4149
return bdf.BDF(font_file, bitmap)
4250
if filename.endswith("pcf") and first_four == b"\x01fcp":
43-
from . import pcf
44-
4551
return pcf.PCF(font_file, bitmap)
4652
if filename.endswith("ttf") and first_four == b"\x00\x01\x00\x00":
47-
from . import ttf
48-
4953
return ttf.TTF(font_file, bitmap)
5054

5155
raise ValueError("Unknown magic number %r" % first_four)

adafruit_bitmap_font/glyph_cache.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
2323
"""
2424

25+
try:
26+
from typing import Union, Optional
27+
except ImportError:
28+
pass
29+
2530
import gc
31+
from fontio import Glyph
2632

2733
__version__ = "0.0.0-auto.0"
2834
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font.git"
@@ -31,13 +37,13 @@
3137
class GlyphCache:
3238
"""Caches glyphs loaded by a subclass."""
3339

34-
def __init__(self):
40+
def __init__(self) -> None:
3541
self._glyphs = {}
3642

37-
def load_glyphs(self, code_points):
43+
def load_glyphs(self, code_points: Union[int, str, set]) -> None:
3844
"""Loads displayio.Glyph objects into the GlyphCache from the font."""
3945

40-
def get_glyph(self, code_point):
46+
def get_glyph(self, code_point: int) -> Optional[Glyph]:
4147
"""Returns a displayio.Glyph for the given code point or None is unsupported."""
4248
if code_point in self._glyphs:
4349
return self._glyphs[code_point]

adafruit_bitmap_font/pcf.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@
2222
2323
"""
2424

25+
try:
26+
from typing import Union, Generator
27+
except ImportError:
28+
pass
29+
2530
from collections import namedtuple
2631
import gc
2732
import struct
33+
from io import FileIO
2834
from micropython import const
29-
3035
from fontio import Glyph
36+
from displayio import Bitmap as displayioBitmap
3137
from .glyph_cache import GlyphCache
3238

3339
try:
@@ -96,7 +102,7 @@
96102
class PCF(GlyphCache):
97103
"""Loads glyphs from a PCF file in the given bitmap_class."""
98104

99-
def __init__(self, f, bitmap_class):
105+
def __init__(self, f: FileIO, bitmap_class: displayioBitmap) -> None:
100106
super().__init__()
101107
self.file = f
102108
self.name = f
@@ -133,27 +139,27 @@ def __init__(self, f, bitmap_class):
133139
)
134140

135141
@property
136-
def ascent(self):
142+
def ascent(self) -> int:
137143
"""The number of pixels above the baseline of a typical ascender"""
138144
return self._ascent
139145

140146
@property
141-
def descent(self):
147+
def descent(self) -> int:
142148
"""The number of pixels below the baseline of a typical descender"""
143149
return self._descent
144150

145-
def get_bounding_box(self):
151+
def get_bounding_box(self) -> tuple:
146152
"""Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
147153
return self._bounding_box
148154

149-
def _read(self, format_):
155+
def _read(self, format_: str) -> tuple:
150156
size = struct.calcsize(format_)
151157
if size != len(self.buffer):
152158
self.buffer = bytearray(size)
153159
self.file.readinto(self.buffer)
154160
return struct.unpack_from(format_, self.buffer)
155161

156-
def _seek_table(self, table):
162+
def _seek_table(self, table: dict) -> int:
157163
self.file.seek(table.offset)
158164
(format_,) = self._read("<I")
159165

@@ -162,13 +168,13 @@ def _seek_table(self, table):
162168

163169
return format_
164170

165-
def _read_encoding_table(self):
171+
def _read_encoding_table(self) -> Encoding:
166172
encoding = self.tables[_PCF_BDF_ENCODINGS]
167173
self._seek_table(encoding)
168174

169175
return Encoding(*self._read(">hhhhh"))
170176

171-
def _read_bitmap_table(self):
177+
def _read_bitmap_table(self) -> Bitmap:
172178
bitmaps = self.tables[_PCF_BITMAPS]
173179
format_ = self._seek_table(bitmaps)
174180

@@ -177,7 +183,7 @@ def _read_bitmap_table(self):
177183
bitmap_sizes = self._read(">4I")
178184
return Bitmap(glyph_count, bitmap_sizes[format_ & 3])
179185

180-
def _read_metrics(self, compressed_metrics):
186+
def _read_metrics(self, compressed_metrics: bool) -> Metrics:
181187
if compressed_metrics:
182188
(
183189
left_side_bearing,
@@ -210,7 +216,7 @@ def _read_metrics(self, compressed_metrics):
210216
attributes,
211217
)
212218

213-
def _read_accelerator_tables(self):
219+
def _read_accelerator_tables(self) -> Accelerators:
214220
# pylint: disable=too-many-locals
215221
accelerators = self.tables.get(_PCF_BDF_ACCELERATORS)
216222
if not accelerators:
@@ -260,7 +266,7 @@ def _read_accelerator_tables(self):
260266
ink_maxbounds,
261267
)
262268

263-
def _read_properties(self):
269+
def _read_properties(self) -> Generator[tuple, None, None]:
264270
property_table_offset = self.tables[_PCF_PROPERTIES]["offset"]
265271
self.file.seek(property_table_offset)
266272
(format_,) = self._read("<I")
@@ -291,7 +297,7 @@ def _read_properties(self):
291297
else:
292298
yield (string_map[name_offset], value)
293299

294-
def load_glyphs(self, code_points):
300+
def load_glyphs(self, code_points: Union[int, str, set]) -> None:
295301
# pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals
296302
if isinstance(code_points, int):
297303
code_points = (code_points,)

adafruit_bitmap_font/ttf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
# Remove the above when TTF is actually supported.
77

88
import struct
9-
9+
from io import FileIO
10+
from displayio import Bitmap
1011

1112
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6glyf.html
1213

1314

1415
class TTF:
15-
def __init__(self, f, bitmap):
16+
def __init__(self, f: FileIO, bitmap: Bitmap) -> None:
1617
f.seek(0)
1718
self.file = f
1819

1920
self.characters = {}
2021

21-
def read(format):
22+
def read(format: str) -> tuple:
2223
s = struct.calcsize(format)
2324
return struct.unpack_from(format, f.read(s))
2425

0 commit comments

Comments
 (0)