@@ -631,28 +631,41 @@ def _get_wrapped_text(self):
631631
632632 # Build the line incrementally, for a more accurate measure of length
633633 line_width = self ._get_wrap_line_width ()
634- wrapped_str = ""
635- line = ""
636-
637- for word in self .get_text ().split (' ' ):
638- # New lines in the user's test need to force a split, so that it's
639- # not using the longest current line width in the line being built
640- sub_words = word .split ('\n ' )
641- for i in range (len (sub_words )):
642- current_width = self ._get_rendered_text_width (
643- line + ' ' + sub_words [i ])
644-
645- # Split long lines, and each newline found in the current word
646- if current_width > line_width or i > 0 :
647- wrapped_str += line + '\n '
648- line = ""
649-
650- if line == "" :
651- line = sub_words [i ]
652- else :
653- line += ' ' + sub_words [i ]
654-
655- return wrapped_str + line
634+ wrapped_lines = []
635+
636+ # New lines in the user's text force a split
637+ unwrapped_lines = self .get_text ().split ('\n ' )
638+
639+ # Now wrap each individual unwrapped line
640+ for unwrapped_line in unwrapped_lines :
641+
642+ sub_words = unwrapped_line .split (' ' )
643+ # Remove items from sub_words as we go, so stop when empty
644+ while len (sub_words ) > 0 :
645+ if len (sub_words ) == 1 :
646+ # Only one word, so just add it to the end
647+ wrapped_lines .append (sub_words .pop (0 ))
648+ continue
649+
650+ for i in range (2 , len (sub_words ) + 1 ):
651+ # Get width of all words up to and including here
652+ line = ' ' .join (sub_words [:i ])
653+ current_width = self ._get_rendered_text_width (line )
654+
655+ # If all these words are too wide, append all not including
656+ # last word
657+ if current_width > line_width :
658+ wrapped_lines .append (' ' .join (sub_words [:i - 1 ]))
659+ sub_words = sub_words [i - 1 :]
660+ break
661+
662+ # Otherwise if all words fit in the width, append them all
663+ elif i == len (sub_words ):
664+ wrapped_lines .append (' ' .join (sub_words [:i ]))
665+ sub_words = []
666+ break
667+
668+ return '\n ' .join (wrapped_lines )
656669
657670 @artist .allow_rasterization
658671 def draw (self , renderer ):
0 commit comments