|
7 | 7 | """ |
8 | 8 |
|
9 | 9 |
|
10 | | -def wrap_text_to_pixels(text, max_width, font=None, indent0="", indent1=""): |
| 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 | +
|
| 14 | + :param str string: The text to be wrapped. |
| 15 | + :param int max_width: The maximum number of pixels on a line before wrapping. |
| 16 | + :param Font font: The font to use for measuring the text. |
| 17 | + :param str indent0: Additional character(s) to add to the first line. |
| 18 | + :param str indent1: Additional character(s) to add to all other lines. |
| 19 | +
|
| 20 | +
|
| 21 | + :return str lines: A string with newlines inserted appropriately to wrap |
| 22 | + the input string at the given max_width in pixels |
| 23 | +
|
| 24 | + """ |
| 25 | + # pylint: disable=too-many-locals too-many-branches |
11 | 26 | if font is None: |
12 | | - def measure(s): |
13 | | - return len(s) |
| 27 | + |
| 28 | + def measure(string): |
| 29 | + return len(string) |
| 30 | + |
14 | 31 | else: |
15 | | - if hasattr(font, 'load_glyphs'): |
16 | | - font.load_glyphs(text) |
| 32 | + if hasattr(font, "load_glyphs"): |
| 33 | + font.load_glyphs(string) |
17 | 34 |
|
18 | | - def measure(s): |
19 | | - return sum(font.get_glyph(ord(c)).shift_x for c in s) |
| 35 | + def measure(string): |
| 36 | + return sum(font.get_glyph(ord(c)).shift_x for c in string) |
20 | 37 |
|
21 | 38 | lines = [] |
22 | 39 | partial = [indent0] |
23 | 40 | width = measure(indent0) |
24 | | - swidth = measure(' ') |
| 41 | + swidth = measure(" ") |
25 | 42 | firstword = True |
26 | | - for word in text.split(): |
| 43 | + for word in string.split(): |
27 | 44 | # TODO: split words that are larger than max_width |
28 | 45 | wwidth = measure(word) |
29 | 46 | print("{} - {}".format(word, wwidth)) |
30 | | - part_width = 0 |
31 | 47 | word_parts = [] |
32 | 48 | cur_part = "" |
33 | 49 | if wwidth > max_width: |
34 | 50 | if partial: |
35 | 51 | lines.append("".join(partial)) |
36 | 52 | partial = [] |
37 | 53 | for char in word: |
38 | | - #print(measure(cur_part) + measure(char) + measure("-")) |
| 54 | + # print(measure(cur_part) + measure(char) + measure("-")) |
39 | 55 | if measure(cur_part) + measure(char) + measure("-") > max_width: |
40 | 56 | word_parts.append(cur_part + "-") |
41 | 57 | cur_part = char |
42 | 58 | else: |
43 | 59 | cur_part += char |
44 | 60 | if cur_part: |
45 | 61 | word_parts.append(cur_part) |
46 | | - #print(word_parts) |
| 62 | + # print(word_parts) |
47 | 63 | for line in word_parts[:-1]: |
48 | 64 | lines.append(line) |
49 | 65 | partial.append(word_parts[-1]) |
@@ -71,6 +87,7 @@ def measure(s): |
71 | 87 | lines.append("".join(partial)) |
72 | 88 | return "\n".join(lines) |
73 | 89 |
|
| 90 | + |
74 | 91 | def wrap_text_to_lines(string, max_chars): |
75 | 92 | """wrap_text_to_lines function |
76 | 93 | A helper that will return a list of lines with word-break wrapping |
|
0 commit comments