Skip to content

Commit b656b2b

Browse files
authored
Merge pull request #58 from isacben/bitmap-font-type-annotations
Added Type Annotations
2 parents 511e0d8 + 781b2d3 commit b656b2b

File tree

7 files changed

+69
-29
lines changed

7 files changed

+69
-29
lines changed

adafruit_bitmap_font/bdf.py

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

25+
try:
26+
from typing import Union, Optional, Tuple, Iterable
27+
from io import FileIO
28+
from displayio import Bitmap
29+
except ImportError:
30+
pass
31+
2532
import gc
2633
from fontio import Glyph
2734
from .glyph_cache import GlyphCache
@@ -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, Iterable[int]]) -> 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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,24 @@
2222
2323
"""
2424

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

2837

29-
def load_font(filename, bitmap=None):
38+
def load_font(
39+
filename: str, bitmap: Optional[Bitmap] = None
40+
) -> Union[bdf.BDF, pcf.PCF, ttf.TTF]:
3041
"""Loads a font file. Returns None if unsupported."""
31-
# pylint: disable=import-outside-toplevel, consider-using-with
42+
# pylint: disable=import-outside-toplevel, redefined-outer-name, consider-using-with
3243
if not bitmap:
3344
import displayio
3445

adafruit_bitmap_font/glyph_cache.py

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

25+
try:
26+
from typing import Union, Iterable
27+
from fontio import Glyph
28+
except ImportError:
29+
pass
30+
2531
import gc
2632

2733
__version__ = "0.0.0-auto.0"
@@ -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, Iterable[int]]) -> 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) -> 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,11 +22,17 @@
2222
2323
"""
2424

25+
try:
26+
from typing import Union, Tuple, Iterator, Iterable
27+
from io import FileIO
28+
from displayio import Bitmap as displayioBitmap
29+
except ImportError:
30+
pass
31+
2532
from collections import namedtuple
2633
import gc
2734
import struct
2835
from micropython import const
29-
3036
from fontio import Glyph
3137
from .glyph_cache import GlyphCache
3238

@@ -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[int, int, int, int]:
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: Table) -> 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) -> Iterator[Tuple[bytes, Union[bytes, int]]]:
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, Iterable[int]]) -> 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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@
55
# pylint: skip-file
66
# Remove the above when TTF is actually supported.
77

8-
import struct
8+
try:
9+
from typing import Tuple
10+
from io import FileIO
11+
from displayio import Bitmap
12+
except ImportError:
13+
pass
914

15+
import struct
1016

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

1319

1420
class TTF:
15-
def __init__(self, f, bitmap):
21+
def __init__(self, f: FileIO, bitmap: Bitmap) -> None:
1622
f.seek(0)
1723
self.file = f
1824

1925
self.characters = {}
2026

21-
def read(format):
27+
def read(format: str) -> Tuple:
2228
s = struct.calcsize(format)
2329
return struct.unpack_from(format, f.read(s))
2430

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
# SPDX-License-Identifier: Unlicense
44

55
Adafruit-Blinka
6+
Adafruit-Blinka-displayio

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
# Author details
3434
author="Adafruit Industries",
3535
author_email="circuitpython@adafruit.com",
36-
install_requires=["Adafruit-Blinka"],
36+
install_requires=[
37+
"Adafruit-Blinka",
38+
"Adafruit-Blinka-displayio",
39+
],
3740
# Choose your license
3841
license="MIT",
3942
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)