|
16 | 16 | from datetime import datetime |
17 | 17 | from math import ceil, cos, floor, pi, sin |
18 | 18 |
|
19 | | -from matplotlib import __version__, rcParams, agg |
| 19 | +from matplotlib import __version__, rcParams, agg, get_data_path |
20 | 20 | from matplotlib._pylab_helpers import Gcf |
21 | 21 | from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\ |
22 | 22 | FigureManagerBase, FigureCanvasBase |
23 | 23 | from matplotlib.cbook import Bunch, enumerate, is_string_like, reverse_dict |
24 | 24 | from matplotlib.figure import Figure |
25 | 25 | from matplotlib.font_manager import fontManager |
26 | 26 | from matplotlib.afm import AFM |
| 27 | +from matplotlib.dviread import Dvi |
27 | 28 | from matplotlib.ft2font import FT2Font, FIXED_WIDTH, ITALIC, LOAD_NO_SCALE |
28 | 29 | from matplotlib.mathtext import math_parse_s_pdf |
29 | 30 | from matplotlib.numerix import Float32, UInt8, fromstring, arange, infinity, isnan, asarray |
@@ -313,6 +314,7 @@ class PdfFile: |
313 | 314 | """PDF file with one page.""" |
314 | 315 |
|
315 | 316 | def __init__(self, width, height, filename): |
| 317 | + self.width, self.height = width, height |
316 | 318 | self.nextObject = 1 # next free object id |
317 | 319 | self.xrefTable = [ [0, 65535, 'the zero object'] ] |
318 | 320 | fh = file(filename, 'wb') |
@@ -832,13 +834,16 @@ def writeTrailer(self): |
832 | 834 | class RendererPdf(RendererBase): |
833 | 835 |
|
834 | 836 | def __init__(self, file): |
| 837 | + RendererBase.__init__(self) |
835 | 838 | self.file = file |
836 | 839 | self.gc = self.new_gc() |
837 | 840 | self.truetype_font_cache = {} |
838 | 841 | self.afm_font_cache = {} |
839 | 842 |
|
840 | 843 | def finalize(self): |
841 | 844 | self.gc.finalize() |
| 845 | + del self.truetype_font_cache |
| 846 | + del self.afm_font_cache |
842 | 847 |
|
843 | 848 | def check_gc(self, gc, fillcolor=None): |
844 | 849 | orig_fill = gc._fillcolor |
@@ -1031,6 +1036,58 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): |
1031 | 1036 | self.file.output(string, Op.show) |
1032 | 1037 | self.file.output(Op.end_text) |
1033 | 1038 |
|
| 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 | + |
1034 | 1091 | def draw_text(self, gc, x, y, s, prop, angle, ismath=False): |
1035 | 1092 | # TODO: combine consecutive texts into one BT/ET delimited section |
1036 | 1093 |
|
|
0 commit comments