Skip to content

Commit 5d6ecb1

Browse files
authored
Merge pull request #43 from jposada202020/raise_error_no_boundingbox
Including exception regarding missing FONTBOUNDINGBOX parameter
2 parents b5fa275 + 85af48c commit 5d6ecb1

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

adafruit_bitmap_font/bdf.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(self, f, bitmap_class):
4343
line = str(line, "utf-8")
4444
if not line or not line.startswith("STARTFONT 2.1"):
4545
raise ValueError("Unsupported file version")
46+
self._verify_bounding_box()
4647
self.point_size = None
4748
self.x_resolution = None
4849
self.y_resolution = None
@@ -82,19 +83,32 @@ def ascent(self):
8283

8384
return self._ascent
8485

85-
def get_bounding_box(self):
86-
"""Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
86+
def _verify_bounding_box(self):
87+
"""Private function to verify FOUNTBOUNDINGBOX parameter
88+
This function will parse the first 10 lines of the font source
89+
file to verify the value or raise an exception in case is not found
90+
"""
8791
self.file.seek(0)
88-
while True:
92+
# Normally information about the FONT is in the first four lines.
93+
# Exception is when font file have a comment. Comments are three lines
94+
# 10 lines is a safe bet
95+
for _ in range(11):
8996
line = self.file.readline()
9097
line = str(line, "utf-8")
91-
if not line:
92-
break
93-
9498
if line.startswith("FONTBOUNDINGBOX "):
9599
_, x, y, x_offset, y_offset = line.split()
96-
return (int(x), int(y), int(x_offset), int(y_offset))
97-
return None
100+
self._boundingbox = (int(x), int(y), int(x_offset), int(y_offset))
101+
102+
try:
103+
self._boundingbox
104+
except AttributeError as error:
105+
raise Exception(
106+
"Source file does not have the FOUNTBONDINGBOX parameter"
107+
) from error
108+
109+
def get_bounding_box(self):
110+
"""Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
111+
return self._boundingbox
98112

99113
def load_glyphs(self, code_points):
100114
# pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals
@@ -121,7 +135,7 @@ def load_glyphs(self, code_points):
121135
if not remaining:
122136
return
123137

124-
x, _, _, _ = self.get_bounding_box()
138+
x, _, _, _ = self._boundingbox
125139

126140
self.file.seek(0)
127141
while True:
@@ -135,8 +149,6 @@ def load_glyphs(self, code_points):
135149
elif line.startswith(b"COMMENT"):
136150
pass
137151
elif line.startswith(b"STARTCHAR"):
138-
# print(lineno, line.strip())
139-
# _, character_name = line.split()
140152
character = True
141153
elif line.startswith(b"ENDCHAR"):
142154
character = False
@@ -208,5 +220,4 @@ def load_glyphs(self, code_points):
208220
x += 1
209221
current_y += 1
210222
elif metadata:
211-
# print(lineno, line.strip())
212223
pass

0 commit comments

Comments
 (0)