@@ -430,6 +430,11 @@ def __init__(self, default_font_prop, mathtext_backend):
430430 self .mathtext_backend .fonts_object = self
431431 self .used_characters = {}
432432
433+ def destroy (self ):
434+ """Fix any cyclical references before the object is about
435+ to be destroyed."""
436+ self .used_characters = None
437+
433438 def get_kern (self , font1 , sym1 , fontsize1 ,
434439 font2 , sym2 , fontsize2 , dpi ):
435440 """
@@ -461,7 +466,7 @@ def get_metrics(self, font, sym, fontsize, dpi):
461466 xmin, xmax, ymin, ymax - the ink rectangle of the glyph
462467 iceberg - the distance from the baseline to the top of the glyph.
463468 horiBearingY in Truetype parlance, height in TeX parlance
464- """
469+ """
465470 info = self ._get_info (font , sym , fontsize , dpi )
466471 return info .metrics
467472
@@ -494,19 +499,17 @@ def get_results(self, box):
494499 return self .mathtext_backend .get_results (box )
495500
496501 def get_sized_alternatives_for_symbol (self , fontname , sym ):
497- """Override if your font provides multiple sizes of the same
498- symbol."""
502+ """
503+ Override if your font provides multiple sizes of the same
504+ symbol.
505+ """
499506 return [(fontname , sym )]
500507
501508class TruetypeFonts (Fonts ):
502509 """
503510 A generic base class for all font setups that use Truetype fonts
504511 (through ft2font)
505512 """
506- """
507- Use the Bakoma true type fonts for rendering
508- """
509- # allocate a new set of fonts
510513 basepath = os .path .join ( get_data_path (), 'fonts' , 'ttf' )
511514
512515 class CachedFont :
@@ -529,6 +532,14 @@ def __init__(self, default_font_prop, mathtext_backend):
529532
530533 self .fonts ['default' ] = default_font
531534
535+ def destroy (self ):
536+ self .glyphd = None
537+ for cached_font in self .fonts .values ():
538+ cached_font .charmap = None
539+ cached_font .glyphmap = None
540+ cached_font .font = None
541+ Fonts .destroy (self )
542+
532543 def _get_font (self , font ):
533544 """Looks up a CachedFont with its charmap and inverse charmap.
534545 font may be a TeX font name (cal, rm, it etc.), or postscript name."""
@@ -2401,7 +2412,7 @@ def auto_sized_delimiter(self, s, loc, toks):
24012412##############################################################################
24022413# MAIN
24032414
2404- class MathTextParser :
2415+ class MathTextParser ( object ) :
24052416 """
24062417 Parse the math expression s, return the (bbox, fonts) tuple needed
24072418 to render it.
@@ -2453,7 +2464,10 @@ def parse(self, s, dpi, prop):
24532464 self ._cache [cacheKey ] = result
24542465 # Free up the transient data structures
24552466 self ._parser .clear ()
2456- # Remove a cyclical reference
2457- font_output .mathtext_backend .fonts_object = None
24582467
2468+ # Fix cyclical references
2469+ font_output .destroy ()
2470+ font_output .mathtext_backend .fonts_object = None
2471+ font_output .mathtext_backend = None
2472+
24592473 return result
0 commit comments