-
Notifications
You must be signed in to change notification settings - Fork 2
/
textmap.py
executable file
·305 lines (237 loc) · 12 KB
/
textmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# The MIT License (MIT)
#
# Copyright (c) 2020 Kevin Matocha
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`kmatch98_textmap`
================================================================================
Text graphics handling for CircuitPython, including ttext boxes
* Author(s): Kevin Matocha
Implementation Notes
--------------------
**Hardware:**
.. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST
inline format: "* `Link Text <url>`_"
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either.
# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
"""
# imports
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/kmatch98/CircuitPython_textMap.git"
def lineSpacingY(font, lineSpacing, scale=1):
# Note: Scale is not implemented at this time
fontHeight = font.get_glyph(ord('M')).height
returnValue = int(lineSpacing * fontHeight)
return returnValue
def bounding_box(text, font, lineSpacing, scale=1):
# bounding_box - determines the bounding box size around the new text to be added.
# To be used to calculate if the new text will be printed within the bounding_box
# This function can used to determine character-wrapping or word-wrapping for a
# text terminal box, prior to actually printing the text in the bitmap.
#
# Note: Scale is not implemented at this time
#print('bounding_box text: {}'.format(text))
boxHeight = boxWidth = 0
fontHeight = font.get_glyph(ord("M")).height
thisLineWidth = 0
for char in text:
if char == '\n': # newline
boxWidth = max(boxWidth, thisLineWidth) # check to see if the last line is wider than any others.
thisLineWidth = 0 # new line, so restart thislineWidth at 0
boxHeight = boxHeight + lineSpacingY(font, lineSpacing, scale) # add a lineSpacing to the boxHeight
else:
myGlyph = font.get_glyph(ord(char))
if myGlyph == None: # Error checking: no glyph found
print('Glyph not found: {}'.format(repr(char)))
else:
width = myGlyph.width
height = myGlyph.height
dx = myGlyph.dx
dy = myGlyph.dy
shift_x = myGlyph.shift_x
shift_y = myGlyph.shift_x
# Not working yet***
# This offset is used to match the label.py function from Adafruit_Display_Text library
# y_offset = int(
# (
# self._font.get_glyph(ord("M")).height
# - new_text.count("\n") * self.height * self.line_spacing
# )
# / 2 )
# yOffset = int( (fontHeight-height*lineSpacing)/2 )
yOffset = fontHeight - height
thisLineWidth = thisLineWidth + shift_x
boxHeight = max(boxHeight, height - dy + yOffset)
boxWidth = max(boxWidth, thisLineWidth)
return (boxWidth, boxHeight)
def placeText(
bitmap, text, font, lineSpacing, xPosition, yPosition,
textPaletteIndex=1,
backgroundPaletteIndex=0,
scale=1,
printOnlyPixels=True, # only update the bitmap where the glyph pixel color is > 0
# this is especially useful for script fonts
):
# placeText - Writes text into a bitmap at the specified location.
#
# (xPosition, yPosition) correspond to upper left corner of the height of the 'M' glyph
# To Do: Add anchor positions, and adjust the default baseline position to match
# the current "label" function
# Verify paletteIndex is working properly with * operator, especially if accommodating multicolored fonts
#
# Note: Scale is not implemented at this time
import terminalio
fontHeight = font.get_glyph(ord("M")).height
bitmapWidth = bitmap.width
bitmapHeight = bitmap.height
xStart=xPosition # starting x position (left margin)
if backgroundPaletteIndex != 0: # the textbackground is different from the bitmap background
# draw a bounding box where the text will go
(ignore, fontLineHeight)=bounding_box('M g', font, lineSpacing, scale) # check height with ascender and descender.
(boxX, boxY) = bounding_box(text, font, lineSpacing, scale)
boxY=max(fontLineHeight, boxY)
for y in range(boxY):
for x in range(boxX):
if (xPosition+x < bitmapWidth) and (yPosition+y < bitmapHeight): # check boundaries
#bitmap[xPosition+x, yPosition+y]=backgroundPaletteIndex
bitmap[(yPosition+y)*bitmapWidth + (xPosition + x)]=backgroundPaletteIndex
for char in text:
if char == '\n': # newline
xPosition=xStart # reset to left column
yPosition = yPosition + lineSpacingY(font, lineSpacing, scale) # Add a newline
else:
myGlyph = font.get_glyph(ord(char))
if myGlyph == None: # Error checking: no glyph found
print('Glyph not found: {}'.format(repr(char)))
else:
width = myGlyph.width
height = myGlyph.height
# print('glyph width: {}, height: {}'.format(width, height))
dx = myGlyph.dx
dy = myGlyph.dy
shift_x = myGlyph.shift_x
shift_y = myGlyph.shift_x
glyph_offset_x = myGlyph.tile_index * width # for type BuiltinFont, this creates the x-offset in the glyph bitmap.
# for BDF loaded fonts, this should equal 0
# Not working yet***
# This offset is used to match the label.py function from Adafruit_Display_Text library
# y_offset = int(
# (
# self._font.get_glyph(ord("M")).height
# - new_text.count("\n") * self.height * self.line_spacing
# )
# / 2 )
# position_y = y - glyph.height - glyph.dy + y_offset
# yOffset = int( (fontHeight-height*lineSpacing)/2 )
yOffset = fontHeight - height
for y in range(height):
for x in range(width):
xPlacement = x + xPosition + dx
# yPlacement=y+yPosition-height-dy+yOffset
yPlacement = y + yPosition - dy + yOffset
if (
(xPlacement >= 0)
and (yPlacement >= 0)
and (xPlacement < bitmapWidth)
and (yPlacement < bitmapHeight)
):
paletteIndexes=(backgroundPaletteIndex, textPaletteIndex)
# print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y]))
#bitmap[xPlacement, yPlacement] = (
# myGlyph.bitmap[x, y] * paletteIndex
#)
# Allows for different paletteIndex for background and text.
#bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x+glyph_offset_x,y]]
thisPixelColor=paletteIndexes[myGlyph.bitmap[y*width + x+glyph_offset_x]]
if not printOnlyPixels or thisPixelColor > 0:
# write all characters if printOnlyPixels = False, or if thisPixelColor is 1
bitmap[yPlacement*bitmapWidth + xPlacement] = thisPixelColor
elif (yPlacement > bitmapHeight):
break
xPosition = xPosition + shift_x
return (xPosition, yPosition)
class textBox:
def __init__(
self, text, font, width, height, backgroundColor=0x000000, textColor=0xFFFFFF, lineSpacing=1.25
):
import displayio
# import terminalio
# To save memory, set self._memorySaver=True, avoids storing the text string in the class.
# If set to False, the class saves the text string for future reference.
self._memorySaver=True
if self._memorySaver == False:
self._text = text # text on the display
self._font = font
self._lineSpacing = lineSpacing
self._fontHeight = self._font.get_glyph(ord("M")).height
self._width = width # in pixels
self._height = height # in pixels
self._backgroundColor = backgroundColor
self._textColor = textColor
self.bitmap = displayio.Bitmap(self._width, self._height, 2)
self.palette = displayio.Palette(2)
self.palette[0] = self._backgroundColor
self.palette[1] = self._textColor
self._cursorX = 1 # controls insertion point for text
self._cursorY = 1
self._startX = self._cursorX # the left column start position
self._startY = self._cursorY # the top row start position
self.addText(text)
import gc
gc.collect()
def addText(self, newText): # add text to a textBox
# print('startX: {}'.format(self._cursorX) )
import gc
for char in newText:
(charWidth, charHeight) = bounding_box(char, self._font, self._lineSpacing)
if (self._cursorX + charWidth >= self._width - 1) or (char == "\n"):
# make a newline
self.setCursor(
self._startX,
self._cursorY + int(self._fontHeight * self._lineSpacing),
)
(newX, newY) = placeText(
self.bitmap,
char,
self._font,
self._lineSpacing,
self._cursorX,
self._cursorY,
)
# print('newX: {}'.format(newX) )
self.setCursor(newX, newY)
if self._memorySaver == False:
self._text = self._text + newText # add this text to the instance text string.
gc.collect()
return self.getCursor() # return tuple: (self._cursorX , self._cursorY)
def setCursor(self, newCursorX, newCursorY): # set cursor position
self._cursorX = newCursorX
self._cursorY = newCursorY
def getCursor(self): # get current cursor position, tuple
return (self._cursorX, self._cursorY)
def clearBitmap(self):
self.bitmap.fill(0) # quick builtin bitmap fill operation
self.setCursor(self._startX, self._startY)
if self._memorySaver == False:
self._text='' # reset the text string