-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfont_simpletest.py
73 lines (57 loc) · 2.14 KB
/
font_simpletest.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
"""
font_simpletest.py -- Simple test of the Font class.
inspired by Russ Hughes's hello.py
Draws on a framebuffer and blits it to the display.
"""
from board_config import display_drv
import random
from graphics import Font, FrameBuffer, RGB565
from palettes import get_palette
BPP = display_drv.color_depth // 8 # Bytes per pixel
def write(font, string, x, y, fg_color, bg_color, scale):
"""
Write text to the display.
"""
buffer_width = font.width * scale * len(string)
buffer_height = font.height * scale
buffer = bytearray(buffer_width * buffer_height * BPP)
fb = FrameBuffer(buffer, buffer_width, buffer_height, RGB565)
fb.fill(bg_color)
font.text(fb, string, 0, 0, fg_color, scale)
display_drv.blit_rect(buffer, x, y, buffer_width, buffer_height)
def main():
"""
The big show!
"""
pal = get_palette()
write_text = "Hello!"
text_len = len(write_text)
iterations = 96
directory = "examples/assets/"
font1 = Font(f"{directory}font_8x8.bin")
font2 = Font(f"{directory}font_8x14.bin")
font3 = Font(f"{directory}font_8x16.bin")
fonts = [font1, font2, font3]
max_width = max([font.width for font in fonts])
max_height = max([font.height for font in fonts])
while True:
for rotation in range(4):
scale = rotation + 1
display_drv.rotation = rotation * 90
width, height = display_drv.width, display_drv.height
# display_drv.fill_rect(0, 0, width, height, 0x0000)
col_max = width - max_width * scale * text_len
row_max = height - max_height * scale
if col_max < 0 or row_max < 0:
raise RuntimeError("This font is too big to display on this screen.")
for _ in range(iterations):
write(
fonts[random.randint(0, len(fonts) - 1)],
write_text,
random.randint(0, col_max),
random.randint(0, row_max),
pal[random.randint(0, len(pal) - 1)],
pal[random.randint(0, len(pal) - 1)],
scale,
)
main()