@@ -147,28 +147,17 @@ def _create_background_box(self, lines, y_offset):
147147 y_box_offset = self ._boundingbox [1 ]
148148
149149 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 ()
162151
163152 box_width = self ._boundingbox [2 ] + self ._padding_left + self ._padding_right
164153 x_box_offset = - self ._padding_left
165154 box_height = (
166- (ascender_max + descender_max )
155+ (ascent + descent )
167156 + int ((lines - 1 ) * self .height * self ._line_spacing )
168157 + self ._padding_top
169158 + self ._padding_bottom
170159 )
171- y_box_offset = - ascender_max + y_offset - self ._padding_top
160+ y_box_offset = - ascent + y_offset - self ._padding_top
172161
173162 box_width = max (0 , box_width ) # remove any negative values
174163 box_height = max (0 , box_height ) # remove any negative values
@@ -183,6 +172,29 @@ def _create_background_box(self, lines, y_offset):
183172
184173 return tile_grid
185174
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+
186198 def _update_background_color (self , new_color ):
187199
188200 if new_color is None :
@@ -196,7 +208,7 @@ def _update_background_color(self, new_color):
196208 self ._background_color = new_color
197209
198210 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
200212
201213 if not self ._added_background_tilegrid : # no bitmap is in the self Group
202214 # add bitmap if text is present and bitmap sizes > 0 pixels
@@ -248,13 +260,7 @@ def _update_text(
248260 i = 0
249261 tilegrid_count = i
250262
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
258264
259265 right = top = bottom = 0
260266 left = None
0 commit comments