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

Skip to content

Commit 0216dd2

Browse files
committed
Merged revisions 3824-3830 via svnmerge from
http://matplotlib.svn.sf.net/svnroot/matplotlib/trunk/matplotlib ........ r3825 | pkienzle | 2007-09-10 14:45:32 -0400 (Mon, 10 Sep 2007) | 1 line Windows needs binary open flag for font files ........ r3827 | jouni | 2007-09-10 16:31:01 -0400 (Mon, 10 Sep 2007) | 2 lines Better bounding boxes for pdf usetex, including descents. ........ r3828 | jouni | 2007-09-10 16:55:29 -0400 (Mon, 10 Sep 2007) | 2 lines Bugfixes in pdf usetex ........ r3829 | mdboom | 2007-09-11 08:46:27 -0400 (Tue, 11 Sep 2007) | 3 lines Fix bug in PDF clip routine that resulted in the cryptic error message "There are too many arguments" in Adobe Acrobat. ........ svn path=/branches/transforms/; revision=3831
1 parent 8ea0875 commit 0216dd2

3 files changed

Lines changed: 55 additions & 36 deletions

File tree

lib/matplotlib/backends/backend_pdf.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,13 @@ def embedType1(self, filename, fontinfo):
519519
ul_position, ul_thickness = font.get_ps_font_info()
520520

521521
if fontinfo.encodingfile is not None:
522-
differencesArray = [ Name(ch) for ch in
523-
dviread.Encoding(fontinfo.encodingfile) ]
522+
enc = dviread.Encoding(fontinfo.encodingfile)
523+
widths = [ afmdata.get_width_from_char_name(ch)
524+
for ch in enc ]
525+
differencesArray = [ Name(ch) for ch in enc ]
524526
differencesArray = [ 0 ] + differencesArray
525527
firstchar = 0
526528
lastchar = len(differencesArray) - 2
527-
widths = [ 100 for x in range(firstchar,lastchar+1) ] # XXX TODO
528529
else:
529530
widths = [ None for i in range(256) ]
530531
for ch in range(256):
@@ -1434,7 +1435,7 @@ def _draw_tex(self, gc, x, y, s, prop, angle):
14341435
fontsize = prop.get_size_in_points()
14351436
dvifile = texmanager.make_dvi(s, fontsize)
14361437
dvi = dviread.Dvi(dvifile, 72)
1437-
text, boxes = iter(dvi).next()
1438+
page = iter(dvi).next()
14381439
dvi.close()
14391440

14401441
if angle == 0: # avoid rounding errors in common case
@@ -1448,7 +1449,7 @@ def mytrans(x1, y1, x=x, y=y, a=angle / 180.0 * pi):
14481449
# Gather font information and do some setup for combining
14491450
# characters into strings.
14501451
oldfontnum, seq = None, []
1451-
for x1, y1, fontnum, glyph, width in text:
1452+
for x1, y1, fontnum, glyph, width in page.text:
14521453
if fontnum != oldfontnum:
14531454
texname, fontsize = dvi.fontinfo(fontnum)
14541455
fontinfo = self.tex_font_mapping(texname)
@@ -1462,8 +1463,8 @@ def mytrans(x1, y1, x=x, y=y, a=angle / 180.0 * pi):
14621463
seq += [('end',)]
14631464

14641465
# Find consecutive text strings with constant x coordinate and
1465-
# combine into one string (if needed kern would be less than
1466-
# 0.1 points) or several strings interspersed with kerns.
1466+
# combine into a sequence of strings and kerns, or just one
1467+
# string (if any kerns would be less than 0.1 points).
14671468
i, curx = 0, 0
14681469
while i < len(seq)-1:
14691470
elt, next = seq[i:i+2]
@@ -1503,7 +1504,7 @@ def mytrans(x1, y1, x=x, y=y, a=angle / 180.0 * pi):
15031504
boxgc = self.new_gc()
15041505
boxgc.copy_properties(gc)
15051506
boxgc.set_linewidth(0)
1506-
for x1, y1, h, w in boxes:
1507+
for x1, y1, h, w in page.boxes:
15071508
(x1, y1), (x2, y2), (x3, y3), (x4, y4) = \
15081509
mytrans(x1, y1), mytrans(x1+w, y1), \
15091510
mytrans(x1+w, y1+h), mytrans(x1, y1+h)
@@ -1653,14 +1654,9 @@ def get_text_width_height_descent(self, s, prop, ismath):
16531654
fontsize = prop.get_size_in_points()
16541655
dvifile = texmanager.make_dvi(s, fontsize)
16551656
dvi = dviread.Dvi(dvifile, 72)
1656-
text, boxes = iter(dvi).next()
1657-
# TODO: better bounding box -- this is not quite right:
1658-
l = min(p[0] for p in text+boxes)
1659-
r = max(p[0] for p in text+boxes) + fontsize
1660-
b = min(p[1] for p in text+boxes)
1661-
t = max(p[1] for p in text+boxes) + fontsize
1662-
# (not to even mention finding the baseline)
1663-
return r-l, t-b, t-b
1657+
page = iter(dvi).next()
1658+
dvi.close()
1659+
return page.width, page.height, page.descent
16641660
if ismath:
16651661
w, h, d, glyphs, rects, used_characters = \
16661662
self.mathtext_parser.parse(s, 72, prop)
@@ -1837,7 +1833,7 @@ def clip_cmd(self, cliprect, clippath):
18371833
cmds.extend(self.pop())
18381834
# Unless we hit the right one, set the clip polygon
18391835
if (self._cliprect, self._clippath) != (cliprect, clippath):
1840-
cmds.append(self.push())
1836+
cmds.extend(self.push())
18411837
if self._cliprect != cliprect:
18421838
cmds.extend([t for t in cliprect] +
18431839
[Op.rectangle, Op.clip, Op.endpath])

