FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Reduce numerical precision in Type 1 fonts by jkseppan · Pull Request #18080 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 8 additions & 0 deletions lib/matplotlib/cbook/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2383,3 +2383,11 @@ def _setup_new_guiapp():
except OSError:
_c_internal_utils.Win32_SetCurrentProcessExplicitAppUserModelID(
"matplotlib")


def format_approx(number, precision):
"""
Format the number with at most the number of decimals given as precision.
Remove trailing zeros and possibly the decimal point.
"""
return f'{number:.{precision}f}'.rstrip('0').rstrip('.') or '0'
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,15 @@ def verify_pre_post_state(obj):
assert a.static == 'static'

verify_pre_post_state(a)


def test_format_approx():
f = cbook.format_approx
assert f(0, 1) == '0'
assert f(0, 2) == '0'
assert f(0, 3) == '0'
assert f(-0.0123, 1) == '-0'
assert f(1e-7, 5) == '0'
assert f(0.0012345600001, 5) == '0.00123'
assert f(-0.0012345600001, 5) == '-0.00123'
assert f(0.0012345600001, 8) == f(0.0012345600001, 10) == '0.00123456'
22 changes: 20 additions & 2 deletions lib/matplotlib/tests/test_type1font.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_Type1Font():
'+ /FontName /CMR10_Slant_1000 def',
# Alters FontMatrix
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
'+ /FontMatrix [0.001 0.0 0.001 0.001 0.0 0.0]readonly def',
'+ /FontMatrix [0.001 0 0.001 0.001 0 0]readonly def',
# Alters ItalicAngle
'- /ItalicAngle 0 def',
'+ /ItalicAngle -45.0 def'):
Expand All @@ -47,5 +47,23 @@ def test_Type1Font():
'+ /FontName /CMR10_Extend_500 def',
# Alters FontMatrix
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
'+ /FontMatrix [0.0005 0.0 0.0 0.001 0.0 0.0]readonly def'):
'+ /FontMatrix [0.0005 0 0 0.001 0 0]readonly def'):
assert line in diff, 'diff to condensed font must contain %s' % line


def test_overprecision():
# We used to output too many digits in FontMatrix entries and
# ItalicAngle, which could make Type-1 parsers unhappy.
filename = os.path.join(os.path.dirname(__file__), 'cmr10.pfb')
font = t1f.Type1Font(filename)
slanted = font.transform({'slant': .167})
lines = slanted.parts[0].decode('ascii').splitlines()
matrix, = [line[line.index('[')+1:line.index(']')]
for line in lines if '/FontMatrix' in line]
angle, = [word
for line in lines if '/ItalicAngle' in line
for word in line.split() if word[0] in '-0123456789']
# the following used to include 0.00016700000000000002
assert matrix == '0.001 0 0.000167 0.001 0 0'
# and here we had -9.48090361795083
assert angle == '-9.4809'
14 changes: 9 additions & 5 deletions lib/matplotlib/type1font.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import numpy as np

from matplotlib.cbook import format_approx


# token types
_TokenType = enum.Enum('_TokenType',
Expand Down Expand Up @@ -251,7 +253,10 @@ def fontname(name):
return result

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

def fontmatrix(array):
array = array.lstrip(b'[').rstrip(b']').split()
Expand All @@ -265,10 +270,9 @@ def fontmatrix(array):
newmatrix = np.dot(modifier, oldmatrix)
array[::2] = newmatrix[0:3, 0]
array[1::2] = newmatrix[0:3, 1]
# Not directly using `b'%a' % x for x in array` for now as that
# produces longer reprs on numpy<1.14, causing test failures.
as_string = '[' + ' '.join(str(x) for x in array) + ']'
return as_string.encode('latin-1')
return (
'[%s]' % ' '.join(format_approx(x, 6) for x in array)
).encode('ascii')

def replace(fun):
def replacer(tokens):
Expand Down

Back | FazBrowse Home | New Git URL