Skip to content

Cleanup dviread. #12715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 33 additions & 36 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -541,28 +543,25 @@ 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)
_log.debug('No width for char %d in font %s.', char, self.texname)
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")):
Expand All @@ -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::

Expand All @@ -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`
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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)