From 502392f02776300851fd449cec03cc445b2ec461 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 2 Nov 2018 18:47:14 +0100 Subject: [PATCH] Cleanup dviread. In the repr of DviFont, we don't include the tfm anf vf fields, as they are actually always directly derived from texname anyways. --- lib/matplotlib/dviread.py | 69 +++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/lib/matplotlib/dviread.py b/lib/matplotlib/dviread.py index 93fdbc677624..c204c6c2362e 100644 --- a/lib/matplotlib/dviread.py +++ b/lib/matplotlib/dviread.py @@ -530,8 +530,10 @@ def __init__(self, scale, tfm, texname, vf): if not isinstance(texname, bytes): raise ValueError("texname must be a bytestring, got %s" % type(texname)) - self._scale, self._tfm, self.texname, self._vf = \ - scale, tfm, texname, vf + self._scale = scale + self._tfm = tfm + self.texname = texname + self._vf = vf self.size = scale * (72.0 / (72.27 * 2**16)) try: nchars = max(tfm.width) + 1 @@ -541,17 +543,17 @@ def __init__(self, scale, tfm, texname, vf): for char in range(nchars)] def __eq__(self, other): - return self.__class__ == other.__class__ and \ - self.texname == other.texname and self.size == other.size + return (type(self) == type(other) + and self.texname == other.texname and self.size == other.size) def __ne__(self, other): return not self.__eq__(other) - def _width_of(self, char): - """ - Width of char in dvi units. For internal use by dviread.py. - """ + def __repr__(self): + return "<{}: {}>".format(type(self).__name__, self.texname) + def _width_of(self, char): + """Width of char in dvi units.""" width = self._tfm.width.get(char, None) if width is not None: return _mul2012(width, self._scale) @@ -559,10 +561,7 @@ def _width_of(self, char): return 0 def _height_depth_of(self, char): - """ - Height and depth of char in dvi units. For internal use by dviread.py. - """ - + """Height and depth of char in dvi units.""" result = [] for metric, name in ((self._tfm.height, "height"), (self._tfm.depth, "depth")): @@ -577,8 +576,8 @@ def _height_depth_of(self, char): class Vf(Dvi): - """ - A virtual font (\\*.vf file) containing subroutines for dvi files. + r""" + A virtual font (\*.vf file) containing subroutines for dvi files. Usage:: @@ -588,12 +587,10 @@ class Vf(Dvi): Parameters ---------- - filename : string or bytestring Notes ----- - The virtual font format is a derivative of dvi: http://mirrors.ctan.org/info/knuth/virtual-fonts This class reuses some of the machinery of `Dvi` @@ -689,9 +686,7 @@ def _pre(self, i, x, cs, ds): def _fix2comp(num): - """ - Convert from two's complement to negative. - """ + """Convert from two's complement to negative.""" assert 0 <= num < 2**32 if num & 2**31: return num - 2**32 @@ -700,9 +695,7 @@ def _fix2comp(num): def _mul2012(num1, num2): - """ - Multiply two numbers in 20.12 fixed point format. - """ + """Multiply two numbers in 20.12 fixed point format.""" # Separated into a function because >> has surprising precedence return (num1*num2) >> 20 @@ -931,8 +924,8 @@ def _parse(self, file): class Encoding(object): - """ - Parses a \\*.enc file referenced from a psfonts.map style file. + r""" + Parses a \*.enc file referenced from a psfonts.map style file. The format this class understands is a very limited subset of PostScript. @@ -1045,21 +1038,25 @@ def _fontfile(cls, suffix, texname): if __name__ == '__main__': + from argparse import ArgumentParser + import itertools import sys - fname = sys.argv[1] - try: - dpi = float(sys.argv[2]) - except IndexError: - dpi = None - with Dvi(fname, dpi) as dvi: + + parser = ArgumentParser() + parser.add_argument("filename") + parser.add_argument("dpi", nargs="?", type=float, default=None) + args = parser.parse_args() + with Dvi(args.filename, args.dpi) as dvi: fontmap = PsfontsMap(find_tex_file('pdftex.map')) for page in dvi: print('=== new page ===') - fPrev = None - for x, y, f, c, w in page.text: - if f != fPrev: - print('font', f.texname, 'scaled', f._scale/pow(2.0, 20)) - fPrev = f - print(x, y, c, 32 <= c < 128 and chr(c) or '.', w) + for font, group in itertools.groupby( + page.text, lambda text: text.font): + print('font', font.texname, 'scaled', font._scale / 2 ** 20) + for text in group: + print(text.x, text.y, text.glyph, + chr(text.glyph) if chr(text.glyph).isprintable() + else ".", + text.width) for x, y, w, h in page.boxes: print(x, y, 'BOX', w, h)