|
1 |
| -# SPDX-FileCopyrightText: 2020 Tim C for Adafruit Industries |
| 1 | +# SPDX-FileCopyrightText: 2020 Tim C, 2021 Jeff Epler for Adafruit Industries |
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 | 4 |
|
|
7 | 7 | """
|
8 | 8 |
|
9 | 9 |
|
| 10 | +def wrap_text_to_pixels(string, max_width, font=None, indent0="", indent1=""): |
| 11 | + """wrap_text_to_pixels function |
| 12 | + A helper that will return a list of lines with word-break wrapping. |
| 13 | + Leading and trailing whitespace in your string will be removed. If |
| 14 | + you wish to use leading whitespace see `indend0` and `indent1` |
| 15 | + parameters. |
| 16 | +
|
| 17 | + :param str string: The text to be wrapped. |
| 18 | + :param int max_width: The maximum number of pixels on a line before wrapping. |
| 19 | + :param Font font: The font to use for measuring the text. |
| 20 | + :param str indent0: Additional character(s) to add to the first line. |
| 21 | + :param str indent1: Additional character(s) to add to all other lines. |
| 22 | +
|
| 23 | +
|
| 24 | + :return list lines: A list of the lines resulting from wrapping the |
| 25 | + input text at max_width pixels size |
| 26 | +
|
| 27 | + """ |
| 28 | + # pylint: disable=too-many-locals too-many-branches |
| 29 | + if font is None: |
| 30 | + |
| 31 | + def measure(string): |
| 32 | + return len(string) |
| 33 | + |
| 34 | + else: |
| 35 | + if hasattr(font, "load_glyphs"): |
| 36 | + font.load_glyphs(string) |
| 37 | + |
| 38 | + def measure(string): |
| 39 | + return sum(font.get_glyph(ord(c)).shift_x for c in string) |
| 40 | + |
| 41 | + lines = [] |
| 42 | + partial = [indent0] |
| 43 | + width = measure(indent0) |
| 44 | + swidth = measure(" ") |
| 45 | + firstword = True |
| 46 | + for line_in_input in string.split("\n"): |
| 47 | + for index, word in enumerate(line_in_input.split(" ")): |
| 48 | + wwidth = measure(word) |
| 49 | + word_parts = [] |
| 50 | + cur_part = "" |
| 51 | + |
| 52 | + if wwidth > max_width: |
| 53 | + for char in word: |
| 54 | + if ( |
| 55 | + measure("".join(partial)) |
| 56 | + + measure(cur_part) |
| 57 | + + measure(char) |
| 58 | + + measure("-") |
| 59 | + > max_width |
| 60 | + ): |
| 61 | + word_parts.append("".join(partial) + cur_part + "-") |
| 62 | + cur_part = char |
| 63 | + partial = [indent1] |
| 64 | + else: |
| 65 | + cur_part += char |
| 66 | + if cur_part: |
| 67 | + word_parts.append(cur_part) |
| 68 | + for line in word_parts[:-1]: |
| 69 | + lines.append(line) |
| 70 | + partial.append(word_parts[-1]) |
| 71 | + width = measure(word_parts[-1]) |
| 72 | + if firstword: |
| 73 | + firstword = False |
| 74 | + else: |
| 75 | + if firstword: |
| 76 | + partial.append(word) |
| 77 | + firstword = False |
| 78 | + width += wwidth |
| 79 | + elif width + swidth + wwidth < max_width: |
| 80 | + if index > 0: |
| 81 | + partial.append(" ") |
| 82 | + partial.append(word) |
| 83 | + width += wwidth + swidth |
| 84 | + else: |
| 85 | + lines.append("".join(partial)) |
| 86 | + partial = [indent1, word] |
| 87 | + width = measure(indent1) + wwidth |
| 88 | + |
| 89 | + lines.append("".join(partial)) |
| 90 | + partial = [indent1] |
| 91 | + width = measure(indent1) |
| 92 | + |
| 93 | + return lines |
| 94 | + |
| 95 | + |
10 | 96 | def wrap_text_to_lines(string, max_chars):
|
11 | 97 | """wrap_text_to_lines function
|
12 | 98 | A helper that will return a list of lines with word-break wrapping
|
|
0 commit comments