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

Skip to content

Commit 314289e

Browse files
committed
Fix PDF font size bug.
svn path=/branches/transforms/; revision=4493
1 parent ff162fd commit 314289e

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def pdfRepr(obj):
175175
# A bounding box
176176
elif isinstance(obj, BboxBase):
177177
return fill([pdfRepr(val) for val in obj.bounds])
178-
178+
179179
else:
180180
raise TypeError, \
181181
"Don't know a PDF representation for %s objects." \
@@ -341,7 +341,7 @@ def __init__(self, width, height, dpi, filename):
341341
self.passed_in_file_object = True
342342
else:
343343
raise ValueError("filename must be a path or a file-like object")
344-
344+
345345
self.fh = fh
346346
self.currentstream = None # stream object to write to, if any
347347
fh.write("%PDF-1.4\n") # 1.4 is the first version to have alpha
@@ -530,7 +530,7 @@ def embedType1(self, filename, fontinfo):
530530

531531
firstchar = 0
532532
lastchar = len(fontinfo.widths) - 1
533-
533+
534534
fontdict = {
535535
'Type': Name('Font'),
536536
'Subtype': Name('Type1'),
@@ -575,7 +575,7 @@ def embedType1(self, filename, fontinfo):
575575
'XHeight': 500, # TODO: this one too
576576
'FontFile': fontfileObject,
577577
'FontFamily': familyname,
578-
'StemV': 50, # TODO
578+
'StemV': 50, # TODO
579579
# (see also revision 3874; but not all TeX distros have AFM files!)
580580
#'FontWeight': a number where 400 = Regular, 700 = Bold
581581
}
@@ -620,7 +620,7 @@ def _get_xobject_symbol_name(self, filename, symbol_name):
620620
CMapName currentdict /CMap defineresource pop
621621
end
622622
end"""
623-
623+
624624
def embedTTF(self, filename, characters):
625625
"""Embed the TTF font from the named file into the document."""
626626

@@ -833,7 +833,7 @@ def embedTTFType42(font, characters, descriptor):
833833
unicode_cmap = (self._identityToUnicodeCMap %
834834
(len(unicode_groups),
835835
"\n".join(unicode_bfrange)))
836-
836+
837837
# CIDToGIDMap stream
838838
cid_to_gid_map = "".join(cid_to_gid_map).encode("utf-16be")
839839
self.beginStream(cidToGidMapObject.id,
@@ -848,7 +848,7 @@ def embedTTFType42(font, characters, descriptor):
848848
{'Length': unicode_cmap})
849849
self.currentstream.write(unicode_cmap)
850850
self.endStream()
851-
851+
852852
descriptor['MaxWidth'] = max_width
853853

854854
# Write everything out
@@ -902,7 +902,7 @@ def embedTTFType42(font, characters, descriptor):
902902
warnings.warn(("'%s' can not be subsetted into a Type 3 font. " +
903903
"The entire font will be embedded in the output.") %
904904
os.path.basename(filename))
905-
905+
906906
if fonttype == 3:
907907
return embedTTFType3(font, characters, descriptor)
908908
elif fonttype == 42:
@@ -1054,7 +1054,7 @@ def writeImages(self):
10541054
'SMask': smaskObject})
10551055
self.currentstream.write(data) # TODO: predictors (i.e., output png)
10561056
self.endStream()
1057-
1057+
10581058
img.flipud_out()
10591059

10601060
def markerObject(self, path, trans, fillp, lw):
@@ -1068,7 +1068,7 @@ def markerObject(self, path, trans, fillp, lw):
10681068
else:
10691069
name = result[0]
10701070
return name
1071-
1071+
10721072
def writeMarkers(self):
10731073
for tup in self.markers.values():
10741074
name, object, path, trans, fillp, lw = tup
@@ -1088,7 +1088,7 @@ def writeMarkers(self):
10881088
#@staticmethod
10891089
def pathOperations(path, transform):
10901090
tpath = transform.transform_path(path)
1091-
1091+
10921092
cmds = []
10931093
for points, code in tpath.iter_segments():
10941094
if code == Path.MOVETO:
@@ -1109,11 +1109,11 @@ def pathOperations(path, transform):
11091109
cmds.append(Op.closepath)
11101110
return cmds
11111111
pathOperations = staticmethod(pathOperations)
1112-
1112+
11131113
def writePath(self, path, transform):
11141114
cmds = self.pathOperations(path, transform)
11151115
self.output(*cmds)
1116-
1116+
11171117
def reserveObject(self, name=''):
11181118
"""Reserve an ID for an indirect object.
11191119
The name is used for debugging in case we forget to print out
@@ -1249,7 +1249,7 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
12491249
marker, Op.use_xobject)
12501250
lastx, lasty = x, y
12511251
output(Op.grestore)
1252-
1252+
12531253
def _setup_textpos(self, x, y, angle, oldx=0, oldy=0, oldangle=0):
12541254
if angle == oldangle == 0:
12551255
self.file.output(x - oldx, y - oldy, Op.textpos)
@@ -1285,11 +1285,12 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
12851285
fonttype = 42
12861286
else:
12871287
fonttype = global_fonttype
1288-
1288+
12891289
if fonttype == 42 or num <= 255:
12901290
self._setup_textpos(ox, oy, 0, oldx, oldy)
12911291
oldx, oldy = ox, oy
12921292
if (fontname, fontsize) != prev_font:
1293+
fontsize *= self.dpi/72.0
12931294
self.file.output(self.file.fontName(fontname), fontsize,
12941295
Op.selectfont)
12951296
prev_font = fontname, fontsize
@@ -1300,11 +1301,12 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
13001301
# as XObjects using the 'Do' command.
13011302
if global_fonttype == 3:
13021303
for ox, oy, fontname, fontsize, num, symbol_name in glyphs:
1304+
fontsize *= self.dpi/72.0
13031305
if is_opentype_cff_font(fontname):
13041306
fonttype = 42
13051307
else:
13061308
fonttype = global_fonttype
1307-
1309+
13081310
if fonttype == 3 and num > 255:
13091311
self.file.output(Op.gsave,
13101312
0.001 * fontsize, 0,
@@ -1427,7 +1429,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
14271429
self.check_gc(gc, gc._rgb)
14281430
if ismath: return self.draw_mathtext(gc, x, y, s, prop, angle)
14291431

1430-
fontsize = prop.get_size_in_points()
1432+
fontsize = prop.get_size_in_points() * self.dpi/72.0
14311433

14321434
if rcParams['pdf.use14corefonts']:
14331435
font = self._get_font_afm(prop)
@@ -1441,12 +1443,12 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
14411443
y += font.get_descent() / 64.0
14421444

14431445
fonttype = rcParams['pdf.fonttype']
1444-
1446+
14451447
# We can't subset all OpenType fonts, so switch to Type 42
14461448
# in that case.
14471449
if is_opentype_cff_font(font.fname):
14481450
fonttype = 42
1449-
1451+
14501452
def check_simple_method(s):
14511453
"""Determine if we should use the simple or woven method
14521454
to output this text, and chunks the string into 1-byte and
@@ -1476,7 +1478,7 @@ def draw_text_simple():
14761478
"""Outputs text using the simple method."""
14771479
self.file.output(Op.begin_text,
14781480
self.file.fontName(prop),
1479-
prop.get_size_in_points(),
1481+
fontsize,
14801482
Op.selectfont)
14811483
self._setup_textpos(x, y, angle)
14821484
self.file.output(self.encode_string(s, fonttype), Op.show, Op.end_text)
@@ -1503,7 +1505,7 @@ def draw_text_woven(chunks):
15031505
if mode == 1:
15041506
self.file.output(Op.begin_text,
15051507
self.file.fontName(prop),
1506-
prop.get_size_in_points(),
1508+
fontsize,
15071509
Op.selectfont)
15081510

15091511
for chunk_type, chunk in chunks:
@@ -1566,18 +1568,19 @@ def get_text_width_height_descent(self, s, prop, ismath):
15661568
elif rcParams['pdf.use14corefonts']:
15671569
font = self._get_font_afm(prop)
15681570
l, b, w, h, d = font.get_str_bbox_and_descent(s)
1569-
scale = prop.get_size_in_points() / 1000.0
1571+
scale = prop.get_size_in_points()
15701572
w *= scale
15711573
h *= scale
15721574
d *= scale
15731575
else:
15741576
font = self._get_font_ttf(prop)
15751577
font.set_text(s, 0.0, flags=LOAD_NO_HINTING)
15761578
w, h = font.get_width_height()
1577-
w /= 64.0
1578-
h /= 64.0
1579+
scale = (1.0 / 64.0)
1580+
w *= scale
1581+
h *= scale
15791582
d = font.get_descent()
1580-
d /= 64.0
1583+
d *= scale
15811584
return w, h, d
15821585

15831586
def _get_font_afm(self, prop):
@@ -1780,7 +1783,7 @@ def delta(self, other):
17801783
different = ours.shape != theirs.shape or npy.any(ours != theirs)
17811784
if different:
17821785
break
1783-
1786+
17841787
if different:
17851788
theirs = [getattr(other, p) for p in params]
17861789
cmds.extend(cmd(self, *theirs))
@@ -1841,10 +1844,10 @@ def draw(self):
18411844
pass
18421845

18431846
filetypes = {'pdf': 'Portable Document Format'}
1844-
1847+
18451848
def get_default_filetype(self):
18461849
return 'pdf'
1847-
1850+
18481851
def print_pdf(self, filename, **kwargs):
18491852
dpi = kwargs.get('dpi', 72)
18501853
self.figure.set_dpi(dpi) # Override the dpi kwarg

0 commit comments

Comments
 (0)