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

Skip to content

Commit b151a85

Browse files
committed
Deprecated classes in dviread
1 parent 68d6b79 commit b151a85

File tree

10 files changed

+803
-759
lines changed

10 files changed

+803
-759
lines changed

lib/matplotlib/_dviread.py

Lines changed: 492 additions & 0 deletions
Large diffs are not rendered by default.

lib/matplotlib/_dviread_dispatch.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from functools import partial, wraps
2+
import logging
3+
4+
_log = logging.getLogger(__name__)
5+
6+
# Opcode argument parsing
7+
#
8+
# Each of the following functions takes a Dvi object and delta,
9+
# which is the difference between the opcode and the minimum opcode
10+
# with the same meaning. Dvi opcodes often encode the number of
11+
# argument bytes in this delta.
12+
13+
def _arg_raw(dvi, delta):
14+
"""Return *delta* without reading anything more from the dvi file."""
15+
return delta
16+
17+
18+
def _arg(nbytes, signed, dvi, _):
19+
"""
20+
Read *nbytes* bytes, returning the bytes interpreted as a signed integer
21+
if *signed* is true, unsigned otherwise.
22+
"""
23+
return dvi._arg(nbytes, signed)
24+
25+
26+
def _arg_slen(dvi, delta):
27+
"""
28+
Read *delta* bytes, returning None if *delta* is zero, and the bytes
29+
interpreted as a signed integer otherwise.
30+
"""
31+
if delta == 0:
32+
return None
33+
return dvi._arg(delta, True)
34+
35+
36+
def _arg_slen1(dvi, delta):
37+
"""
38+
Read *delta*+1 bytes, returning the bytes interpreted as signed.
39+
"""
40+
return dvi._arg(delta + 1, True)
41+
42+
43+
def _arg_ulen1(dvi, delta):
44+
"""
45+
Read *delta*+1 bytes, returning the bytes interpreted as unsigned.
46+
"""
47+
return dvi._arg(delta + 1, False)
48+
49+
50+
def _arg_olen1(dvi, delta):
51+
"""
52+
Read *delta*+1 bytes, returning the bytes interpreted as
53+
unsigned integer for 0<=*delta*<3 and signed if *delta*==3.
54+
"""
55+
return dvi._arg(delta + 1, delta == 3)
56+
57+
58+
_arg_mapping = dict(raw=_arg_raw,
59+
u1=partial(_arg, 1, False),
60+
u4=partial(_arg, 4, False),
61+
s4=partial(_arg, 4, True),
62+
slen=_arg_slen,
63+
olen1=_arg_olen1,
64+
slen1=_arg_slen1,
65+
ulen1=_arg_ulen1)
66+
67+
68+
def _dispatch(table, min, max=None, state=None, args=('raw',)):
69+
"""
70+
Decorator for dispatch by opcode. Sets the values in *table*
71+
from *min* to *max* to this method, adds a check that the Dvi state
72+
matches *state* if not None, reads arguments from the file according
73+
to *args*.
74+
75+
Parameters
76+
----------
77+
table : dict[int, callable]
78+
The dispatch table to be filled in.
79+
80+
min, max : int
81+
Range of opcodes that calls the registered function; *max* defaults to
82+
*min*.
83+
84+
state : _dvistate, optional
85+
State of the Dvi object in which these opcodes are allowed.
86+
87+
args : list[str], default: ['raw']
88+
Sequence of argument specifications:
89+
90+
- 'raw': opcode minus minimum
91+
- 'u1': read one unsigned byte
92+
- 'u4': read four bytes, treat as an unsigned number
93+
- 's4': read four bytes, treat as a signed number
94+
- 'slen': read (opcode - minimum) bytes, treat as signed
95+
- 'slen1': read (opcode - minimum + 1) bytes, treat as signed
96+
- 'ulen1': read (opcode - minimum + 1) bytes, treat as unsigned
97+
- 'olen1': read (opcode - minimum + 1) bytes, treat as unsigned
98+
if under four bytes, signed if four bytes
99+
"""
100+
def decorate(method):
101+
get_args = [_arg_mapping[x] for x in args]
102+
103+
@wraps(method)
104+
def wrapper(self, byte):
105+
if state is not None and self.state != state:
106+
raise ValueError("state precondition failed")
107+
return method(self, *[f(self, byte-min) for f in get_args])
108+
if max is None:
109+
table[min] = wrapper
110+
else:
111+
for i in range(min, max+1):
112+
assert table[i] is None
113+
table[i] = wrapper
114+
return wrapper
115+
return decorate

