@@ -147,28 +147,17 @@ def _create_background_box(self, lines, y_offset):
147
147
y_box_offset = self ._boundingbox [1 ]
148
148
149
149
else : # draw a "loose" bounding box to include any ascenders/descenders.
150
-
151
- # check a few glyphs for maximum ascender and descender height
152
- # Enhancement: it would be preferred to access the font "FONT_ASCENT" and
153
- # "FONT_DESCENT" in the imported BDF file
154
- glyphs = "M j'" # choose glyphs with highest ascender and lowest
155
- # descender, will depend upon font used
156
- ascender_max = descender_max = 0
157
- for char in glyphs :
158
- this_glyph = self ._font .get_glyph (ord (char ))
159
- if this_glyph :
160
- ascender_max = max (ascender_max , this_glyph .height + this_glyph .dy )
161
- descender_max = max (descender_max , - this_glyph .dy )
150
+ ascent , descent = self ._get_ascent_descent ()
162
151
163
152
box_width = self ._boundingbox [2 ] + self ._padding_left + self ._padding_right
164
153
x_box_offset = - self ._padding_left
165
154
box_height = (
166
- (ascender_max + descender_max )
155
+ (ascent + descent )
167
156
+ int ((lines - 1 ) * self .height * self ._line_spacing )
168
157
+ self ._padding_top
169
158
+ self ._padding_bottom
170
159
)
171
- y_box_offset = - ascender_max + y_offset - self ._padding_top
160
+ y_box_offset = - ascent + y_offset - self ._padding_top
172
161
173
162
box_width = max (0 , box_width ) # remove any negative values
174
163
box_height = max (0 , box_height ) # remove any negative values
@@ -183,6 +172,29 @@ def _create_background_box(self, lines, y_offset):
183
172
184
173
return tile_grid
185
174
175
+ def _get_ascent_descent (self ):
176
+ if hasattr (self .font , "ascent" ):
177
+ return self .font .ascent , self .font .descent
178
+
179
+ # check a few glyphs for maximum ascender and descender height
180
+ glyphs = "M j'" # choose glyphs with highest ascender and lowest
181
+ try :
182
+ self ._font .load_glyphs (glyphs )
183
+ except AttributeError :
184
+ # Builtin font doesn't have or need load_glyphs
185
+ pass
186
+ # descender, will depend upon font used
187
+ ascender_max = descender_max = 0
188
+ for char in glyphs :
189
+ this_glyph = self ._font .get_glyph (ord (char ))
190
+ if this_glyph :
191
+ ascender_max = max (ascender_max , this_glyph .height + this_glyph .dy )
192
+ descender_max = max (descender_max , - this_glyph .dy )
193
+ return ascender_max , descender_max
194
+
195
+ def _get_ascent (self ):
196
+ return self ._get_ascent_descent ()[0 ]
197
+
186
198
def _update_background_color (self , new_color ):
187
199
188
200
if new_color is None :
@@ -196,7 +208,7 @@ def _update_background_color(self, new_color):
196
208
self ._background_color = new_color
197
209
198
210
lines = self ._text .rstrip ("\n " ).count ("\n " ) + 1
199
- y_offset = int (( self ._font . get_glyph ( ord ( "M" )). height ) / 2 )
211
+ y_offset = self ._get_ascent () // 2
200
212
201
213
if not self ._added_background_tilegrid : # no bitmap is in the self Group
202
214
# add bitmap if text is present and bitmap sizes > 0 pixels
@@ -248,13 +260,7 @@ def _update_text(
248
260
i = 0
249
261
tilegrid_count = i
250
262
251
- try :
252
- self ._font .load_glyphs (new_text + "M" )
253
- except AttributeError :
254
- # ignore if font does not have load_glyphs
255
- pass
256
-
257
- y_offset = int ((self ._font .get_glyph (ord ("M" )).height ) / 2 )
263
+ y_offset = self ._get_ascent () // 2
258
264
259
265
right = top = bottom = 0
260
266
left = None
0 commit comments