Skip to content

Commit f79585f

Browse files
committed
Add beginnings of PCF support
1 parent 1121366 commit f79585f

File tree

1 file changed

+142
-44
lines changed

1 file changed

+142
-44
lines changed

adafruit_bitmap_font/pcf.py

Lines changed: 142 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,160 @@
11
# pylint: skip-file
22
# Remove the above when PCF is actually supported.
33

4-
class BDF:
4+
from .glyph_cache import GlyphCache
5+
import displayio
6+
import struct
7+
8+
_PCF_PROPERTIES = (1<<0)
9+
_PCF_ACCELERATORS = (1<<1)
10+
_PCF_METRICS = (1<<2)
11+
_PCF_BITMAPS = (1<<3)
12+
_PCF_INK_METRICS = (1<<4)
13+
_PCF_BDF_ENCODINGS = (1<<5)
14+
_PCF_SWIDTHS = (1<<6)
15+
_PCF_GLYPH_NAMES = (1<<7)
16+
_PCF_BDF_ACCELERATORS = (1<<8)
17+
18+
_PCF_DEFAULT_FORMAT = 0x00000000
19+
_PCF_INKBOUNDS = 0x00000200
20+
_PCF_ACCEL_W_INKBOUNDS = 0x00000100
21+
_PCF_COMPRESSED_METRICS = 0x00000100
22+
23+
_PCF_GLYPH_PAD_MASK = (3<<0) # See the bitmap table for explanation */
24+
_PCF_BYTE_MASK = (1<<2) # If set then Most Sig Byte First */
25+
_PCF_BIT_MASK = (1<<3) # If set then Most Sig Bit First */
26+
_PCF_SCAN_UNIT_MASK = (3<<4)
27+
28+
# https://fontforge.github.io/en-US/documentation/reference/pcf-format/
29+
30+
class PCF(GlyphCache):
531
def __init__(self, f):
32+
super().__init__()
633
self.file = f
7-
34+
self.name = f
835
f.seek(0)
36+
header, table_count = self.read("<4sI")
37+
self.tables = {}
38+
for _ in range(table_count):
39+
type, format, size, offset = self.read("<IIII")
40+
self.tables[type] = {"format": format, "size": size, "offset": offset}
41+
print(type)
42+
43+
def read(self, format):
44+
s = struct.calcsize(format)
45+
return struct.unpack_from(format, self.file.read(s))
46+
47+
def get_bounding_box(self):
48+
property_table_offset = self.tables[_PCF_PROPERTIES]["offset"]
49+
self.file.seek(property_table_offset)
50+
format, = self.read("<I")
951

10-
self.characters = {}
52+
if format & _PCF_BYTE_MASK == 0:
53+
raise RuntimeError("Only big endian supported")
54+
nprops, = self.read(">I")
55+
self.file.seek(property_table_offset + 8 + 9 * nprops)
1156

