Skip to content

Commit d33018a

Browse files
authored
Merge pull request #111 from FoamyGuy/wrap_by_pixels
Adding the wrap_by_pixels function
2 parents 89562e3 + 264e5c8 commit d33018a

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

adafruit_display_text/__init__.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2020 Tim C for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2020 Tim C, 2021 Jeff Epler for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
44

@@ -7,6 +7,92 @@
77
"""
88

99

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+
1096
def wrap_text_to_lines(string, max_chars):
1197
"""wrap_text_to_lines function
1298
A helper that will return a list of lines with word-break wrapping
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Test the wrap_text_to_pixels function. Try changing WRAP_WIDTH or text
6+
and observe the results. The red bar represents the full size of
7+
WRAP_WIDTH.
8+
"""
9+
10+
import board
11+
import displayio
12+
import terminalio
13+
from adafruit_display_text import label, wrap_text_to_pixels
14+
15+
WRAP_WIDTH = 140
16+
text = (
17+
"CircuitPython is a programming language designed to simplify experimenting "
18+
"and learning to code on low-cost microcontroller boards. "
19+
)
20+
21+
# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
22+
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
23+
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
24+
display = board.DISPLAY
25+
26+
# Make the display context
27+
main_group = displayio.Group(max_size=10)
28+
display.show(main_group)
29+
30+
font = terminalio.FONT
31+
32+
print(text)
33+
print(display.width)
34+
35+
text_area = label.Label(
36+
font,
37+
text="\n".join(wrap_text_to_pixels(text, WRAP_WIDTH, font)),
38+
background_color=0x0000DD,
39+
)
40+
41+
text_area.anchor_point = (0, 0)
42+
text_area.anchored_position = (0, 0)
43+
44+
main_group.append(text_area)
45+
46+
# Create a bitmap with two colors
47+
size_checker = displayio.Bitmap(WRAP_WIDTH, 10, 2)
48+
# Create a two color palette
49+
palette = displayio.Palette(2)
50+
palette[0] = 0x0000DD
51+
palette[1] = 0xDD0000
52+
53+
# Create a TileGrid using the Bitmap and Palette
54+
tile_grid = displayio.TileGrid(size_checker, pixel_shader=palette)
55+
56+
tile_grid.y = text_area.bounding_box[1] + text_area.bounding_box[3] + 10
57+
58+
size_checker.fill(1)
59+
60+
main_group.append(tile_grid)
61+
62+
while True:
63+
pass

0 commit comments

Comments
 (0)