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

Skip to content

Commit 372b402

Browse files
committed
Fix bytes/str handling all over type1font and its test
Add a test for changing the font name (this wasn't working before)
1 parent 66d974a commit 372b402

2 files changed

Lines changed: 35 additions & 28 deletions

File tree

lib/matplotlib/tests/test_type1font.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ def test_Type1Font():
2222
assert_equal(font.parts[1:], condensed.parts[1:])
2323

2424
differ = difflib.Differ()
25-
diff = set(differ.compare(font.parts[0].decode('latin-1').splitlines(),
26-
slanted.parts[0].decode('latin-1').splitlines()))
25+
diff = list(differ.compare(font.parts[0].decode('latin-1').splitlines(),
26+
slanted.parts[0].decode('latin-1').splitlines()))
2727
for line in (
2828
# Removes UniqueID
2929
'- FontDirectory/CMR10 known{/CMR10 findfont dup/UniqueID known{dup',
3030
'+ FontDirectory/CMR10 known{/CMR10 findfont dup',
31+
# Changes the font name
32+
'- /FontName /CMR10 def',
33+
'+ /FontName /CMR10_Slant_1000 def',
3134
# Alters FontMatrix
3235
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
3336
'+ /FontMatrix [0.001 0.0 0.001 0.001 0.0 0.0]readonly def',
@@ -36,12 +39,15 @@ def test_Type1Font():
3639
'+ /ItalicAngle -45.0 def'):
3740
assert_in(line, diff, 'diff to slanted font must contain %s' % line)
3841

39-
diff = set(differ.compare(font.parts[0].splitlines(),
40-
condensed.parts[0].splitlines()))
42+
diff = list(differ.compare(font.parts[0].decode('latin-1').splitlines(),
43+
condensed.parts[0].decode('latin-1').splitlines()))
4144
for line in (
4245
# Removes UniqueID
4346
'- FontDirectory/CMR10 known{/CMR10 findfont dup/UniqueID known{dup',
4447
'+ FontDirectory/CMR10 known{/CMR10 findfont dup',
48+
# Changes the font name
49+
'- /FontName /CMR10 def',
50+
'+ /FontName /CMR10_Extend_500 def',
4551
# Alters FontMatrix
4652
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
4753
'+ /FontMatrix [0.0005 0.0 0.0 0.001 0.0 0.0]readonly def'):

lib/matplotlib/type1font.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _tokens(cls, text):
164164
if match:
165165
yield (cls._whitespace, match.group())
166166
pos += match.end()
167-
elif text[pos] == '(':
167+
elif text[pos] == b'(':
168168
start = pos
169169
pos += 1
170170
depth = 1
@@ -173,19 +173,19 @@ def _tokens(cls, text):
173173
if match is None:
174174
return
175175
pos += match.end()
176-
if match.group() == '(':
176+
if match.group() == b'(':
177177
depth += 1
178-
elif match.group() == ')':
178+
elif match.group() == b')':
179179
depth -= 1
180180
else: # a backslash - skip the next character
181181
pos += 1
182182
yield (cls._string, text[start:pos])
183-
elif text[pos:pos + 2] in ('<<', '>>'):
183+
elif text[pos:pos + 2] in (b'<<', b'>>'):
184184
yield (cls._delimiter, text[pos:pos + 2])
185185
pos += 2
186-
elif text[pos] == '<':
186+
elif text[pos] == b'<':
187187
start = pos
188-
pos += text[pos:].index('>')
188+
pos += text[pos:].index(b'>')
189189
yield (cls._string, text[start:pos])
190190
else:
191191
match = cls._token_re.match(text[pos:])
@@ -254,16 +254,16 @@ def _transformer(cls, tokens, slant, extend):
254254
def fontname(name):
255255
result = name
256256
if slant:
257-
result += b'_Slant_' + bytes(int(1000 * slant))
257+
result += b'_Slant_' + str(int(1000 * slant)).encode('latin-1')
258258
if extend != 1.0:
259-
result += b'_Extend_' + bytes(int(1000 * extend))
259+
result += b'_Extend_' + str(int(1000 * extend)).encode('latin-1')
260260
return result
261261

262262
def italicangle(angle):
263-
return bytes(float(angle) - np.arctan(slant) / np.pi * 180)
263+
return str(float(angle) - np.arctan(slant) / np.pi * 180).encode('latin-1')
264264

265265
def fontmatrix(array):
266-
array = array.lstrip('[').rstrip(']').strip().split()
266+
array = array.lstrip(b'[').rstrip(b']').strip().split()
267267
array = [float(x) for x in array]
268268
oldmatrix = np.eye(3, 3)
269269
oldmatrix[0:3, 0] = array[::2]
@@ -274,7 +274,8 @@ def fontmatrix(array):
274274
newmatrix = np.dot(modifier, oldmatrix)
275275
array[::2] = newmatrix[0:3, 0]
276276
array[1::2] = newmatrix[0:3, 1]
277-
return b'[' + ' '.join(bytes(x) for x in array) + b']'
277+
as_string = u'[' + u' '.join(str(x) for x in array) + u']'
278+
return as_string.encode('latin-1')
278279

279280
def replace(fun):
280281
def replacer(tokens):
@@ -284,26 +285,26 @@ def replacer(tokens):
284285
while token is cls._whitespace:
285286
yield bytes(value)
286287
token, value = next(tokens)
287-
if value != '[': # name/number/etc.
288+
if value != b'[': # name/number/etc.
288289
yield bytes(fun(value))
289-
else: # array, e.g., [1 2 3]
290-
array = []
291-
while value != ']':
292-
array += value
290+
else: # array, e.g., [1 2 3]
291+
result = b''
292+
while value != b']':
293+
result += value
293294
token, value = next(tokens)
294-
array += value
295-
yield bytes(fun(''.join(array)))
295+
result += value
296+
yield fun(result)
296297
return replacer
297298

298299
def suppress(tokens):
299-
for x in itertools.takewhile(lambda x: x[1] != 'def', tokens):
300+
for x in itertools.takewhile(lambda x: x[1] != b'def', tokens):
300301
pass
301302
yield b''
302303

303-
table = {'/FontName': replace(fontname),
304-
'/ItalicAngle': replace(italicangle),
305-
'/FontMatrix': replace(fontmatrix),
306-
'/UniqueID': suppress}
304+
table = {b'/FontName': replace(fontname),
305+
b'/ItalicAngle': replace(italicangle),
306+
b'/FontMatrix': replace(fontmatrix),
307+
b'/UniqueID': suppress}
307308

308309
while True:
309310
token, value = next(tokens)
@@ -328,5 +329,5 @@ def transform(self, effects):
328329
transformed = self._transformer(tokenizer,
329330
slant=effects.get('slant', 0.0),
330331
extend=effects.get('extend', 1.0))
331-
map(buffer.write, transformed)
332+
list(map(buffer.write, transformed))
332333
return Type1Font((buffer.getvalue(), self.parts[1], self.parts[2]))

0 commit comments

Comments
 (0)