MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / _get_wrapped_text

Method _get_wrapped_text

lib/matplotlib/text.py:789–838  ·  view source on GitHub ↗

Return a copy of the text string with new lines added so that the text is wrapped relative to the parent figure (if `get_wrap` is True).

(self)

Source from the content-addressed store, hash-verified

787 return math.ceil(w)
788
789 def _get_wrapped_text(self):
790 """
791 Return a copy of the text string with new lines added so that the text
792 is wrapped relative to the parent figure (if `get_wrap` is True).
793 """
794 if not self.get_wrap():
795 return self.get_text()
796
797 # Not fit to handle breaking up latex syntax correctly, so
798 # ignore latex for now.
799 if self.get_usetex():
800 return self.get_text()
801
802 # Build the line incrementally, for a more accurate measure of length
803 line_width = self._get_wrap_line_width()
804 wrapped_lines = []
805
806 # New lines in the user's text force a split
807 unwrapped_lines = self.get_text().split('\n')
808
809 # Now wrap each individual unwrapped line
810 for unwrapped_line in unwrapped_lines:
811
812 sub_words = unwrapped_line.split(' ')
813 # Remove items from sub_words as we go, so stop when empty
814 while len(sub_words) > 0:
815 if len(sub_words) == 1:
816 # Only one word, so just add it to the end
817 wrapped_lines.append(sub_words.pop(0))
818 continue
819
820 for i in range(2, len(sub_words) + 1):
821 # Get width of all words up to and including here
822 line = ' '.join(sub_words[:i])
823 current_width = self._get_rendered_text_width(line)
824
825 # If all these words are too wide, append all not including
826 # last word
827 if current_width > line_width:
828 wrapped_lines.append(' '.join(sub_words[:i - 1]))
829 sub_words = sub_words[i - 1:]
830 break
831
832 # Otherwise if all words fit in the width, append them all
833 elif i == len(sub_words):
834 wrapped_lines.append(' '.join(sub_words[:i]))
835 sub_words = []
836 break
837
838 return '\n'.join(wrapped_lines)
839
840 @artist.allow_rasterization
841 def draw(self, renderer):

Callers 5

_get_layoutMethod · 0.95
test_wrapFunction · 0.80
test_mathwrapFunction · 0.80
test_long_word_wrapFunction · 0.80
test_wrap_no_wrapFunction · 0.80

Calls 7

get_wrapMethod · 0.95
get_textMethod · 0.95
get_usetexMethod · 0.95
_get_wrap_line_widthMethod · 0.95
joinMethod · 0.80
popMethod · 0.45

Tested by 4

test_wrapFunction · 0.64
test_mathwrapFunction · 0.64
test_long_word_wrapFunction · 0.64
test_wrap_no_wrapFunction · 0.64