dviread.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. """
  2. A module for reading dvi files output by TeX. Several limitations make
  3. this not (currently) useful as a general-purpose dvi preprocessor, but
  4. it is currently used by the pdf backend for processing usetex text.
  5. Interface::
  6. with Dvi(filename, 72) as dvi:
  7. # iterate over pages:
  8. for page in dvi:
  9. w, h, d = page.width, page.height, page.descent
  10. for x, y, font, glyph, width in page.text:
  11. fontname = font.texname
  12. pointsize = font.size
  13. ...
  14. for x, y, height, width in page.boxes:
  15. ...
  16. """
  17. from collections import namedtuple
  18. import enum
  19. from functools import lru_cache, partial, wraps
  20. import logging
  21. import os
  22. from pathlib import Path
  23. import re
  24. import struct
  25. import sys
  26. import textwrap
  27. import numpy as np
  28. from matplotlib import cbook, rcParams
  29. _log = logging.getLogger(__name__)
  30. # Many dvi related files are looked for by external processes, require
  31. # additional parsing, and are used many times per rendering, which is why they
  32. # are cached using lru_cache().
  33. # Dvi is a bytecode format documented in
  34. # http://mirrors.ctan.org/systems/knuth/dist/texware/dvitype.web
  35. # http://texdoc.net/texmf-dist/doc/generic/knuth/texware/dvitype.pdf
  36. #
  37. # The file consists of a preamble, some number of pages, a postamble,
  38. # and a finale. Different opcodes are allowed in different contexts,
  39. # so the Dvi object has a parser state:
  40. #
  41. # pre: expecting the preamble
  42. # outer: between pages (followed by a page or the postamble,
  43. # also e.g. font definitions are allowed)
  44. # page: processing a page
  45. # post_post: state after the postamble (our current implementation
  46. # just stops reading)
  47. # finale: the finale (unimplemented in our current implementation)
  48. _dvistate = enum.Enum('DviState', 'pre outer inpage post_post finale')
  49. # The marks on a page consist of text and boxes. A page also has dimensions.
  50. Page = namedtuple('Page', 'text boxes height width descent')
  51. Text = namedtuple('Text', 'x y font glyph width')
  52. Box = namedtuple('Box', 'x y height width')
  53. # Opcode argument parsing
  54. #
  55. # Each of the following functions takes a Dvi object and delta,
  56. # which is the difference between the opcode and the minimum opcode
  57. # with the same meaning. Dvi opcodes often encode the number of
  58. # argument bytes in this delta.
  59. def _arg_raw(dvi, delta):
  60. """Return *delta* without reading anything more from the dvi file."""
  61. return delta
  62. def _arg(nbytes, signed, dvi, _):
  63. """
  64. Read *nbytes* bytes, returning the bytes interpreted as a signed integer
  65. if *signed* is true, unsigned otherwise.
  66. """
  67. return dvi._arg(nbytes, signed)
  68. def _arg_slen(dvi, delta):
  69. """
  70. Signed, length *delta*
  71. Read *delta* bytes, returning None if *delta* is zero, and the bytes
  72. interpreted as a signed integer otherwise.
  73. """
  74. if delta == 0:
  75. return None
  76. return dvi._arg(delta, True)
  77. def _arg_slen1(dvi, delta):
  78. """
  79. Signed, length *delta*+1
  80. Read *delta*+1 bytes, returning the bytes interpreted as signed.
  81. """
  82. return dvi._arg(delta+1, True)
  83. def _arg_ulen1(dvi, delta):
  84. """
  85. Unsigned length *delta*+1
  86. Read *delta*+1 bytes, returning the bytes interpreted as unsigned.
  87. """
  88. return dvi._arg(delta+1, False)
  89. def _arg_olen1(dvi, delta):
  90. """
  91. Optionally signed, length *delta*+1
  92. Read *delta*+1 bytes, returning the bytes interpreted as
  93. unsigned integer for 0<=*delta*<3 and signed if *delta*==3.
  94. """
  95. return dvi._arg(delta + 1, delta == 3)
  96. _arg_mapping = dict(raw=_arg_raw,
  97. u1=partial(_arg, 1, False),
  98. u4=partial(_arg, 4, False),
  99. s4=partial(_arg, 4, True),
  100. slen=_arg_slen,
  101. olen1=_arg_olen1,
  102. slen1=_arg_slen1,
  103. ulen1=_arg_ulen1)
  104. def _dispatch(table, min, max=None, state=None, args=('raw',)):
  105. """
  106. Decorator for dispatch by opcode. Sets the values in *table*
  107. from *min* to *max* to this method, adds a check that the Dvi state
  108. matches *state* if not None, reads arguments from the file according
  109. to *args*.
  110. *table*
  111. the dispatch table to be filled in
  112. *min*
  113. minimum opcode for calling this function
  114. *max*
  115. maximum opcode for calling this function, None if only *min* is allowed
  116. *state*
  117. state of the Dvi object in which these opcodes are allowed
  118. *args*
  119. sequence of argument specifications:
  120. ``'raw'``: opcode minus minimum
  121. ``'u1'``: read one unsigned byte
  122. ``'u4'``: read four bytes, treat as an unsigned number
  123. ``'s4'``: read four bytes, treat as a signed number
  124. ``'slen'``: read (opcode - minimum) bytes, treat as signed
  125. ``'slen1'``: read (opcode - minimum + 1) bytes, treat as signed
  126. ``'ulen1'``: read (opcode - minimum + 1) bytes, treat as unsigned
  127. ``'olen1'``: read (opcode - minimum + 1) bytes, treat as unsigned
  128. if under four bytes, signed if four bytes
  129. """
  130. def decorate(method):
  131. get_args = [_arg_mapping[x] for x in args]
  132. @wraps(method)
  133. def wrapper(self, byte):
  134. if state is not None and self.state != state:
  135. raise ValueError("state precondition failed")
  136. return method(self, *[f(self, byte-min) for f in get_args])
  137. if max is None:
  138. table[min] = wrapper
  139. else:
  140. for i in range(min, max+1):
  141. assert table[i] is None
  142. table[i] = wrapper
  143. return wrapper
  144. return decorate
  145. class Dvi:
  146. """
  147. A reader for a dvi ("device-independent") file, as produced by TeX.
  148. The current implementation can only iterate through pages in order,
  149. and does not even attempt to verify the postamble.
  150. This class can be used as a context manager to close the underlying
  151. file upon exit. Pages can be read via iteration. Here is an overly
  152. simple way to extract text without trying to detect whitespace::
  153. >>> with matplotlib.dviread.Dvi('input.dvi', 72) as dvi:
  154. ... for page in dvi:
  155. ... print(''.join(chr(t.glyph) for t in page.text))
  156. """
  157. # dispatch table
  158. _dtable = [None] * 256
  159. _dispatch = partial(_dispatch, _dtable)
  160. def __init__(self, filename, dpi):
  161. """
  162. Read the data from the file named *filename* and convert
  163. TeX's internal units to units of *dpi* per inch.
  164. *dpi* only sets the units and does not limit the resolution.
  165. Use None to return TeX's internal units.
  166. """
  167. _log.debug('Dvi: %s', filename)
  168. self.file = open(filename, 'rb')
  169. self.dpi = dpi
  170. self.fonts = {}
  171. self.state = _dvistate.pre
  172. self.baseline = self._get_baseline(filename)
  173. def _get_baseline(self, filename):
  174. if dict.__getitem__(rcParams, 'text.latex.preview'):
  175. baseline = Path(filename).with_suffix(".baseline")
  176. if baseline.exists():
  177. height, depth, width = baseline.read_bytes().split()
  178. return float(depth)
  179. return None
  180. def __enter__(self):
  181. """Context manager enter method, does nothing."""
  182. return self
  183. def __exit__(self, etype, evalue, etrace):
  184. """
  185. Context manager exit method, closes the underlying file if it is open.
  186. """
  187. self.close()
  188. def __iter__(self):
  189. """
  190. Iterate through the pages of the file.
  191. Yields
  192. ------
  193. Page
  194. Details of all the text and box objects on the page.
  195. The Page tuple contains lists of Text and Box tuples and
  196. the page dimensions, and the Text and Box tuples contain
  197. coordinates transformed into a standard Cartesian
  198. coordinate system at the dpi value given when initializing.
  199. The coordinates are floating point numbers, but otherwise
  200. precision is not lost and coordinate values are not clipped to
  201. integers.
  202. """
  203. while self._read():
  204. yield self._output()
  205. def close(self):
  206. """Close the underlying file if it is open."""
  207. if not self.file.closed:
  208. self.file.close()
  209. def _output(self):
  210. """
  211. Output the text and boxes belonging to the most recent page.
  212. page = dvi._output()
  213. """
  214. minx, miny, maxx, maxy = np.inf, np.inf, -np.inf, -np.inf
  215. maxy_pure = -np.inf
  216. for elt in self.text + self.boxes:
  217. if isinstance(elt, Box):
  218. x, y, h, w = elt
  219. e = 0 # zero depth
  220. else: # glyph
  221. x, y, font, g, w = elt
  222. h, e = font._height_depth_of(g)
  223. minx = min(minx, x)
  224. miny = min(miny, y - h)
  225. maxx = max(maxx, x + w)
  226. maxy = max(maxy, y + e)
  227. maxy_pure = max(maxy_pure, y)
  228. if self._baseline_v is not None:
  229. maxy_pure = self._baseline_v # This should normally be the case.
  230. self._baseline_v = None
  231. if not self.text and not self.boxes: # Avoid infs/nans from inf+/-inf.
  232. return Page(text=[], boxes=[], width=0, height=0, descent=0)
  233. if self.dpi is None:
  234. # special case for ease of debugging: output raw dvi coordinates
  235. return Page(text=self.text, boxes=self.boxes,
  236. width=maxx-minx, height=maxy_pure-miny,
  237. descent=maxy-maxy_pure)
  238. # convert from TeX's "scaled points" to dpi units
  239. d = self.dpi / (72.27 * 2**16)
  240. if self.baseline is None:
  241. descent = (maxy - maxy_pure) * d
  242. else:
  243. descent = self.baseline
  244. text = [Text((x-minx)*d, (maxy-y)*d - descent, f, g, w*d)
  245. for (x, y, f, g, w) in self.text]
  246. boxes = [Box((x-minx)*d, (maxy-y)*d - descent, h*d, w*d)
  247. for (x, y, h, w) in self.boxes]
  248. return Page(text=text, boxes=boxes, width=(maxx-minx)*d,
  249. height=(maxy_pure-miny)*d, descent=descent)
  250. def _read(self):
  251. """
  252. Read one page from the file. Return True if successful,
  253. False if there were no more pages.
  254. """
  255. # Pages appear to start with the sequence
  256. # bop (begin of page)
  257. # xxx comment
  258. # down
  259. # push
  260. # down, down
  261. # push
  262. # down (possibly multiple)
  263. # push <= here, v is the baseline position.
  264. # etc.
  265. # (dviasm is useful to explore this structure.)
  266. self._baseline_v = None
  267. while True:
  268. byte = self.file.read(1)[0]
  269. self._dtable[byte](self, byte)
  270. if (self._baseline_v is None
  271. and len(getattr(self, "stack", [])) == 3):
  272. self._baseline_v = self.v
  273. if byte == 140: # end of page
  274. return True
  275. if self.state is _dvistate.post_post: # end of file
  276. self.close()
  277. return False
  278. def _arg(self, nbytes, signed=False):
  279. """
  280. Read and return an integer argument *nbytes* long.
  281. Signedness is determined by the *signed* keyword.
  282. """
  283. buf = self.file.read(nbytes)
  284. value = buf[0]
  285. if signed and value >= 0x80:
  286. value = value - 0x100
  287. for b in buf[1:]:
  288. value = 0x100*value + b
  289. return value
  290. @_dispatch(min=0, max=127, state=_dvistate.inpage)
  291. def _set_char_immediate(self, char):
  292. self._put_char_real(char)
  293. self.h += self.fonts[self.f]._width_of(char)
  294. @_dispatch(min=128, max=131, state=_dvistate.inpage, args=('olen1',))
  295. def _set_char(self, char):
  296. self._put_char_real(char)
  297. self.h += self.fonts[self.f]._width_of(char)
  298. @_dispatch(132, state=_dvistate.inpage, args=('s4', 's4'))
  299. def _set_rule(self, a, b):
  300. self._put_rule_real(a, b)
  301. self.h += b
  302. @_dispatch(min=133, max=136, state=_dvistate.inpage, args=('olen1',))
  303. def _put_char(self, char):
  304. self._put_char_real(char)
  305. def _put_char_real(self, char):
  306. font = self.fonts[self.f]
  307. if font._vf is None:
  308. self.text.append(Text(self.h, self.v, font, char,
  309. font._width_of(char)))
  310. else:
  311. scale = font._scale
  312. for x, y, f, g, w in font._vf[char].text:
  313. newf = DviFont(scale=_mul2012(scale, f._scale),
  314. tfm=f._tfm, texname=f.texname, vf=f._vf)
  315. self.text.append(Text(self.h + _mul2012(x, scale),
  316. self.v + _mul2012(y, scale),
  317. newf, g, newf._width_of(g)))
  318. self.boxes.extend([Box(self.h + _mul2012(x, scale),
  319. self.v + _mul2012(y, scale),
  320. _mul2012(a, scale), _mul2012(b, scale))
  321. for x, y, a, b in font._vf[char].boxes])
  322. @_dispatch(137, state=_dvistate.inpage, args=('s4', 's4'))
  323. def _put_rule(self, a, b):
  324. self._put_rule_real(a, b)
  325. def _put_rule_real(self, a, b):
  326. if a > 0 and b > 0:
  327. self.boxes.append(Box(self.h, self.v, a, b))
  328. @_dispatch(138)
  329. def _nop(self, _):
  330. pass
  331. @_dispatch(139, state=_dvistate.outer, args=('s4',)*11)
  332. def _bop(self, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, p):
  333. self.state = _dvistate.inpage
  334. self.h, self.v, self.w, self.x, self.y, self.z = 0, 0, 0, 0, 0, 0
  335. self.stack = []
  336. self.text = [] # list of Text objects
  337. self.boxes = [] # list of Box objects
  338. @_dispatch(140, state=_dvistate.inpage)
  339. def _eop(self, _):
  340. self.state = _dvistate.outer
  341. del self.h, self.v, self.w, self.x, self.y, self.z, self.stack
  342. @_dispatch(141, state=_dvistate.inpage)
  343. def _push(self, _):
  344. self.stack.append((self.h, self.v, self.w, self.x, self.y, self.z))
  345. @_dispatch(142, state=_dvistate.inpage)
  346. def _pop(self, _):
  347. self.h, self.v, self.w, self.x, self.y, self.z = self.stack.pop()
  348. @_dispatch(min=143, max=146, state=_dvistate.inpage, args=('slen1',))
  349. def _right(self, b):
  350. self.h += b
  351. @_dispatch(min=147, max=151, state=_dvistate.inpage, args=('slen',))
  352. def _right_w(self, new_w):
  353. if new_w is not None:
  354. self.w = new_w
  355. self.h += self.w
  356. @_dispatch(min=152, max=156, state=_dvistate.inpage, args=('slen',))
  357. def _right_x(self, new_x):
  358. if new_x is not None:
  359. self.x = new_x
  360. self.h += self.x
  361. @_dispatch(min=157, max=160, state=_dvistate.inpage, args=('slen1',))
  362. def _down(self, a):
  363. self.v += a
  364. @_dispatch(min=161, max=165, state=_dvistate.inpage, args=('slen',))
  365. def _down_y(self, new_y):
  366. if new_y is not None:
  367. self.y = new_y
  368. self.v += self.y
  369. @_dispatch(min=166, max=170, state=_dvistate.inpage, args=('slen',))
  370. def _down_z(self, new_z):
  371. if new_z is not None:
  372. self.z = new_z
  373. self.v += self.z
  374. @_dispatch(min=171, max=234, state=_dvistate.inpage)
  375. def _fnt_num_immediate(self, k):
  376. self.f = k
  377. @_dispatch(min=235, max=238, state=_dvistate.inpage, args=('olen1',))
  378. def _fnt_num(self, new_f):
  379. self.f = new_f
  380. @_dispatch(min=239, max=242, args=('ulen1',))
  381. def _xxx(self, datalen):
  382. special = self.file.read(datalen)
  383. _log.debug(
  384. 'Dvi._xxx: encountered special: %s',
  385. ''.join([chr(ch) if 32 <= ch < 127 else '<%02x>' % ch
  386. for ch in special]))
  387. @_dispatch(min=243, max=246, args=('olen1', 'u4', 'u4', 'u4', 'u1', 'u1'))
  388. def _fnt_def(self, k, c, s, d, a, l):
  389. self._fnt_def_real(k, c, s, d, a, l)
  390. def _fnt_def_real(self, k, c, s, d, a, l):
  391. n = self.file.read(a + l)
  392. fontname = n[-l:].decode('ascii')
  393. tfm = _tfmfile(fontname)
  394. if tfm is None:
  395. raise FileNotFoundError("missing font metrics file: %s" % fontname)
  396. if c != 0 and tfm.checksum != 0 and c != tfm.checksum:
  397. raise ValueError('tfm checksum mismatch: %s' % n)
  398. vf = _vffile(fontname)
  399. self.fonts[k] = DviFont(scale=s, tfm=tfm, texname=n, vf=vf)
  400. @_dispatch(247, state=_dvistate.pre, args=('u1', 'u4', 'u4', 'u4', 'u1'))
  401. def _pre(self, i, num, den, mag, k):
  402. self.file.read(k) # comment in the dvi file
  403. if i != 2:
  404. raise ValueError("Unknown dvi format %d" % i)
  405. if num != 25400000 or den != 7227 * 2**16:
  406. raise ValueError("Nonstandard units in dvi file")
  407. # meaning: TeX always uses those exact values, so it
  408. # should be enough for us to support those
  409. # (There are 72.27 pt to an inch so 7227 pt =
  410. # 7227 * 2**16 sp to 100 in. The numerator is multiplied
  411. # by 10^5 to get units of 10**-7 meters.)
  412. if mag != 1000:
  413. raise ValueError("Nonstandard magnification in dvi file")
  414. # meaning: LaTeX seems to frown on setting \mag, so
  415. # I think we can assume this is constant
  416. self.state = _dvistate.outer
  417. @_dispatch(248, state=_dvistate.outer)
  418. def _post(self, _):
  419. self.state = _dvistate.post_post
  420. # TODO: actually read the postamble and finale?
  421. # currently post_post just triggers closing the file
  422. @_dispatch(249)
  423. def _post_post(self, _):
  424. raise NotImplementedError
  425. @_dispatch(min=250, max=255)
  426. def _malformed(self, offset):
  427. raise ValueError(f"unknown command: byte {250 + offset}")
  428. class DviFont:
  429. """
  430. Encapsulation of a font that a DVI file can refer to.
  431. This class holds a font's texname and size, supports comparison,
  432. and knows the widths of glyphs in the same units as the AFM file.
  433. There are also internal attributes (for use by dviread.py) that
  434. are *not* used for comparison.
  435. The size is in Adobe points (converted from TeX points).
  436. Parameters
  437. ----------
  438. scale : float
  439. Factor by which the font is scaled from its natural size.
  440. tfm : Tfm
  441. TeX font metrics for this font
  442. texname : bytes
  443. Name of the font as used internally by TeX and friends, as an
  444. ASCII bytestring. This is usually very different from any external
  445. font names, and :class:`dviread.PsfontsMap` can be used to find
  446. the external name of the font.
  447. vf : Vf
  448. A TeX "virtual font" file, or None if this font is not virtual.
  449. Attributes
  450. ----------
  451. texname : bytes
  452. size : float
  453. Size of the font in Adobe points, converted from the slightly
  454. smaller TeX points.
  455. widths : list
  456. Widths of glyphs in glyph-space units, typically 1/1000ths of
  457. the point size.
  458. """
  459. __slots__ = ('texname', 'size', 'widths', '_scale', '_vf', '_tfm')
  460. def __init__(self, scale, tfm, texname, vf):
  461. cbook._check_isinstance(bytes, texname=texname)
  462. self._scale = scale
  463. self._tfm = tfm
  464. self.texname = texname
  465. self._vf = vf
  466. self.size = scale * (72.0 / (72.27 * 2**16))
  467. try:
  468. nchars = max(tfm.width) + 1
  469. except ValueError:
  470. nchars = 0
  471. self.widths = [(1000*tfm.width.get(char, 0)) >> 20
  472. for char in range(nchars)]
  473. def __eq__(self, other):
  474. return (type(self) == type(other)
  475. and self.texname == other.texname and self.size == other.size)
  476. def __ne__(self, other):
  477. return not self.__eq__(other)
  478. def __repr__(self):
  479. return "<{}: {}>".format(type(self).__name__, self.texname)
  480. def _width_of(self, char):
  481. """Width of char in dvi units."""
  482. width = self._tfm.width.get(char, None)
  483. if width is not None:
  484. return _mul2012(width, self._scale)
  485. _log.debug('No width for char %d in font %s.', char, self.texname)
  486. return 0
  487. def _height_depth_of(self, char):
  488. """Height and depth of char in dvi units."""
  489. result = []
  490. for metric, name in ((self._tfm.height, "height"),
  491. (self._tfm.depth, "depth")):
  492. value = metric.get(char, None)
  493. if value is None:
  494. _log.debug('No %s for char %d in font %s',
  495. name, char, self.texname)
  496. result.append(0)
  497. else:
  498. result.append(_mul2012(value, self._scale))
  499. # cmsy10 glyph 0 ("minus") has a nonzero descent so that TeX aligns
  500. # equations properly (https://tex.stackexchange.com/questions/526103/),
  501. # but we actually care about the rasterization depth to align the
  502. # dvipng-generated images.
  503. if self.texname == b"cmsy10" and char == 0:
  504. result[-1] = 0
  505. return result
  506. class Vf(Dvi):
  507. r"""
  508. A virtual font (\*.vf file) containing subroutines for dvi files.
  509. Parameters
  510. ----------
  511. filename : str or path-like
  512. Notes
  513. -----
  514. The virtual font format is a derivative of dvi:
  515. http://mirrors.ctan.org/info/knuth/virtual-fonts
  516. This class reuses some of the machinery of `Dvi`
  517. but replaces the `_read` loop and dispatch mechanism.
  518. Examples
  519. --------
  520. ::
  521. vf = Vf(filename)
  522. glyph = vf[code]
  523. glyph.text, glyph.boxes, glyph.width
  524. """
  525. def __init__(self, filename):
  526. Dvi.__init__(self, filename, 0)
  527. try:
  528. self._first_font = None
  529. self._chars = {}
  530. self._read()
  531. finally:
  532. self.close()
  533. def __getitem__(self, code):
  534. return self._chars[code]
  535. def _read(self):
  536. """
  537. Read one page from the file. Return True if successful,
  538. False if there were no more pages.
  539. """
  540. packet_char, packet_ends = None, None
  541. packet_len, packet_width = None, None
  542. while True:
  543. byte = self.file.read(1)[0]
  544. # If we are in a packet, execute the dvi instructions
  545. if self.state is _dvistate.inpage:
  546. byte_at = self.file.tell()-1
  547. if byte_at == packet_ends:
  548. self._finalize_packet(packet_char, packet_width)
  549. packet_len, packet_char, packet_width = None, None, None
  550. # fall through to out-of-packet code
  551. elif byte_at > packet_ends:
  552. raise ValueError("Packet length mismatch in vf file")
  553. else:
  554. if byte in (139, 140) or byte >= 243:
  555. raise ValueError(
  556. "Inappropriate opcode %d in vf file" % byte)
  557. Dvi._dtable[byte](self, byte)
  558. continue
  559. # We are outside a packet
  560. if byte < 242: # a short packet (length given by byte)
  561. packet_len = byte
  562. packet_char, packet_width = self._arg(1), self._arg(3)
  563. packet_ends = self._init_packet(byte)
  564. self.state = _dvistate.inpage
  565. elif byte == 242: # a long packet
  566. packet_len, packet_char, packet_width = \
  567. [self._arg(x) for x in (4, 4, 4)]
  568. self._init_packet(packet_len)
  569. elif 243 <= byte <= 246:
  570. k = self._arg(byte - 242, byte == 246)
  571. c, s, d, a, l = [self._arg(x) for x in (4, 4, 4, 1, 1)]
  572. self._fnt_def_real(k, c, s, d, a, l)
  573. if self._first_font is None:
  574. self._first_font = k
  575. elif byte == 247: # preamble
  576. i, k = self._arg(1), self._arg(1)
  577. x = self.file.read(k)
  578. cs, ds = self._arg(4), self._arg(4)
  579. self._pre(i, x, cs, ds)
  580. elif byte == 248: # postamble (just some number of 248s)
  581. break
  582. else:
  583. raise ValueError("Unknown vf opcode %d" % byte)
  584. def _init_packet(self, pl):
  585. if self.state != _dvistate.outer:
  586. raise ValueError("Misplaced packet in vf file")
  587. self.h, self.v, self.w, self.x, self.y, self.z = 0, 0, 0, 0, 0, 0
  588. self.stack, self.text, self.boxes = [], [], []
  589. self.f = self._first_font
  590. return self.file.tell() + pl
  591. def _finalize_packet(self, packet_char, packet_width):
  592. self._chars[packet_char] = Page(
  593. text=self.text, boxes=self.boxes, width=packet_width,
  594. height=None, descent=None)
  595. self.state = _dvistate.outer
  596. def _pre(self, i, x, cs, ds):
  597. if self.state is not _dvistate.pre:
  598. raise ValueError("pre command in middle of vf file")
  599. if i != 202:
  600. raise ValueError("Unknown vf format %d" % i)
  601. if len(x):
  602. _log.debug('vf file comment: %s', x)
  603. self.state = _dvistate.outer
  604. # cs = checksum, ds = design size
  605. def _fix2comp(num):
  606. """Convert from two's complement to negative."""
  607. assert 0 <= num < 2**32
  608. if num & 2**31:
  609. return num - 2**32
  610. else:
  611. return num
  612. def _mul2012(num1, num2):
  613. """Multiply two numbers in 20.12 fixed point format."""
  614. # Separated into a function because >> has surprising precedence
  615. return (num1*num2) >> 20
  616. class Tfm:
  617. """
  618. A TeX Font Metric file.
  619. This implementation covers only the bare minimum needed by the Dvi class.
  620. Parameters
  621. ----------
  622. filename : str or path-like
  623. Attributes
  624. ----------
  625. checksum : int
  626. Used for verifying against the dvi file.
  627. design_size : int
  628. Design size of the font (unknown units)
  629. width, height, depth : dict
  630. Dimensions of each character, need to be scaled by the factor
  631. specified in the dvi file. These are dicts because indexing may
  632. not start from 0.
  633. """
  634. __slots__ = ('checksum', 'design_size', 'width', 'height', 'depth')
  635. def __init__(self, filename):
  636. _log.debug('opening tfm file %s', filename)
  637. with open(filename, 'rb') as file:
  638. header1 = file.read(24)
  639. lh, bc, ec, nw, nh, nd = \
  640. struct.unpack('!6H', header1[2:14])
  641. _log.debug('lh=%d, bc=%d, ec=%d, nw=%d, nh=%d, nd=%d',
  642. lh, bc, ec, nw, nh, nd)
  643. header2 = file.read(4*lh)
  644. self.checksum, self.design_size = \
  645. struct.unpack('!2I', header2[:8])
  646. # there is also encoding information etc.
  647. char_info = file.read(4*(ec-bc+1))
  648. widths = file.read(4*nw)
  649. heights = file.read(4*nh)
  650. depths = file.read(4*nd)
  651. self.width, self.height, self.depth = {}, {}, {}
  652. widths, heights, depths = \
  653. [struct.unpack('!%dI' % (len(x)/4), x)
  654. for x in (widths, heights, depths)]
  655. for idx, char in enumerate(range(bc, ec+1)):
  656. byte0 = char_info[4*idx]
  657. byte1 = char_info[4*idx+1]
  658. self.width[char] = _fix2comp(widths[byte0])
  659. self.height[char] = _fix2comp(heights[byte1 >> 4])
  660. self.depth[char] = _fix2comp(depths[byte1 & 0xf])
  661. PsFont = namedtuple('PsFont', 'texname psname effects encoding filename')
  662. class PsfontsMap:
  663. """
  664. A psfonts.map formatted file, mapping TeX fonts to PS fonts.
  665. Parameters
  666. ----------
  667. filename : str or path-like
  668. Notes
  669. -----
  670. For historical reasons, TeX knows many Type-1 fonts by different
  671. names than the outside world. (For one thing, the names have to
  672. fit in eight characters.) Also, TeX's native fonts are not Type-1
  673. but Metafont, which is nontrivial to convert to PostScript except
  674. as a bitmap. While high-quality conversions to Type-1 format exist
  675. and are shipped with modern TeX distributions, we need to know
  676. which Type-1 fonts are the counterparts of which native fonts. For
  677. these reasons a mapping is needed from internal font names to font
  678. file names.
  679. A texmf tree typically includes mapping files called e.g.
  680. :file:`psfonts.map`, :file:`pdftex.map`, or :file:`dvipdfm.map`.
  681. The file :file:`psfonts.map` is used by :program:`dvips`,
  682. :file:`pdftex.map` by :program:`pdfTeX`, and :file:`dvipdfm.map`
  683. by :program:`dvipdfm`. :file:`psfonts.map` might avoid embedding
  684. the 35 PostScript fonts (i.e., have no filename for them, as in
  685. the Times-Bold example above), while the pdf-related files perhaps
  686. only avoid the "Base 14" pdf fonts. But the user may have
  687. configured these files differently.
  688. Examples
  689. --------
  690. >>> map = PsfontsMap(find_tex_file('pdftex.map'))
  691. >>> entry = map[b'ptmbo8r']
  692. >>> entry.texname
  693. b'ptmbo8r'
  694. >>> entry.psname
  695. b'Times-Bold'
  696. >>> entry.encoding
  697. '/usr/local/texlive/2008/texmf-dist/fonts/enc/dvips/base/8r.enc'
  698. >>> entry.effects
  699. {'slant': 0.16700000000000001}
  700. >>> entry.filename
  701. """
  702. __slots__ = ('_font', '_filename')
  703. # Create a filename -> PsfontsMap cache, so that calling
  704. # `PsfontsMap(filename)` with the same filename a second time immediately
  705. # returns the same object.
  706. @lru_cache()
  707. def __new__(cls, filename):
  708. self = object.__new__(cls)
  709. self._font = {}
  710. self._filename = os.fsdecode(filename)
  711. with open(filename, 'rb') as file:
  712. self._parse(file)
  713. return self
  714. def __getitem__(self, texname):
  715. assert isinstance(texname, bytes)
  716. try:
  717. result = self._font[texname]
  718. except KeyError:
  719. fmt = ('A PostScript file for the font whose TeX name is "{0}" '
  720. 'could not be found in the file "{1}". The dviread module '
  721. 'can only handle fonts that have an associated PostScript '
  722. 'font file. '
  723. 'This problem can often be solved by installing '
  724. 'a suitable PostScript font package in your (TeX) '
  725. 'package manager.')
  726. msg = fmt.format(texname.decode('ascii'), self._filename)
  727. msg = textwrap.fill(msg, break_on_hyphens=False,
  728. break_long_words=False)
  729. _log.info(msg)
  730. raise
  731. fn, enc = result.filename, result.encoding
  732. if fn is not None and not fn.startswith(b'/'):
  733. fn = find_tex_file(fn)
  734. if enc is not None and not enc.startswith(b'/'):
  735. enc = find_tex_file(result.encoding)
  736. return result._replace(filename=fn, encoding=enc)
  737. def _parse(self, file):
  738. """
  739. Parse the font mapping file.
  740. The format is, AFAIK: texname fontname [effects and filenames]
  741. Effects are PostScript snippets like ".177 SlantFont",
  742. filenames begin with one or two less-than signs. A filename
  743. ending in enc is an encoding file, other filenames are font
  744. files. This can be overridden with a left bracket: <[foobar
  745. indicates an encoding file named foobar.
  746. There is some difference between <foo.pfb and <<bar.pfb in
  747. subsetting, but I have no example of << in my TeX installation.
  748. """
  749. # If the map file specifies multiple encodings for a font, we
  750. # follow pdfTeX in choosing the last one specified. Such
  751. # entries are probably mistakes but they have occurred.
  752. # http://tex.stackexchange.com/questions/10826/
  753. # http://article.gmane.org/gmane.comp.tex.pdftex/4914
  754. empty_re = re.compile(br'%|\s*$')
  755. word_re = re.compile(
  756. br'''(?x) (?:
  757. "<\[ (?P<enc1> [^"]+ )" | # quoted encoding marked by [
  758. "< (?P<enc2> [^"]+.enc)" | # quoted encoding, ends in .enc
  759. "<<? (?P<file1> [^"]+ )" | # quoted font file name
  760. " (?P<eff1> [^"]+ )" | # quoted effects or font name
  761. <\[ (?P<enc3> \S+ ) | # encoding marked by [
  762. < (?P<enc4> \S+ .enc) | # encoding, ends in .enc
  763. <<? (?P<file2> \S+ ) | # font file name
  764. (?P<eff2> \S+ ) # effects or font name
  765. )''')
  766. effects_re = re.compile(
  767. br'''(?x) (?P<slant> -?[0-9]*(?:\.[0-9]+)) \s* SlantFont
  768. | (?P<extend>-?[0-9]*(?:\.[0-9]+)) \s* ExtendFont''')
  769. lines = (line.strip()
  770. for line in file
  771. if not empty_re.match(line))
  772. for line in lines:
  773. effects, encoding, filename = b'', None, None
  774. words = word_re.finditer(line)
  775. # The named groups are mutually exclusive and are
  776. # referenced below at an estimated order of probability of
  777. # occurrence based on looking at my copy of pdftex.map.
  778. # The font names are probably unquoted:
  779. w = next(words)
  780. texname = w.group('eff2') or w.group('eff1')
  781. w = next(words)
  782. psname = w.group('eff2') or w.group('eff1')
  783. for w in words:
  784. # Any effects are almost always quoted:
  785. eff = w.group('eff1') or w.group('eff2')
  786. if eff:
  787. effects = eff
  788. continue
  789. # Encoding files usually have the .enc suffix
  790. # and almost never need quoting:
  791. enc = (w.group('enc4') or w.group('enc3') or
  792. w.group('enc2') or w.group('enc1'))
  793. if enc:
  794. if encoding is not None:
  795. _log.debug('Multiple encodings for %s = %s',
  796. texname, psname)
  797. encoding = enc
  798. continue
  799. # File names are probably unquoted:
  800. filename = w.group('file2') or w.group('file1')
  801. effects_dict = {}
  802. for match in effects_re.finditer(effects):
  803. slant = match.group('slant')
  804. if slant:
  805. effects_dict['slant'] = float(slant)
  806. else:
  807. effects_dict['extend'] = float(match.group('extend'))
  808. self._font[texname] = PsFont(
  809. texname=texname, psname=psname, effects=effects_dict,
  810. encoding=encoding, filename=filename)
  811. @cbook.deprecated("3.3")
  812. class Encoding:
  813. r"""
  814. Parse a \*.enc file referenced from a psfonts.map style file.
  815. The format this class understands is a very limited subset of PostScript.
  816. Usage (subject to change)::
  817. for name in Encoding(filename):
  818. whatever(name)
  819. Parameters
  820. ----------
  821. filename : str or path-like
  822. Attributes
  823. ----------
  824. encoding : list
  825. List of character names
  826. """
  827. __slots__ = ('encoding',)
  828. def __init__(self, filename):
  829. with open(filename, 'rb') as file:
  830. _log.debug('Parsing TeX encoding %s', filename)
  831. self.encoding = self._parse(file)
  832. _log.debug('Result: %s', self.encoding)
  833. def __iter__(self):
  834. yield from self.encoding
  835. @staticmethod
  836. def _parse(file):
  837. lines = (line.split(b'%', 1)[0].strip() for line in file)
  838. data = b''.join(lines)
  839. beginning = data.find(b'[')
  840. if beginning < 0:
  841. raise ValueError("Cannot locate beginning of encoding in {}"
  842. .format(file))
  843. data = data[beginning:]
  844. end = data.find(b']')
  845. if end < 0:
  846. raise ValueError("Cannot locate end of encoding in {}"
  847. .format(file))
  848. data = data[:end]
  849. return re.findall(br'/([^][{}<>\s]+)', data)
  850. # Note: this function should ultimately replace the Encoding class, which
  851. # appears to be mostly broken: because it uses b''.join(), there is no
  852. # whitespace left between glyph names (only slashes) so the final re.findall
  853. # returns a single string with all glyph names. However this does not appear
  854. # to bother backend_pdf, so that needs to be investigated more. (The fixed
  855. # version below is necessary for textpath/backend_svg, though.)
  856. def _parse_enc(path):
  857. r"""
  858. Parses a \*.enc file referenced from a psfonts.map style file.
  859. The format this class understands is a very limited subset of PostScript.
  860. Parameters
  861. ----------
  862. path : os.PathLike
  863. Returns
  864. -------
  865. list
  866. The nth entry of the list is the PostScript glyph name of the nth
  867. glyph.
  868. """
  869. no_comments = re.sub("%.*", "", Path(path).read_text(encoding="ascii"))
  870. array = re.search(r"(?s)\[(.*)\]", no_comments).group(1)
  871. lines = [line for line in array.split() if line]
  872. if all(line.startswith("/") for line in lines):
  873. return [line[1:] for line in lines]
  874. else:
  875. raise ValueError(
  876. "Failed to parse {} as Postscript encoding".format(path))
  877. @lru_cache()
  878. def find_tex_file(filename, format=None):
  879. """
  880. Find a file in the texmf tree.
  881. Calls :program:`kpsewhich` which is an interface to the kpathsea
  882. library [1]_. Most existing TeX distributions on Unix-like systems use
  883. kpathsea. It is also available as part of MikTeX, a popular
  884. distribution on Windows.
  885. *If the file is not found, an empty string is returned*.
  886. Parameters
  887. ----------
  888. filename : str or path-like
  889. format : str or bytes
  890. Used as the value of the ``--format`` option to :program:`kpsewhich`.
  891. Could be e.g. 'tfm' or 'vf' to limit the search to that type of files.
  892. References
  893. ----------
  894. .. [1] `Kpathsea documentation <http://www.tug.org/kpathsea/>`_
  895. The library that :program:`kpsewhich` is part of.
  896. """
  897. # we expect these to always be ascii encoded, but use utf-8
  898. # out of caution
  899. if isinstance(filename, bytes):
  900. filename = filename.decode('utf-8', errors='replace')
  901. if isinstance(format, bytes):
  902. format = format.decode('utf-8', errors='replace')
  903. if os.name == 'nt':
  904. # On Windows only, kpathsea can use utf-8 for cmd args and output.
  905. # The `command_line_encoding` environment variable is set to force it
  906. # to always use utf-8 encoding. See Matplotlib issue #11848.
  907. kwargs = {'env': {**os.environ, 'command_line_encoding': 'utf-8'},
  908. 'encoding': 'utf-8'}
  909. else: # On POSIX, run through the equivalent of os.fsdecode().
  910. kwargs = {'encoding': sys.getfilesystemencoding(),
  911. 'errors': 'surrogatescape'}
  912. cmd = ['kpsewhich']
  913. if format is not None:
  914. cmd += ['--format=' + format]
  915. cmd += [filename]
  916. try:
  917. result = cbook._check_and_log_subprocess(cmd, _log, **kwargs)
  918. except (FileNotFoundError, RuntimeError):
  919. return ''
  920. return result.rstrip('\n')
  921. @lru_cache()
  922. def _fontfile(cls, suffix, texname):
  923. filename = find_tex_file(texname + suffix)
  924. return cls(filename) if filename else None
  925. _tfmfile = partial(_fontfile, Tfm, ".tfm")
  926. _vffile = partial(_fontfile, Vf, ".vf")
  927. if __name__ == '__main__':
  928. from argparse import ArgumentParser
  929. import itertools
  930. parser = ArgumentParser()
  931. parser.add_argument("filename")
  932. parser.add_argument("dpi", nargs="?", type=float, default=None)
  933. args = parser.parse_args()
  934. with Dvi(args.filename, args.dpi) as dvi:
  935. fontmap = PsfontsMap(find_tex_file('pdftex.map'))
  936. for page in dvi:
  937. print('=== new page ===')
  938. for font, group in itertools.groupby(
  939. page.text, lambda text: text.font):
  940. print('font', font.texname, 'scaled', font._scale / 2 ** 20)
  941. for text in group:
  942. print(text.x, text.y, text.glyph,
  943. chr(text.glyph) if chr(text.glyph).isprintable()
  944. else ".",
  945. text.width)
  946. for x, y, w, h in page.boxes:
  947. print(x, y, 'BOX', w, h)