Skip to content

Commit 1c0d724

Browse files
authored
Merge pull request #11 from tannewt/improve_preload
Speed up preloads with duplicate code points
2 parents 2f64725 + 57e9e98 commit 1c0d724

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 code_point in remaining:
98+
if code_point in self._glyphs and self._glyphs[code_point]:
99+
remaining.remove(code_point)
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)