Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc4dcc0

Browse files
committed
More work on supporting Type 1 fonts in PDF,
still doesn't produce usable files. svn path=/trunk/matplotlib/; revision=3775
1 parent 69c654a commit dc4dcc0

4 files changed

Lines changed: 43 additions & 13 deletions

File tree

API_CHANGES

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
to read an afm file in addition to a pfa/pfb file, to get metrics
1616
and kerning information for a Type 1 font.
1717

18-
The AFM class now supports querying CapHeight and stem widths.
18+
The AFM class now supports querying CapHeight and stem widths. The
19+
get_name_char method now has an isord kwarg like get_width_char.
1920

2021
Changed pcolor default to shading='flat'; but as noted now in the
2122
docstring, it is preferable to simply use the edgecolor kwarg.

lib/matplotlib/afm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,12 @@ def get_str_bbox(self, s):
378378
"""
379379
return self.get_str_bbox_and_descent(s)[:4]
380380

381-
def get_name_char(self, c):
381+
def get_name_char(self, c, isord=False):
382382
"""
383383
Get the name of the character, ie, ';' is 'semicolon'
384384
"""
385-
wx, name, bbox = self._metrics[ord(c)]
385+
if not isord: c=ord(c)
386+
wx, name, bbox = self._metrics[c]
386387
return name
387388

388389
def get_width_char(self, c, isord=False):

lib/matplotlib/backends/backend_pdf.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from math import ceil, cos, floor, pi, sin
1919
from sets import Set
2020

21+
import matplotlib
2122
from matplotlib import __version__, rcParams, agg, get_data_path
2223
from matplotlib._pylab_helpers import Gcf
2324
from 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],

lib/matplotlib/dviread.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def __init__(self, filename, dpi):
3535
opens the file; actually reading the file happens when
3636
iterating through the pages of the file.
3737
"""
38+
matplotlib.verbose.report('Dvi: ' + filename, 'debug')
3839
self.file = open(filename, 'rb')
3940
self.dpi = dpi
4041
self.fonts = {}
@@ -57,7 +58,7 @@ def __iter__(self):
5758
while True:
5859
have_page = self._read()
5960
if have_page:
60-
yield self.text, self.boxes
61+
yield self._output()
6162
else:
6263
break
6364

0 commit comments

Comments
 (0)