Skip to content

Including exception regarding missing FONTBOUNDINGBOX parameter #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions adafruit_bitmap_font/bdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -121,7 +135,7 @@ def load_glyphs(self, code_points):
if not remaining:
return

x, _, _, _ = self.get_bounding_box()
x, _, _, _ = self._boundingbox

self.file.seek(0)
while True:
Expand All @@ -135,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
Expand Down Expand Up @@ -208,5 +220,4 @@ def load_glyphs(self, code_points):
x += 1
current_y += 1
elif metadata:
# print(lineno, line.strip())
pass