texmanager.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 _api, 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 = _api.deprecate_privatize_attribute("3.8")
  50. _texcache = os.path.join(mpl.get_cachedir(), 'tex.cache')
  51. _grey_arrayd = {}
  52. _font_families = ('serif', 'sans-serif', 'cursive', 'monospace')
  53. _font_preambles = {
  54. 'new century schoolbook': r'\renewcommand{\rmdefault}{pnc}',
  55. 'bookman': r'\renewcommand{\rmdefault}{pbk}',
  56. 'times': r'\usepackage{mathptmx}',
  57. 'palatino': r'\usepackage{mathpazo}',
  58. 'zapf chancery': r'\usepackage{chancery}',
  59. 'cursive': r'\usepackage{chancery}',
  60. 'charter': r'\usepackage{charter}',
  61. 'serif': '',
  62. 'sans-serif': '',
  63. 'helvetica': r'\usepackage{helvet}',
  64. 'avant garde': r'\usepackage{avant}',
  65. 'courier': r'\usepackage{courier}',
  66. # Loading the type1ec package ensures that cm-super is installed, which
  67. # is necessary for Unicode computer modern. (It also allows the use of
  68. # computer modern at arbitrary sizes, but that's just a side effect.)
  69. 'monospace': r'\usepackage{type1ec}',
  70. 'computer modern roman': r'\usepackage{type1ec}',
  71. 'computer modern sans serif': r'\usepackage{type1ec}',
  72. 'computer modern typewriter': r'\usepackage{type1ec}',
  73. }
  74. _font_types = {
  75. 'new century schoolbook': 'serif',
  76. 'bookman': 'serif',
  77. 'times': 'serif',
  78. 'palatino': 'serif',
  79. 'zapf chancery': 'cursive',
  80. 'charter': 'serif',
  81. 'helvetica': 'sans-serif',
  82. 'avant garde': 'sans-serif',
  83. 'courier': 'monospace',
  84. 'computer modern roman': 'serif',
  85. 'computer modern sans serif': 'sans-serif',
  86. 'computer modern typewriter': 'monospace',
  87. }
  88. @functools.lru_cache # Always return the same instance.
  89. def __new__(cls):
  90. Path(cls._texcache).mkdir(parents=True, exist_ok=True)
  91. return object.__new__(cls)
  92. @classmethod
  93. def _get_font_family_and_reduced(cls):
  94. """Return the font family name and whether the font is reduced."""
  95. ff = mpl.rcParams['font.family']
  96. ff_val = ff[0].lower() if len(ff) == 1 else None
  97. if len(ff) == 1 and ff_val in cls._font_families:
  98. return ff_val, False
  99. elif len(ff) == 1 and ff_val in cls._font_preambles:
  100. return cls._font_types[ff_val], True
  101. else:
  102. _log.info('font.family must be one of (%s) when text.usetex is '
  103. 'True. serif will be used by default.',
  104. ', '.join(cls._font_families))
  105. return 'serif', False
  106. @classmethod
  107. def _get_font_preamble_and_command(cls):
  108. requested_family, is_reduced_font = cls._get_font_family_and_reduced()
  109. preambles = {}
  110. for font_family in cls._font_families:
  111. if is_reduced_font and font_family == requested_family:
  112. preambles[font_family] = cls._font_preambles[
  113. mpl.rcParams['font.family'][0].lower()]
  114. else:
  115. for font in mpl.rcParams['font.' + font_family]:
  116. if font.lower() in cls._font_preambles:
  117. preambles[font_family] = \
  118. cls._font_preambles[font.lower()]
  119. _log.debug(
  120. 'family: %s, font: %s, info: %s',
  121. font_family, font,
  122. cls._font_preambles[font.lower()])
  123. break
  124. else:
  125. _log.debug('%s font is not compatible with usetex.',
  126. font)
  127. else:
  128. _log.info('No LaTeX-compatible font found for the %s font'
  129. 'family in rcParams. Using default.',
  130. font_family)
  131. preambles[font_family] = cls._font_preambles[font_family]
  132. # The following packages and commands need to be included in the latex
  133. # file's preamble:
  134. cmd = {preambles[family]
  135. for family in ['serif', 'sans-serif', 'monospace']}
  136. if requested_family == 'cursive':
  137. cmd.add(preambles['cursive'])
  138. cmd.add(r'\usepackage{type1cm}')
  139. preamble = '\n'.join(sorted(cmd))
  140. fontcmd = (r'\sffamily' if requested_family == 'sans-serif' else
  141. r'\ttfamily' if requested_family == 'monospace' else
  142. r'\rmfamily')
  143. return preamble, fontcmd
  144. @classmethod
  145. def get_basefile(cls, tex, fontsize, dpi=None):
  146. """
  147. Return a filename based on a hash of the string, fontsize, and dpi.
  148. """
  149. src = cls._get_tex_source(tex, fontsize) + str(dpi)
  150. filehash = hashlib.md5(src.encode('utf-8')).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. basefile = cls.get_basefile(tex, fontsize)
  246. dvifile = '%s.dvi' % basefile
  247. if not os.path.exists(dvifile):
  248. texfile = Path(cls.make_tex(tex, fontsize))
  249. # Generate the dvi in a temporary directory to avoid race
  250. # conditions e.g. if multiple processes try to process the same tex
  251. # string at the same time. Having tmpdir be a subdirectory of the
  252. # final output dir ensures that they are on the same filesystem,
  253. # and thus replace() works atomically. It also allows referring to
  254. # the texfile with a relative path (for pathological MPLCONFIGDIRs,
  255. # the absolute path may contain characters (e.g. ~) that TeX does
  256. # not support; n.b. relative paths cannot traverse parents, or it
  257. # will be blocked when `openin_any = p` in texmf.cnf).
  258. cwd = Path(dvifile).parent
  259. with TemporaryDirectory(dir=cwd) as tmpdir:
  260. tmppath = Path(tmpdir)
  261. cls._run_checked_subprocess(
  262. ["latex", "-interaction=nonstopmode", "--halt-on-error",
  263. f"--output-directory={tmppath.name}",
  264. f"{texfile.name}"], tex, cwd=cwd)
  265. (tmppath / Path(dvifile).name).replace(dvifile)
  266. return 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. basefile = cls.get_basefile(tex, fontsize, dpi)
  274. pngfile = '%s.png' % basefile
  275. # see get_rgba for a discussion of the background
  276. if not os.path.exists(pngfile):
  277. dvifile = cls.make_dvi(tex, fontsize)
  278. cmd = ["dvipng", "-bg", "Transparent", "-D", str(dpi),
  279. "-T", "tight", "-o", pngfile, dvifile]
  280. # When testing, disable FreeType rendering for reproducibility; but
  281. # dvipng 1.16 has a bug (fixed in f3ff241) that breaks --freetype0
  282. # mode, so for it we keep FreeType enabled; the image will be
  283. # 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)
  288. return pngfile
  289. @classmethod
  290. def get_grey(cls, tex, fontsize=None, dpi=None):
  291. """Return the alpha channel."""
  292. if not fontsize:
  293. fontsize = mpl.rcParams['font.size']
  294. if not dpi:
  295. dpi = mpl.rcParams['savefig.dpi']
  296. key = cls._get_tex_source(tex, fontsize), dpi
  297. alpha = cls._grey_arrayd.get(key)
  298. if alpha is None:
  299. pngfile = cls.make_png(tex, fontsize, dpi)
  300. rgba = mpl.image.imread(os.path.join(cls._texcache, pngfile))
  301. cls._grey_arrayd[key] = alpha = rgba[:, :, -1]
  302. return alpha
  303. @classmethod
  304. def get_rgba(cls, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)):
  305. r"""
  306. Return latex's rendering of the tex string as an RGBA array.
  307. Examples
  308. --------
  309. >>> texmanager = TexManager()
  310. >>> s = r"\TeX\ is $\displaystyle\sum_n\frac{-e^{i\pi}}{2^n}$!"
  311. >>> Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1, 0, 0))
  312. """
  313. alpha = cls.get_grey(tex, fontsize, dpi)
  314. rgba = np.empty((*alpha.shape, 4))
  315. rgba[..., :3] = mpl.colors.to_rgb(rgb)
  316. rgba[..., -1] = alpha
  317. return rgba
  318. @classmethod
  319. def get_text_width_height_descent(cls, tex, fontsize, renderer=None):
  320. """Return width, height and descent of the text."""
  321. if tex.strip() == '':
  322. return 0, 0, 0
  323. dvifile = cls.make_dvi(tex, fontsize)
  324. dpi_fraction = renderer.points_to_pixels(1.) if renderer else 1
  325. with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
  326. page, = dvi
  327. # A total height (including the descent) needs to be returned.
  328. return page.width, page.height + page.descent, page.descent