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

Skip to content

Commit 66d974a

Browse files
committed
Improve test, fix an off-by-one and simplify the code
1 parent 3b4b79d commit 66d974a

2 files changed

Lines changed: 12 additions & 21 deletions

File tree

lib/matplotlib/tests/test_type1font.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,23 @@
77
import matplotlib.type1font as t1f
88
import os.path
99
import difflib
10-
import hashlib
11-
12-
13-
def sha1(data):
14-
hash = hashlib.sha1()
15-
hash.update(data)
16-
return hash.hexdigest()
1710

1811

1912
def test_Type1Font():
2013
filename = os.path.join(os.path.dirname(__file__), 'cmr10.pfb')
2114
font = t1f.Type1Font(filename)
2215
slanted = font.transform({'slant': 1})
2316
condensed = font.transform({'extend': 0.5})
24-
assert_equal(map(sha1, font.parts),
25-
['f4ce890d648e67d413a91b3109fe67a732ced96f',
26-
'af46adb6c528956580c125c6abf3b5eb9983bbc1',
27-
'e2538a88a810bc207cfa1194b658ee8967042db8'])
17+
rawdata = open(filename, 'rb').read()
18+
assert_equal(font.parts[0], rawdata[0x0006:0x10c5])
19+
assert_equal(font.parts[1], rawdata[0x10cb:0x897f])
20+
assert_equal(font.parts[2], rawdata[0x8985:0x8ba6])
2821
assert_equal(font.parts[1:], slanted.parts[1:])
2922
assert_equal(font.parts[1:], condensed.parts[1:])
3023

3124
differ = difflib.Differ()
32-
diff = set(differ.compare(font.parts[0].splitlines(),
33-
slanted.parts[0].splitlines()))
25+
diff = set(differ.compare(font.parts[0].decode('latin-1').splitlines(),
26+
slanted.parts[0].decode('latin-1').splitlines()))
3427
for line in (
3528
# Removes UniqueID
3629
'- FontDirectory/CMR10 known{/CMR10 findfont dup/UniqueID known{dup',

lib/matplotlib/type1font.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from six.moves import filter
3030
from six import unichr
3131

32+
import binascii
3233
import io
3334
import itertools
3435
import numpy as np
@@ -92,8 +93,7 @@ def _read(self, file):
9293
if type == 1: # ASCII text: include verbatim
9394
data += segment
9495
elif type == 2: # binary data: encode in hexadecimal
95-
data += b''.join([('%02x' % ord(char)).encode('ascii')
96-
for char in segment])
96+
data += binascii.hexlify(segment)
9797
elif type == 3: # end of file
9898
break
9999
else:
@@ -123,9 +123,8 @@ def _split(self, data):
123123
# zeros backward
124124
idx = data.rindex(b'cleartomark') - 1
125125
zeros = 512
126-
while zeros and ord(data[idx]) in (
127-
ord(b'0'[0]), ord(b'\n'[0]), ord(b'\r'[0])):
128-
if ord(data[idx]) == ord(b'0'[0]):
126+
while zeros and data[idx] in b'0\n\r':
127+
if data[idx] in b'0':
129128
zeros -= 1
130129
idx -= 1
131130
if zeros:
@@ -136,10 +135,9 @@ def _split(self, data):
136135
# but if we read a pfa file, this part is already in hex, and
137136
# I am not quite sure if even the pfb format guarantees that
138137
# it will be in binary).
139-
binary = b''.join([unichr(int(data[i:i + 2], 16)).encode('latin-1')
140-
for i in range(len1, idx, 2)])
138+
binary = binascii.unhexlify(data[len1:idx+1])
141139

142-
return data[:len1], binary, data[idx:]
140+
return data[:len1], binary, data[idx+1:]
143141

144142
_whitespace_re = re.compile(br'[\0\t\r\014\n ]+')
145143
_token_re = re.compile(br'/{0,2}[^]\0\t\r\v\n ()<>{}/%[]+')

0 commit comments

Comments
 (0)