test_dviread.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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) as exc:
  44. fontmap[b'no-such-font']
  45. assert 'no-such-font' in str(exc.value)
  46. @pytest.mark.skipif(shutil.which("kpsewhich") is None,
  47. reason="kpsewhich is not available")
  48. def test_dviread():
  49. dirpath = Path(__file__).parent / 'baseline_images/dviread'
  50. with (dirpath / 'test.json').open() as f:
  51. correct = json.load(f)
  52. with dr.Dvi(str(dirpath / 'test.dvi'), None) as dvi:
  53. data = [{'text': [[t.x, t.y,
  54. chr(t.glyph),
  55. t.font.texname.decode('ascii'),
  56. round(t.font.size, 2)]
  57. for t in page.text],
  58. 'boxes': [[b.x, b.y, b.height, b.width] for b in page.boxes]}
  59. for page in dvi]
  60. assert data == correct