lib/matplotlib/dviread.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
Interface:
77
88
dvi = Dvi(filename, 72)
9-
for text, boxes in dvi: # iterate over pages
10-
text, boxes = dvi.output(72)
11-
for x,y,font,glyph,width in text:
9+
for page in dvi: # iterate over pages
10+
w, h, d = page.width, page.height, page.descent
11+
for x,y,font,glyph,width in page.text:
1212
fontname, pointsize = dvi.fontinfo(font)
1313
...
14-
for x,y,height,width in boxes:
14+
for x,y,height,width in page.boxes:
1515
...
1616
"""
1717

18-
# TODO: support for TeX virtual fonts (*.vf) which are a dvi-like format
18+
# TODO: support TeX virtual fonts (*.vf) which are a sort of
19+
# subroutine collections for dvi files
1920

2021
import matplotlib
2122
import matplotlib.cbook as mpl_cbook
23+
import numpy as npy
2224
import os
2325
import struct
2426

@@ -74,20 +76,33 @@ def close(self):
7476
def _output(self):
7577
"""
7678
Output the text and boxes belonging to the most recent page.
77-
text, boxes = dvi._output()
79+
page = dvi._output()
7880
"""
79-
t0 = self.text[0]
80-
minx, miny, maxx, maxy = t0[0], t0[1], t0[0], t0[1]
81+
minx, miny, maxx, maxy = npy.inf, npy.inf, -npy.inf, -npy.inf
82+
maxy_pure = -npy.inf
8183
for elt in self.text + self.boxes:
82-
x,y = elt[:2]
83-
if x < minx: minx = x
84-
if y < miny: miny = y
85-
if x > maxx: maxx = x
86-
if y > maxy: maxy = y
84+
if len(elt) == 4: # box
85+
x,y,h,w = elt
86+
e = 0 # zero depth
87+
else: # glyph
88+
x,y,f,g,w = elt
89+
font = self.fonts[f]
90+
h = (font.scale * font.tfm.height[g]) >> 20
91+
e = (font.scale * font.tfm.depth[g]) >> 20
92+
minx = min(minx, x)
93+
miny = min(miny, y - h)
94+
maxx = max(maxx, x + w)
95+
maxy = max(maxy, y + e)
96+
maxy_pure = max(maxy_pure, y)
97+
8798
d = self.dpi / (72.27 * 2**16) # from TeX's "scaled points" to dpi units
8899
text = [ ((x-minx)*d, (maxy-y)*d, f, g, w*d) for (x,y,f,g,w) in self.text ]
89100
boxes = [ ((x-minx)*d, (maxy-y)*d, h*d, w*d) for (x,y,h,w) in self.boxes ]
90-
return text, boxes
101+
102+
return mpl_cbook.Bunch(text=text, boxes=boxes,
103+
width=(maxx-minx)*d,
104+
height=(maxy_pure-miny)*d,
105+
descent=(maxy-maxy_pure)*d)
91106

92107
def fontinfo(self, f):
93108
"""
@@ -361,28 +376,36 @@ class Tfm(object):
361376
width[i]: width of character #i, needs to be scaled
362377
by the factor specified in the dvi file
363378
(this is a dict because indexing may not start from 0)
379+
height[i], depth[i]: height and depth of character #i
364380
"""
365381

366382
def __init__(self, filename):
367383
file = open(filename, 'rb')
368384

369385
try:
370386
header1 = file.read(24)
371-
lh, bc, ec, nw = \
372-
struct.unpack('!4H', header1[2:10])
387+
lh, bc, ec, nw, nh, nd = \
388+
struct.unpack('!6H', header1[2:14])
373389
header2 = file.read(4*lh)
374390
self.checksum, self.design_size = \
375391
struct.unpack('!2I', header2[:8])
376392
# there is also encoding information etc.
377393
char_info = file.read(4*(ec-bc+1))
378394
widths = file.read(4*nw)
395+
heights = file.read(4*nh)
396+
depths = file.read(4*nd)
379397
finally:
380398
file.close()
381399

382-
widths = struct.unpack('!%dI' % nw, widths)
383-
self.width = {}
400+
self.width, self.height, self.depth = {}, {}, {}
401+
widths, heights, depths = \
402+
[ struct.unpack('!%dI' % n, x)
403+
for n,x in [(nw, widths), (nh, heights), (nd, depths)] ]
384404
for i in range(ec-bc):
385405
self.width[bc+i] = widths[ord(char_info[4*i])]
406+
self.height[bc+i] = heights[ord(char_info[4*i+1]) >> 4]
407+
self.depth[bc+i] = depths[ord(char_info[4*i+1]) & 0xf]
408+
386409

387410
class PsfontsMap(object):
388411
"""

ttconv/pprdrv_tt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ void read_font(const char *filename, font_type_enum target_type, std::vector<int
10881088
font.filename=filename;
10891089

10901090
/* Open the font file */
1091-
if( (font.file = fopen(filename,"r")) == (FILE*)NULL )
1091+
if( (font.file = fopen(filename,"rb")) == (FILE*)NULL )
10921092
throw TTException("Failed to open TrueType font");
10931093

10941094
/* Allocate space for the unvarying part of the offset table. */

0 commit comments

Comments
 (0)