@@ -71,6 +71,7 @@ def __init__(self, f, bitmap_class):
71
71
self .file = f
72
72
self .name = f
73
73
f .seek (0 )
74
+ self .buffer = bytearray (1 )
74
75
self .bitmap_class = bitmap_class
75
76
header , table_count = self .read ("<4sI" )
76
77
self .tables = {}
@@ -106,7 +107,10 @@ def get_bounding_box(self):
106
107
107
108
def read (self , format ):
108
109
s = struct .calcsize (format )
109
- return struct .unpack_from (format , self .file .read (s ))
110
+ if s != len (self .buffer ):
111
+ self .buffer = bytearray (s )
112
+ self .file .readinto (self .buffer )
113
+ return struct .unpack_from (format , self .buffer )
110
114
111
115
def seek_table (self , table ):
112
116
self .file .seek (table .offset )
@@ -117,10 +121,6 @@ def seek_table(self, table):
117
121
118
122
return format
119
123
120
- def seek_glyph (self , idx ):
121
- encoding = self .tables [_PCF_BDF_ENCODINGS ]
122
- self .seek_table (encoding )
123
-
124
124
def read_encoding_table (self ):
125
125
encoding = self .tables [_PCF_BDF_ENCODINGS ]
126
126
self .seek_table (encoding )
@@ -261,7 +261,6 @@ def load_glyphs(self, code_points):
261
261
code_points = sorted (
262
262
c for c in code_points if self ._glyphs .get (c , None ) is None
263
263
)
264
-
265
264
if not code_points :
266
265
return
267
266
@@ -316,6 +315,28 @@ def load_glyphs(self, code_points):
316
315
(bitmap_offset ,) = self .read (">I" )
317
316
bitmap_offsets [i ] = bitmap_offset
318
317
318
+ # Batch creation of glyphs and bitmaps so that we need only gc.collect
319
+ # once
320
+ gc .collect ()
321
+ bitmaps = [None ] * len (code_points )
322
+ for i in range (len (all_metrics )):
323
+ metrics = all_metrics [i ]
324
+ if metrics is not None :
325
+ shift = metrics .character_width
326
+ width = metrics .right_side_bearing - metrics .left_side_bearing
327
+ height = metrics .character_ascent + metrics .character_descent
328
+ bitmap = bitmaps [i ] = self .bitmap_class (width , height , 2 )
329
+ self ._glyphs [code_points [i ]] = Glyph (
330
+ bitmap ,
331
+ 0 ,
332
+ width ,
333
+ height ,
334
+ metrics .left_side_bearing ,
335
+ - metrics .character_descent ,
336
+ metrics .character_width ,
337
+ 0 ,
338
+ )
339
+
319
340
for i , code_point in enumerate (code_points ):
320
341
metrics = all_metrics [i ]
321
342
if metrics is None :
@@ -325,18 +346,7 @@ def load_glyphs(self, code_points):
325
346
width = metrics .right_side_bearing - metrics .left_side_bearing
326
347
height = metrics .character_ascent + metrics .character_descent
327
348
328
- gc .collect ()
329
- bitmap = self .bitmap_class (width , height , 2 )
330
- self ._glyphs [code_point ] = Glyph (
331
- bitmap ,
332
- 0 ,
333
- width ,
334
- height ,
335
- metrics .left_side_bearing ,
336
- - metrics .character_descent ,
337
- metrics .character_width ,
338
- 0 ,
339
- )
349
+ bitmap = bitmaps [i ]
340
350
words_per_row = (width + 31 ) // 32
341
351
buf = bytearray (4 * words_per_row )
342
352
start = 0
0 commit comments