test_texmanager.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import os
  2. from pathlib import Path
  3. import re
  4. import subprocess
  5. import sys
  6. import matplotlib.pyplot as plt
  7. from matplotlib.texmanager import TexManager
  8. from matplotlib.testing._markers import needs_usetex
  9. import pytest
  10. def test_fontconfig_preamble():
  11. """Test that the preamble is included in the source."""
  12. plt.rcParams['text.usetex'] = True
  13. src1 = TexManager()._get_tex_source("", fontsize=12)
  14. plt.rcParams['text.latex.preamble'] = '\\usepackage{txfonts}'
  15. src2 = TexManager()._get_tex_source("", fontsize=12)
  16. assert src1 != src2
  17. @pytest.mark.parametrize(
  18. "rc, preamble, family", [
  19. ({"font.family": "sans-serif", "font.sans-serif": "helvetica"},
  20. r"\usepackage{helvet}", r"\sffamily"),
  21. ({"font.family": "serif", "font.serif": "palatino"},
  22. r"\usepackage{mathpazo}", r"\rmfamily"),
  23. ({"font.family": "cursive", "font.cursive": "zapf chancery"},
  24. r"\usepackage{chancery}", r"\rmfamily"),
  25. ({"font.family": "monospace", "font.monospace": "courier"},
  26. r"\usepackage{courier}", r"\ttfamily"),
  27. ({"font.family": "helvetica"}, r"\usepackage{helvet}", r"\sffamily"),
  28. ({"font.family": "palatino"}, r"\usepackage{mathpazo}", r"\rmfamily"),
  29. ({"font.family": "zapf chancery"},
  30. r"\usepackage{chancery}", r"\rmfamily"),
  31. ({"font.family": "courier"}, r"\usepackage{courier}", r"\ttfamily")
  32. ])
  33. def test_font_selection(rc, preamble, family):
  34. plt.rcParams.update(rc)
  35. tm = TexManager()
  36. src = Path(tm.make_tex("hello, world", fontsize=12)).read_text()
  37. assert preamble in src
  38. assert [*re.findall(r"\\\w+family", src)] == [family]
  39. @needs_usetex
  40. def test_unicode_characters():
  41. # Smoke test to see that Unicode characters does not cause issues
  42. # See #23019
  43. plt.rcParams['text.usetex'] = True
  44. fig, ax = plt.subplots()
  45. ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}')
  46. ax.set_xlabel('\N{VULGAR FRACTION ONE QUARTER}Öøæ')
  47. fig.canvas.draw()
  48. # But not all characters.
  49. # Should raise RuntimeError, not UnicodeDecodeError
  50. with pytest.raises(RuntimeError):
  51. ax.set_title('\N{SNOWMAN}')
  52. fig.canvas.draw()
  53. @needs_usetex
  54. def test_openin_any_paranoid():
  55. completed = subprocess.run(
  56. [sys.executable, "-c",
  57. 'import matplotlib.pyplot as plt;'
  58. 'plt.rcParams.update({"text.usetex": True});'
  59. 'plt.title("paranoid");'
  60. 'plt.show(block=False);'],
  61. env={**os.environ, 'openin_any': 'p'}, check=True, capture_output=True)
  62. assert completed.stderr == b""