From fc75a1aade9c46b7cd59609017897fc097feb8de Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 21 May 2019 15:46:10 -0700 Subject: [PATCH 1/2] 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. --- adafruit_bitmap_font/bdf.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/adafruit_bitmap_font/bdf.py b/adafruit_bitmap_font/bdf.py index fea1470..1e340b6 100644 --- a/adafruit_bitmap_font/bdf.py +++ b/adafruit_bitmap_font/bdf.py @@ -89,7 +89,16 @@ def load_glyphs(self, code_points): current_info = {} current_y = 0 rounded_x = 1 - total_remaining = len(code_points) + if isinstance(code_points, int): + remaining = set() + remaining.add(code_points) + else: + remaining = set(code_points) + for cp in remaining: + if cp in self._glyphs and self._glyphs[cp]: + remaining.remove(cp) + if not remaining: + return x, _, _, _ = self.get_bounding_box() @@ -113,6 +122,7 @@ def load_glyphs(self, code_points): if desired_character: bounds = current_info["bounds"] shift = current_info["shift"] + gc.collect() self._glyphs[code_point] = Glyph(current_info["bitmap"], 0, bounds[0], @@ -121,8 +131,8 @@ def load_glyphs(self, code_points): bounds[3], shift[0], shift[1]) - gc.collect() - if total_remaining == 0: + remaining.remove(code_point) + if not remaining: return desired_character = False elif line.startswith(b"BBX"): @@ -146,11 +156,9 @@ def load_glyphs(self, code_points): elif line.startswith(b"ENCODING"): _, code_point = line.split() code_point = int(code_point) - if code_point == code_points or code_point in code_points: - total_remaining -= 1 - if code_point not in self._glyphs or not self._glyphs[code_point]: - desired_character = True - current_info = {"bitmap": None, "bounds": None, "shift": None} + if code_point in remaining: + desired_character = True + current_info = {"bitmap": None, "bounds": None, "shift": None} elif line.startswith(b"DWIDTH"): if desired_character: _, shift_x, shift_y = line.split() From 57e9e98bfd18c330bf1c4a233dc31d35d59664b3 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 21 May 2019 16:02:22 -0700 Subject: [PATCH 2/2] Rename variable for lint. --- adafruit_bitmap_font/bdf.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_bitmap_font/bdf.py b/adafruit_bitmap_font/bdf.py index 1e340b6..e38283d 100644 --- a/adafruit_bitmap_font/bdf.py +++ b/adafruit_bitmap_font/bdf.py @@ -94,9 +94,9 @@ def load_glyphs(self, code_points): remaining.add(code_points) else: remaining = set(code_points) - for cp in remaining: - if cp in self._glyphs and self._glyphs[cp]: - remaining.remove(cp) + for code_point in remaining: + if code_point in self._glyphs and self._glyphs[code_point]: + remaining.remove(code_point) if not remaining: return