@@ -43,6 +43,7 @@ def __init__(self, f, bitmap_class):
43
43
line = str (line , "utf-8" )
44
44
if not line or not line .startswith ("STARTFONT 2.1" ):
45
45
raise ValueError ("Unsupported file version" )
46
+ self ._verify_bounding_box ()
46
47
self .point_size = None
47
48
self .x_resolution = None
48
49
self .y_resolution = None
@@ -82,19 +83,32 @@ def ascent(self):
82
83
83
84
return self ._ascent
84
85
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
+ """
87
91
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 ):
89
96
line = self .file .readline ()
90
97
line = str (line , "utf-8" )
91
- if not line :
92
- break
93
-
94
98
if line .startswith ("FONTBOUNDINGBOX " ):
95
99
_ , 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
98
112
99
113
def load_glyphs (self , code_points ):
100
114
# 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):
121
135
if not remaining :
122
136
return
123
137
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
130
139
131
140
self .file .seek (0 )
132
141
while True :
@@ -140,8 +149,6 @@ def load_glyphs(self, code_points):
140
149
elif line .startswith (b"COMMENT" ):
141
150
pass
142
151
elif line .startswith (b"STARTCHAR" ):
143
- # print(lineno, line.strip())
144
- # _, character_name = line.split()
145
152
character = True
146
153
elif line .startswith (b"ENDCHAR" ):
147
154
character = False
@@ -213,5 +220,4 @@ def load_glyphs(self, code_points):
213
220
x += 1
214
221
current_y += 1
215
222
elif metadata :
216
- # print(lineno, line.strip())
217
223
pass
0 commit comments