1818from math import ceil , cos , floor , pi , sin
1919from sets import Set
2020
21+ import matplotlib
2122from matplotlib import __version__ , rcParams , agg , get_data_path
2223from matplotlib ._pylab_helpers import Gcf
2324from matplotlib .backend_bases import RendererBase , GraphicsContextBase ,\
@@ -493,12 +494,16 @@ def _write_afm_font(self, filename):
493494
494495 def embedType1 (self , filename , fontinfo ):
495496 fh = open (filename , 'rb' )
497+ matplotlib .verbose .report (
498+ 'Embedding Type 1 font ' + filename , 'debug' )
496499 try :
497500 fontdata = fh .read ()
498501 finally :
499502 fh .close ()
500503
501504 fh = open (fontinfo .afmfile , 'rb' )
505+ matplotlib .verbose .report (
506+ 'Reading metrics from ' + fontinfo .afmfile , 'debug' )
502507 try :
503508 afmdata = AFM (fh )
504509 finally :
@@ -519,9 +524,26 @@ def embedType1(self, filename, fontinfo):
519524 differencesArray = [ Name (ch ) for ch in
520525 dviread .Encoding (fontinfo .encodingfile ) ]
521526 differencesArray = [ 0 ] + differencesArray
527+ firstchar = 0
522528 lastchar = len (differencesArray ) - 2
529+ widths = [ 100 for x in range (firstchar ,lastchar + 1 ) ] # XXX TODO
523530 else :
524- lastchar = 255 # ?
531+ widths = [ None for i in range (256 ) ]
532+ for ch in range (256 ):
533+ try :
534+ widths [ch ] = afmdata .get_width_char (ch , isord = True )
535+ except KeyError :
536+ pass
537+ not_None = (ch for ch in range (256 )
538+ if widths [ch ] is not None )
539+ firstchar = not_None .next ()
540+ lastchar = max (not_None )
541+ widths = widths [firstchar :lastchar + 1 ]
542+
543+ differencesArray = [ firstchar ]
544+ for ch in range (firstchar , lastchar + 1 ):
545+ differencesArray .append (Name (
546+ afmdata .get_name_char (ch , isord = True )))
525547
526548 fontdict = {
527549 'Type' : Name ('Font' ),
@@ -533,16 +555,15 @@ def embedType1(self, filename, fontinfo):
533555 'FontDescriptor' : fontdescObject ,
534556 }
535557
536- if fontinfo .encodingfile is not None :
537- fontdict .update ({
538- 'Encoding' : { 'Type' : Name ('Encoding' ),
539- 'Differences' : differencesArray },
540- })
558+ fontdict .update ({
559+ 'Encoding' : { 'Type' : Name ('Encoding' ),
560+ 'Differences' : differencesArray },
561+ })
541562
542563 flags = 0
543564 if fixed_pitch : flags |= 1 << 0 # fixed width
544565 if 0 : flags |= 1 << 1 # TODO: serif
545- if 0 : flags |= 1 << 2 # TODO: symbolic
566+ if 1 : flags |= 1 << 2 # TODO: symbolic
546567 else : flags |= 1 << 5 # non-symbolic
547568 if italic_angle : flags |= 1 << 6 # italic
548569 if 0 : flags |= 1 << 16 # TODO: all caps
@@ -557,12 +578,16 @@ def embedType1(self, filename, fontinfo):
557578 'ItalicAngle' : italic_angle ,
558579 'Ascent' : font .ascender ,
559580 'Descent' : font .descender ,
560- 'CapHeight' : afmdata . get_capheight (),
581+ 'CapHeight' : 1000 , # default guess if missing from AFM file
561582 'XHeight' : afmdata .get_xheight (),
562583 'FontFile' : fontfileObject ,
563584 'FontFamily' : Name (familyname ),
564585 #'FontWeight': a number where 400 = Regular, 700 = Bold
565586 }
587+ try :
588+ descriptor ['CapHeight' ] = afmdata .get_capheight ()
589+ except KeyError :
590+ pass
566591
567592 # StemV is obligatory in PDF font descriptors but optional in
568593 # AFM files. The collection of AFM files in my TeX Live 2007
@@ -579,7 +604,7 @@ def embedType1(self, filename, fontinfo):
579604 descriptor ['StemH' ] = StemH
580605
581606 self .writeObject (fontdictObject , fontdict )
582- self .writeObject (widthsObject , [ 100 for i in range ( 256 )]) # XXX TODO
607+ self .writeObject (widthsObject , widths )
583608 self .writeObject (fontdescObject , descriptor )
584609
585610 fontdata = type1font .Type1Font (filename )
@@ -591,6 +616,8 @@ def embedType1(self, filename, fontinfo):
591616 self .currentstream .write (fontdata .data )
592617 self .endStream ()
593618
619+ return fontdictObject
620+
594621 def _get_xobject_symbol_name (self , filename , symbol_name ):
595622 return "%s-%s" % (
596623 os .path .splitext (os .path .basename (filename ))[0 ],
0 commit comments