test_dviread.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.decode())
  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 == 'font%d.enc' % n
  18. elif n == 3:
  19. assert entry.encoding == '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 == 'font%d.pfa' % n
  24. else:
  25. assert entry.filename == 'font%d.pfb' % n
  26. if n == 4:
  27. assert entry.effects == {'slant': -0.1, 'extend': 1.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 == 'font7.enc'
  37. entry = fontmap[b'TeXfont8']
  38. assert entry.filename == 'font8.pfb'
  39. assert entry.encoding is None
  40. entry = fontmap[b'TeXfont9']
  41. assert entry.psname == b'TeXfont9'
  42. assert entry.filename == '/absolute/font9.pfb'
  43. # First of duplicates only.
  44. entry = fontmap[b'TeXfontA']
  45. assert entry.psname == b'PSfontA1'
  46. # Slant/Extend only works for T1 fonts.
  47. entry = fontmap[b'TeXfontB']
  48. assert entry.psname == b'PSfontB6'
  49. # Subsetted TrueType must have encoding.
  50. entry = fontmap[b'TeXfontC']
  51. assert entry.psname == b'PSfontC3'
  52. # Missing font
  53. with pytest.raises(LookupError, match='no-such-font'):
  54. fontmap[b'no-such-font']
  55. with pytest.raises(LookupError, match='%'):
  56. fontmap[b'%']
  57. @pytest.mark.skipif(shutil.which("kpsewhich") is None,
  58. reason="kpsewhich is not available")
  59. def test_dviread():
  60. dirpath = Path(__file__).parent / 'baseline_images/dviread'
  61. with (dirpath / 'test.json').open() as f:
  62. correct = json.load(f)
  63. with dr.Dvi(str(dirpath / 'test.dvi'), None) as dvi:
  64. data = [{'text': [[t.x, t.y,
  65. chr(t.glyph),
  66. t.font.texname.decode('ascii'),
  67. round(t.font.size, 2)]
  68. for t in page.text],
  69. 'boxes': [[b.x, b.y, b.height, b.width] for b in page.boxes]}
  70. for page in dvi]
  71. assert data == correct