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

Skip to content

Commit ca0c0f9

Browse files
committed
Support draw_path (importantly for ellipses) in Pdf, Svg and Cairo
backends. Fix SVG text rendering bug. svn path=/trunk/matplotlib/; revision=4686
1 parent 78825c8 commit ca0c0f9

4 files changed

Lines changed: 105 additions & 7 deletions

File tree

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2007-12-10 Fix SVG text rendering bug.
2+
3+
2007-12-10 Increase accuracy of circle and ellipse drawing by using an 8-piece
4+
bezier approximation, rather than a 4-piece one. Fix PDF, SVG and
5+
Cairo backends so they can draw paths (meaning ellipses as well).
6+
17
2007-12-07 Issue a warning when drawing an image on a non-linear axis. - MGD
28

39
2007-12-06 let widgets.Cursor initialize to the lower x and y bounds

lib/matplotlib/backends/backend_cairo.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
3434
backend_version = cairo.version
3535
del _version_required
3636

37+
from matplotlib import agg
3738
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
3839
FigureManagerBase, FigureCanvasBase
3940
from matplotlib.cbook import enumerate, izip, is_string_like
@@ -124,6 +125,34 @@ def _fill_and_stroke (self, ctx, fill_c):
124125

125126
#_.ctx.restore() # revert to the default attributes
126127

128+
def draw_path(self, gc, rgbFace, path):
129+
ctx = gc.ctx
130+
ctx.new_path()
131+
132+
while 1:
133+
code, xp, yp = path.vertex()
134+
yp = self.height - yp
135+
136+
if code == agg.path_cmd_stop:
137+
ctx.close_path()
138+
break
139+
elif code == agg.path_cmd_move_to:
140+
ctx.move_to(xp, yp)
141+
elif code == agg.path_cmd_line_to:
142+
ctx.line_to(xp, yp)
143+
elif code == agg.path_cmd_curve3:
144+
_, xp1, yp1 = path.vertex()
145+
yp1 = self.height - yp1
146+
ctx.curve_to(xp, yp, xp, yp, xp1, yp1)
147+
elif code == agg.path_cmd_curve4:
148+
_, xp1, yp1 = path.vertex()
149+
yp1 = self.height - yp1
150+
_, xp2, yp2 = path.vertex()
151+
yp2 = self.height - yp2
152+
ctx.curve_to(xp, yp, xp1, yp1, xp2, yp2)
153+
elif code == agg.path_cmd_end_poly:
154+
ctx.close_path()
155+
self._fill_and_stroke(ctx, rgbFace)
127156

128157
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2,
129158
rotation):
@@ -307,11 +336,11 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
307336
ctx.translate(x, y)
308337
if angle:
309338
ctx.rotate (-angle * npy.pi / 180)
310-
339+
311340
for font, fontsize, s, ox, oy in glyphs:
312341
ctx.new_path()
313342
ctx.move_to(ox, oy)
314-
343+
315344
fontProp = ttfFontProperty(font)
316345
ctx.save()
317346
ctx.select_font_face (fontProp.name,
@@ -332,7 +361,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
332361

333362
ctx.restore()
334363

335-
364+
336365
def flipy(self):
337366
if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name())
338367
return True
@@ -494,7 +523,7 @@ def print_png(self, fobj, *args, **kwargs):
494523

495524
self.figure.draw (renderer)
496525
surface.write_to_png (fobj)
497-
526+
498527
def print_pdf(self, fobj, *args, **kwargs):
499528
return self._save(fobj, 'pdf', *args, **kwargs)
500529

@@ -506,10 +535,10 @@ def print_svg(self, fobj, *args, **kwargs):
506535

507536
def print_svgz(self, fobj, *args, **kwargs):
508537
return self._save(fobj, 'svgz', *args, **kwargs)
509-
538+
510539
def get_default_filetype(self):
511540
return rcParams['cairo.format']
512-
541+
513542
def _save (self, fo, format, **kwargs):
514543
# save PDF/PS/SVG
515544
orientation = kwargs.get('orientation', 'portrait')

