Implement a basic rewrite of redundant commands

This basic implementation can drop and rewrite some cases of "m0 0"
and "z" without triggering the issues experienced in #163.  It works
by analysing the path backwards and tracking "z" and "m" commands.

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2018-03-11 08:22:27 +00:00
parent a2c94c96fb
commit 38274f75bc
4 changed files with 68 additions and 13 deletions

View file

@ -2054,13 +2054,25 @@ class StyleToAttr(unittest.TestCase):
self.assertEqual(line.getAttribute('marker-end'), 'url(#m)')
class PathEmptyMove(unittest.TestCase):
class PathCommandRewrites(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-empty-move.svg')
# This path can actually be optimized to avoid the "m0 0z".
self.assertEqual(doc.getElementsByTagName('path')[0].getAttribute('d'), 'm100 100 200 100m0 0z')
self.assertEqual(doc.getElementsByTagName('path')[1].getAttribute('d'), 'm100 100v200m0 0 100 100z')
doc = scourXmlFile('unittests/path-command-rewrites.svg')
paths = doc.getElementsByTagName('path')
expected_paths = [
('m100 100 200 100', "Trailing m0 0z not removed"),
('m100 100v200m0 0 100 100z', "Mangled m0 0 100 100"),
("m100 100v200m0 0 2-1-2 1z", "Should have removed empty m0 0"),
("m100 100v200l3-5-5 3m0 0 2-1-2 1z", "Rewrite m0 0 3-5-5 3 ... -> l3-5-5 3 ..."),
("m100 100v200m0 0 3-5-5 3zm0 0 2-1-2 1z", "No rewrite of m0 0 3-5-5 3z"),
]
self.assertEqual(len(paths), len(expected_paths), "len(actual_paths) != len(expected_paths)")
for i in range(len(paths)):
actual_path = paths[i].getAttribute('d')
expected_path, message = expected_paths[i]
self.assertEqual(actual_path,
expected_path,
'%s: "%s" != "%s"' % (message, actual_path, expected_path))
class DefaultsRemovalToplevel(unittest.TestCase):