@@ -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,12 +135,7 @@ def load_glyphs(self, code_points):
121135 if not remaining :
122136 return
123137
124- try :
125- x , _ , _ , _ = self .get_bounding_box ()
126- except TypeError as error :
127- raise Exception (
128- "Font file does not have the FONTBOUNDINGBOX property. Try a different font"
129- ) from error
138+ x , _ , _ , _ = self ._boundingbox
130139
131140 self .file .seek (0 )
132141 while True :
@@ -140,8 +149,6 @@ def load_glyphs(self, code_points):
140149 elif line .startswith (b"COMMENT" ):
141150 pass
142151 elif line .startswith (b"STARTCHAR" ):
143- # print(lineno, line.strip())
144- # _, character_name = line.split()
145152 character = True
146153 elif line .startswith (b"ENDCHAR" ):
147154 character = False
@@ -213,5 +220,4 @@ def load_glyphs(self, code_points):
213220 x += 1
214221 current_y += 1
215222 elif metadata :
216- # print(lineno, line.strip())
217223 pass
0 commit comments