Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

Reduce numerical precision in Type 1 fonts#18080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tacaswell merged 2 commits intomatplotlib:masterfromjkseppan:type1-rounding
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletionslib/matplotlib/cbook/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslib/matplotlib/tests/test_cbook.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslib/matplotlib/tests/test_type1font.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 00]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 00]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 deletionslib/matplotlib/type1font.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp