conventionschedule-pwa/xml2svg/cvt.py

35 lines
1.4 KiB
Python

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from io import StringIO
from pathlib import Path
import re
import xml.etree.ElementTree as ET
RGX_ICON = re.compile(r'([a-zA-Z0-9_]+)\(R\.drawable\.([a-zA-Z0-9_]+)\)')
def main():
iconskt = Path('Icons.kt').read_text(encoding='utf-8')
icon_matches = RGX_ICON.findall(iconskt)
for (icon_svg_name, icon_xml_name) in icon_matches:
xml_contents = Path('xml').joinpath(f'{icon_xml_name}.xml').read_text(encoding='utf-8')
tree = ET.parse(StringIO(xml_contents))
root = tree.getroot()
w = root.attrib['{http://schemas.android.com/apk/res/android}width']
h = root.attrib['{http://schemas.android.com/apk/res/android}height']
vw = root.attrib['{http://schemas.android.com/apk/res/android}viewportWidth']
vh = root.attrib['{http://schemas.android.com/apk/res/android}viewportHeight']
ps = [p.attrib['{http://schemas.android.com/apk/res/android}pathData'] for p in root.findall('path')]
svg_contents = f'<svg xmlns="http://www.w3.org/2000/svg" width="{w[:-2]}" height="{h[:-2]}" viewBox="0 0 {vw} {vh}">'
svg_contents += '\n'
for p in ps:
svg_contents += f' <path d="{p}"/>'
svg_contents += '\n'
svg_contents += '\n'
svg_contents += '</svg>\n'
Path('svg').joinpath(f'{icon_svg_name}.svg').write_text(svg_contents, encoding='utf-8')
if __name__ == '__main__':
main()