|
1 | 1 | import atexit
|
2 | 2 | import codecs
|
| 3 | +import functools |
3 | 4 | import logging
|
4 | 5 | import math
|
5 | 6 | import os
|
@@ -187,6 +188,7 @@ def __init__(self, message, latex_output=""):
|
187 | 188 | self.latex_output = latex_output
|
188 | 189 |
|
189 | 190 |
|
| 191 | +@cbook.deprecated("3.1") |
190 | 192 | class LatexManagerFactory:
|
191 | 193 | previous_instance = None
|
192 | 194 |
|
@@ -223,14 +225,31 @@ def _build_latex_header():
|
223 | 225 | # Create LaTeX header with some content, else LaTeX will load some math
|
224 | 226 | # fonts later when we don't expect the additional output on stdout.
|
225 | 227 | # TODO: is this sufficient?
|
226 |
| - latex_header = [r"\documentclass{minimal}", |
227 |
| - latex_preamble, |
228 |
| - latex_fontspec, |
229 |
| - r"\begin{document}", |
230 |
| - r"text $math \mu$", # force latex to load fonts now |
231 |
| - r"\typeout{pgf_backend_query_start}"] |
| 228 | + latex_header = [ |
| 229 | + # Include TeX program name as a comment for cache invalidation. |
| 230 | + r"% !TeX program = {}".format(rcParams["pgf.texsystem"]), |
| 231 | + r"\documentclass{minimal}", |
| 232 | + latex_preamble, |
| 233 | + latex_fontspec, |
| 234 | + r"\begin{document}", |
| 235 | + r"text $math \mu$", # force latex to load fonts now |
| 236 | + r"\typeout{pgf_backend_query_start}", |
| 237 | + ] |
232 | 238 | return "\n".join(latex_header)
|
233 | 239 |
|
| 240 | + @classmethod |
| 241 | + def _get_cached_or_new(cls): |
| 242 | + """ |
| 243 | + Return the previous LatexManager if the header and tex system did not |
| 244 | + change, or a new instance otherwise. |
| 245 | + """ |
| 246 | + return cls._get_cached_or_new_impl(cls._build_latex_header()) |
| 247 | + |
| 248 | + @classmethod |
| 249 | + @functools.lru_cache(1) |
| 250 | + def _get_cached_or_new_impl(cls, header): # Helper for _get_cached_or_new. |
| 251 | + return cls() |
| 252 | + |
234 | 253 | @staticmethod
|
235 | 254 | def _cleanup_remaining_instances():
|
236 | 255 | unclean_instances = list(LatexManager._unclean_instances)
|
@@ -388,7 +407,7 @@ def __init__(self, figure, fh, dummy=False):
|
388 | 407 | self.image_counter = 0
|
389 | 408 |
|
390 | 409 | # get LatexManager instance
|
391 |
| - self.latexManager = LatexManagerFactory.get_latex_manager() |
| 410 | + self.latexManager = LatexManager._get_cached_or_new() |
392 | 411 |
|
393 | 412 | if dummy:
|
394 | 413 | # dummy==True deactivate all methods
|
|
0 commit comments