Even better fix for 8f87118725

(previous solution still did not work for numbers like 123.4 with precision < 3)
This commit is contained in:
Eduard Braun 2017-02-19 00:39:36 +01:00
parent 7c2e035644
commit 4d84194621
3 changed files with 29 additions and 15 deletions

View file

@ -2622,16 +2622,7 @@ def scourUnitlessLength(length, needsRendererWorkaround=False): # length is of
""" """
if not isinstance(length, Decimal): if not isinstance(length, Decimal):
length = getcontext().create_decimal(str(length)) length = getcontext().create_decimal(str(length))
initial_length = length
# remove trailing zeroes as we do not care for significance
intLength = length.to_integral_value()
if length == intLength:
length = Decimal(intLength)
else:
length = length.normalize()
# Gather the initial non-scientific notation version of the coordinate (we want to compare with it later)
initial_value = '{0:f}'.format(length)
# reduce numeric precision # reduce numeric precision
# plus() corresponds to the unary prefix plus operator and applies context precision and rounding # plus() corresponds to the unary prefix plus operator and applies context precision and rounding
@ -2645,7 +2636,10 @@ def scourUnitlessLength(length, needsRendererWorkaround=False): # length is of
length = length.normalize() length = length.normalize()
# Gather the non-scientific notation version of the coordinate. # Gather the non-scientific notation version of the coordinate.
# Re-quantize from the initial value to prevent unnecessary loss of precision
# (e.g. 123.4 should become 123, not 120 or even 100)
nonsci = '{0:f}'.format(length) nonsci = '{0:f}'.format(length)
nonsci = '{0:f}'.format(initial_length.quantize(Decimal(nonsci)))
if not needsRendererWorkaround: if not needsRendererWorkaround:
if len(nonsci) > 2 and nonsci[:2] == '0.': if len(nonsci) > 2 and nonsci[:2] == '0.':
nonsci = nonsci[1:] # remove the 0, leave the dot nonsci = nonsci[1:] # remove the 0, leave the dot
@ -2666,11 +2660,7 @@ def scourUnitlessLength(length, needsRendererWorkaround=False): # length is of
if len(sci) < len(nonsci): if len(sci) < len(nonsci):
return_value = sci return_value = sci
# Return the shortest representation (if they are equal prefer the original as it still has the full precision)
if len(return_value) < len(initial_value):
return return_value return return_value
else:
return initial_value
def reducePrecision(element): def reducePrecision(element):

View file

@ -968,6 +968,9 @@ class KeepPrecisionInPathDataIfSameLength(unittest.TestCase):
self.assertEqual(paths[4].getAttribute('d'), "m-1-12-123-1e3 -1e4 -1e5", self.assertEqual(paths[4].getAttribute('d'), "m-1-12-123-1e3 -1e4 -1e5",
'Precision not correctly reduced with "--set-precision=1" ' 'Precision not correctly reduced with "--set-precision=1" '
'for path with ID ' + paths[4].getAttribute('id')) 'for path with ID ' + paths[4].getAttribute('id'))
self.assertEqual(paths[5].getAttribute('d'), "m123 101-123-101",
'Precision not correctly reduced with "--set-precision=1" '
'for path with ID ' + paths[5].getAttribute('id'))
doc = scourXmlFile('unittests/path-precision.svg', parse_args(['--set-precision=2'])) doc = scourXmlFile('unittests/path-precision.svg', parse_args(['--set-precision=2']))
paths = doc.getElementsByTagNameNS(SVGNS, 'path') paths = doc.getElementsByTagNameNS(SVGNS, 'path')
@ -978,6 +981,9 @@ class KeepPrecisionInPathDataIfSameLength(unittest.TestCase):
self.assertEqual(paths[4].getAttribute('d'), "m-1-12-123-1234-12345-1.2e5", self.assertEqual(paths[4].getAttribute('d'), "m-1-12-123-1234-12345-1.2e5",
'Precision not correctly reduced with "--set-precision=2" ' 'Precision not correctly reduced with "--set-precision=2" '
'for path with ID ' + paths[4].getAttribute('id')) 'for path with ID ' + paths[4].getAttribute('id'))
self.assertEqual(paths[5].getAttribute('d'), "m123 101-123-101",
'Precision not correctly reduced with "--set-precision=2" '
'for path with ID ' + paths[5].getAttribute('id'))
doc = scourXmlFile('unittests/path-precision.svg', parse_args(['--set-precision=3'])) doc = scourXmlFile('unittests/path-precision.svg', parse_args(['--set-precision=3']))
paths = doc.getElementsByTagNameNS(SVGNS, 'path') paths = doc.getElementsByTagNameNS(SVGNS, 'path')
@ -988,6 +994,22 @@ class KeepPrecisionInPathDataIfSameLength(unittest.TestCase):
self.assertEqual(paths[4].getAttribute('d'), "m-1-12-123-1234-12345-123456", self.assertEqual(paths[4].getAttribute('d'), "m-1-12-123-1234-12345-123456",
'Precision not correctly reduced with "--set-precision=3" ' 'Precision not correctly reduced with "--set-precision=3" '
'for path with ID ' + paths[4].getAttribute('id')) 'for path with ID ' + paths[4].getAttribute('id'))
self.assertEqual(paths[5].getAttribute('d'), "m123 101-123-101",
'Precision not correctly reduced with "--set-precision=3" '
'for path with ID ' + paths[5].getAttribute('id'))
doc = scourXmlFile('unittests/path-precision.svg', parse_args(['--set-precision=4']))
paths = doc.getElementsByTagNameNS(SVGNS, 'path')
for path in paths[1:3]:
self.assertEqual(path.getAttribute('d'), "m1 12 123 1234 12345 123456",
'Precision not correctly reduced with "--set-precision=4" '
'for path with ID ' + path.getAttribute('id'))
self.assertEqual(paths[4].getAttribute('d'), "m-1-12-123-1234-12345-123456",
'Precision not correctly reduced with "--set-precision=4" '
'for path with ID ' + paths[4].getAttribute('id'))
self.assertEqual(paths[5].getAttribute('d'), "m123.5 101-123.5-101",
'Precision not correctly reduced with "--set-precision=4" '
'for path with ID ' + paths[5].getAttribute('id'))
class RemoveEmptyLineSegmentsFromPath(unittest.TestCase): class RemoveEmptyLineSegmentsFromPath(unittest.TestCase):

View file

@ -6,4 +6,6 @@
<path id="p2" d="m 1.0 12.0 123.0 1234.0 12345.0 123456.0" /> <path id="p2" d="m 1.0 12.0 123.0 1234.0 12345.0 123456.0" />
<path id="p3" d="m 01 012 0123 01234 012345 0123456 " /> <path id="p3" d="m 01 012 0123 01234 012345 0123456 " />
<path id="p4" d="m -1 -12 -123 -1234 -12345 -123456 " /> <path id="p4" d="m -1 -12 -123 -1234 -12345 -123456 " />
<path id="p6" d="m 123.456 101.001 -123.456 -101.001" />
</svg> </svg>

Before

Width:  |  Height:  |  Size: 456 B

After

Width:  |  Height:  |  Size: 518 B

Before After
Before After