@@ -5467,67 +5467,52 @@ def build_subset(buffer, unc_set, gid_set):
5467
5467
pymupdf .message ("This method requires fontTools to be installed." )
5468
5468
raise
5469
5469
import tempfile
5470
- tmp_dir = tempfile .gettempdir ()
5471
- oldfont_path = f"{ tmp_dir } /oldfont.ttf"
5472
- newfont_path = f"{ tmp_dir } /newfont.ttf"
5473
- uncfile_path = f"{ tmp_dir } /uncfile.txt"
5474
- args = [
5475
- oldfont_path ,
5476
- "--retain-gids" ,
5477
- f"--output-file={ newfont_path } " ,
5478
- "--layout-features='*'" ,
5479
- "--passthrough-tables" ,
5480
- "--ignore-missing-glyphs" ,
5481
- "--ignore-missing-unicodes" ,
5482
- "--symbol-cmap" ,
5483
- ]
5484
-
5485
- # store glyph ids or unicodes as file
5486
- with open (f"{ tmp_dir } /uncfile.txt" , "w" , encoding = 'utf8' ) as unc_file :
5487
- if 0xFFFD in unc_set : # error unicode exists -> use glyphs
5488
- args .append (f"--gids-file={ uncfile_path } " )
5489
- gid_set .add (189 )
5490
- unc_list = list (gid_set )
5491
- for unc in unc_list :
5492
- unc_file .write ("%i\n " % unc )
5493
- else :
5494
- args .append (f"--unicodes-file={ uncfile_path } " )
5495
- unc_set .add (255 )
5496
- unc_list = list (unc_set )
5497
- for unc in unc_list :
5498
- unc_file .write ("%04x\n " % unc )
5499
-
5500
- # store fontbuffer as a file
5501
- with open (oldfont_path , "wb" ) as fontfile :
5502
- fontfile .write (buffer )
5503
- try :
5504
- os .remove (newfont_path ) # remove old file
5505
- except Exception :
5506
- pass
5507
- try : # invoke fontTools subsetter
5508
- fts .main (args )
5509
- font = pymupdf .Font (fontfile = newfont_path )
5510
- new_buffer = font .buffer # subset font binary
5511
- if font .glyph_count == 0 : # intercept empty font
5470
+ with tempfile .TemporaryDirectory () as tmp_dir :
5471
+ oldfont_path = f"{ tmp_dir } /oldfont.ttf"
5472
+ newfont_path = f"{ tmp_dir } /newfont.ttf"
5473
+ uncfile_path = f"{ tmp_dir } /uncfile.txt"
5474
+ args = [
5475
+ oldfont_path ,
5476
+ "--retain-gids" ,
5477
+ f"--output-file={ newfont_path } " ,
5478
+ "--layout-features='*'" ,
5479
+ "--passthrough-tables" ,
5480
+ "--ignore-missing-glyphs" ,
5481
+ "--ignore-missing-unicodes" ,
5482
+ "--symbol-cmap" ,
5483
+ ]
5484
+
5485
+ # store glyph ids or unicodes as file
5486
+ with open (f"{ tmp_dir } /uncfile.txt" , "w" , encoding = 'utf8' ) as unc_file :
5487
+ if 0xFFFD in unc_set : # error unicode exists -> use glyphs
5488
+ args .append (f"--gids-file={ uncfile_path } " )
5489
+ gid_set .add (189 )
5490
+ unc_list = list (gid_set )
5491
+ for unc in unc_list :
5492
+ unc_file .write ("%i\n " % unc )
5493
+ else :
5494
+ args .append (f"--unicodes-file={ uncfile_path } " )
5495
+ unc_set .add (255 )
5496
+ unc_list = list (unc_set )
5497
+ for unc in unc_list :
5498
+ unc_file .write ("%04x\n " % unc )
5499
+
5500
+ # store fontbuffer as a file
5501
+ with open (oldfont_path , "wb" ) as fontfile :
5502
+ fontfile .write (buffer )
5503
+ try :
5504
+ os .remove (newfont_path ) # remove old file
5505
+ except Exception :
5506
+ pass
5507
+ try : # invoke fontTools subsetter
5508
+ fts .main (args )
5509
+ font = pymupdf .Font (fontfile = newfont_path )
5510
+ new_buffer = font .buffer # subset font binary
5511
+ if font .glyph_count == 0 : # intercept empty font
5512
+ new_buffer = None
5513
+ except Exception :
5514
+ pymupdf .exception_info ()
5512
5515
new_buffer = None
5513
- except Exception :
5514
- pymupdf .exception_info ()
5515
- new_buffer = None
5516
- try :
5517
- os .remove (uncfile_path )
5518
- except Exception :
5519
- pymupdf .exception_info ()
5520
- pass
5521
- try :
5522
- os .remove (oldfont_path )
5523
- except Exception :
5524
- pymupdf .exception_info ()
5525
- pass
5526
- try :
5527
- os .remove (newfont_path )
5528
- except Exception :
5529
- pymupdf .exception_info ()
5530
- pass
5531
5516
return new_buffer
5532
5517
5533
5518
def repl_fontnames (doc ):
0 commit comments