Skip to content

Commit f245bf8

Browse files
authored
Create Display_BDF_PCF_Font.py
For Guide
1 parent e579992 commit f245bf8

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# SPDX-FileCopyrightText: 2025 Anne Barela for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Take a BDF or PCF font file and display it on a CircuitPython
6+
# HDMI/DVI display in 320x200 resolution. "." is used for unknown characters
7+
8+
import gc
9+
import terminalio
10+
import displayio
11+
import supervisor
12+
from adafruit_bitmap_font import bitmap_font
13+
from adafruit_display_text import label
14+
from adafruit_fruitjam.peripherals import request_display_config
15+
16+
# Use the easy library call to set the resolution
17+
request_display_config(320, 240)
18+
19+
# Initialize display
20+
display = supervisor.runtime.display
21+
main_group = displayio.Group()
22+
display.root_group = main_group
23+
24+
# Use Labels to display characters that exist in font
25+
font_file = "fonts/cp437-8x12a.pcf"
26+
font = bitmap_font.load_font(font_file) # Use font = terminalio.FONT for built-in
27+
char_width = 8 # font character width
28+
char_height = 12 # font character height
29+
chars_per_line = 32 # Fixed at 32 characters per line
30+
line_number_width = 35 # Space for line numbers like "000: "
31+
32+
displayed_count = 0
33+
skipped_count = 0
34+
35+
current_x = line_number_width # Start after line number space
36+
current_y = char_height
37+
current_line_start = 0
38+
39+
# Add first line number
40+
line_label = label.Label(
41+
font,
42+
text=f"{current_line_start:03d}: ",
43+
color=0xFFFFFF,
44+
x=0,
45+
y=current_y
46+
)
47+
main_group.append(line_label)
48+
49+
# Try all characters from 0-255 and display ones that exist
50+
for char_code in range(256):
51+
try:
52+
# Check if we need to wrap to next line
53+
if (char_code > 0) and (char_code % chars_per_line == 0):
54+
current_x = line_number_width # Reset to after line number
55+
current_y += char_height + 2 # Add some line spacing
56+
current_line_start = char_code
57+
58+
# Stop if we run out of vertical space
59+
if current_y + char_height > display.height:
60+
print(f"Display full, stopped at {char_code}")
61+
break
62+
63+
# Add line number for this new line
64+
line_label = label.Label(
65+
font,
66+
text=f"{current_line_start:03d}: ",
67+
color=0xFFFFFF,
68+
x=0,
69+
y=current_y
70+
)
71+
main_group.append(line_label)
72+
73+
# Check if glyph exists
74+
glyph = font.get_glyph(char_code)
75+
76+
if glyph is None:
77+
# No glyph available - display a period instead
78+
display_char = "."
79+
skipped_count += 1
80+
else:
81+
# Glyph exists - display the actual character
82+
display_char = chr(char_code)
83+
84+
# Create label for this character (or replacement)
85+
char_label = label.Label(
86+
font,
87+
text=display_char,
88+
color=0xFFFFFF,
89+
x=current_x,
90+
y=current_y
91+
)
92+
93+
main_group.append(char_label)
94+
current_x += char_width
95+
displayed_count += 1
96+
97+
except (MemoryError, ValueError) as e:
98+
print(f"Memory limit reached at char {char_code}")
99+
break
100+
101+
# Garbage collection every 16 characters
102+
if char_code % 16 == 0:
103+
gc.collect()
104+
105+
print(f"Found {displayed_count - skipped_count} chars with glyphs")
106+
print(f"{skipped_count} missing chars -> periods")
107+
print(f"Free memory: {gc.mem_free()} bytes")
108+
109+
# Keep display active
110+
while True:
111+
pass

0 commit comments

Comments
 (0)