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

Skip to content

Commit 2a1891f

Browse files
committed
Add SVG tests. Fix a hatching bug found in the process of this.
svn path=/trunk/matplotlib/; revision=7875
1 parent 6c62ee6 commit 2a1891f

45 files changed

Lines changed: 61524 additions & 20 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/matplotlib/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,30 @@ def checkdep_pdftops():
301301
except (IndexError, ValueError, UnboundLocalError, OSError):
302302
return None
303303

304+
def checkdep_inkscape():
305+
try:
306+
s = subprocess.Popen(['inkscape','-V'], stdout=subprocess.PIPE,
307+
stderr=subprocess.PIPE)
308+
for line in s.stdout:
309+
if 'Inkscape' in line:
310+
v = line.split()[1]
311+
break
312+
return v
313+
except (IndexError, ValueError, UnboundLocalError, OSError):
314+
return None
315+
316+
def checkdep_xmllint():
317+
try:
318+
s = subprocess.Popen(['xmllint','--version'], stdout=subprocess.PIPE,
319+
stderr=subprocess.PIPE)
320+
for line in s.stderr:
321+
if 'version' in line:
322+
v = line.split()[-1]
323+
break
324+
return v
325+
except (IndexError, ValueError, UnboundLocalError, OSError):
326+
return None
327+
304328
def compare_versions(a, b):
305329
"return True if a is greater than or equal to b"
306330
if a:

lib/matplotlib/backends/backend_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ def draw_image(self, gc, x, y, im):
13441344

13451345
def draw_path(self, gc, path, transform, rgbFace=None):
13461346
self.check_gc(gc, rgbFace)
1347-
self.file.writePath(path, transform, rgbFace is None)
1347+
self.file.writePath(path, transform, (rgbFace is None and gc.get_hatch_path() is None))
13481348
self.file.output(self.gc.paint())
13491349

13501350
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):

lib/matplotlib/backends/backend_ps.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,9 @@ def draw_path(self, gc, path, transform, rgbFace=None):
471471
"""
472472
Draws a Path instance using the given affine transform.
473473
"""
474-
ps = self._convert_path(path, transform, clip=(rgbFace is None))
474+
ps = self._convert_path(
475+
path, transform,
476+
clip=(rgbFace is None and gc.get_hatch_path() is None))
475477
self._draw_ps(ps, gc, rgbFace)
476478

477479
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):

lib/matplotlib/backends/backend_svg.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self, width, height, svgwriter, basename=None):
6161

6262
RendererBase.__init__(self)
6363
self._glyph_map = dict()
64-
64+
6565
svgwriter.write(svgProlog%(width,height,width,height))
6666

6767
def _draw_svg_element(self, element, details, gc, rgbFace):
@@ -241,7 +241,8 @@ def _convert_path(self, path, transform, clip=False):
241241

242242
def draw_path(self, gc, path, transform, rgbFace=None):
243243
trans_and_flip = self._make_flip_transform(transform)
244-
path_data = self._convert_path(path, trans_and_flip, clip=(rgbFace is None))
244+
path_data = self._convert_path(path, trans_and_flip,
245+
clip=(rgbFace is None and gc.get_hatch_path() is None))
245246
self._draw_svg_element('path', 'd="%s"' % path_data, gc, rgbFace)
246247

247248
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
@@ -412,7 +413,7 @@ def draw_image(self, gc, x, y, im):
412413

413414
def _adjust_char_id(self, char_id):
414415
return char_id.replace("%20","_")
415-
416+
416417
def draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):
417418
"""
418419
draw the text by converting them to paths using textpath module.
@@ -422,26 +423,26 @@ def draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):
422423
423424
*s*
424425
text to be converted
425-
426+
426427
*usetex*
427428
If True, use matplotlib usetex mode.
428429
429430
*ismath*
430431
If True, use mathtext parser. If "TeX", use *usetex* mode.
431432
432-
433+
433434
"""
434435
# this method works for normal text, mathtext and usetex mode.
435436
# But currently only utilized by draw_tex method.
436-
437+
437438
glyph_map=self._glyph_map
438-
439+
439440
text2path = self._text2path
440441
color = rgb2hex(gc.get_rgb()[:3])
441442
fontsize = prop.get_size_in_points()
442443

443444
write = self._svgwriter.write
444-
445+
445446
if ismath == False:
446447
font = text2path._get_font(prop)
447448
_glyphs = text2path.get_glyphs_with_font(font, s, glyph_map=glyph_map,
@@ -460,7 +461,7 @@ def draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):
460461
write('</defs>\n')
461462

462463
glyph_map.update(glyph_map_new)
463-
464+
464465
svg = []
465466
clipid = self._get_gc_clip_svg(gc)
466467
if clipid is not None:
@@ -508,7 +509,7 @@ def draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):
508509
write('</defs>\n')
509510