lib/matplotlib/_dviread_vf.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import logging
2+
from functools import partial
3+
4+
from matplotlib.dviread import Dvi, _dvistate
5+
from matplotlib import _dviread
6+
7+
8+
_log = logging.getLogger(__name__)
9+
10+
11+
class Vf(Dvi):
12+
r"""
13+
A virtual font (\*.vf file) containing subroutines for dvi files.
14+
15+
Parameters
16+
----------
17+
filename : str or path-like
18+
19+
Notes
20+
-----
21+
The virtual font format is a derivative of dvi:
22+
http://mirrors.ctan.org/info/knuth/virtual-fonts
23+
This class reuses some of the machinery of `Dvi`
24+
but replaces the `_read` loop and dispatch mechanism.
25+
26+
Examples
27+
--------
28+
::
29+
30+
vf = Vf(filename)
31+
glyph = vf[code]
32+
glyph.text, glyph.boxes, glyph.width
33+
"""
34+
35+
def __init__(self, filename):
36+
super().__init__(filename, 0)
37+
try:
38+
self._first_font = None
39+
self._chars = {}
40+
self._read()
41+
finally:
42+
self.close()
43+
44+
def __getitem__(self, code):
45+
return self._chars[code]
46+
47+
def _read(self):
48+
"""
49+
Read one page from the file. Return True if successful,
50+
False if there were no more pages.
51+
"""
52+
packet_char, packet_ends = None, None
53+
packet_len, packet_width = None, None
54+
while True:
55+
byte = self.file.read(1)[0]
56+
# If we are in a packet, execute the dvi instructions
57+
if self.state is _dvistate.inpage:
58+
byte_at = self.file.tell()-1
59+
if byte_at == packet_ends:
60+
self._finalize_packet(packet_char, packet_width)
61+
packet_len, packet_char, packet_width = None, None, None
62+
# fall through to out-of-packet code
63+
elif byte_at > packet_ends:
64+
raise ValueError("Packet length mismatch in vf file")
65+
else:
66+
if byte in (139, 140) or byte >= 243:
67+
raise ValueError(
68+
"Inappropriate opcode %d in vf file" % byte)
69+
Dvi._dtable[byte](self, byte)
70+
continue
71+
72+
# We are outside a packet
73+
if byte < 242: # a short packet (length given by byte)
74+
packet_len = byte
75+
packet_char, packet_width = self._arg(1), self._arg(3)
76+
packet_ends = self._init_packet(byte)
77+
self.state = _dvistate.inpage
78+
elif byte == 242: # a long packet
79+
packet_len, packet_char, packet_width = \
80+
[self._arg(x) for x in (4, 4, 4)]
81+
self._init_packet(packet_len)
82+
elif 243 <= byte <= 246:
83+
k = self._arg(byte - 242, byte == 246)
84+
c, s, d, a, l = [self._arg(x) for x in (4, 4, 4, 1, 1)]
85+
self._fnt_def_real(k, c, s, d, a, l)
86+
if self._first_font is None:
87+
self._first_font = k
88+
elif byte == 247: # preamble
89+
i, k = self._arg(1), self._arg(1)
90+
x = self.file.read(k)
91+
cs, ds = self._arg(4), self._arg(4)
92+
self._pre(i, x, cs, ds)
93+
elif byte == 248: # postamble (just some number of 248s)
94+
break
95+
else:
96+
raise ValueError("Unknown vf opcode %d" % byte)
97+
98+
def _init_packet(self, pl):
99+
if self.state != _dvistate.outer:
100+
raise ValueError("Misplaced packet in vf file")
101+
self.h, self.v, self.w, self.x, self.y, self.z = 0, 0, 0, 0, 0, 0
102+
self.stack, self.text, self.boxes = [], [], []
103+
self.f = self._first_font
104+
return self.file.tell() + pl
105+
106+
def _finalize_packet(self, packet_char, packet_width):
107+
self._chars[packet_char] = _dviread.Page(
108+
text=self.text, boxes=self.boxes, width=packet_width,
109+
height=None, descent=None)
110+
self.state = _dvistate.outer
111+
112+
def _pre(self, i, x, cs, ds):
113+
if self.state is not _dvistate.pre:
114+
raise ValueError("pre command in middle of vf file")
115+
if i != 202:
116+
raise ValueError("Unknown vf format %d" % i)
117+
if len(x):
118+
_log.debug('vf file comment: %s', x)
119+
self.state = _dvistate.outer
120+
# cs = checksum, ds = design size
121+
122+
123+
_vffile = partial(_dviread._fontfile, Vf, ".vf")

lib/matplotlib/backends/backend_pdf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from matplotlib.font_manager import findfont, get_font
3737
from matplotlib.afm import AFM
3838
import matplotlib.type1font as type1font
39+
import matplotlib._dviread as _dviread
3940
import matplotlib.dviread as dviread
4041
from matplotlib.ft2font import (FIXED_WIDTH, ITALIC, LOAD_NO_SCALE,
4142
LOAD_NO_HINTING, KERNING_UNFITTED, FT2Font)
@@ -891,7 +892,8 @@ def dviFontName(self, dvifont):
891892
if dvi_info is not None:
892893
return dvi_info.pdfname
893894

894-
tex_font_map = dviread.PsfontsMap(dviread._find_tex_file('pdftex.map'))
895+
tex_font_map = _dviread.PsfontsMap(
896+
_dviread._find_tex_file('pdftex.map'))
895897
psfont = tex_font_map[dvifont.texname]
896898
if psfont.filename is None:
897899
raise ValueError(
@@ -966,7 +968,7 @@ def _embedTeXFont(self, fontinfo):
966968
fontdict['Encoding'] = {
967969
'Type': Name('Encoding'),
968970
'Differences': [
969-
0, *map(Name, dviread._parse_enc(fontinfo.encodingfile))],
971+
0, *map(Name, _dviread._parse_enc(fontinfo.encodingfile))],
970972
}
971973

972974
# If no file is specified, stop short

0 commit comments

Comments
 (0)