22Classes for including text in a figure.
33"""
44
5- import contextlib
65import logging
76import math
87import weakref
2221_log = logging .getLogger (__name__ )
2322
2423
25- @contextlib .contextmanager
26- def _wrap_text (textobj ):
27- """Temporarily inserts newlines if the wrap option is enabled."""
28- if textobj .get_wrap ():
29- old_text = textobj .get_text ()
30- try :
31- textobj .set_text (textobj ._get_wrapped_text ())
32- yield textobj
33- finally :
34- textobj .set_text (old_text )
35- else :
36- yield textobj
37-
38-
3924# Extracted from Text's method to serve as a function
4025def get_rotation (rotation ):
4126 """
@@ -631,9 +616,12 @@ def _get_rendered_text_width(self, text):
631616
632617 def _get_wrapped_text (self ):
633618 """
634- Return a copy of the text with new lines added, so that
635- the text is wrapped relative to the parent figure.
619+ Return a copy of the text string with new lines added so that the text
620+ is wrapped relative to the parent figure (if `get_wrap` is True) .
636621 """
622+ if not self .get_wrap ():
623+ return self .get_text ()
624+
637625 # Not fit to handle breaking up latex syntax correctly, so
638626 # ignore latex for now.
639627 if self .get_usetex ():
@@ -690,14 +678,14 @@ def draw(self, renderer):
690678
691679 renderer .open_group ('text' , self .get_gid ())
692680
693- with _wrap_text ( self ) as textobj :
694- bbox , info , descent = textobj ._get_layout (renderer )
695- trans = textobj .get_transform ()
681+ with self . _cm_set ( text = self . _get_wrapped_text ()) :
682+ bbox , info , descent = self ._get_layout (renderer )
683+ trans = self .get_transform ()
696684
697- # don't use textobj .get_position here, which refers to text
685+ # don't use self .get_position here, which refers to text
698686 # position in Text:
699- posx = float (textobj .convert_xunits (textobj ._x ))
700- posy = float (textobj .convert_yunits (textobj ._y ))
687+ posx = float (self .convert_xunits (self ._x ))
688+ posy = float (self .convert_yunits (self ._y ))
701689 posx , posy = trans .transform ((posx , posy ))
702690 if not np .isfinite (posx ) or not np .isfinite (posy ):
703691 _log .warning ("posx and posy should be finite values" )
@@ -706,41 +694,41 @@ def draw(self, renderer):
706694
707695 # Update the location and size of the bbox
708696 # (`.patches.FancyBboxPatch`), and draw it.
709- if textobj ._bbox_patch :
697+ if self ._bbox_patch :
710698 self .update_bbox_position_size (renderer )
711699 self ._bbox_patch .draw (renderer )
712700
713701 gc = renderer .new_gc ()
714- gc .set_foreground (textobj .get_color ())
715- gc .set_alpha (textobj .get_alpha ())
716- gc .set_url (textobj ._url )
717- textobj ._set_gc_clip (gc )
702+ gc .set_foreground (self .get_color ())
703+ gc .set_alpha (self .get_alpha ())
704+ gc .set_url (self ._url )
705+ self ._set_gc_clip (gc )
718706
719- angle = textobj .get_rotation ()
707+ angle = self .get_rotation ()
720708
721709 for line , wh , x , y in info :
722710
723- mtext = textobj if len (info ) == 1 else None
711+ mtext = self if len (info ) == 1 else None
724712 x = x + posx
725713 y = y + posy
726714 if renderer .flipy ():
727715 y = canvash - y
728- clean_line , ismath = textobj ._preprocess_math (line )
716+ clean_line , ismath = self ._preprocess_math (line )
729717
730- if textobj .get_path_effects ():
718+ if self .get_path_effects ():
731719 from matplotlib .patheffects import PathEffectRenderer
732720 textrenderer = PathEffectRenderer (
733- textobj .get_path_effects (), renderer )
721+ self .get_path_effects (), renderer )
734722 else :
735723 textrenderer = renderer
736724
737- if textobj .get_usetex ():
725+ if self .get_usetex ():
738726 textrenderer .draw_tex (gc , x , y , clean_line ,
739- textobj ._fontproperties , angle ,
727+ self ._fontproperties , angle ,
740728 mtext = mtext )
741729 else :
742730 textrenderer .draw_text (gc , x , y , clean_line ,
743- textobj ._fontproperties , angle ,
731+ self ._fontproperties , angle ,
744732 ismath = ismath , mtext = mtext )
745733
746734 gc .restore ()
0 commit comments