@@ -279,6 +279,8 @@ def __init__(self, width, height, svgwriter, basename=None):
279279 self ._write_default_style ()
280280
281281 def finalize (self ):
282+ self ._write_clips ()
283+ self ._write_hatches ()
282284 self ._write_svgfonts ()
283285 self .writer .close (self ._start_id )
284286
@@ -321,26 +323,35 @@ def _get_hatch(self, gc, rgbFace):
321323 """
322324 Create a new hatch pattern
323325 """
324- writer = self .writer
325- HATCH_SIZE = 72
326326 dictkey = (gc .get_hatch (), rgbFace , gc .get_rgb ())
327327 oid = self ._hatchd .get (dictkey )
328328 if oid is None :
329329 oid = self ._make_id ('h' , dictkey )
330- writer .start ('defs' )
330+ self ._hatchd [dictkey ] = ((gc .get_hatch_path (), rgbFace , gc .get_rgb ()), oid )
331+ else :
332+ _ , oid = oid
333+ return oid
334+
335+ def _write_hatches (self ):
336+ if not len (self ._hatchd ):
337+ return
338+ HATCH_SIZE = 72
339+ writer = self .writer
340+ writer .start ('defs' )
341+ for ((path , face , stroke ), oid ) in self ._hatchd .values ():
331342 writer .start (
332343 'pattern' ,
333344 id = oid ,
334345 patternUnits = "userSpaceOnUse" ,
335346 x = "0" , y = "0" , width = str (HATCH_SIZE ), height = str (HATCH_SIZE ))
336347 path_data = self ._convert_path (
337- gc . get_hatch_path () ,
348+ path ,
338349 Affine2D ().scale (HATCH_SIZE ).scale (1.0 , - 1.0 ).translate (0 , HATCH_SIZE ),
339350 simplify = False )
340- if rgbFace is None :
351+ if face is None :
341352 fill = 'none'
342353 else :
343- fill = rgb2hex (rgbFace )
354+ fill = rgb2hex (face )
344355 writer .element (
345356 'rect' ,
346357 x = "0" , y = "0" , width = str (HATCH_SIZE + 1 ), height = str (HATCH_SIZE + 1 ),
@@ -349,17 +360,15 @@ def _get_hatch(self, gc, rgbFace):
349360 'path' ,
350361 d = path_data ,
351362 style = generate_css ({
352- 'fill' : rgb2hex (gc . get_rgb () ),
353- 'stroke' : rgb2hex (gc . get_rgb () ),
363+ 'fill' : rgb2hex (stroke ),
364+ 'stroke' : rgb2hex (stroke ),
354365 'stroke-width' : str (1.0 ),
355366 'stroke-linecap' : 'butt' ,
356367 'stroke-linejoin' : 'miter'
357368 })
358369 )
359370 writer .end ('pattern' )
360- writer .end ('defs' )
361- self ._hatchd [dictkey ] = oid
362- return oid
371+ writer .end ('defs' )
363372
364373 def _get_style (self , gc , rgbFace ):
365374 """
@@ -409,22 +418,34 @@ def _get_clip(self, gc):
409418 else :
410419 return None
411420
412- oid = self ._clipd .get (dictkey )
413- if oid is None :
414- writer = self .writer
421+ clip = self ._clipd .get (dictkey )
422+ if clip is None :
415423 oid = self ._make_id ('p' , dictkey )
416- writer .start ('defs' )
417- writer .start ('clipPath' , id = oid )
418424 if clippath is not None :
425+ self ._clipd [dictkey ] = ((clippath , clippath_trans ), oid )
426+ else :
427+ self ._clipd [dictkey ] = (dictkey , oid )
428+ else :
429+ clip , oid = clip
430+ return oid
431+
432+ def _write_clips (self ):
433+ if not len (self ._clipd ):
434+ return
435+ writer = self .writer
436+ writer .start ('defs' )
437+ for clip , oid in self ._clipd .values ():
438+ writer .start ('clipPath' , id = oid )
439+ if len (clip ) == 2 :
440+ clippath , clippath_trans = clip
419441 path_data = self ._convert_path (clippath , clippath_trans , simplify = False )
420442 writer .element ('path' , d = path_data )
421443 else :
444+ x , y , w , h = clip
422445 writer .element ('rect' , x = str (x ), y = str (y ), width = str (w ), height = str (h ))
423446 writer .end ('clipPath' )
424- writer .end ('defs' )
425- self ._clipd [dictkey ] = oid
426- return oid
427-
447+ writer .end ('defs' )
448+
428449 def _write_svgfonts (self ):
429450 if not rcParams ['svg.fonttype' ] == 'svgfont' :
430451 return
0 commit comments