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)
| 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): |