summaryrefslogtreecommitdiffstats
path: root/share/extensions/text_extract.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:24:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:24:48 +0000
commitcca66b9ec4e494c1d919bff0f71a820d8afab1fa (patch)
tree146f39ded1c938019e1ed42d30923c2ac9e86789 /share/extensions/text_extract.py
parentInitial commit. (diff)
downloadinkscape-cca66b9ec4e494c1d919bff0f71a820d8afab1fa.tar.xz
inkscape-cca66b9ec4e494c1d919bff0f71a820d8afab1fa.zip
Adding upstream version 1.2.2.upstream/1.2.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'share/extensions/text_extract.py')
-rwxr-xr-xshare/extensions/text_extract.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/share/extensions/text_extract.py b/share/extensions/text_extract.py
new file mode 100755
index 0000000..212fde9
--- /dev/null
+++ b/share/extensions/text_extract.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+# coding=utf-8
+#
+# Copyright (C) 2011 Nicolas Dufour (jazzynico)
+# Direction code from the Restack extension, by Rob Antonishen
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+#
+"""
+Extract text and print it to the error console.
+"""
+
+from lxml.etree import tostring
+
+import inkex
+from inkex import TextElement, FlowRoot
+from inkex.utils import KeyDict
+
+# Old settings, supported because users click 'ok' without looking.
+XAN = KeyDict({"l": "left", "r": "right", "m": "center_x"})
+YAN = KeyDict({"t": "top", "b": "bottom", "m": "center_y"})
+
+
+class Extract(inkex.EffectExtension):
+ """Extract text and print out"""
+
+ select_all = (TextElement, FlowRoot)
+
+ def add_arguments(self, pars):
+ pars.add_argument(
+ "-d", "--direction", default="lr", help="direction to extract text"
+ )
+ pars.add_argument(
+ "-x", "--xanchor", default="left", help="horiz point to compare"
+ )
+ pars.add_argument(
+ "-y", "--yanchor", default="top", help="vertical point to compare"
+ )
+
+ def effect(self):
+ # move them to the top of the object stack in this order.
+ for node in sorted(
+ self.svg.selection.get(TextElement, FlowRoot), key=self._sort
+ ):
+ self.recurse(node)
+
+ def _sort(self, node):
+ return node.bounding_box().get_anchor(
+ self.options.xanchor, self.options.yanchor, self.options.direction
+ )
+
+ def recurse(self, node):
+ """Go through each node and recusively self call for all children"""
+ if node.text is not None or node.tail is not None:
+ for child in node:
+ if child.get("sodipodi:role"):
+ child.tail = "\n"
+ inkex.errormsg(tostring(node, encoding="unicode", method="text").strip())
+ else:
+ for child in node:
+ self.recurse(child)
+
+
+if __name__ == "__main__":
+ Extract().run()