| 1 |
#!/usr/bin/python3 |
| 2 |
# Cut a cursor from a monolithic SVG file |
| 3 |
|
| 4 |
import sys |
| 5 |
import xml.etree.ElementTree as ET |
| 6 |
import os.path |
| 7 |
|
| 8 |
GROUP_TAG = '{http://www.w3.org/2000/svg}g' |
| 9 |
LABEL_ATTR = '{http://www.inkscape.org/namespaces/inkscape}label' |
| 10 |
|
| 11 |
def cut(src_path, dest_path, cursor_name): |
| 12 |
tree = ET.parse(src_path) |
| 13 |
root = tree.getroot() |
| 14 |
|
| 15 |
new_tree = ET.ElementTree(ET.Element('svg')) |
| 16 |
root = tree.getroot() |
| 17 |
|
| 18 |
cursor_found = False |
| 19 |
|
| 20 |
for child in root.findall(GROUP_TAG): |
| 21 |
if child.attrib[LABEL_ATTR] != 'Cursors': |
| 22 |
# help layers |
| 23 |
root.remove(child) |
| 24 |
continue |
| 25 |
|
| 26 |
for row in child.findall(GROUP_TAG): |
| 27 |
found_in_row = False; |
| 28 |
for cursor in row.findall(GROUP_TAG): |
| 29 |
if cursor.attrib[LABEL_ATTR] != cursor_name: |
| 30 |
row.remove(cursor) |
| 31 |
else: |
| 32 |
found_in_row = True |
| 33 |
|
| 34 |
hotspot = cursor.find(f"*[@{LABEL_ATTR}='hotspot']") |
| 35 |
if hotspot is not None: |
| 36 |
hotspot.set('id', 'hotspot') |
| 37 |
else: |
| 38 |
pass |
| 39 |
# print(f'hotspot not found: {cursor_name}') |
| 40 |
# sys.exit(2) |
| 41 |
|
| 42 |
if not found_in_row: |
| 43 |
child.remove(row) |
| 44 |
else: |
| 45 |
cursor_found = True |
| 46 |
|
| 47 |
if cursor_found is None: |
| 48 |
print('Cursor %s not found' % cursor_name) |
| 49 |
sys.exit(2) |
| 50 |
|
| 51 |
tree.write(dest_path) |
| 52 |
|
| 53 |
def help(): |
| 54 |
script_name = os.path.basename(sys.argv[0]) |
| 55 |
print(f'''Usage: |
| 56 |
{script_name} <src_path> <dest_path> <cursor_name>''') |
| 57 |
|
| 58 |
if __name__ == '__main__': |
| 59 |
if len(sys.argv) != 4: |
| 60 |
help() |
| 61 |
sys.exit(1) |
| 62 |
|
| 63 |
src_path = sys.argv[1] |
| 64 |
dest_path = sys.argv[2] |
| 65 |
cursor_name = sys.argv[3] |
| 66 |
cut(src_path, dest_path, cursor_name) |