Skip to content

Commit 906cd68

Browse files
committed
updating text
1 parent 19f21a2 commit 906cd68

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

adafruit_display_emoji_text.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,38 @@ def __init__(
9595
self.ascii_palette = displayio.Palette(2)
9696
self.ascii_palette[0] = 0x000000
9797
self.ascii_palette[1] = 0xFFFFFF
98+
self._text = text
99+
self._width = 0
100+
self._height = 12
101+
self._bounding_box = [0, 0, 0, 12]
102+
self._row_width = 0
103+
self._next_x = 0
104+
self._next_y = 0
105+
106+
self._update_text(self._text)
107+
108+
def _update_text(self, new_text):
109+
while len(self) > 0:
110+
del self[0]
98111

99112
self._width = 0
100113
self._height = 12
101-
self._bounding_box = [0, 0, 0, 0]
114+
self._bounding_box = [0, 0, 0, 12]
102115
self._row_width = 0
103116

104-
self._last_x = 0
105-
self._last_y = 0
117+
self._next_x = 0
118+
self._next_y = 0
106119

107120
skip_count = 0
108-
for i, char in enumerate(text):
121+
for i, char in enumerate(new_text):
109122
if skip_count > 0:
110123
skip_count -= 1
111124
continue
112125
# print(char)
113126
if char == "\n":
114127
# print("newline")
115-
self._last_y += 12
116-
self._last_x = 0
128+
self._next_y += 12
129+
self._next_x = 0
117130
self._height += 12
118131
self._row_width = 0
119132
self._bounding_box[3] = self._height
@@ -135,14 +148,16 @@ def __init__(
135148
skip_source_index=0,
136149
)
137150
tg = displayio.TileGrid(bitmap=bmp, pixel_shader=self.ascii_palette)
138-
tg.x = self._last_x
139-
tg.y = self._last_y
151+
self.append(tg)
152+
153+
tg.x = self._next_x
154+
tg.y = self._next_y
140155
self._row_width += bmp.width
141156
if self._width < self._row_width:
142157
self._width = self._row_width
143158
self._bounding_box[2] = self._width
144-
self._last_x += found_glyph.width
145-
self.append(tg)
159+
self._next_x += found_glyph.width
160+
146161
else:
147162
bmp = None
148163
for cur_range in EmojiLabel.MULTI_CODE_RANGES:
@@ -151,23 +166,24 @@ def __init__(
151166
if ord(char) in EmojiLabel.FIVE_WIDES:
152167
# try 5 wide file
153168
try:
154-
filename = f"emoji/U+{ord(char):X}_U+{ord(text[i + 1]):X}_U+{ord(text[i + 2]):X}_U+{ord(text[i + 3]):X}_U+{ord(text[i + 4]):X}.png" # noqa: E501, Line too long
169+
filename = f"emoji/U+{ord(char):X}_U+{ord(new_text[i + 1]):X}_U+{ord(new_text[i + 2]):X}_U+{ord(new_text[i + 3]):X}_U+{ord(new_text[i + 4]):X}.png" # noqa: E501, Line too long
155170
bmp, palette = adafruit_imageload.load(filename)
171+
156172
skip_count = 4
157173
break
158174
except (OSError, IndexError):
159175
pass
160176

161177
# try 4 wide file
162178
try:
163-
filename = f"emoji/U+{ord(char):X}_U+{ord(text[i + 1]):X}_U+{ord(text[i + 2]):X}_U+{ord(text[i + 3]):X}.png" # noqa: E501, Line too long
179+
filename = f"emoji/U+{ord(char):X}_U+{ord(new_text[i + 1]):X}_U+{ord(new_text[i + 2]):X}_U+{ord(new_text[i + 3]):X}.png" # noqa: E501, Line too long
164180
bmp, palette = adafruit_imageload.load(filename)
165181
skip_count = 3
166182
break
167183
except (OSError, IndexError):
168184
# try double wide file
169185
try:
170-
filename = f"emoji/U+{ord(char):X}_U+{ord(text[i + 1]):X}.png"
186+
filename = f"emoji/U+{ord(char):X}_U+{ord(new_text[i + 1]):X}.png"
171187
bmp, palette = adafruit_imageload.load(filename)
172188
skip_count = 1
173189
break
@@ -179,20 +195,30 @@ def __init__(
179195
filename = f"emoji/U+{ord(char):X}.png"
180196
try:
181197
bmp, palette = adafruit_imageload.load(filename)
198+
182199
except OSError:
183200
print(f"Unable to render: {hex(ord(char))}")
184201

185202
try:
186203
tg = displayio.TileGrid(bitmap=bmp, pixel_shader=palette)
187-
tg.x = self._last_x
188-
tg.y = self._last_y
204+
tg.x = self._next_x
205+
tg.y = self._next_y
189206

190207
self._row_width += bmp.width
191208
if self._width < self._row_width:
192209
self._width = self._row_width
193210
self._bounding_box[2] = self._width
194-
self._last_x += bmp.width + 1
211+
self._next_x += bmp.width + 1
195212
self.append(tg)
196213
except TypeError:
197214
# Unsupported bitmap type
198215
print(f"Unable to render {hex(ord(char))}. Unsupported bitmap")
216+
217+
@property
218+
def text(self):
219+
return self._text
220+
221+
@text.setter
222+
def text(self, new_text):
223+
if new_text != self._text:
224+
self._update_text(new_text)

0 commit comments

Comments
 (0)