From 5a8c9e3190bf2a29193370cbf49f87631c4551d5 Mon Sep 17 00:00:00 2001 From: Adafruit Adabot Date: Sat, 3 Apr 2021 18:16:46 -0400 Subject: [PATCH 1/2] Including exception regarding missing FONTBOUNDINGBOX parameter in the font file --- adafruit_bitmap_font/bdf.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/adafruit_bitmap_font/bdf.py b/adafruit_bitmap_font/bdf.py index 4412817..9227e46 100644 --- a/adafruit_bitmap_font/bdf.py +++ b/adafruit_bitmap_font/bdf.py @@ -121,7 +121,12 @@ def load_glyphs(self, code_points): if not remaining: return - x, _, _, _ = self.get_bounding_box() + try: + x, _, _, _ = self.get_bounding_box() + except TypeError as error: + raise Exception( + "Font file does not have the FONTBOUNDINGBOX property. Try a different font" + ) from error self.file.seek(0) while True: From 85af48c8e4db4783e3047201c762bdcd85b6c9ac Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Wed, 7 Apr 2021 13:12:23 -0400 Subject: [PATCH 2/2] Verifying FOUNTBONDINGBOX at __init__ --- adafruit_bitmap_font/bdf.py | 40 +++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/adafruit_bitmap_font/bdf.py b/adafruit_bitmap_font/bdf.py index 9227e46..e15ab0c 100644 --- a/adafruit_bitmap_font/bdf.py +++ b/adafruit_bitmap_font/bdf.py @@ -43,6 +43,7 @@ def __init__(self, f, bitmap_class): line = str(line, "utf-8") if not line or not line.startswith("STARTFONT 2.1"): raise ValueError("Unsupported file version") + self._verify_bounding_box() self.point_size = None self.x_resolution = None self.y_resolution = None @@ -82,19 +83,32 @@ def ascent(self): return self._ascent - def get_bounding_box(self): - """Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset""" + def _verify_bounding_box(self): + """Private function to verify FOUNTBOUNDINGBOX parameter + This function will parse the first 10 lines of the font source + file to verify the value or raise an exception in case is not found + """ self.file.seek(0) - while True: + # Normally information about the FONT is in the first four lines. + # Exception is when font file have a comment. Comments are three lines + # 10 lines is a safe bet + for _ in range(11): line = self.file.readline() line = str(line, "utf-8") - if not line: - break - if line.startswith("FONTBOUNDINGBOX "): _, x, y, x_offset, y_offset = line.split() - return (int(x), int(y), int(x_offset), int(y_offset)) - return None + self._boundingbox = (int(x), int(y), int(x_offset), int(y_offset)) + + try: + self._boundingbox + except AttributeError as error: + raise Exception( + "Source file does not have the FOUNTBONDINGBOX parameter" + ) from error + + def get_bounding_box(self): + """Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset""" + return self._boundingbox def load_glyphs(self, code_points): # pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals @@ -121,12 +135,7 @@ def load_glyphs(self, code_points): if not remaining: return - try: - x, _, _, _ = self.get_bounding_box() - except TypeError as error: - raise Exception( - "Font file does not have the FONTBOUNDINGBOX property. Try a different font" - ) from error + x, _, _, _ = self._boundingbox self.file.seek(0) while True: @@ -140,8 +149,6 @@ def load_glyphs(self, code_points): elif line.startswith(b"COMMENT"): pass elif line.startswith(b"STARTCHAR"): - # print(lineno, line.strip()) - # _, character_name = line.split() character = True elif line.startswith(b"ENDCHAR"): character = False @@ -213,5 +220,4 @@ def load_glyphs(self, code_points): x += 1 current_y += 1 elif metadata: - # print(lineno, line.strip()) pass