Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 694b482

Browse files
authored
Merge pull request #12715 from anntzer/dviread
MNT: Cleanup dviread.
2 parents 9406ff0 + 502392f commit 694b482

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

lib/matplotlib/dviread.py

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,10 @@ def __init__(self, scale, tfm, texname, vf):
530530
if not isinstance(texname, bytes):
531531
raise ValueError("texname must be a bytestring, got %s"
532532
% type(texname))
533-
self._scale, self._tfm, self.texname, self._vf = \
534-
scale, tfm, texname, vf
533+
self._scale = scale
534+
self._tfm = tfm
535+
self.texname = texname
536+
self._vf = vf
535537
self.size = scale * (72.0 / (72.27 * 2**16))
536538
try:
537539
nchars = max(tfm.width) + 1
@@ -541,28 +543,25 @@ def __init__(self, scale, tfm, texname, vf):
541543
for char in range(nchars)]
542544

543545
def __eq__(self, other):
544-
return self.__class__ == other.__class__ and \
545-
self.texname == other.texname and self.size == other.size
546+
return (type(self) == type(other)
547+
and self.texname == other.texname and self.size == other.size)
546548

547549
def __ne__(self, other):
548550
return not self.__eq__(other)
549551

550-
def _width_of(self, char):
551-
"""
552-
Width of char in dvi units. For internal use by dviread.py.
553-
"""
552+
def __repr__(self):
553+
return "<{}: {}>".format(type(self).__name__, self.texname)
554554

555+
def _width_of(self, char):
556+
"""Width of char in dvi units."""
555557
width = self._tfm.width.get(char, None)
556558
if width is not None:
557559
return _mul2012(width, self._scale)
558560
_log.debug('No width for char %d in font %s.', char, self.texname)
559561
return 0
560562

561563
def _height_depth_of(self, char):
562-
"""
563-
Height and depth of char in dvi units. For internal use by dviread.py.
564-
"""
565-
564+
"""Height and depth of char in dvi units."""
566565
result = []
567566
for metric, name in ((self._tfm.height, "height"),
568567
(self._tfm.depth, "depth")):
@@ -577,8 +576,8 @@ def _height_depth_of(self, char):
577576

578577

579578
class Vf(Dvi):
580-
"""
581-
A virtual font (\\*.vf file) containing subroutines for dvi files.
579+
r"""
580+
A virtual font (\*.vf file) containing subroutines for dvi files.
582581
583582
Usage::
584583
@@ -588,12 +587,10 @@ class Vf(Dvi):
588587
589588
Parameters
590589
----------
591-
592590
filename : string or bytestring
593591
594592
Notes
595593
-----
596-
597594
The virtual font format is a derivative of dvi:
598595
http://mirrors.ctan.org/info/knuth/virtual-fonts
599596
This class reuses some of the machinery of `Dvi`
@@ -689,9 +686,7 @@ def _pre(self, i, x, cs, ds):
689686

690687

691688
def _fix2comp(num):
692-
"""
693-
Convert from two's complement to negative.
694-
"""
689+
"""Convert from two's complement to negative."""
695690
assert 0 <= num < 2**32
696691
if num & 2**31:
697692
return num - 2**32
@@ -700,9 +695,7 @@ def _fix2comp(num):
700695

701696

702697
def _mul2012(num1, num2):
703-
"""
704-
Multiply two numbers in 20.12 fixed point format.
705-
"""
698+
"""Multiply two numbers in 20.12 fixed point format."""
706699
# Separated into a function because >> has surprising precedence
707700
return (num1*num2) >> 20
708701

@@ -931,8 +924,8 @@ def _parse(self, file):
931924

932925

933926
class Encoding(object):
934-
"""
935-
Parses a \\*.enc file referenced from a psfonts.map style file.
927+
r"""
928+
Parses a \*.enc file referenced from a psfonts.map style file.
936929
The format this class understands is a very limited subset of
937930
PostScript.
938931
@@ -1045,21 +1038,25 @@ def _fontfile(cls, suffix, texname):
10451038

10461039

10471040
if __name__ == '__main__':
1041+
from argparse import ArgumentParser
1042+
import itertools
10481043
import sys
1049-
fname = sys.argv[1]
1050-
try:
1051-
dpi = float(sys.argv[2])
1052-
except IndexError:
1053-
dpi = None
1054-
with Dvi(fname, dpi) as dvi:
1044+
1045+
parser = ArgumentParser()
1046+
parser.add_argument("filename")
1047+
parser.add_argument("dpi", nargs="?", type=float, default=None)
1048+
args = parser.parse_args()
1049+
with Dvi(args.filename, args.dpi) as dvi:
10551050
fontmap = PsfontsMap(find_tex_file('pdftex.map'))
10561051
for page in dvi:
10571052
print('=== new page ===')
1058-
fPrev = None
1059-
for x, y, f, c, w in page.text:
1060-
if f != fPrev:
1061-
print('font', f.texname, 'scaled', f._scale/pow(2.0, 20))
1062-
fPrev = f
1063-
print(x, y, c, 32 <= c < 128 and chr(c) or '.', w)
1053+
for font, group in itertools.groupby(
1054+
page.text, lambda text: text.font):
1055+
print('font', font.texname, 'scaled', font._scale / 2 ** 20)
1056+
for text in group:
1057+
print(text.x, text.y, text.glyph,
1058+
chr(text.glyph) if chr(text.glyph).isprintable()
1059+
else ".",
1060+
text.width)
10641061
for x, y, w, h in page.boxes:
10651062
print(x, y, 'BOX', w, h)

0 commit comments

Comments
 (0)