From 6002d409143a6726899a4de15c3a6b279a6b1d71 Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 27 Sep 2019 10:07:38 +0200 Subject: Directory reorg: Put renderer into its own subdir --- renderer/support/inkscape_svg_filter_layers.py | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 renderer/support/inkscape_svg_filter_layers.py (limited to 'renderer/support/inkscape_svg_filter_layers.py') diff --git a/renderer/support/inkscape_svg_filter_layers.py b/renderer/support/inkscape_svg_filter_layers.py new file mode 100755 index 0000000..9301e1e --- /dev/null +++ b/renderer/support/inkscape_svg_filter_layers.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import xml.etree.ElementTree as xe +import argparse +import re + + +if __name__ != '__main__': + raise SystemError('Not running as shell script') + + +parser = argparse.ArgumentParser() +parser.add_argument('infile', metavar='input.svg', type=argparse.FileType('r')) +parser.add_argument('outfile', metavar='output.svg', type=argparse.FileType('wb')) +parser.add_argument('-n', '--name', nargs='+', default=[], help='Remove layers with this exact name (case-insensitive)') +parser.add_argument('-r', '--regex', nargs='+', default=[], help='Remove layers with names matching this regex') +parser.add_argument('-i', '--invisible', action='store_true', help='Remove hidden (invisible) layers') +parser.add_argument('-o', '--only', action='store_true', help='Invert logic, i.e. keep matched layers and discard others') +parser.add_argument('-d', '--strip-defs', action='store_true', help='Also strip any tags (off by default)') +args = parser.parse_args() + +doc = xe.fromstring(args.infile.read()) +ns = { + 'svg': 'http://www.w3.org/2000/svg', + 'inkscape': 'http://www.inkscape.org/namespaces/inkscape', + 'sodipodi': 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd' +} + +if args.strip_defs: + for elem in doc.findall('svg:defs', ns): + doc.remove(elem) + +for i, g in enumerate(doc.findall('svg:g', ns)): + if g.attrib.get(f'{{{ns["inkscape"]}}}groupmode') != 'layer': + continue + + label = g.attrib.get(f'{{{ns["inkscape"]}}}label', '') + match = ( + any(label == name for name in args.name) or + any(re.match(regex, label) for regex in args.regex) or + ('display:none' in g.attrib.get('style', '') and args.hidden) + ) + print(f'Layer {i} "{label}": {"match" if match else "not found"}', end='') + + if match != args.only: + print(', removing.') + doc.remove(g) + else: + print() + +args.outfile.write(xe.tostring(doc)) -- cgit