test_dviread.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import json
  2. from pathlib import Path
  3. import shutil
  4. import matplotlib.dviread as dr
  5. import pytest
  6. def test_PsfontsMap(monkeypatch):
  7. monkeypatch.setattr(dr, 'find_tex_file', lambda x: x)
  8. filename = str(Path(__file__).parent / 'baseline_images/dviread/test.map')
  9. fontmap = dr.PsfontsMap(filename)
  10. # Check all properties of a few fonts
  11. for n in [1, 2, 3, 4, 5]:
  12. key = b'TeXfont%d' % n
  13. entry = fontmap[key]
  14. assert entry.texname == key
  15. assert entry.psname == b'PSfont%d' % n
  16. if n not in [3, 5]:
  17. assert entry.encoding == b'font%d.enc' % n
  18. elif n == 3:
  19. assert entry.encoding == b'enc3.foo'
  20. # We don't care about the encoding of TeXfont5, which specifies
  21. # multiple encodings.
  22. if n not in [1, 5]:
  23. assert entry.filename == b'font%d.pfa' % n
  24. else:
  25. assert entry.filename == b'font%d.pfb' % n
  26. if n == 4:
  27. assert entry.effects == {'slant': -0.1, 'extend': 2.2}
  28. else:
  29. assert entry.effects == {}
  30. # Some special cases
  31. entry = fontmap[b'TeXfont6']
  32. assert entry.filename is None
  33. assert entry.encoding is None
  34. entry = fontmap[b'TeXfont7']
  35. assert entry.filename is None
  36. assert entry.encoding == b'font7.enc'
  37. entry = fontmap[b'TeXfont8']
  38. assert entry.filename == b'font8.pfb'
  39. assert entry.encoding is None
  40. entry = fontmap[b'TeXfont9']
  41. assert entry.filename == b'/absolute/font9.pfb'
  42. # Missing font
  43. with pytest.raises(KeyError, match='no-such-font'):
  44. fontmap[b'no-such-font']
  45. @pytest.mark.skipif(shutil.which("kpsewhich") is None,
  46. reason="kpsewhich is not available")
  47. def test_dviread():
  48. dirpath = Path(__file__).parent / 'baseline_images/dviread'
  49. with (dirpath / 'test.json').open() as f:
  50. correct = json.load(f)
  51. with dr.Dvi(str(dirpath / 'test.dvi'), None) as dvi:
  52. data = [{'text': [[t.x, t.y,
  53. chr(t.glyph),
  54. t.font.texname.decode('ascii'),
  55. round(t.font.size, 2)]
  56. for t in page.text],
  57. 'boxes': [[b.x, b.y, b.height, b.width] for b in page.boxes]}
  58. for page in dvi]
  59. assert data == correct