8000 Reduce numerical precision in Type 1 fonts · matplotlib/matplotlib@6657540 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

8000
Appearance settings

Commit 6657540

Browse files
committed
Reduce numerical precision in Type 1 fonts
Fixes #16087
1 parent 3e439a4 commit 6657540

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/matplotlib/tests/test_type1font.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_Type1Font():
2929
'+ /FontName /CMR10_Slant_1000 def',
3030
# Alters FontMatrix
3131
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
32-
'+ /FontMatrix [0.001 0.0 0.001 0.001 0.0 0.0]readonly def',
32+
'+ /FontMatrix [0.001 0 0.001 0.001 0 0]readonly def',
3333
# Alters ItalicAngle
3434
'- /ItalicAngle 0 def',
3535
'+ /ItalicAngle -45.0 def'):
@@ -47,5 +47,23 @@ def test_Type1Font():
4747
'+ /FontName /CMR10_Extend_500 def',
4848
# Alters FontMatrix
4949
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
50-
'+ /FontMatrix [0.0005 0.0 0.0 0.001 0.0 0.0]readonly def'):
50+
'+ /FontMatrix [0.0005 0 0 0.001 0 0]readonly def'):
5151
assert line in diff, 'diff to condensed font must contain %s' % line
52+
53+
54+
def test_overprecision():
55+
# We used to output too many digits in FontMatrix entries and
56+
# ItalicAngle, which could make Type-1 parsers unhappy.
57+
filename = os.path.join(os.path.dirname(__file__), 'cmr10.pfb')
58+
font = t1f.Type1Font(filename)
59+
slanted = font.transform({'slant': .167})
60+
lines = slanted.parts[0].decode('ascii').splitlines()
61+
matrix, = [line[line.index('[')+1:line.index(']')]
62+
for line in lines if '/FontMatrix' in line]
63+
angle, = [word
64+
for line in lines if '/ItalicAngle' in line
65+
for word in line.split() if word[0] in '-0123456789']
66+
# the following used to include 0.00016700000000000002
67+
assert matrix == '0.001 0 0.000167 0.001 0 0'
68+
# and here we had -9.48090361795083
69+
assert angle == '-9.4809'

lib/matplotlib/type1font.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def fontname(name):
251251
return result
252252

253253
def italicangle(angle):
254-
return b'%a' % (float(angle) - np.arctan(slant) / np.pi * 180)
254+
return b'%a' % round(float(angle) - np.arctan(slant) / np.pi * 180, 5)
255255

256256
def fontmatrix(array):
257257
array = array.lstrip(b'[').rstrip(b']').split()
@@ -265,10 +265,9 @@ def fontmatrix(array):
265265
newmatrix = np.dot(modifier, oldmatrix)
266266
array[::2] = newmatrix[0:3, 0]
267267
array[1::2] = newmatrix[0:3, 1]
268-
# Not directly using `b'%a' % x for x in array` for now as that
269-
# produces longer reprs on numpy<1.14, causing test failures.
270-
as_string = '[' + ' '.join(str(x) for x in array) + ']'
271-
return as_string.encode('latin-1')
268+
return (
269+
'[' + ' '.join(f'{x:.5g}' for x in array) + ']'
270+
).encode('ascii')
272271

273272
def replace(fun):
274273
def replacer(tokens):

0 commit comments

Comments
 (0)
0