2
2
Classes for including text in a figure.
3
3
"""
4
4
5
- import contextlib
6
5
import logging
7
6
import math
8
7
import weakref
22
21
_log = logging .getLogger (__name__ )
23
22
24
23
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
-
39
24
# Extracted from Text's method to serve as a function
40
25
def get_rotation (rotation ):
41
26
"""
@@ -631,9 +616,12 @@ def _get_rendered_text_width(self, text):
631
616
632
617
def _get_wrapped_text (self ):
633
618
"""
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) .
636
621
"""
622
+ if not self .get_wrap ():
623
+ return self .get_text ()
624
+
637
625
# Not fit to handle breaking up latex syntax correctly, so
638
626
# ignore latex for now.
639
627
if self .get_usetex ():
@@ -690,14 +678,14 @@ def draw(self, renderer):
690
678
691
679
renderer .open_group ('text' , self .get_gid ())
692
680
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 ()
696
684
697
- # don't use textobj .get_position here, which refers to text
685
+ # don't use self .get_position here, which refers to text
698
686
# 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 ))
701
689
posx , posy = trans .transform ((posx , posy ))
702
690
if not np .isfinite (posx ) or not np .isfinite (posy ):
703
691
_log .warning ("posx and posy should be finite values" )
@@ -706,41 +694,41 @@ def draw(self, renderer):
706
694
707
695
# Update the location and size of the bbox
708
696
# (`.patches.FancyBboxPatch`), and draw it.
709
- if textobj ._bbox_patch :
697
+ if self ._bbox_patch :
710
698
self .update_bbox_position_size (renderer )
711
699
self ._bbox_patch .draw (renderer )
712
700
713
701
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 )
718
706
719
- angle = textobj .get_rotation ()
707
+ angle = self .get_rotation ()
720
708
721
709
for line , wh , x , y in info :
722
710
723
- mtext = textobj if len (info ) == 1 else None
711
+ mtext = self if len (info ) == 1 else None
724
712
x = x + posx
725
713
y = y + posy
726
714
if renderer .flipy ():
727
715
y = canvash - y
728
- clean_line , ismath = textobj ._preprocess_math (line )
716
+ clean_line , ismath = self ._preprocess_math (line )
729
717
730
- if textobj .get_path_effects ():
718
+ if self .get_path_effects ():
731
719
from matplotlib .patheffects import PathEffectRenderer
732
720
textrenderer = PathEffectRenderer (
733
- textobj .get_path_effects (), renderer )
721
+ self .get_path_effects (), renderer )
734
722
else :
735
723
textrenderer = renderer
736
724
737
- if textobj .get_usetex ():
725
+ if self .get_usetex ():
738
726
textrenderer .draw_tex (gc , x , y , clean_line ,
739
- textobj ._fontproperties , angle ,
727
+ self ._fontproperties , angle ,
740
728
mtext = mtext )
741
729
else :
742
730
textrenderer .draw_text (gc , x , y , clean_line ,
743
- textobj ._fontproperties , angle ,
731
+ self ._fontproperties , angle ,
744
732
ismath = ismath , mtext = mtext )
745
733
746
734
gc .restore ()
0 commit comments