22
22
23
23
"""
24
24
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
+
25
32
from collections import namedtuple
26
33
import gc
27
34
import struct
28
35
from micropython import const
29
-
30
36
from fontio import Glyph
31
37
from .glyph_cache import GlyphCache
32
38
96
102
class PCF (GlyphCache ):
97
103
"""Loads glyphs from a PCF file in the given bitmap_class."""
98
104
99
- def __init__ (self , f , bitmap_class ) :
105
+ def __init__ (self , f : FileIO , bitmap_class : displayioBitmap ) -> None :
100
106
super ().__init__ ()
101
107
self .file = f
102
108
self .name = f
@@ -133,27 +139,27 @@ def __init__(self, f, bitmap_class):
133
139
)
134
140
135
141
@property
136
- def ascent (self ):
142
+ def ascent (self ) -> int :
137
143
"""The number of pixels above the baseline of a typical ascender"""
138
144
return self ._ascent
139
145
140
146
@property
141
- def descent (self ):
147
+ def descent (self ) -> int :
142
148
"""The number of pixels below the baseline of a typical descender"""
143
149
return self ._descent
144
150
145
- def get_bounding_box (self ):
151
+ def get_bounding_box (self ) -> Tuple [ int , int , int , int ] :
146
152
"""Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
147
153
return self ._bounding_box
148
154
149
- def _read (self , format_ ) :
155
+ def _read (self , format_ : str ) -> Tuple :
150
156
size = struct .calcsize (format_ )
151
157
if size != len (self .buffer ):
152
158
self .buffer = bytearray (size )
153
159
self .file .readinto (self .buffer )
154
160
return struct .unpack_from (format_ , self .buffer )
155
161
156
- def _seek_table (self , table ) :
162
+ def _seek_table (self , table : Table ) -> int :
157
163
self .file .seek (table .offset )
158
164
(format_ ,) = self ._read ("<I" )
159
165
@@ -162,13 +168,13 @@ def _seek_table(self, table):
162
168
163
169
return format_
164
170
165
- def _read_encoding_table (self ):
171
+ def _read_encoding_table (self ) -> Encoding :
166
172
encoding = self .tables [_PCF_BDF_ENCODINGS ]
167
173
self ._seek_table (encoding )
168
174
169
175
return Encoding (* self ._read (">hhhhh" ))
170
176
171
- def _read_bitmap_table (self ):
177
+ def _read_bitmap_table (self ) -> Bitmap :
172
178
bitmaps = self .tables [_PCF_BITMAPS ]
173
179
format_ = self ._seek_table (bitmaps )
174
180
@@ -177,7 +183,7 @@ def _read_bitmap_table(self):
177
183
bitmap_sizes = self ._read (">4I" )
178
184
return Bitmap (glyph_count , bitmap_sizes [format_ & 3 ])
179
185
180
- def _read_metrics (self , compressed_metrics ) :
186
+ def _read_metrics (self , compressed_metrics : bool ) -> Metrics :
181
187
if compressed_metrics :
182
188
(
183
189
left_side_bearing ,
@@ -210,7 +216,7 @@ def _read_metrics(self, compressed_metrics):
210
216
attributes ,
211
217
)
212
218
213
- def _read_accelerator_tables (self ):
219
+ def _read_accelerator_tables (self ) -> Accelerators :
214
220
# pylint: disable=too-many-locals
215
221
accelerators = self .tables .get (_PCF_BDF_ACCELERATORS )
216
222
if not accelerators :
@@ -260,7 +266,7 @@ def _read_accelerator_tables(self):
260
266
ink_maxbounds ,
261
267
)
262
268
263
- def _read_properties (self ):
269
+ def _read_properties (self ) -> Iterator [ Tuple [ bytes , Union [ bytes , int ]]] :
264
270
property_table_offset = self .tables [_PCF_PROPERTIES ]["offset" ]
265
271
self .file .seek (property_table_offset )
266
272
(format_ ,) = self ._read ("<I" )
@@ -291,7 +297,7 @@ def _read_properties(self):
291
297
else :
292
298
yield (string_map [name_offset ], value )
293
299
294
- def load_glyphs (self , code_points ) :
300
+ def load_glyphs (self , code_points : Union [ int , str , Iterable [ int ]]) -> None :
295
301
# pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals
296
302
if isinstance (code_points , int ):
297
303
code_points = (code_points ,)
0 commit comments