summaryrefslogtreecommitdiff
path: root/renderer/support/pogojig/inkscape/inkscape.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2019-09-27 10:07:38 +0200
committerjaseg <git@jaseg.net>2019-09-27 10:07:38 +0200
commit6002d409143a6726899a4de15c3a6b279a6b1d71 (patch)
treefcde90f9b0ece9d552164effcc710b7fad978126 /renderer/support/pogojig/inkscape/inkscape.py
parent3340885ade322e8ae6b75257cc760ff48e218a0a (diff)
downloadpogojig-6002d409143a6726899a4de15c3a6b279a6b1d71.tar.gz
pogojig-6002d409143a6726899a4de15c3a6b279a6b1d71.tar.bz2
pogojig-6002d409143a6726899a4de15c3a6b279a6b1d71.zip
Directory reorg: Put renderer into its own subdir
Diffstat (limited to 'renderer/support/pogojig/inkscape/inkscape.py')
-rw-r--r--renderer/support/pogojig/inkscape/inkscape.py124
1 files changed, 124 insertions, 0 deletions
diff --git a/renderer/support/pogojig/inkscape/inkscape.py b/renderer/support/pogojig/inkscape/inkscape.py
new file mode 100644
index 0000000..efe7677
--- /dev/null
+++ b/renderer/support/pogojig/inkscape/inkscape.py
@@ -0,0 +1,124 @@
+import os
+import subprocess
+import xml.etree.ElementTree as etree
+
+def get_inkscape_layers(svg_path):
+ document = etree.parse(svg_path)
+
+ layers = []
+ nodes = document.findall(
+ '{http://www.w3.org/2000/svg}g[@{http://www.inkscape.org/namespaces/inkscape}groupmode="layer"]')
+
+ for i in nodes:
+ inkscape_name = i.get('{http://www.inkscape.org/namespaces/inkscape}label').strip()
+
+ if inkscape_name.endswith(']'):
+ export_name, args = inkscape_name[:-1].rsplit('[', 1)
+
+ export_name = export_name.strip()
+ args = args.strip()
+
+ use_paths = 'p' in args
+ else:
+ use_paths = False
+ export_name = inkscape_name
+
+ layers.append(Layer(inkscape_name, export_name, use_paths))
+ return layers
+
+
+def _inkscape(svg_path, verbs):
+ subprocess.run([os.environ.get('INKSCAPE', 'inkscape'), *(x for verb in verbs for x in ('--verb', verb)), svg_path])
+
+
+class Layer(object):
+ def __init__(self, inkscape_name, export_name, use_paths):
+ self.inkscape_name = inkscape_name
+ self.export_name = export_name
+ self.use_paths = use_paths
+
+
+class InkscapeCommandLine(object):
+ def __init__(self, path):
+ self._path = path
+ self._layers = get_inkscape_layers(path)
+ self._current_layer_index = None
+ self._verbs = []
+
+ def apply_to_document(self, *verb):
+ self._verbs.extend(verb)
+
+ def apply_to_layer(self, layer, *verb):
+ self._go_to_layer(layer)
+ self.apply_to_document(*verb)
+
+ def select_all_in_layer(self, layer):
+ self.apply_to_layer(layer, 'EditSelectAll')
+
+ def apply_to_layer_content(self, layer, *verbs):
+ self.select_all_in_layer(layer)
+ self.apply_to_document(*verbs)
+
+ def _go_to_layer(self, layer, with_selection=False):
+ if self._current_layer_index is None:
+ # Initialize to a known state. We cannot assume that any layer is
+ # selected and thus we need as many LayerPrev as we have layers.
+ self._current_layer_index = len(self._layers)
+ self._go_to_layer(self._layers[0])
+
+ target_index = self._layers.index(layer)
+
+ if with_selection:
+ next_command = 'LayerMoveToNext'
+ previous_command = 'LayerMoveToPrev'
+ else:
+ next_command = 'LayerNext'
+ previous_command = 'LayerPrev'
+
+ while self._current_layer_index != target_index:
+ if self._current_layer_index < target_index:
+ self.apply_to_document(next_command)
+ self._current_layer_index += 1
+ else:
+ self.apply_to_document(previous_command)
+ self._current_layer_index -= 1
+
+ if with_selection:
+ # When using LayerMoveToNext and LayerMoveToPrev, inkscape does
+ # not reliably select the next/previous layer.
+ self._current_layer_index = None
+
+ def duplicate_layer(self, layer):
+ self.apply_to_layer(layer, 'LayerDuplicate')
+
+ # Inkscape 0.91 places a duplicated layer above (after) the selected
+ # one and selects the new layer.
+ new_layer = Layer(layer.inkscape_name + ' copy', layer.export_name, layer.use_paths)
+ self._layers.insert(self._current_layer_index + 1, new_layer)
+
+ # Whether the original or the new layer is selected after the operation
+ # fluctuates between Inkscape versions.
+ self._current_layer_index = None
+
+ return new_layer
+
+ def delete_layer(self, layer):
+ self.apply_to_layer(layer, 'LayerDelete')
+
+ # Inkscape 0.91 selects the layer above (after) the deleted layer.
+ del self._layers[self._current_layer_index]
+
+ def clear_layer(self, layer):
+ self.select_all_in_layer(layer)
+ self.apply_to_document('EditDelete')
+
+ def move_content(self, source_layer, target_layer):
+ self.select_all_in_layer(source_layer)
+ self._go_to_layer(target_layer, True)
+
+ def run(self):
+ _inkscape(self._path, self._verbs)
+
+ @property
+ def layers(self):
+ return list(self._layers)