@@ -349,7 +349,7 @@ def _subset_format2(self, glyphs):
349
349
name_index < numglyphs - N_BASIC_NAMES ):
350
350
needed_indices [name_index - N_BASIC_NAMES ] = gind
351
351
352
- names = []
352
+ names = [b'.removed' ]
353
353
name_index = 0
354
354
i += 2 * numglyphs
355
355
while i < len (content ):
@@ -364,8 +364,8 @@ def _subset_format2(self, glyphs):
364
364
name_index += 1
365
365
366
366
new_content = [content [0 :36 ]]
367
- for i in range (numglyphs ):
368
- val = new_glyph_index .get (i , 0 )
367
+ for i in range (1 , numglyphs ):
368
+ val = new_glyph_index .get (i , N_BASIC_NAMES )
369
369
new_content .append (struct .pack ('>H' , val ))
370
370
371
371
for name in names :
@@ -379,8 +379,64 @@ def subset(self, glyphs):
379
379
self .content = self ._subset_format2 (glyphs )
380
380
381
381
382
+ class _HheaTable (_Table ):
383
+ hhea_table_struct = _BinaryStruct ([
384
+ ('version' , 'I' ),
385
+ ('ascent' , 'h' ),
386
+ ('descent' , 'h' ),
387
+ ('lineGap' , 'h' ),
388
+ ('advanceWidthMax' , 'H' ),
389
+ ('minLeftSideBearing' , 'h' ),
390
+ ('minRightSideBearing' , 'h' ),
391
+ ('xMaxExtent' , 'h' ),
392
+ ('caretSlopeRise' , 'h' ),
393
+ ('caretSlopeRun' , 'h' ),
394
+ ('caretOffset' , 'h' ),
395
+ ('res0' , 'h' ),
396
+ ('res1' , 'h' ),
397
+ ('res2' , 'h' ),
398
+ ('res3' , 'h' ),
399
+ ('metricDataFormat' , 'h' ),
400
+ ('numOfLongHorMetrics' , 'H' )])
401
+
402
+ def __init__ (self , header , content ):
403
+ super (_HheaTable , self ).__init__ (header , content )
404
+
405
+ self .__dict__ .update (self .hhea_table_struct .unpack (content ))
406
+
407
+
408
+ class _HmtxTable (_Table ):
409
+ def subset (self , glyph_set , offsets , hhea ):
410
+ # In keeping with not changing glyph ids, we can't actually
411
+ # remove entries here. However, we can set unused entries to
412
+ # 0 which should aid in compression.
413
+
414
+ n_glyphs = len (offsets ) - 1
415
+ n_long_hor_metrics = hhea .numOfLongHorMetrics
416
+ content = self .content
417
+
418
+ h_metrics = content [:n_long_hor_metrics * 4 ]
419
+ new_values = []
420
+ for i in range (n_long_hor_metrics ):
421
+ if i in glyph_set :
422
+ new_values .append (h_metrics [i * 4 :i * 4 + 4 ])
423
+ else :
424
+ new_values .append (b'\0 \0 \0 \0 ' )
425
+
426
+ left_side_bearing = content [n_long_hor_metrics * 4 :]
427
+ for i in range (n_glyphs - n_long_hor_metrics ):
428
+ if i + n_long_hor_metrics in glyph_set :
429
+ new_values .append (left_side_bearing [i * 2 :i * 2 + 2 ])
430
+ else :
431
+ new_values .append (b'\0 \0 ' )
432
+
433
+ self .content = b'' .join (new_values )
434
+
435
+
382
436
SPECIAL_TABLES = {
383
437
b'head' : _HeadTable ,
438
+ b'hhea' : _HheaTable ,
439
+ b'hmtx' : _HmtxTable ,
384
440
b'loca' : _LocaTable ,
385
441
b'glyf' : _GlyfTable ,
386
442
b'post' : _PostTable
@@ -452,10 +508,14 @@ def subset(self, ccodes):
452
508
# glyphs
453
509
glyphs = self [b'glyf' ].find_all_glyphs (glyphs , offsets )
454
510
511
+ glyph_set = set (glyphs )
512
+
455
513
self [b'glyf' ].subset (glyphs , offsets )
456
514
self [b'loca' ].subset (self , glyphs , offsets )
457
515
if b'post' in self ._tables :
458
516
self [b'post' ].subset (glyphs )
517
+ if b'hmtx' in self ._tables and b'hhea' in self ._tables :
518
+ self [b'hmtx' ].subset (glyph_set , offsets , self [b'hhea' ])
459
519
460
520
def write (self , fd ):
461
521
self ._header ['numTables' ] = len (self ._tables )
0 commit comments