|
10 | 10 | import math |
11 | 11 | import warnings |
12 | 12 |
|
| 13 | +import contextlib |
| 14 | + |
13 | 15 | import numpy as np |
14 | 16 |
|
15 | 17 | from matplotlib import cbook |
@@ -41,6 +43,21 @@ def _process_text_args(override, fontdict=None, **kwargs): |
41 | 43 | override.update(kwargs) |
42 | 44 | return override |
43 | 45 |
|
| 46 | +@contextlib.contextmanager |
| 47 | +def _wrap_text(textobj): |
| 48 | + """ |
| 49 | + Temporarily inserts newlines to the text if the wrap option is enabled. |
| 50 | + """ |
| 51 | + if textobj.get_wrap(): |
| 52 | + old_text = textobj.get_text() |
| 53 | + try: |
| 54 | + textobj.set_text(textobj._get_wrapped_text()) |
| 55 | + yield textobj |
| 56 | + finally: |
| 57 | + textobj.set_text(old_text) |
| 58 | + else: |
| 59 | + yield textobj |
| 60 | + |
44 | 61 |
|
45 | 62 | # Extracted from Text's method to serve as a function |
46 | 63 | def get_rotation(rotation): |
@@ -713,63 +730,58 @@ def draw(self, renderer): |
713 | 730 |
|
714 | 731 | renderer.open_group('text', self.get_gid()) |
715 | 732 |
|
716 | | - if self.get_wrap(): |
717 | | - old_text = self.get_text() |
718 | | - self.set_text(self._get_wrapped_text()) |
719 | | - |
720 | | - bbox, info, descent = self._get_layout(renderer) |
721 | | - trans = self.get_transform() |
722 | | - |
723 | | - # don't use self.get_position here, which refers to text position |
724 | | - # in Text, and dash position in TextWithDash: |
725 | | - posx = float(self.convert_xunits(self._x)) |
726 | | - posy = float(self.convert_yunits(self._y)) |
727 | | - |
728 | | - posx, posy = trans.transform_point((posx, posy)) |
729 | | - canvasw, canvash = renderer.get_canvas_width_height() |
730 | | - |
731 | | - # draw the FancyBboxPatch |
732 | | - if self._bbox_patch: |
733 | | - self._draw_bbox(renderer, posx, posy) |
734 | | - |
735 | | - gc = renderer.new_gc() |
736 | | - gc.set_foreground(self.get_color()) |
737 | | - gc.set_alpha(self.get_alpha()) |
738 | | - gc.set_url(self._url) |
739 | | - self._set_gc_clip(gc) |
740 | | - |
741 | | - if self._bbox: |
742 | | - bbox_artist(self, renderer, self._bbox) |
743 | | - angle = self.get_rotation() |
744 | | - |
745 | | - for line, wh, x, y in info: |
746 | | - if not np.isfinite(x) or not np.isfinite(y): |
747 | | - continue |
748 | | - |
749 | | - mtext = self if len(info) == 1 else None |
750 | | - x = x + posx |
751 | | - y = y + posy |
752 | | - if renderer.flipy(): |
753 | | - y = canvash - y |
754 | | - clean_line, ismath = self.is_math_text(line) |
| 733 | + with _wrap_text(self) as self: |
| 734 | + bbox, info, descent = self._get_layout(renderer) |
| 735 | + trans = self.get_transform() |
755 | 736 |
|
756 | | - if self.get_path_effects(): |
757 | | - from matplotlib.patheffects import PathEffectRenderer |
758 | | - textrenderer = PathEffectRenderer(self.get_path_effects(), |
759 | | - renderer) |
760 | | - else: |
761 | | - textrenderer = renderer |
| 737 | + # don't use self.get_position here, which refers to text position |
| 738 | + # in Text, and dash position in TextWithDash: |
| 739 | + posx = float(self.convert_xunits(self._x)) |
| 740 | + posy = float(self.convert_yunits(self._y)) |
762 | 741 |
|
763 | | - if self.get_usetex(): |
764 | | - textrenderer.draw_tex(gc, x, y, clean_line, |
765 | | - self._fontproperties, angle, mtext=mtext) |
766 | | - else: |
767 | | - textrenderer.draw_text(gc, x, y, clean_line, |
768 | | - self._fontproperties, angle, |
769 | | - ismath=ismath, mtext=mtext) |
| 742 | + posx, posy = trans.transform_point((posx, posy)) |
| 743 | + canvasw, canvash = renderer.get_canvas_width_height() |
| 744 | + |
| 745 | + # draw the FancyBboxPatch |
| 746 | + if self._bbox_patch: |
| 747 | + self._draw_bbox(renderer, posx, posy) |
| 748 | + |
| 749 | + gc = renderer.new_gc() |
| 750 | + gc.set_foreground(self.get_color()) |
| 751 | + gc.set_alpha(self.get_alpha()) |
| 752 | + gc.set_url(self._url) |
| 753 | + self._set_gc_clip(gc) |
| 754 | + |
| 755 | + if self._bbox: |
| 756 | + bbox_artist(self, renderer, self._bbox) |
| 757 | + angle = self.get_rotation() |
| 758 | + |
| 759 | + for line, wh, x, y in info: |
| 760 | + if not np.isfinite(x) or not np.isfinite(y): |
| 761 | + continue |
| 762 | + |
| 763 | + mtext = self if len(info) == 1 else None |
| 764 | + x = x + posx |
| 765 | + y = y + posy |
| 766 | + if renderer.flipy(): |
| 767 | + y = canvash - y |
| 768 | + clean_line, ismath = self.is_math_text(line) |
| 769 | + |
| 770 | + if self.get_path_effects(): |
| 771 | + from matplotlib.patheffects import PathEffectRenderer |
| 772 | + textrenderer = PathEffectRenderer(self.get_path_effects(), |
| 773 | + renderer) |
| 774 | + else: |
| 775 | + textrenderer = renderer |
770 | 776 |
|
771 | | - if self.get_wrap(): |
772 | | - self.set_text(old_text) |
| 777 | + if self.get_usetex(): |
| 778 | + textrenderer.draw_tex(gc, x, y, clean_line, |
| 779 | + self._fontproperties, angle, |
| 780 | + mtext=mtext) |
| 781 | + else: |
| 782 | + textrenderer.draw_text(gc, x, y, clean_line, |
| 783 | + self._fontproperties, angle, |
| 784 | + ismath=ismath, mtext=mtext) |
773 | 785 |
|
774 | 786 | gc.restore() |
775 | 787 | renderer.close_group('text') |
|
0 commit comments