Skip to content

Commit 572e952

Browse files
authored
Merge pull request #3080 from adafruit/TheKitty-patch-9
Create Display_BDF_PCF_Font.py
2 parents e579992 + ea96a5d commit 572e952

File tree

1 file changed

+110
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)