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

Skip to content

Commit d65d339

Browse files
committed
Beginnings of usetex support for pdf backend.
See backend_pdf.py for caveats and how to enable. svn path=/trunk/matplotlib/; revision=3193
1 parent b910468 commit d65d339

4 files changed

Lines changed: 375 additions & 1 deletion

File tree

API_CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
The file dviread.py has a (very limited and fragile) dvi reader
2+
for usetex support. The API might change in the future so don't
3+
depend on it yet.
4+
15
Removed deprecated support for a float value as a gray-scale;
26
now it must be a string, like '0.5'. Added alpha kwarg to
37
ColorConverter.to_rgba_list.

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2007-04-09 Beginnings of usetex support for pdf backend. -JKS
2+
13
2007-04-07 Fixed legend/LineCollection bug. Added label support
24
to collections. - EF
35

lib/matplotlib/backends/backend_pdf.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
from datetime import datetime
1717
from math import ceil, cos, floor, pi, sin
1818

19-
from matplotlib import __version__, rcParams, agg
19+
from matplotlib import __version__, rcParams, agg, get_data_path
2020
from matplotlib._pylab_helpers import Gcf
2121
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
2222
FigureManagerBase, FigureCanvasBase
2323
from matplotlib.cbook import Bunch, enumerate, is_string_like, reverse_dict
2424
from matplotlib.figure import Figure
2525
from matplotlib.font_manager import fontManager
2626
from matplotlib.afm import AFM
27+
from matplotlib.dviread import Dvi
2728
from matplotlib.ft2font import FT2Font, FIXED_WIDTH, ITALIC, LOAD_NO_SCALE
2829
from matplotlib.mathtext import math_parse_s_pdf
2930
from matplotlib.numerix import Float32, UInt8, fromstring, arange, infinity, isnan, asarray
@@ -313,6 +314,7 @@ class PdfFile:
313314
"""PDF file with one page."""
314315

315316
def __init__(self, width, height, filename):
317+
self.width, self.height = width, height
316318
self.nextObject = 1 # next free object id
317319
self.xrefTable = [ [0, 65535, 'the zero object'] ]
318320
fh = file(filename, 'wb')
@@ -832,13 +834,16 @@ def writeTrailer(self):
832834
class RendererPdf(RendererBase):
833835

834836
def __init__(self, file):
837+
RendererBase.__init__(self)
835838
self.file = file
836839
self.gc = self.new_gc()
837840
self.truetype_font_cache = {}
838841
self.afm_font_cache = {}
839842

840843
def finalize(self):
841844
self.gc.finalize()
845+
del self.truetype_font_cache
846+
del self.afm_font_cache
842847

843848
def check_gc(self, gc, fillcolor=None):
844849
orig_fill = gc._fillcolor
@@ -1031,6 +1036,58 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
10311036
self.file.output(string, Op.show)
10321037
self.file.output(Op.end_text)
10331038

1039+
def _draw_tex(self, gc, x, y, s, prop, angle):
1040+
# Rename to draw_tex to enable, but note the following:
1041+
# TODO:
1042+
# - font sizes other than 10pt
1043+
# - fonts other than the three ttf files included with matplotlib
1044+
# (will need to support Type-1 fonts and find them with kpsewhich)
1045+
# - encoding issues (e.g. \alpha doesn't work now)
1046+
# - overall robustness
1047+
# - ...
1048+
texmanager = self.get_texmanager()
1049+
fontsize = prop.get_size_in_points()
1050+
dvifile = texmanager.make_dvi(s, fontsize)
1051+
dvi = Dvi(dvifile)
1052+
dvi.read()
1053+
text, boxes = dvi.output(72)
1054+
fontdir = os.path.join(get_data_path(), 'fonts', 'ttf')
1055+
1056+
if angle == 0: # avoid rounding errors in common case
1057+
def mytrans(x1, y1):
1058+
return x+x1, y+y1
1059+
else:
1060+
def mytrans(x1, y1, x=x, y=y, a=angle / 180.0 * pi):
1061+
x1 = x + cos(a)*x1 - sin(a)*y1
1062+
y1 = y + sin(a)*x1 + cos(a)*y1
1063+
return x1, y1
1064+
1065+
self.check_gc(gc, gc._rgb)
1066+
self.file.output(Op.begin_text)
1067+
oldfont, oldx, oldy = None, 0, 0
1068+
for x1, y1, font, glyph in text:
1069+
if font != oldfont:
1070+
fontname, fontsize = dvi.fontinfo(font)
1071+
fontfile = os.path.join(fontdir, fontname+'.ttf')
1072+
self.file.output(self.file.fontName(fontfile),
1073+
fontsize, Op.selectfont)
1074+
oldfont = font
1075+
x1, y1 = mytrans(x1, y1)
1076+
self._setup_textpos(x1, y1, angle, oldx, oldy)
1077+
self.file.output(chr(glyph), Op.show)
1078+
oldx, oldy = x1, y1
1079+
self.file.output(Op.end_text)
1080+
1081+
boxgc = self.new_gc()
1082+
boxgc.copy_properties(gc)
1083+
boxgc.set_linewidth(0)
1084+
for x1, y1, h, w in boxes:
1085+
(x1, y1), (x2, y2), (x3, y3), (x4, y4) = \
1086+
mytrans(x1, y1), mytrans(x1+w, y1), \
1087+
mytrans(x1+w, y1+h), mytrans(x1, y1+h)
1088+
self.draw_polygon(boxgc, gc._rgb,
1089+
((x1,y1), (x2,y2), (x3,y3), (x4,y4)))
1090+
10341091
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
10351092
# TODO: combine consecutive texts into one BT/ET delimited section
10361093

0 commit comments

Comments
 (0)