Skip to content

Commit fc75a1a

Browse files
committed
Speed up preloads with duplicate code points
The original logic wouldn't recognize duplicate characters in the given set to preload and therefore it'd read the whole font file looking for a character it already found. A set deduplicates the given characters at a small memory cost.
1 parent 2f64725 commit fc75a1a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

adafruit_bitmap_font/bdf.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,16 @@ def load_glyphs(self, code_points):
8989
current_info = {}
9090
current_y = 0
9191
rounded_x = 1
92-
total_remaining = len(code_points)
92+
if isinstance(code_points, int):
93+
remaining = set()
94+
remaining.add(code_points)
95+
else:
96+
remaining = set(code_points)
97+
for cp in remaining:
98+
if cp in self._glyphs and self._glyphs[cp]:
99+
remaining.remove(cp)
100+
if not remaining:
101+
return
93102

94103
x, _, _, _ = self.get_bounding_box()
95104

@@ -113,6 +122,7 @@ def load_glyphs(self, code_points):
113122
if desired_character:
114123
bounds = current_info["bounds"]
115124
shift = current_info["shift"]
125+
gc.collect()
116126
self._glyphs[code_point] = Glyph(current_info["bitmap"],
117127
0,
118128
bounds[0],
@@ -121,8 +131,8 @@ def load_glyphs(self, code_points):
121131
bounds[3],
122132
shift[0],
123133
shift[1])
124-
gc.collect()
125-
if total_remaining == 0:
134+
remaining.remove(code_point)
135+
if not remaining:
126136
return
127137
desired_character = False
128138
elif line.startswith(b"BBX"):
@@ -146,11 +156,9 @@ def load_glyphs(self, code_points):
146156
elif line.startswith(b"ENCODING"):
147157
_, code_point = line.split()
148158
code_point = int(code_point)
149-
if code_point == code_points or code_point in code_points:
150-
total_remaining -= 1
151-
if code_point not in self._glyphs or not self._glyphs[code_point]:
152-
desired_character = True
153-
current_info = {"bitmap": None, "bounds": None, "shift": None}
159+
if code_point in remaining:
160+
desired_character = True
161+
current_info = {"bitmap": None, "bounds": None, "shift": None}
154162
elif line.startswith(b"DWIDTH"):
155163
if desired_character:
156164
_, shift_x, shift_y = line.split()

0 commit comments

Comments
 (0)