Created
May 20, 2021 08:26
-
-
Save lindenbergresearch/90d8a856d60eb537401e3855b6374b17 to your computer and use it in GitHub Desktop.
Fixes svg images written by Affinity etc. to work with nanosvg.
This file contains 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
#!/usr/bin/python3 | |
# | |
# __ ___ ______ | |
# / / / _ \/_ __/ | |
# / /__/ , _/ / / Lindenberg | |
# /____/_/|_| /_/ Research Tec. | |
# | |
# | |
# https://github.com/lindenbergresearch/LRTRack | |
# heapdump@icloud.com | |
# | |
# Sound Modules for VCV Rack | |
# Copyright 2017-2021 by Patrick Lindenberg / LRT | |
# | |
# For Redistribution and use in source and binary forms, | |
# with or without modification please see LICENSE. | |
# | |
import xml.etree.ElementTree as ET | |
import os | |
# basic config | |
NAMESPACE_URI = 'http://www.w3.org/2000/svg' | |
SVG_PATHS = ['./res/elements', './res/knobs', './res/panels'] | |
MATCHING_TAG = '{' + NAMESPACE_URI + '}defs' | |
ET.register_namespace('', NAMESPACE_URI) | |
fixcount = 0 | |
def reorder_svg(filename): | |
""" Scan for a matching tag and reorder it on success """ | |
tree = ET.parse(filename) | |
root = tree.getroot() | |
defs = root.find(MATCHING_TAG) | |
# TODO: test for empty matches | |
if not defs: | |
# print('noting to reorder for: ' + filename) | |
return False | |
root.remove(defs) | |
root.insert(0, defs) | |
# print('reorder tag: ' + MATCHING_TAG + '...') | |
tree.write(filename) | |
return True | |
def scan_path(paths, suffix): | |
""" scan a specific path for the given files that matches suffix """ | |
matches = [] | |
for path in paths: | |
try: | |
print('scanning path: ' + path) | |
entries = os.listdir(path) | |
except OSError: | |
print('error: invalid path: ' + path) | |
return [] | |
else: | |
for entry in entries: | |
if entry.endswith(suffix): | |
matches.append(path + '/' + entry) | |
return matches | |
print('SVG quick-fixer for nanosvg compatible vector graphics.') | |
print('Copyright 2021 by Lindenberg Research\n') | |
files = scan_path(SVG_PATHS, '.svg') | |
if len(files) <= 0: | |
print("warning: no images found.") | |
exit(255) | |
print("found " + str(len(files)) + " image(s) to examine.") | |
for file in files: | |
# print("examining: " + file) | |
if reorder_svg(file): | |
print('reorder file: ' + file) | |
fixcount += 1 | |
print('fixed: ' + str(fixcount) + ' images.\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fix path scanning
reduce log messages