@@ -201,7 +201,7 @@ class Font:
201201 ]
202202 _WIDTHS_BY_VALUE = {width ["value" ]: width for width in _WIDTHS }
203203
204- def __init__ (self , filepath , lazy = False ):
204+ def __init__ (self , filepath , ** kwargs ):
205205 """
206206 Constructs a new Font instance loading a font file from the given filepath.
207207
@@ -213,22 +213,22 @@ def __init__(self, filepath, lazy=False):
213213 super ().__init__ ()
214214
215215 self ._filepath = None
216- self ._lazy = None
216+ self ._kwargs = None
217217 self ._ttfont = None
218218
219219 if isinstance (filepath , str ):
220- self ._init_with_filepath (filepath , lazy = lazy )
220+ self ._init_with_filepath (filepath , ** kwargs )
221221 else :
222222 filepath_type = type (filepath ).__name__
223223 raise ValueError (
224224 f"Invalid filepath type: expected str, found '{ filepath_type } '."
225225 )
226226
227- def _init_with_filepath (self , filepath , lazy = False ):
227+ def _init_with_filepath (self , filepath , ** kwargs ):
228228 try :
229229 self ._filepath = filepath
230- self ._lazy = lazy
231- self ._ttfont = TTFont (self ._filepath , lazy = self . _lazy )
230+ self ._kwargs = kwargs
231+ self ._ttfont = TTFont (self ._filepath , ** kwargs )
232232
233233 except TTLibError :
234234 raise ValueError (f"Invalid font at filepath: '{ filepath } '." )
@@ -243,7 +243,7 @@ def clone(self):
243243 """
244244 Creates a new Font instance reading the same binary file.
245245 """
246- return Font (self ._filepath , lazy = self ._lazy )
246+ return Font (self ._filepath , ** self ._kwargs )
247247
248248 def close (self ):
249249 """
@@ -271,17 +271,17 @@ def get_characters(self, ignore_blank=False):
271271 glyfs = font .get ("glyf" )
272272 for code , char_name in cmap .items ():
273273 code_hex = f"{ code :04X} "
274- char = chr (code )
274+ if 0 <= code < 0x110000 :
275+ char = chr (code )
276+ else :
277+ continue
275278 if ascii .iscntrl (char ):
276279 continue
277280 if glyfs and ignore_blank :
278281 glyf = glyfs .get (char_name )
279282 if glyf and glyf .numberOfContours == 0 :
280283 continue
281- try :
282- unicode_name = unicodedata .name (char )
283- except ValueError :
284- pass
284+ unicode_name = unicodedata .name (char , None )
285285 unicode_block_name = unicodedata .block (code )
286286 unicode_script_tag = unicodedata .script (code )
287287 unicode_script_name = unicodedata .script_name (unicode_script_tag )
@@ -337,7 +337,10 @@ def get_features_tags(self):
337337 for table_tag in ["GPOS" , "GSUB" ]:
338338 if table_tag in font :
339339 table = font [table_tag ].table
340- feature_record = table .FeatureList .FeatureRecord or []
340+ try :
341+ feature_record = table .FeatureList .FeatureRecord or []
342+ except AttributeError :
343+ feature_record = []
341344 for feature in feature_record :
342345 features_tags .add (feature .FeatureTag )
343346 return sorted (features_tags )
@@ -357,11 +360,11 @@ def get_fingerprint(self, text=""):
357360
358361 text = text or "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
359362
360- img = self .get_image (text = text [ 0 ] , size = 72 )
363+ img = self .get_image (text = text , size = 72 )
361364 img_size = img .size
362365 img = img .resize ((img_size [0 ] // 2 , img_size [1 ] // 2 ))
363366 img = img .resize ((img_size [0 ], img_size [1 ]), Image .Resampling .NEAREST )
364- img = img .quantize (colors = 2 )
367+ img = img .quantize (colors = 8 )
365368 # img.show()
366369
367370 hash = imagehash .average_hash (img , hash_size = 64 )
@@ -484,7 +487,8 @@ def get_image(
484487 img_size = (img_width , img_height )
485488 img = img .resize (img_size )
486489 draw = ImageDraw .Draw (img )
487- draw .text ((0 , 0 ), text , font = img_font , fill = color )
490+ draw .text ((- img_bbox [0 ], - img_bbox [1 ]), text , font = img_font , fill = color )
491+ del img_font
488492 return img
489493
490494 def get_italic_angle (self ):
0 commit comments