1818from __future__ import annotations
1919
2020import io
21+ from typing import BinaryIO , Callable
2122
2223from . import FontFile , Image
2324from ._binary import i8
4142PCF_GLYPH_NAMES = 1 << 7
4243PCF_BDF_ACCELERATORS = 1 << 8
4344
44- BYTES_PER_ROW = [
45+ BYTES_PER_ROW : list [ Callable [[ int ], int ]] = [
4546 lambda bits : ((bits + 7 ) >> 3 ),
4647 lambda bits : ((bits + 15 ) >> 3 ) & ~ 1 ,
4748 lambda bits : ((bits + 31 ) >> 3 ) & ~ 3 ,
4849 lambda bits : ((bits + 63 ) >> 3 ) & ~ 7 ,
4950]
5051
5152
52- def sz (s , o ) :
53+ def sz (s : bytes , o : int ) -> bytes :
5354 return s [o : s .index (b"\0 " , o )]
5455
5556
@@ -58,7 +59,7 @@ class PcfFontFile(FontFile.FontFile):
5859
5960 name = "name"
6061
61- def __init__ (self , fp , charset_encoding = "iso8859-1" ):
62+ def __init__ (self , fp : BinaryIO , charset_encoding : str = "iso8859-1" ):
6263 self .charset_encoding = charset_encoding
6364
6465 magic = l32 (fp .read (4 ))
@@ -104,7 +105,9 @@ def __init__(self, fp, charset_encoding="iso8859-1"):
104105 bitmaps [ix ],
105106 )
106107
107- def _getformat (self , tag ):
108+ def _getformat (
109+ self , tag : int
110+ ) -> tuple [BinaryIO , int , Callable [[bytes ], int ], Callable [[bytes ], int ]]:
108111 format , size , offset = self .toc [tag ]
109112
110113 fp = self .fp
@@ -119,7 +122,7 @@ def _getformat(self, tag):
119122
120123 return fp , format , i16 , i32
121124
122- def _load_properties (self ):
125+ def _load_properties (self ) -> dict [ bytes , bytes | int ] :
123126 #
124127 # font properties
125128
@@ -138,18 +141,16 @@ def _load_properties(self):
138141 data = fp .read (i32 (fp .read (4 )))
139142
140143 for k , s , v in p :
141- k = sz (data , k )
142- if s :
143- v = sz (data , v )
144- properties [k ] = v
144+ property_value : bytes | int = sz (data , v ) if s else v
145+ properties [sz (data , k )] = property_value
145146
146147 return properties
147148
148- def _load_metrics (self ):
149+ def _load_metrics (self ) -> list [ tuple [ int , int , int , int , int , int , int , int ]] :
149150 #
150151 # font metrics
151152
152- metrics = []
153+ metrics : list [ tuple [ int , int , int , int , int , int , int , int ]] = []
153154
154155 fp , format , i16 , i32 = self ._getformat (PCF_METRICS )
155156
@@ -182,7 +183,9 @@ def _load_metrics(self):
182183
183184 return metrics
184185
185- def _load_bitmaps (self , metrics ):
186+ def _load_bitmaps (
187+ self , metrics : list [tuple [int , int , int , int , int , int , int , int ]]
188+ ) -> list [Image .Image ]:
186189 #
187190 # bitmap data
188191
@@ -222,7 +225,7 @@ def _load_bitmaps(self, metrics):
222225
223226 return bitmaps
224227
225- def _load_encoding (self ):
228+ def _load_encoding (self ) -> list [ int | None ] :
226229 fp , format , i16 , i32 = self ._getformat (PCF_BDF_ENCODINGS )
227230
228231 first_col , last_col = i16 (fp .read (2 )), i16 (fp .read (2 ))
@@ -233,7 +236,7 @@ def _load_encoding(self):
233236 nencoding = (last_col - first_col + 1 ) * (last_row - first_row + 1 )
234237
235238 # map character code to bitmap index
236- encoding = [None ] * min (256 , nencoding )
239+ encoding : list [ int | None ] = [None ] * min (256 , nencoding )
237240
238241 encoding_offsets = [i16 (fp .read (2 )) for _ in range (nencoding )]
239242
0 commit comments