Skip to content

Commit 7c152aa

Browse files
committed
Fix problem with (deep)copy of TextPath
The problem is that deepcopy of TextPath calls deepcopy of Path. In turn `Path` utilizes `super().__init__` for creating a new instance. However, `Path.__init__ and TextPath.__init__ are completely different. Moreover, `TextPath.__init__` converts some arguments directly to list of vertices, hence it is not possible to create a new instance of `TextPath` from its members. Additionally, there is a problem with caching. When copying, the vertices can be copied uncached, which causes discrepancies between members of copy and original members (recaching creates new members). Therefore validation of cache was removed and new vertices are generated at every size set.
1 parent b600026 commit 7c152aa

3 files changed

Lines changed: 49 additions & 15 deletions

File tree

‎lib/matplotlib/path.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def __deepcopy__(self, memo=None):
276276
codes = self.codes.copy()
277277
except AttributeError:
278278
codes = None
279-
return self.__class__(
279+
return Path(
280280
self.vertices.copy(), codes,
281281
_interpolation_steps=self._interpolation_steps)
282282

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from copy import copy, deepcopy
2+
3+
from numpy.testing import assert_array_equal
4+
from matplotlib.textpath import TextPath
5+
6+
7+
def test_set_size():
8+
path = TextPath((0, 0), ".")
9+
_size = path.get_size()
10+
verts = path.vertices.copy()
11+
codes = path.codes.copy()
12+
path.set_size(20)
13+
assert_array_equal(verts/_size*path.get_size(), path.vertices)
14+
assert_array_equal(codes, path.codes)
15+
16+
17+
def test_deepcopy():
18+
# Should not raise any error
19+
path = TextPath((0, 0), ".")
20+
path_copy = deepcopy(path)
21+
assert path is not path_copy
22+
assert path.vertices is not path_copy.vertices
23+
assert path.codes is not path_copy.codes
24+
25+
26+
def test_copy():
27+
# Should not raise any error
28+
path = TextPath((0, 0), ".")
29+
path_copy = copy(path)
30+
assert path is not path_copy
31+
assert path.vertices is path_copy.vertices
32+
assert path.codes is path_copy.codes
33+
path = TextPath((0, 0), ".")
34+
path_copy = path.copy()
35+
assert path is not path_copy
36+
assert path.vertices is path_copy.vertices
37+
assert path.codes is path_copy.codes

‎lib/matplotlib/textpath.py‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ def __init__(self, xy, s, size=None, prop=None,
381381
size = prop.get_size_in_points()
382382

383383
self._xy = xy
384-
self.set_size(size)
385384

386385
self._cached_vertices = None
387386
s, ismath = Text(usetex=usetex)._preprocess_math(s)
@@ -390,11 +389,12 @@ def __init__(self, xy, s, size=None, prop=None,
390389
_interpolation_steps=_interpolation_steps,
391390
readonly=True)
392391
self._should_simplify = False
392+
self.set_size(size)
393393

394394
def set_size(self, size):
395395
"""Set the text size."""
396396
self._size = size
397-
self._invalid = True
397+
self._recache_path()
398398

399399
def get_size(self):
400400
"""Get the text size."""
@@ -403,9 +403,8 @@ def get_size(self):
403403
@property
404404
def vertices(self):
405405
"""
406-
Return the cached path after updating it if necessary.
406+
Return the cached path.
407407
"""
408-
self._revalidate_path()
409408
return self._cached_vertices
410409

411410
@property
@@ -415,17 +414,15 @@ def codes(self):
415414
"""
416415
return self._codes
417416

418-
def _revalidate_path(self):
417+
def _recache_path(self):
419418
"""
420-
Update the path if necessary.
419+
Update the path.
421420
422-
The path for the text is initially create with the font size of
421+
The path for the text is initially created with the font size of
423422
`.FONT_SCALE`, and this path is rescaled to other size when necessary.
424423
"""
425-
if self._invalid or self._cached_vertices is None:
426-
tr = (Affine2D()
427-
.scale(self._size / text_to_path.FONT_SCALE)
428-
.translate(*self._xy))
429-
self._cached_vertices = tr.transform(self._vertices)
430-
self._cached_vertices.flags.writeable = False
431-
self._invalid = False
424+
tr = (Affine2D()
425+
.scale(self._size / text_to_path.FONT_SCALE)
426+
.translate(*self._xy))
427+
self._cached_vertices = tr.transform(self._vertices)
428+
self._cached_vertices.flags.writeable = False

0 commit comments

Comments
 (0)