510511
glyph_map.update(glyph_map_new)
511-
512+
512513
svg = []
513514
clipid = self._get_gc_clip_svg(gc)
514515
if clipid is not None:
@@ -800,7 +801,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
800801
w, h, d = texmanager.get_text_width_height_descent(s, fontsize,
801802
renderer=self)
802803
return w, h, d
803-
804+
804805
if ismath:
805806
width, height, descent, trash, used_characters = \
806807
self.mathtext_parser.parse(s, 72, prop)

lib/matplotlib/testing/compare.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,32 @@ def compare_float( expected, actual, relTol = None, absTol = None ):
9595
converter['pdf'] = cmd
9696
converter['eps'] = cmd
9797

98+
if matplotlib.checkdep_inkscape() is not None:
99+
cmd = lambda old, new: \
100+
['inkscape', old, '--export-png=' + new]
101+
converter['svg'] = cmd
102+
98103
def comparable_formats():
99104
'''Returns the list of file formats that compare_images can compare
100105
on this system.'''
101106
return ['png'] + converter.keys()
102107

103108
def convert(filename):
104-
'''Convert the named file into a png file.
109+
'''
110+
Convert the named file into a png file.
105111
Returns the name of the created file.
106112
'''
107113
base, extension = filename.rsplit('.', 1)
108114
if extension not in converter:
109115
raise ImageComparisonFailure, "Don't know how to convert %s files to png" % extension
110116
newname = base + '_' + extension + '.png'
117+
if not os.path.exists(filename):
118+
raise IOError, "'%s' does not exist" % filename
111119
cmd = converter[extension](filename, newname)
112120
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
113121
stdout, stderr = pipe.communicate()
114-
if not os.path.exists(newname):
122+
errcode = pipe.wait()
123+
if not os.path.exists(newname) or errcode:
115124
msg = "Conversion command failed:\n%s\n" % ' '.join(cmd)
116125
if stdout:
117126
msg += "Standard output:\n%s\n" % stdout
@@ -120,6 +129,33 @@ def convert(filename):
120129
raise IOError, msg
121130
return newname
122131

132+
verifiers = { }
133+
134+
def verify(filename):
135+
"""
136+
Verify the file through some sort of verification tool.
137+
"""
138+
if not os.path.exists(filename):
139+
raise IOError, "'%s' does not exist" % filename
140+
base, extension = filename.rsplit('.', 1)
141+
verifier = verifiers.get(extension, None)
142+
if verifier is not None:
143+
cmd = verifier(filename)
144+
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
145+
stdout, stderr = pipe.communicate()
146+
errcode = pipe.wait()
147+
if errcode != 0:
148+
msg = "File verification command failed:\n%s\n" % ' '.join(cmd)
149+
if stdout:
150+
msg += "Standard output:\n%s\n" % stdout
151+
if stderr:
152+
msg += "Standard error:\n%s\n" % stderr
153+
raise IOError, msg
154+
155+
if matplotlib.checkdep_xmllint():
156+
verifiers['svg'] = lambda filename: [
157+
'xmllint', '--valid', '--nowarning', '--noout', filename]
158+
123159
def compare_images( expected, actual, tol, in_decorator=False ):
124160
'''Compare two image files - not the greatest, but fast and good enough.
125161
@@ -134,7 +170,7 @@ def compare_images( expected, actual, tol, in_decorator=False ):
134170
- expected The filename of the expected image.
135171
- actual The filename of the actual image.
136172
- tol The tolerance (a unitless float). This is used to
137-
determinte the 'fuzziness' to use when comparing images.
173+
determine the 'fuzziness' to use when comparing images.
138174
- in_decorator If called from image_comparison decorator, this should be
139175
True. (default=False)
140176
'''
@@ -151,10 +187,12 @@ def compare_images( expected, actual, tol, in_decorator=False ):
151187
else:
152188
return msg
153189

190+
verify(actual)
191+
154192
# Convert the image to png
155193
extension = expected.split('.')[-1]
156194
if extension != 'png':
157-
expected, actual = convert(expected), convert(actual)
195+
actual, expected = convert(actual), convert(expected)
158196

159197
# open the image files and remove the alpha channel (if it exists)
160198
expectedImage = Image.open( expected ).convert("RGB")

lib/matplotlib/testing/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def image_comparison(baseline_images=None,extensions=None):
7474

7575
if extensions is None:
7676
# default extensions to test
77-
extensions = ['png', 'pdf']
77+
extensions = ['png', 'pdf', 'svg']
7878

7979
# The multiple layers of defs are required because of how
8080
# parameterized decorators work, and because we want to turn the

0 commit comments

Comments
 (0)