test_mathtext.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import io
  2. import re
  3. import numpy as np
  4. import pytest
  5. import matplotlib
  6. from matplotlib.testing.decorators import check_figures_equal, image_comparison
  7. import matplotlib.pyplot as plt
  8. from matplotlib import mathtext
  9. math_tests = [
  10. r'$a+b+\dot s+\dot{s}+\ldots$',
  11. r'$x \doteq y$',
  12. r'\$100.00 $\alpha \_$',
  13. r'$\frac{\$100.00}{y}$',
  14. r'$x y$',
  15. r'$x+y\ x=y\ x<y\ x:y\ x,y\ x@y$',
  16. r'$100\%y\ x*y\ x/y x\$y$',
  17. r'$x\leftarrow y\ x\forall y\ x-y$',
  18. r'$x \sf x \bf x {\cal X} \rm x$',
  19. r'$x\ x\,x\;x\quad x\qquad x\!x\hspace{ 0.5 }y$',
  20. r'$\{ \rm braces \}$',
  21. r'$\left[\left\lfloor\frac{5}{\frac{\left(3\right)}{4}} y\right)\right]$',
  22. r'$\left(x\right)$',
  23. r'$\sin(x)$',
  24. r'$x_2$',
  25. r'$x^2$',
  26. r'$x^2_y$',
  27. r'$x_y^2$',
  28. r'$\prod_{i=\alpha_{i+1}}^\infty$',
  29. r'$x = \frac{x+\frac{5}{2}}{\frac{y+3}{8}}$',
  30. r'$dz/dt = \gamma x^2 + {\rm sin}(2\pi y+\phi)$',
  31. r'Foo: $\alpha_{i+1}^j = {\rm sin}(2\pi f_j t_i) e^{-5 t_i/\tau}$',
  32. r'$\mathcal{R}\prod_{i=\alpha_{i+1}}^\infty a_i \sin(2 \pi f x_i)$',
  33. r'Variable $i$ is good',
  34. r'$\Delta_i^j$',
  35. r'$\Delta^j_{i+1}$',
  36. r'$\ddot{o}\acute{e}\grave{e}\hat{O}\breve{\imath}\tilde{n}\vec{q}$',
  37. r"$\arccos((x^i))$",
  38. r"$\gamma = \frac{x=\frac{6}{8}}{y} \delta$",
  39. r'$\limsup_{x\to\infty}$',
  40. r'$\oint^\infty_0$',
  41. r"$f'\quad f'''(x)\quad ''/\mathrm{yr}$",
  42. r'$\frac{x_2888}{y}$',
  43. r"$\sqrt[3]{\frac{X_2}{Y}}=5$",
  44. r"$\sqrt[5]{\prod^\frac{x}{2\pi^2}_\infty}$",
  45. r"$\sqrt[3]{x}=5$",
  46. r'$\frac{X}{\frac{X}{Y}}$',
  47. r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$",
  48. r'$\mathcal{H} = \int d \tau \left(\epsilon E^2 + \mu H^2\right)$',
  49. r'$\widehat{abc}\widetilde{def}$',
  50. '$\\Gamma \\Delta \\Theta \\Lambda \\Xi \\Pi \\Sigma \\Upsilon \\Phi \\Psi \\Omega$',
  51. '$\\alpha \\beta \\gamma \\delta \\epsilon \\zeta \\eta \\theta \\iota \\lambda \\mu \\nu \\xi \\pi \\kappa \\rho \\sigma \\tau \\upsilon \\phi \\chi \\psi$',
  52. # The examples prefixed by 'mmltt' are from the MathML torture test here:
  53. # https://developer.mozilla.org/en-US/docs/Mozilla/MathML_Project/MathML_Torture_Test
  54. r'${x}^{2}{y}^{2}$',
  55. r'${}_{2}F_{3}$',
  56. r'$\frac{x+{y}^{2}}{k+1}$',
  57. r'$x+{y}^{\frac{2}{k+1}}$',
  58. r'$\frac{a}{b/2}$',
  59. r'${a}_{0}+\frac{1}{{a}_{1}+\frac{1}{{a}_{2}+\frac{1}{{a}_{3}+\frac{1}{{a}_{4}}}}}$',
  60. r'${a}_{0}+\frac{1}{{a}_{1}+\frac{1}{{a}_{2}+\frac{1}{{a}_{3}+\frac{1}{{a}_{4}}}}}$',
  61. r'$\binom{n}{k/2}$',
  62. r'$\binom{p}{2}{x}^{2}{y}^{p-2}-\frac{1}{1-x}\frac{1}{1-{x}^{2}}$',
  63. r'${x}^{2y}$',
  64. r'$\sum _{i=1}^{p}\sum _{j=1}^{q}\sum _{k=1}^{r}{a}_{ij}{b}_{jk}{c}_{ki}$',
  65. r'$\sqrt{1+\sqrt{1+\sqrt{1+\sqrt{1+\sqrt{1+\sqrt{1+\sqrt{1+x}}}}}}}$',
  66. r'$\left(\frac{{\partial }^{2}}{\partial {x}^{2}}+\frac{{\partial }^{2}}{\partial {y}^{2}}\right){|\varphi \left(x+iy\right)|}^{2}=0$',
  67. r'${2}^{{2}^{{2}^{x}}}$',
  68. r'${\int }_{1}^{x}\frac{\mathrm{dt}}{t}$',
  69. r'$\int {\int }_{D}\mathrm{dx} \mathrm{dy}$',
  70. # mathtex doesn't support array
  71. # 'mmltt18' : r'$f\left(x\right)=\left\{\begin{array}{cc}\hfill 1/3\hfill & \text{if_}0\le x\le 1;\hfill \\ \hfill 2/3\hfill & \hfill \text{if_}3\le x\le 4;\hfill \\ \hfill 0\hfill & \text{elsewhere.}\hfill \end{array}$',
  72. # mathtex doesn't support stackrel
  73. # 'mmltt19' : r'$\stackrel{\stackrel{k\text{times}}{\ufe37}}{x+...+x}$',
  74. r'${y}_{{x}^{2}}$',
  75. # mathtex doesn't support the "\text" command
  76. # 'mmltt21' : r'$\sum _{p\text{\prime}}f\left(p\right)={\int }_{t>1}f\left(t\right) d\pi \left(t\right)$',
  77. # mathtex doesn't support array
  78. # 'mmltt23' : r'$\left(\begin{array}{cc}\hfill \left(\begin{array}{cc}\hfill a\hfill & \hfill b\hfill \\ \hfill c\hfill & \hfill d\hfill \end{array}\right)\hfill & \hfill \left(\begin{array}{cc}\hfill e\hfill & \hfill f\hfill \\ \hfill g\hfill & \hfill h\hfill \end{array}\right)\hfill \\ \hfill 0\hfill & \hfill \left(\begin{array}{cc}\hfill i\hfill & \hfill j\hfill \\ \hfill k\hfill & \hfill l\hfill \end{array}\right)\hfill \end{array}\right)$',
  79. # mathtex doesn't support array
  80. # 'mmltt24' : r'$det|\begin{array}{ccccc}\hfill {c}_{0}\hfill & \hfill {c}_{1}\hfill & \hfill {c}_{2}\hfill & \hfill \dots \hfill & \hfill {c}_{n}\hfill \\ \hfill {c}_{1}\hfill & \hfill {c}_{2}\hfill & \hfill {c}_{3}\hfill & \hfill \dots \hfill & \hfill {c}_{n+1}\hfill \\ \hfill {c}_{2}\hfill & \hfill {c}_{3}\hfill & \hfill {c}_{4}\hfill & \hfill \dots \hfill & \hfill {c}_{n+2}\hfill \\ \hfill \u22ee\hfill & \hfill \u22ee\hfill & \hfill \u22ee\hfill & \hfill \hfill & \hfill \u22ee\hfill \\ \hfill {c}_{n}\hfill & \hfill {c}_{n+1}\hfill & \hfill {c}_{n+2}\hfill & \hfill \dots \hfill & \hfill {c}_{2n}\hfill \end{array}|>0$',
  81. r'${y}_{{x}_{2}}$',
  82. r'${x}_{92}^{31415}+\pi $',
  83. r'${x}_{{y}_{b}^{a}}^{{z}_{c}^{d}}$',
  84. r'${y}_{3}^{\prime \prime \prime }$',
  85. r"$\left( \xi \left( 1 - \xi \right) \right)$", # Bug 2969451
  86. r"$\left(2 \, a=b\right)$", # Sage bug #8125
  87. r"$? ! &$", # github issue #466
  88. r'$\operatorname{cos} x$', # github issue #553
  89. r'$\sum _{\genfrac{}{}{0}{}{0\leq i\leq m}{0<j<n}}P\left(i,j\right)$',
  90. r"$\left\Vert a \right\Vert \left\vert b \right\vert \left| a \right| \left\| b\right\| \Vert a \Vert \vert b \vert$",
  91. r'$\mathring{A} \stackrel{\circ}{A} \AA$',
  92. r'$M \, M \thinspace M \/ M \> M \: M \; M \ M \enspace M \quad M \qquad M \! M$',
  93. r'$\Cup$ $\Cap$ $\leftharpoonup$ $\barwedge$ $\rightharpoonup$',
  94. r'$\dotplus$ $\doteq$ $\doteqdot$ $\ddots$',
  95. r'$xyz^kx_kx^py^{p-2} d_i^jb_jc_kd x^j_i E^0 E^0_u$', # github issue #4873
  96. r'${xyz}^k{x}_{k}{x}^{p}{y}^{p-2} {d}_{i}^{j}{b}_{j}{c}_{k}{d} {x}^{j}_{i}{E}^{0}{E}^0_u$',
  97. r'${\int}_x^x x\oint_x^x x\int_{X}^{X}x\int_x x \int^x x \int_{x} x\int^{x}{\int}_{x} x{\int}^{x}_{x}x$',
  98. r'testing$^{123}$',
  99. ' '.join('$\\' + p + '$' for p in sorted(mathtext.Parser._snowflake)),
  100. r'$6-2$; $-2$; $ -2$; ${-2}$; ${ -2}$; $20^{+3}_{-2}$',
  101. r'$\overline{\omega}^x \frac{1}{2}_0^x$', # github issue #5444
  102. r'$,$ $.$ $1{,}234{, }567{ , }890$ and $1,234,567,890$', # github issue 5799
  103. r'$\left(X\right)_{a}^{b}$', # github issue 7615
  104. r'$\dfrac{\$100.00}{y}$', # github issue #1888
  105. ]
  106. digits = "0123456789"
  107. uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  108. lowercase = "abcdefghijklmnopqrstuvwxyz"
  109. uppergreek = ("\\Gamma \\Delta \\Theta \\Lambda \\Xi \\Pi \\Sigma \\Upsilon \\Phi \\Psi "
  110. "\\Omega")
  111. lowergreek = ("\\alpha \\beta \\gamma \\delta \\epsilon \\zeta \\eta \\theta \\iota "
  112. "\\lambda \\mu \\nu \\xi \\pi \\kappa \\rho \\sigma \\tau \\upsilon "
  113. "\\phi \\chi \\psi")
  114. all = [digits, uppercase, lowercase, uppergreek, lowergreek]
  115. font_test_specs = [
  116. ([], all),
  117. (['mathrm'], all),
  118. (['mathbf'], all),
  119. (['mathit'], all),
  120. (['mathtt'], [digits, uppercase, lowercase]),
  121. (['mathcircled'], [digits, uppercase, lowercase]),
  122. (['mathrm', 'mathcircled'], [digits, uppercase, lowercase]),
  123. (['mathbf', 'mathcircled'], [digits, uppercase, lowercase]),
  124. (['mathbb'], [digits, uppercase, lowercase,
  125. r'\Gamma \Pi \Sigma \gamma \pi']),
  126. (['mathrm', 'mathbb'], [digits, uppercase, lowercase,
  127. r'\Gamma \Pi \Sigma \gamma \pi']),
  128. (['mathbf', 'mathbb'], [digits, uppercase, lowercase,
  129. r'\Gamma \Pi \Sigma \gamma \pi']),
  130. (['mathcal'], [uppercase]),
  131. (['mathfrak'], [uppercase, lowercase]),
  132. (['mathbf', 'mathfrak'], [uppercase, lowercase]),
  133. (['mathscr'], [uppercase, lowercase]),
  134. (['mathsf'], [digits, uppercase, lowercase]),
  135. (['mathrm', 'mathsf'], [digits, uppercase, lowercase]),
  136. (['mathbf', 'mathsf'], [digits, uppercase, lowercase])
  137. ]
  138. font_tests = []
  139. for fonts, chars in font_test_specs:
  140. wrapper = ''.join([
  141. ' '.join(fonts),
  142. ' $',
  143. *(r'\%s{' % font for font in fonts),
  144. '%s',
  145. *('}' for font in fonts),
  146. '$',
  147. ])
  148. for set in chars:
  149. font_tests.append(wrapper % set)
  150. @pytest.fixture
  151. def baseline_images(request, fontset, index):
  152. return ['%s_%s_%02d' % (request.param, fontset, index)]
  153. # In the following two tests, use recwarn to suppress warnings regarding the
  154. # deprecation of \stackrel and \mathcircled.
  155. @pytest.mark.parametrize('index, test', enumerate(math_tests),
  156. ids=[str(index) for index in range(len(math_tests))])
  157. @pytest.mark.parametrize('fontset',
  158. ['cm', 'stix', 'stixsans', 'dejavusans',
  159. 'dejavuserif'])
  160. @pytest.mark.parametrize('baseline_images', ['mathtext'], indirect=True)
  161. @image_comparison(baseline_images=None)
  162. def test_mathtext_rendering(baseline_images, fontset, index, test, recwarn):
  163. matplotlib.rcParams['mathtext.fontset'] = fontset
  164. fig = plt.figure(figsize=(5.25, 0.75))
  165. fig.text(0.5, 0.5, test,
  166. horizontalalignment='center', verticalalignment='center')
  167. @pytest.mark.parametrize('index, test', enumerate(font_tests),
  168. ids=[str(index) for index in range(len(font_tests))])
  169. @pytest.mark.parametrize('fontset',
  170. ['cm', 'stix', 'stixsans', 'dejavusans',
  171. 'dejavuserif'])
  172. @pytest.mark.parametrize('baseline_images', ['mathfont'], indirect=True)
  173. @image_comparison(baseline_images=None, extensions=['png'])
  174. def test_mathfont_rendering(baseline_images, fontset, index, test, recwarn):
  175. matplotlib.rcParams['mathtext.fontset'] = fontset
  176. fig = plt.figure(figsize=(5.25, 0.75))
  177. fig.text(0.5, 0.5, test,
  178. horizontalalignment='center', verticalalignment='center')
  179. def test_fontinfo():
  180. import matplotlib.font_manager as font_manager
  181. import matplotlib.ft2font as ft2font
  182. fontpath = font_manager.findfont("DejaVu Sans")
  183. font = ft2font.FT2Font(fontpath)
  184. table = font.get_sfnt_table("head")
  185. assert table['version'] == (1, 0)
  186. @pytest.mark.parametrize(
  187. 'math, msg',
  188. [
  189. (r'$\hspace{}$', r'Expected \hspace{n}'),
  190. (r'$\hspace{foo}$', r'Expected \hspace{n}'),
  191. (r'$\frac$', r'Expected \frac{num}{den}'),
  192. (r'$\frac{}{}$', r'Expected \frac{num}{den}'),
  193. (r'$\stackrel$', r'Expected \stackrel{num}{den}'),
  194. (r'$\stackrel{}{}$', r'Expected \stackrel{num}{den}'),
  195. (r'$\binom$', r'Expected \binom{num}{den}'),
  196. (r'$\binom{}{}$', r'Expected \binom{num}{den}'),
  197. (r'$\genfrac$',
  198. r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'),
  199. (r'$\genfrac{}{}{}{}{}{}$',
  200. r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'),
  201. (r'$\sqrt$', r'Expected \sqrt{value}'),
  202. (r'$\sqrt f$', r'Expected \sqrt{value}'),
  203. (r'$\overline$', r'Expected \overline{value}'),
  204. (r'$\overline{}$', r'Expected \overline{value}'),
  205. (r'$\leftF$', r'Expected a delimiter'),
  206. (r'$\rightF$', r'Unknown symbol: \rightF'),
  207. (r'$\left(\right$', r'Expected a delimiter'),
  208. (r'$\left($', r'Expected "\right"'),
  209. (r'$\dfrac$', r'Expected \dfrac{num}{den}'),
  210. (r'$\dfrac{}{}$', r'Expected \dfrac{num}{den}'),
  211. ],
  212. ids=[
  213. 'hspace without value',
  214. 'hspace with invalid value',
  215. 'frac without parameters',
  216. 'frac with empty parameters',
  217. 'stackrel without parameters',
  218. 'stackrel with empty parameters',
  219. 'binom without parameters',
  220. 'binom with empty parameters',
  221. 'genfrac without parameters',
  222. 'genfrac with empty parameters',
  223. 'sqrt without parameters',
  224. 'sqrt with invalid value',
  225. 'overline without parameters',
  226. 'overline with empty parameter',
  227. 'left with invalid delimiter',
  228. 'right with invalid delimiter',
  229. 'unclosed parentheses with sizing',
  230. 'unclosed parentheses without sizing',
  231. 'dfrac without parameters',
  232. 'dfrac with empty parameters',
  233. ]
  234. )
  235. def test_mathtext_exceptions(math, msg):
  236. parser = mathtext.MathTextParser('agg')
  237. with pytest.raises(ValueError) as excinfo:
  238. parser.parse(math)
  239. excinfo.match(re.escape(msg))
  240. def test_single_minus_sign():
  241. plt.figure(figsize=(0.3, 0.3))
  242. plt.text(0.5, 0.5, '$-$')
  243. for spine in plt.gca().spines.values():
  244. spine.set_visible(False)
  245. plt.gca().set_xticks([])
  246. plt.gca().set_yticks([])
  247. buff = io.BytesIO()
  248. plt.savefig(buff, format="rgba", dpi=1000)
  249. array = np.frombuffer(buff.getvalue(), dtype=np.uint8)
  250. # If this fails, it would be all white
  251. assert not np.all(array == 0xff)
  252. @check_figures_equal(extensions=["png"])
  253. def test_spaces(fig_test, fig_ref):
  254. fig_test.subplots().set_title(r"$1\,2\>3\ 4$")
  255. fig_ref.subplots().set_title(r"$1\/2\:3~4$")
  256. def test_math_to_image(tmpdir):
  257. mathtext.math_to_image('$x^2$', str(tmpdir.join('example.png')))
  258. mathtext.math_to_image('$x^2$', io.BytesIO())
  259. def test_mathtext_to_png(tmpdir):
  260. mt = mathtext.MathTextParser('bitmap')
  261. mt.to_png(str(tmpdir.join('example.png')), '$x^2$')
  262. mt.to_png(io.BytesIO(), '$x^2$')