57+
pos = self.file.tell()
58+
if pos % 4 > 0:
59+
self.file.read(4 - pos % 4)
60+
string_size, = self.read(">I")
61+
62+
strings = self.file.read(string_size)
63+
string_map = {}
64+
i = 0
65+
for s in strings.split(b"\x00"):
66+
string_map[i] = s
67+
i += len(s) + 1
68+
69+
self.file.seek(property_table_offset + 8)
70+
for _ in range(nprops):
71+
name_offset, isStringProp, value = self.read(">IBI")
72+
73+
if isStringProp:
74+
print(string_map[name_offset], string_map[value])
75+
else:
76+
print(string_map[name_offset], value)
77+
return None
78+
79+
def load_glyphs(self, code_points):
1280
metadata = True
1381
character = False
14-
bitmap_lines_left = 0
15-
bounds = None
16-
bitmap = None
1782
code_point = None
18-
character_name = None
19-
for lineno, line in enumerate(self.file.readlines()):
20-
if lineno == 0 and not line.startswith("STARTFONT 2.1"):
21-
raise ValueError("Unsupported file version")
22-
if line.startswith("CHARS "):
83+
rounded_x = 1
84+
bytes_per_row = 1
85+
desired_character = False
86+
current_info = None
87+
current_y = 0
88+
total_remaining = len(code_points)
89+
90+
x, _, _, _ = self.get_bounding_box()
91+
# create a scratch bytearray to load pixels into
92+
scratch_row = memoryview(bytearray((((x-1)//32)+1) * 4))
93+
94+
self.file.seek(0)
95+
while True:
96+
line = self.file.readline()
97+
if not line:
98+
break
99+
if line.startswith(b"CHARS "):
23100
metadata = False
24-
if line.startswith("SIZE"):
101+
elif line.startswith(b"SIZE"):
25102
_, self.point_size, self.x_resolution, self.y_resolution = line.split()
26-
elif line.startswith("COMMENT"):
27-
token, comment = line.split(" ", 1)
28-
print(comment.strip("\n\""))
29-
elif line.startswith("STARTCHAR"):
30-
print(lineno, line.strip())
31-
_, character_name = line.split()
103+
elif line.startswith(b"COMMENT"):
104+
pass
105+
elif line.startswith(b"STARTCHAR"):
106+
# print(lineno, line.strip())
107+
#_, character_name = line.split()
32108
character = True
33-
elif line.startswith("ENDCHAR"):
34-
character = False
35-
elif line.startswith("BBX"):
36-
_, x, y, dx, dy = line.split()
37-
x = int(x)
38-
y = int(y)
39-
dx = int(dx)
40-
dy = int(dy)
41-
bounds = (x, y, dx, dy)
42-
character = False
43-
elif line.startswith("BITMAP"):
109+
elif line.startswith(b"ENDCHAR"):
44110
character = False
45-
bitmap_lines_left = bounds[1]
46-
bitmap = []
47-
elif line.startswith("ENCODING"):
111+
if desired_character:
112+
self._glyphs[code_point] = current_info
113+
if total_remaining == 0:
114+
return
115+
desired_character = False
116+
elif line.startswith(b"BBX"):
117+
if desired_character:
118+
_, x, y, dx, dy = line.split()
119+
x = int(x)
120+
y = int(y)
121+
dx = int(dx)
122+
dy = int(dy)
123+
current_info["bounds"] = (x, y, dx, dy)
124+
current_info["bitmap"] = displayio.Bitmap(x, y, 2)
125+
elif line.startswith(b"BITMAP"):
126+
if desired_character:
127+
rounded_x = x // 8
128+
if x % 8 > 0:
129+
rounded_x += 1
130+
bytes_per_row = rounded_x
131+
if bytes_per_row % 4 > 0:
132+
bytes_per_row += 4 - bytes_per_row % 4
133+
current_y = 0
134+
elif line.startswith(b"ENCODING"):
48135
_, code_point = line.split()
49136
code_point = int(code_point)
50-
print(hex(code_point))
51-
elif bitmap_lines_left > 0:
52-
bits = int(line.strip(), 16)
53-
shift = 8 - bounds[0]
54-
bits >>= shift
55-
pixels = ("{0:0" + str(bounds[0]) +"b}").format(bits).replace("0", " ")
56-
bitmap.append(pixels)
57-
bitmap_lines_left -= 1
58-
59-
if bitmap_lines_left == 0:
60-
self.characters[code_point] = {"name": character_name, "bitmap": bitmap}
137+
if code_point == code_points or code_point in code_points:
138+
total_remaining -= 1
139+
if code_point not in self._glyphs:
140+
desired_character = True
141+
current_info = {"bitmap": None, "bounds": None, "shift": None}
142+
elif line.startswith(b"DWIDTH"):
143+
if desired_character:
144+
_, shift_x, shift_y = line.split()
145+
shift_x = int(shift_x)
146+
shift_y = int(shift_y)
147+
current_info["shift"] = (shift_x, shift_y)
148+
elif line.startswith(b"SWIDTH"):
149+
pass
150+
elif character:
151+
if desired_character:
152+
bits = int(line.strip(), 16)
153+
for i in range(rounded_x):
154+
val = (bits >> ((rounded_x-i-1)*8)) & 0xFF
155+
scratch_row[i] = val
156+
current_info["bitmap"]._load_row(current_y, scratch_row[:bytes_per_row])
157+
current_y += 1
61158
elif metadata:
62-
print(lineno, line.strip())
159+
#print(lineno, line.strip())
160+
pass

0 commit comments

Comments
 (0)