|
1 | 1 | import base64
|
2 | 2 | import io
|
3 | 3 | import math
|
| 4 | +from functools import lru_cache |
4 | 5 |
|
5 | 6 | import numpy as np
|
6 | 7 | from matplotlib import __version__, interactive
|
|
10 | 11 | RendererBase,
|
11 | 12 | _Backend,
|
12 | 13 | )
|
13 |
| -from matplotlib.cbook import maxdict |
14 | 14 | from matplotlib.colors import colorConverter, rgb2hex
|
15 | 15 | from matplotlib.font_manager import findfont
|
16 | 16 | from matplotlib.ft2font import LOAD_NO_HINTING, FT2Font
|
@@ -204,8 +204,8 @@ def __init__(self, ctx, width, height, dpi, fig):
|
204 | 204 | self.ctx.width = self.width
|
205 | 205 | self.ctx.height = self.height
|
206 | 206 | self.dpi = dpi
|
207 |
| - self.fontd = maxdict(50) |
208 | 207 | self.mathtext_parser = MathTextParser("bitmap")
|
| 208 | + self._get_font_helper = lru_cache(maxsize=50)(self._get_font_helper) |
209 | 209 |
|
210 | 210 | # Keep the state of fontfaces that are loading
|
211 | 211 | self.fonts_loading = {}
|
@@ -309,22 +309,22 @@ def draw_image(self, gc, x, y, im, transform=None):
|
309 | 309 | pixels_proxy.destroy()
|
310 | 310 | pixels_buf.release()
|
311 | 311 |
|
| 312 | + def _get_font_helper(self, prop): |
| 313 | + """Cached font lookup |
| 314 | +
|
| 315 | + We wrap this in an lru-cache in the constructor. |
| 316 | + """ |
| 317 | + fname = findfont(prop) |
| 318 | + font = FT2Font(str(fname)) |
| 319 | + font_file_name = fname.rpartition("/")[-1] |
| 320 | + return (font, font_file_name) |
| 321 | + |
312 | 322 | def _get_font(self, prop):
|
313 |
| - key = hash(prop) |
314 |
| - font_value = self.fontd.get(key) |
315 |
| - if font_value is None: |
316 |
| - fname = findfont(prop) |
317 |
| - font_value = self.fontd.get(fname) |
318 |
| - if font_value is None: |
319 |
| - font = FT2Font(str(fname)) |
320 |
| - font_file_name = fname[fname.rfind("/") + 1 :] |
321 |
| - font_value = font, font_file_name |
322 |
| - self.fontd[fname] = font_value |
323 |
| - self.fontd[key] = font_value |
324 |
| - font, font_file_name = font_value |
| 323 | + result = self._get_font_helper(prop) |
| 324 | + font = result[0] |
325 | 325 | font.clear()
|
326 | 326 | font.set_size(prop.get_size_in_points(), self.dpi)
|
327 |
| - return font, font_file_name |
| 327 | + return result |
328 | 328 |
|
329 | 329 | def get_text_width_height_descent(self, s, prop, ismath):
|
330 | 330 | w: float
|
|
0 commit comments