Last active
February 20, 2017 09:41
-
-
Save sonovice/1b261b31f36e55b15c52d20ba3e7ed41 to your computer and use it in GitHub Desktop.
Tool to convert verovio SVG 1.1 output to SVG tiny
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
| """ | |
| Copyright 2017 Simon Waloschek | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files | |
| (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, | |
| merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
| OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | |
| IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| """ | |
| from copy import copy | |
| import re | |
| from lxml import etree | |
| def svg_to_tiny(svg): | |
| svg_xml = etree.fromstring(svg) | |
| # get all <use> elements and their defined height and weight combinations | |
| uses = set() | |
| for elem in svg_xml.xpath('//*[local-name()="use"]'): | |
| old_href = elem.get('{http://www.w3.org/1999/xlink}href').split('#')[-1] | |
| new_href = old_href + '-' + str(elem.get('height')) + '-' + str(elem.get('width')) | |
| elem.set('{http://www.w3.org/1999/xlink}href', '#' + new_href) # set href to new link | |
| del elem.attrib['height'] # attribute is obsolete | |
| del elem.attrib['width'] # attribute is obsolete | |
| uses.add(new_href) | |
| # generate new definitions for all combinations | |
| new_defs = etree.Element('defs') | |
| for elem in svg_xml.xpath('//*[local-name()="symbol"]'): | |
| identifier = elem.get('id') | |
| viewbox = elem.get('viewBox') | |
| old_dim = [int(i) for i in re.search(r'\d+ \d+ (\d+) (\d+)', viewbox).groups()] | |
| for symbol in [e for e in uses if identifier in e]: | |
| g = copy(elem[0]) | |
| scale = [float(f) for f in re.search(r'.*scale\((\d+),\s*(-?\d+)\).*', g.get('transform')).groups()] | |
| new_dim = [int(i) for i in re.search(r'\w+-(\d+)px-(\d+)px', symbol).groups()] | |
| g.set('id', symbol) | |
| g.set('transform', 'scale(' + str(scale[0] * new_dim[0] / old_dim[0]) + ', ' | |
| + str(scale[1] * new_dim[1] / old_dim[1]) + ')') | |
| new_defs.append(g) | |
| # transform SVG | |
| # root = svg_xml.getroot() | |
| del svg_xml[0] # delete defs | |
| viewbox = svg_xml[0].get('viewBox') # get viewBox of SVG | |
| svg_xml.set('viewBox', viewbox) # write viewBox to root SVG element | |
| g = svg_xml[0][0] # temporarily save SVG content... | |
| del svg_xml[0] # ... and delete parent | |
| svg_xml.insert(0, new_defs) # write new defs | |
| svg_xml.insert(1, g) # insert back SVG content to correct position | |
| return etree.tostring(svg_xml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment