Skip to content

Commit d0c7d38

Browse files
author
Margaret Matocha
committed
added font and two code samples, with minor updates to bitmap_label.py
1 parent 25ff802 commit d0c7d38

File tree

4 files changed

+4485
-9
lines changed

4 files changed

+4485
-9
lines changed

adafruit_display_text/bitmap_label.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def text_bounding_box(
104104
if (
105105
y1_min is None
106106
): # A newline was the first character of the first line
107-
y_offset = label_position_yoffset
107+
y_offset = 0 #label_position_yoffset
108108
box_height_adder = (
109109
line_spacing_ypixels(font, line_spacing - 1) + y_offset
110110
)
@@ -399,7 +399,7 @@ def __init__(
399399
label_position_yoffset = int( # To calibrate with label.py positioning
400400
(
401401
font.get_glyph(ord("M")).height
402-
- text.count("\n") * font.get_bounding_box()[1] * self._line_spacing
402+
- font.get_bounding_box()[1] * self._line_spacing
403403
)
404404
/ 2
405405
)
@@ -412,8 +412,9 @@ def __init__(
412412
tile_width=box_x,
413413
tile_height=box_y,
414414
default_tile=0,
415-
x=0,
416-
y=label_position_yoffset - y_offset,
415+
x=-padding_left,
416+
#y=label_position_yoffset - y_offset - padding_left,
417+
y= - y_offset - padding_left,
417418
)
418419

419420
# instance the Group with super... super().__init__(

examples/code.py renamed to examples/code_complex.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@
7979

8080
# Load some proportional fonts
8181
fontFiles = [
82-
'fonts/Helvetica-Bold-16.bdf',
82+
# 'fonts/Helvetica-Bold-16.bdf',
8383
# 'fonts/BitstreamVeraSans-Roman-24.bdf', # Header2
84-
# 'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText
84+
'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText
8585
]
8686

8787
from adafruit_bitmap_font import bitmap_font
@@ -90,7 +90,7 @@
9090
thisFont = bitmap_font.load_font(fontFile)
9191

9292

93-
#thisFont=terminalio.FONT
93+
thisFont=terminalio.FONT # comment this out to switch back to BDF loaded fonts
9494

9595
fontList.append(thisFont)
9696
fontHeight.append( thisFont.get_glyph(ord("M")).height )
@@ -107,7 +107,8 @@
107107

108108
print('loading glyphs...')
109109
for font in fontList:
110-
font.load_glyphs(glyphs)
110+
if font is not terminalio.FONT:
111+
font.load_glyphs(glyphs)
111112

112113
print('Glyphs are loaded.')
113114

@@ -126,7 +127,7 @@
126127

127128

128129
myString12=('Bit Juice ({[]}) Monsters!\"\'ABCDEFGHIJKLMNOPQRSTUVWXYZ\npuppy bug jump ({[]})')
129-
myString34='\nnone'
130+
myString34=' none '
130131
myString_bitmap_label='bitmap_label'
131132
myString_label='label bitmap_label'
132133
#myString=('Full Screen Size: This is a stationary box, not a stationery box')

examples/code_simpler.py

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
# Sample code using the textMap library and the "textBox" wrapper class
2+
# Creates four textBox instances
3+
# Inserts each textBox into a tileGrid group
4+
# Writes text into the box one character at a time
5+
# Moves the position of the textBox around the display
6+
# Clears each textBox after the full string is written (even if the text is outside of the box)
7+
8+
#import textmap
9+
#from textmap import textBox
10+
11+
import board
12+
import displayio
13+
import time
14+
import terminalio
15+
import fontio
16+
import sys
17+
import busio
18+
#from adafruit_st7789 import ST7789
19+
from adafruit_ili9341 import ILI9341
20+
21+
22+
# Use these two options to decide which system and font to use
23+
24+
##########
25+
use_bitmap_label=True
26+
##########
27+
use_builtin_font=False
28+
##########
29+
30+
my_scale=1
31+
32+
if use_bitmap_label: # use bitmap_label.py library (Bitmap)
33+
from adafruit_display_text import bitmap_label as label
34+
myString6='bitmap_label -->'
35+
label6_anchor_point=(1,0.5)
36+
label6_anchored_position=(200,200)
37+
38+
version='bitmap_label.py'
39+
40+
else: # use label.py library (TileGrid)
41+
from adafruit_display_text import label as label
42+
myString6='<-- label'
43+
label6_anchor_point=(0,0.5)
44+
label6_anchored_position=(50,200)
45+
46+
version='label.py'
47+
48+
49+
# Setup the SPI display
50+
51+
print('Starting the display...') # goes to serial only
52+
displayio.release_displays()
53+
54+
spi = board.SPI()
55+
tft_cs = board.D9 # arbitrary, pin not used
56+
tft_dc = board.D10
57+
tft_backlight = board.D12
58+
tft_reset=board.D11
59+
60+
while not spi.try_lock():
61+
spi.configure(baudrate=32000000)
62+
pass
63+
spi.unlock()
64+
65+
display_bus = displayio.FourWire(
66+
spi,
67+
command=tft_dc,
68+
chip_select=tft_cs,
69+
reset=tft_reset,
70+
baudrate=32000000,
71+
polarity=1,
72+
phase=1,
73+
)
74+
75+
print('spi.frequency: {}'.format(spi.frequency))
76+
77+
DISPLAY_WIDTH=320
78+
DISPLAY_HEIGHT=240
79+
80+
#display = ST7789(display_bus, width=240, height=240, rotation=0, rowstart=80, colstart=0)
81+
display = ILI9341(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, rotation=180, auto_refresh=True)
82+
83+
display.show(None)
84+
85+
print ('Display is started')
86+
87+
88+
# load all the fonts
89+
print('loading fonts...')
90+
91+
import terminalio
92+
93+
94+
fontList = []
95+
fontHeight = []
96+
97+
##### the BuiltinFont terminalio.FONT has a different return strategy for get_glyphs and
98+
# is currently not handled by these functions.
99+
#fontList.append(terminalio.FONT)
100+
#fontHeight = [10] # somehow the terminalio.FONT needs to be adjusted to 10
101+
102+
# Load some proportional fonts
103+
fontFiles = [
104+
'fonts/Helvetica-Bold-16.bdf',
105+
# 'fonts/BitstreamVeraSans-Roman-24.bdf', # Header2
106+
# 'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText
107+
# 'fonts/Fayette-HandwrittenScript-64.bdf',
108+
]
109+
110+
from adafruit_bitmap_font import bitmap_font
111+
112+
for i, fontFile in enumerate(fontFiles):
113+
thisFont = bitmap_font.load_font(fontFile)
114+
115+
if use_builtin_font:
116+
thisFont=terminalio.FONT # comment this out to switch back to BDF loaded fonts
117+
118+
fontList.append(thisFont)
119+
fontHeight.append( thisFont.get_glyph(ord("M")).height )
120+
121+
122+
123+
preloadTheGlyphs= True # set this to True if you want to preload the font glyphs into memory
124+
# preloading the glyphs will help speed up the rendering of text but will use more RAM
125+
126+
if preloadTheGlyphs:
127+
128+
# identify the glyphs to load into memory -> increases rendering speed
129+
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! '
130+
131+
print('loading glyphs...')
132+
for font in fontList:
133+
if font is not terminalio.FONT:
134+
font.load_glyphs(glyphs)
135+
136+
print('Glyphs are loaded.')
137+
138+
139+
#for char in glyphs:
140+
# my_glyph=font.get_glyph(char)
141+
# print('char: {}, size x,y ({},{}) offset x,y ({},{})'.format(chr(char), my_glyph.width, my_glyph.height, my_glyph.dx, my_glyph.dy))
142+
143+
print('Fonts completed loading.')
144+
145+
# create group
146+
import gc
147+
148+
#tileGridList=[] # list of tileGrids
149+
#print( 'After creating Group, Memory free: {}'.format(gc.mem_free()) )
150+
151+
152+
myString12=('Bug Juice ({[]}) \'Monsters!\"\'\npuppy jump ({[]})')
153+
myString34='\n none '
154+
myString_bitmap_label='bitmap_label'
155+
myString_label='label bitmap_label'
156+
157+
158+
gc.collect()
159+
label_start_memory=gc.mem_free()
160+
bmap_label = label.Label(font=fontList[0], text=myString12, color=0xFFFFFF, max_glyphs=len(myString12),
161+
background_color=0xFF0000,
162+
padding_bottom=0,
163+
padding_left=0,
164+
padding_right=0,
165+
padding_top=0,
166+
background_tight=False,
167+
x=10,
168+
y=30,
169+
line_spacing=1.25,
170+
scale=my_scale,
171+
)
172+
173+
174+
bmap_label2 = label.Label(font=fontList[0], text=myString12, color=0xFFFFFF, max_glyphs=len(myString12),
175+
background_color=0xFF0000,
176+
padding_bottom=0,
177+
padding_left=0,
178+
padding_right=0,
179+
padding_top=0,
180+
background_tight=True,
181+
x=10,
182+
y=90,
183+
line_spacing=1.25,
184+
scale=my_scale,
185+
#anchored_position=(10,60),
186+
)
187+
label3_padding=0
188+
bmap_label3 = label.Label(font=fontList[0], text=myString34,
189+
color=0x000000,
190+
#color=0xFF00FF,
191+
max_glyphs=len(myString34),
192+
background_color=0xFFFF00,
193+
#background_color=None,
194+
padding_bottom=label3_padding,
195+
padding_left=label3_padding,
196+
padding_right=label3_padding,
197+
padding_top=label3_padding,
198+
background_tight=False,
199+
x=10,
200+
y=150,
201+
line_spacing=1.25,
202+
scale=my_scale,
203+
anchor_point=(0,0),
204+
anchored_position=(200,150),
205+
)
206+
207+
bmap_label4 = label.Label(font=fontList[0], text=myString34,
208+
color=0x000000,
209+
#color=0xFF00FF,
210+
max_glyphs=len(myString34),
211+
background_color=0xFFFF00,
212+
#background_color=None,
213+
padding_bottom=0,
214+
padding_left=0,
215+
padding_right=0,
216+
padding_top=0,
217+
background_tight=True,
218+
x=10,
219+
y=150,
220+
line_spacing=1.25,
221+
scale=my_scale,
222+
#anchored_position=(10,150),
223+
)
224+
225+
bmap_label5 = label.Label(font=fontList[0], text=myString_label, color=0x000000, max_glyphs=len(myString_label),
226+
background_color=0xFFFF00,
227+
padding_bottom=0,
228+
padding_left=0,
229+
padding_right=0,
230+
padding_top=0,
231+
background_tight=False,
232+
x=10,
233+
y=200,
234+
line_spacing=1.25,
235+
scale=my_scale,
236+
#anchored_position=(10,200),
237+
)
238+
239+
240+
bmap_label6 = label.Label(font=fontList[0], text=myString6, color=0xFFFFFF, max_glyphs=len(myString6),
241+
background_color=0x000000,
242+
padding_bottom=0,
243+
padding_left=0,
244+
padding_right=0,
245+
padding_top=0,
246+
background_tight=True,
247+
x=10,
248+
y=200,
249+
line_spacing=1.25,
250+
anchor_point=label6_anchor_point,
251+
anchored_position=label6_anchored_position,
252+
scale=my_scale,
253+
)
254+
255+
256+
257+
gc.collect()
258+
label_end_memory=gc.mem_free()
259+
260+
bmap_group = displayio.Group( max_size=6 ) # Create a group for displaying
261+
#bmap_group.append(bmap_label)
262+
#bmap_group.append(bmap_label2)
263+
#bmap_group.append(bmap_label3)
264+
bmap_group.append(bmap_label4)
265+
#bmap_group.append(bmap_label5)
266+
#bmap_group.append(bmap_label6)
267+
268+
269+
print('***')
270+
print('{} memory used: {}'.format(version, label_start_memory-label_end_memory))
271+
display.auto_refresh=True
272+
273+
#time.sleep(0.1)
274+
display.show(bmap_group)
275+
276+
while True:
277+
pass
278+
279+
280+

0 commit comments

Comments
 (0)