lib/matplotlib/backends/backend_pdf.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,36 @@ def arc_to_bezier(cx, cy, rx, ry, angle1, sweep, rotation):
12491249

12501250
self.file.output(self.gc.close_and_paint())
12511251

1252+
def draw_path(self, gc, rgbFace, path):
1253+
self.check_gc(gc, rgbFace)
1254+
1255+
cmds = []
1256+
1257+
while 1:
1258+
code, xp, yp = path.vertex()
1259+
1260+
if code == agg.path_cmd_stop:
1261+
cmds.append(Op.closepath)
1262+
break
1263+
elif code == agg.path_cmd_move_to:
1264+
cmds.extend([xp, yp, Op.moveto])
1265+
elif code == agg.path_cmd_line_to:
1266+
cmds.extend([xp, yp, Op.lineto])
1267+
elif code == agg.path_cmd_curve3:
1268+
cmds.extend([xp, yp])
1269+
cmds.extend([xp, yp])
1270+
cmds.extend(path.vertex()[1:])
1271+
cmds.append(Op.curveto)
1272+
elif code == agg.path_cmd_curve4:
1273+
cmds.extend([xp, yp])
1274+
cmds.extend(path.vertex()[1:])
1275+
cmds.extend(path.vertex()[1:])
1276+
cmds.append(Op.curveto)
1277+
elif code == agg.path_cmd_end_poly:
1278+
cmds.append(Op.closepath)
1279+
self.file.output(*cmds)
1280+
self.file.output(self.gc.paint())
1281+
12521282
def get_image_magnification(self):
12531283
return self.image_magnification
12541284

lib/matplotlib/backends/backend_svg.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os, codecs, base64, tempfile, urllib, gzip
44

5+
from matplotlib import agg
56
from matplotlib import verbose, __version__, rcParams
67
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
78
FigureManagerBase, FigureCanvasBase
@@ -137,6 +138,38 @@ def open_group(self, s):
137138
def close_group(self, s):
138139
self._svgwriter.write('</g>\n')
139140

141+
def draw_path(self, gc, rgbFace, path):
142+
cmd = []
143+
144+
while 1:
145+
code, xp, yp = path.vertex()
146+
yp = self.height - yp
147+
148+
if code == agg.path_cmd_stop:
149+
cmd.append('z') # Hack, path_cmd_end_poly not found
150+
break
151+
elif code == agg.path_cmd_move_to:
152+
cmd.append('M%g %g' % (xp, yp))
153+
elif code == agg.path_cmd_line_to:
154+
cmd.append('L%g %g' % (xp, yp))
155+
elif code == agg.path_cmd_curve3:
156+
verts = [xp, yp]
157+
verts.extent(path.vertex()[1:])
158+
verts[-1] = self.height - verts[-1]
159+
cmd.append('Q%g %g %g %g' % tuple(verts))
160+
elif code == agg.path_cmd_curve4:
161+
verts = [xp, yp]
162+
verts.extend(path.vertex()[1:])
163+
verts[-1] = self.height - verts[-1]
164+
verts.extend(path.vertex()[1:])
165+
verts[-1] = self.height - verts[-1]
166+
cmd.append('C%g %g %g %g %g %g'%tuple(verts))
167+
elif code == agg.path_cmd_end_poly:
168+
cmd.append('z')
169+
170+
path_data = "".join(cmd)
171+
self._draw_svg_element("path", 'd="%s"' % path_data, gc, rgbFace)
172+
140173
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2, rotation):
141174
"""
142175
Ignores angles for now
@@ -284,7 +317,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
284317
svg.append(' transform="translate(%s)"' %
285318
(currx * (self.FONT_SCALE / fontsize)))
286319
svg.append('/>\n')
287-
currx += (glyph.linearHoriAdvance / 65536.0)
320+
currx += (glyph.linearHoriAdvance / 65536.0) / (self.FONT_SCALE / fontsize)
288321
svg.append('</g>\n')
289322
svg = ''.join(svg)
290323
else:

0 commit comments

Comments
 (0)