texmanager.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. r"""
  2. Support for embedded TeX expressions in Matplotlib.
  3. Requirements:
  4. * LaTeX.
  5. * \*Agg backends: dvipng>=1.6.
  6. * PS backend: PSfrag, dvips, and Ghostscript>=9.0.
  7. * PDF and SVG backends: if LuaTeX is present, it will be used to speed up some
  8. post-processing steps, but note that it is not used to parse the TeX string
  9. itself (only LaTeX is supported).
  10. To enable TeX rendering of all text in your Matplotlib figure, set
  11. :rc:`text.usetex` to True.
  12. TeX and dvipng/dvips processing results are cached
  13. in ~/.matplotlib/tex.cache for reuse between sessions.
  14. `TexManager.get_rgba` can also be used to directly obtain raster output as RGBA
  15. NumPy arrays.
  16. """
  17. import functools
  18. import hashlib
  19. import logging
  20. import os
  21. from pathlib import Path
  22. import subprocess
  23. from tempfile import TemporaryDirectory
  24. import numpy as np
  25. import matplotlib as mpl
  26. from matplotlib import cbook, dviread
  27. _log = logging.getLogger(__name__)
  28. def _usepackage_if_not_loaded(package, *, option=None):
  29. """
  30. Output LaTeX code that loads a package (possibly with an option) if it
  31. hasn't been loaded yet.
  32. LaTeX cannot load twice a package with different options, so this helper
  33. can be used to protect against users loading arbitrary packages/options in
  34. their custom preamble.
  35. """
  36. option = f"[{option}]" if option is not None else ""
  37. return (
  38. r"\makeatletter"
  39. r"\@ifpackageloaded{%(package)s}{}{\usepackage%(option)s{%(package)s}}"
  40. r"\makeatother"
  41. ) % {"package": package, "option": option}
  42. class TexManager:
  43. """
  44. Convert strings to dvi files using TeX, caching the results to a directory.
  45. The cache directory is called ``tex.cache`` and is located in the directory
  46. returned by `.get_cachedir`.
  47. Repeated calls to this constructor always return the same instance.
  48. """
  49. _texcache = os.path.join(mpl.get_cachedir(), 'tex.cache')
  50. _grey_arrayd = {}
  51. _font_families = ('serif', 'sans-serif', 'cursive', 'monospace')
  52. _font_preambles = {
  53. 'new century schoolbook': r'\renewcommand{\rmdefault}{pnc}',
  54. 'bookman': r'\renewcommand{\rmdefault}{pbk}',
  55. 'times': r'\usepackage{mathptmx}',
  56. 'palatino': r'\usepackage{mathpazo}',
  57. 'zapf chancery': r'\usepackage{chancery}',
  58. 'cursive': r'\usepackage{chancery}',
  59. 'charter': r'\usepackage{charter}',
  60. 'serif': '',
  61. 'sans-serif': '',
  62. 'helvetica': r'\usepackage{helvet}',
  63. 'avant garde': r'\usepackage{avant}',
  64. 'courier': r'\usepackage{courier}',
  65. # Loading the type1ec package ensures that cm-super is installed, which
  66. # is necessary for Unicode computer modern. (It also allows the use of
  67. # computer modern at arbitrary sizes, but that's just a side effect.)
  68. 'monospace': r'\usepackage{type1ec}',
  69. 'computer modern roman': r'\usepackage{type1ec}',
  70. 'computer modern sans serif': r'\usepackage{type1ec}',
  71. 'computer modern typewriter': r'\usepackage{type1ec}',
  72. }
  73. _font_types = {
  74. 'new century schoolbook': 'serif',
  75. 'bookman': 'serif',
  76. 'times': 'serif',
  77. 'palatino': 'serif',
  78. 'zapf chancery': 'cursive',
  79. 'charter': 'serif',
  80. 'helvetica': 'sans-serif',
  81. 'avant garde': 'sans-serif',
  82. 'courier': 'monospace',
  83. 'computer modern roman': 'serif',
  84. 'computer modern sans serif': 'sans-serif',
  85. 'computer modern typewriter': 'monospace',
  86. }
  87. @functools.lru_cache # Always return the same instance.
  88. def __new__(cls):
  89. Path(cls._texcache).mkdir(parents=True, exist_ok=True)
  90. return object.__new__(cls)
  91. @classmethod
  92. def _get_font_family_and_reduced(cls):
  93. """Return the font family name and whether the font is reduced."""
  94. ff = mpl.rcParams['font.family']
  95. ff_val = ff[0].lower() if len(ff) == 1 else None
  96. if len(ff) == 1 and ff_val in cls._font_families:
  97. return ff_val, False
  98. elif len(ff) == 1 and ff_val in cls._font_preambles:
  99. return cls._font_types[ff_val], True
  100. else:
  101. _log.info('font.family must be one of (%s) when text.usetex is '
  102. 'True. serif will be used by default.',
  103. ', '.join(cls._font_families))
  104. return 'serif', False
  105. @classmethod
  106. def _get_font_preamble_and_command(cls):
  107. requested_family, is_reduced_font = cls._get_font_family_and_reduced()
  108. preambles = {}
  109. for font_family in cls._font_families:
  110. if is_reduced_font and font_family == requested_family:
  111. preambles[font_family] = cls._font_preambles[
  112. mpl.rcParams['font.family'][0].lower()]
  113. else:
  114. rcfonts = mpl.rcParams[f"font.{font_family}"]
  115. for i, font in enumerate(map(str.lower, rcfonts)):
  116. if font in cls._font_preambles:
  117. preambles[font_family] = cls._font_preambles[font]
  118. _log.debug(
  119. 'family: %s, package: %s, font: %s, skipped: %s',
  120. font_family, cls._font_preambles[font], rcfonts[i],
  121. ', '.join(rcfonts[:i]),
  122. )
  123. break
  124. else:
  125. _log.info('No LaTeX-compatible font found for the %s font'
  126. 'family in rcParams. Using default.',
  127. font_family)
  128. preambles[font_family] = cls._font_preambles[font_family]
  129. # The following packages and commands need to be included in the latex
  130. # file's preamble:
  131. cmd = {preambles[family]
  132. for family in ['serif', 'sans-serif', 'monospace']}
  133. if requested_family == 'cursive':
  134. cmd.add(preambles['cursive'])
  135. cmd.add(r'\usepackage{type1cm}')
  136. preamble = '\n'.join(sorted(cmd))
  137. fontcmd = (r'\sffamily' if requested_family == 'sans-serif' else
  138. r'\ttfamily' if requested_family == 'monospace' else
  139. r'\rmfamily')
  140. return preamble, fontcmd
  141. @classmethod
  142. def get_basefile(cls, tex, fontsize, dpi=None):
  143. """
  144. Return a filename based on a hash of the string, fontsize, and dpi.
  145. """
  146. src = cls._get_tex_source(tex, fontsize) + str(dpi)
  147. filehash = hashlib.sha256(
  148. src.encode('utf-8'),
  149. usedforsecurity=False
  150. ).hexdigest()
  151. filepath = Path(cls._texcache)
  152. num_letters, num_levels = 2, 2
  153. for i in range(0, num_letters*num_levels, num_letters):
  154. filepath = filepath / Path(filehash[i:i+2])
  155. filepath.mkdir(parents=True, exist_ok=True)
  156. return os.path.join(filepath, filehash)
  157. @classmethod
  158. def get_font_preamble(cls):
  159. """
  160. Return a string containing font configuration for the tex preamble.
  161. """
  162. font_preamble, command = cls._get_font_preamble_and_command()
  163. return font_preamble
  164. @classmethod
  165. def get_custom_preamble(cls):
  166. """Return a string containing user additions to the tex preamble."""
  167. return mpl.rcParams['text.latex.preamble']
  168. @classmethod
  169. def _get_tex_source(cls, tex, fontsize):
  170. """Return the complete TeX source for processing a TeX string."""
  171. font_preamble, fontcmd = cls._get_font_preamble_and_command()
  172. baselineskip = 1.25 * fontsize
  173. return "\n".join([
  174. r"\documentclass{article}",
  175. r"% Pass-through \mathdefault, which is used in non-usetex mode",
  176. r"% to use the default text font but was historically suppressed",
  177. r"% in usetex mode.",
  178. r"\newcommand{\mathdefault}[1]{#1}",
  179. font_preamble,
  180. r"\usepackage[utf8]{inputenc}",
  181. r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
  182. r"% geometry is loaded before the custom preamble as ",
  183. r"% convert_psfrags relies on a custom preamble to change the ",
  184. r"% geometry.",
  185. r"\usepackage[papersize=72in, margin=1in]{geometry}",
  186. cls.get_custom_preamble(),
  187. r"% Use `underscore` package to take care of underscores in text.",
  188. r"% The [strings] option allows to use underscores in file names.",
  189. _usepackage_if_not_loaded("underscore", option="strings"),
  190. r"% Custom packages (e.g. newtxtext) may already have loaded ",
  191. r"% textcomp with different options.",
  192. _usepackage_if_not_loaded("textcomp"),
  193. r"\pagestyle{empty}",
  194. r"\begin{document}",
  195. r"% The empty hbox ensures that a page is printed even for empty",
  196. r"% inputs, except when using psfrag which gets confused by it.",
  197. r"% matplotlibbaselinemarker is used by dviread to detect the",
  198. r"% last line's baseline.",
  199. rf"\fontsize{{{fontsize}}}{{{baselineskip}}}%",
  200. r"\ifdefined\psfrag\else\hbox{}\fi%",
  201. rf"{{{fontcmd} {tex}}}%",
  202. r"\end{document}",
  203. ])
  204. @classmethod
  205. def make_tex(cls, tex, fontsize):
  206. """
  207. Generate a tex file to render the tex string at a specific font size.
  208. Return the file name.
  209. """
  210. texfile = cls.get_basefile(tex, fontsize) + ".tex"
  211. Path(texfile).write_text(cls._get_tex_source(tex, fontsize),
  212. encoding='utf-8')
  213. return texfile
  214. @classmethod
  215. def _run_checked_subprocess(cls, command, tex, *, cwd=None):
  216. _log.debug(cbook._pformat_subprocess(command))
  217. try:
  218. report = subprocess.check_output(
  219. command, cwd=cwd if cwd is not None else cls._texcache,
  220. stderr=subprocess.STDOUT)
  221. except FileNotFoundError as exc:
  222. raise RuntimeError(
  223. f'Failed to process string with tex because {command[0]} '
  224. 'could not be found') from exc
  225. except subprocess.CalledProcessError as exc:
  226. raise RuntimeError(
  227. '{prog} was not able to process the following string:\n'
  228. '{tex!r}\n\n'
  229. 'Here is the full command invocation and its output:\n\n'
  230. '{format_command}\n\n'
  231. '{exc}\n\n'.format(
  232. prog=command[0],
  233. format_command=cbook._pformat_subprocess(command),
  234. tex=tex.encode('unicode_escape'),
  235. exc=exc.output.decode('utf-8', 'backslashreplace'))
  236. ) from None
  237. _log.debug(report)
  238. return report
  239. @classmethod
  240. def make_dvi(cls, tex, fontsize):
  241. """
  242. Generate a dvi file containing latex's layout of tex string.
  243. Return the file name.
  244. """
  245. dvifile = Path(cls.get_basefile(tex, fontsize)).with_suffix(".dvi")
  246. if not dvifile.exists():
  247. # Generate the tex and dvi in a temporary directory to avoid race
  248. # conditions e.g. if multiple processes try to process the same tex
  249. # string at the same time. Having tmpdir be a subdirectory of the
  250. # final output dir ensures that they are on the same filesystem,
  251. # and thus replace() works atomically. It also allows referring to
  252. # the texfile with a relative path (for pathological MPLCONFIGDIRs,
  253. # the absolute path may contain characters (e.g. ~) that TeX does
  254. # not support; n.b. relative paths cannot traverse parents, or it
  255. # will be blocked when `openin_any = p` in texmf.cnf).
  256. with TemporaryDirectory(dir=dvifile.parent) as tmpdir:
  257. Path(tmpdir, "file.tex").write_text(
  258. cls._get_tex_source(tex, fontsize), encoding='utf-8')
  259. cls._run_checked_subprocess(
  260. ["latex", "-interaction=nonstopmode", "--halt-on-error",
  261. "file.tex"], tex, cwd=tmpdir)
  262. Path(tmpdir, "file.dvi").replace(dvifile)
  263. # Also move the tex source to the main cache directory, but
  264. # only for backcompat.
  265. Path(tmpdir, "file.tex").replace(dvifile.with_suffix(".tex"))
  266. return str(dvifile)
  267. @classmethod
  268. def make_png(cls, tex, fontsize, dpi):
  269. """
  270. Generate a png file containing latex's rendering of tex string.
  271. Return the file name.
  272. """
  273. pngfile = Path(cls.get_basefile(tex, fontsize, dpi)).with_suffix(".png")
  274. # see get_rgba for a discussion of the background
  275. if not pngfile.exists():
  276. dvifile = cls.make_dvi(tex, fontsize)
  277. with TemporaryDirectory(dir=pngfile.parent) as tmpdir:
  278. cmd = ["dvipng", "-bg", "Transparent", "-D", str(dpi),
  279. "-T", "tight", "-o", "file.png", dvifile]
  280. # When testing, disable FreeType rendering for reproducibility;
  281. # but dvipng 1.16 has a bug (fixed in f3ff241) that breaks
  282. # --freetype0 mode, so for it we keep FreeType enabled; the
  283. # image will be slightly off.
  284. if (getattr(mpl, "_called_from_pytest", False) and
  285. mpl._get_executable_info("dvipng").raw_version != "1.16"):
  286. cmd.insert(1, "--freetype0")
  287. cls._run_checked_subprocess(cmd, tex, cwd=tmpdir)
  288. Path(tmpdir, "file.png").replace(pngfile)
  289. return str(pngfile)
  290. @classmethod
  291. def get_grey(cls, tex, fontsize=None, dpi=None):
  292. """Return the alpha channel."""
  293. if not fontsize:
  294. fontsize = mpl.rcParams['font.size']
  295. if not dpi:
  296. dpi = mpl.rcParams['savefig.dpi']
  297. key = cls._get_tex_source(tex, fontsize), dpi
  298. alpha = cls._grey_arrayd.get(key)
  299. if alpha is None:
  300. pngfile = cls.make_png(tex, fontsize, dpi)
  301. rgba = mpl.image.imread(os.path.join(cls._texcache, pngfile))
  302. cls._grey_arrayd[key] = alpha = rgba[:, :, -1]
  303. return alpha
  304. @classmethod
  305. def get_rgba(cls, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)):
  306. r"""
  307. Return latex's rendering of the tex string as an RGBA array.
  308. Examples
  309. --------
  310. >>> texmanager = TexManager()
  311. >>> s = r"\TeX\ is $\displaystyle\sum_n\frac{-e^{i\pi}}{2^n}$!"
  312. >>> Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1, 0, 0))
  313. """
  314. alpha = cls.get_grey(tex, fontsize, dpi)
  315. rgba = np.empty((*alpha.shape, 4))
  316. rgba[..., :3] = mpl.colors.to_rgb(rgb)
  317. rgba[..., -1] = alpha
  318. return rgba
  319. @classmethod
  320. def get_text_width_height_descent(cls, tex, fontsize, renderer=None):
  321. """Return width, height and descent of the text."""
  322. if tex.strip() == '':
  323. return 0, 0, 0
  324. dvifile = cls.make_dvi(tex, fontsize)
  325. dpi_fraction = renderer.points_to_pixels(1.) if renderer else 1
  326. with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
  327. page, = dvi
  328. # A total height (including the descent) needs to be returned.
  329. return page.width, page.height + page.descent, page.descent