diff --git a/lib/matplotlib/tests/test_textpath.py b/lib/matplotlib/tests/test_textpath.py new file mode 100644 index 000000000000..dcc3f80f4a4b --- /dev/null +++ b/lib/matplotlib/tests/test_textpath.py @@ -0,0 +1,43 @@ +from copy import copy, deepcopy + +from numpy.testing import assert_array_equal +from matplotlib.textpath import TextPath + + +def test_set_size(): + """Set size with zero offset should scale vertices and retain codes.""" + path = TextPath((0, 0), ".") + _size = path.get_size() + verts = path.vertices.copy() + codes = path.codes.copy() + path.set_size(20) + assert_array_equal(verts/_size*path.get_size(), path.vertices) + assert_array_equal(codes, path.codes) + + +def test_deepcopy(): + path = TextPath((0, 0), ".") + path_copy = deepcopy(path) + assert isinstance(path_copy, TextPath) + assert path is not path_copy + assert path.vertices is not path_copy.vertices + assert path.codes is not path_copy.codes + path = TextPath((0, 0), ".") + path_copy = path.deepcopy({}) + assert isinstance(path_copy, TextPath) + assert path is not path_copy + assert path.vertices is not path_copy.vertices + assert path.codes is not path_copy.codes + + +def test_copy(): + path = TextPath((0, 0), ".") + path_copy = copy(path) + assert path is not path_copy + assert path.vertices is path_copy.vertices + assert path.codes is path_copy.codes + path = TextPath((0, 0), ".") + path_copy = path.copy() + assert path is not path_copy + assert path.vertices is path_copy.vertices + assert path.codes is path_copy.codes diff --git a/lib/matplotlib/textpath.py b/lib/matplotlib/textpath.py index 9b14e79ec2d2..c46c3d2a3e1b 100644 --- a/lib/matplotlib/textpath.py +++ b/lib/matplotlib/textpath.py @@ -1,3 +1,4 @@ +from copy import deepcopy from collections import OrderedDict import functools import logging @@ -429,3 +430,23 @@ def _revalidate_path(self): self._cached_vertices = tr.transform(self._vertices) self._cached_vertices.flags.writeable = False self._invalid = False + + def __deepcopy__(self, memo): + """Update path and create deep copy.""" + self._revalidate_path() + cls = self.__class__ + new_instance = cls.__new__(cls) + memo[id(self)] = new_instance + for k, v in self.__dict__.items(): + setattr(new_instance, k, deepcopy(v, memo)) + return new_instance + + deepcopy = __deepcopy__ + + def __copy__(self): + """Update path and create shallow copy.""" + self._revalidate_path() + cls = self.__class__ + new_instance = cls.__new__(cls) + new_instance.__dict__.update(self.__dict__) + return new_instance