A reader for a dvi ("device-independent") file, as produced by TeX. The current implementation can only iterate through pages in order, and does not even attempt to verify the postamble. This class can be used as a context manager to close the underlying file upon exit. Pages
| 217 | |
| 218 | |
| 219 | class Dvi: |
| 220 | """ |
| 221 | A reader for a dvi ("device-independent") file, as produced by TeX. |
| 222 | |
| 223 | The current implementation can only iterate through pages in order, |
| 224 | and does not even attempt to verify the postamble. |
| 225 | |
| 226 | This class can be used as a context manager to close the underlying |
| 227 | file upon exit. Pages can be read via iteration. Here is an overly |
| 228 | simple way to extract text without trying to detect whitespace:: |
| 229 | |
| 230 | >>> with matplotlib.dviread.Dvi('input.dvi', 72) as dvi: |
| 231 | ... for page in dvi: |
| 232 | ... print(''.join(chr(t.glyph) for t in page.text)) |
| 233 | """ |
| 234 | # dispatch table |
| 235 | _dtable = [None] * 256 |
| 236 | _dispatch = partial(_dispatch, _dtable) |
| 237 | |
| 238 | def __init__(self, filename, dpi): |
| 239 | """ |
| 240 | Read the data from the file named *filename* and convert |
| 241 | TeX's internal units to units of *dpi* per inch. |
| 242 | *dpi* only sets the units and does not limit the resolution. |
| 243 | Use None to return TeX's internal units. |
| 244 | """ |
| 245 | _log.debug('Dvi: %s', filename) |
| 246 | self.file = open(filename, 'rb') |
| 247 | self.dpi = dpi |
| 248 | self.fonts = {} |
| 249 | self.state = _dvistate.pre |
| 250 | self._missing_font = None |
| 251 | |
| 252 | def __enter__(self): |
| 253 | """Context manager enter method, does nothing.""" |
| 254 | return self |
| 255 | |
| 256 | def __exit__(self, etype, evalue, etrace): |
| 257 | """ |
| 258 | Context manager exit method, closes the underlying file if it is open. |
| 259 | """ |
| 260 | self.close() |
| 261 | |
| 262 | def __iter__(self): |
| 263 | """ |
| 264 | Iterate through the pages of the file. |
| 265 | |
| 266 | Yields |
| 267 | ------ |
| 268 | Page |
| 269 | Details of all the text and box objects on the page. |
| 270 | The Page tuple contains lists of Text and Box tuples and |
| 271 | the page dimensions, and the Text and Box tuples contain |
| 272 | coordinates transformed into a standard Cartesian |
| 273 | coordinate system at the dpi value given when initializing. |
| 274 | The coordinates are floating point numbers, but otherwise |
| 275 | precision is not lost and coordinate values are not clipped to |
| 276 | integers. |
no outgoing calls
no test coverage detected