From 0860b1b80f4cca7d5e028940dd0ca7824d67e206 Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Sun, 23 Aug 2020 15:35:34 +0200 Subject: [PATCH] Added Ids to Text --- extensions/fablabchemnitz_ids_to_text.inx | 35 ++++++++++++++++ extensions/fablabchemnitz_ids_to_text.py | 50 +++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 extensions/fablabchemnitz_ids_to_text.inx create mode 100644 extensions/fablabchemnitz_ids_to_text.py diff --git a/extensions/fablabchemnitz_ids_to_text.inx b/extensions/fablabchemnitz_ids_to_text.inx new file mode 100644 index 00000000..37fb98a5 --- /dev/null +++ b/extensions/fablabchemnitz_ids_to_text.inx @@ -0,0 +1,35 @@ + + + Ids To Text + org.inkscape.render.ids_to_text + 10 + 255 + Roboto + + + + + + + + + + 0 + false + + + + + all + + + + + + + + \ No newline at end of file diff --git a/extensions/fablabchemnitz_ids_to_text.py b/extensions/fablabchemnitz_ids_to_text.py new file mode 100644 index 00000000..d952bbcd --- /dev/null +++ b/extensions/fablabchemnitz_ids_to_text.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import inkex +from inkex import TextElement, TextPath, Tspan +from inkex.bezier import csparea, cspcofm, csplength +from inkex.colors import Color + +class IdsToText(inkex.Effect): + def __init__(self): + inkex.Effect.__init__(self) + self.arg_parser.add_argument('--fontsize', type = int, default = '10', help = 'Font Size') + self.arg_parser.add_argument('--color', type=Color, default = 255, help = 'Color') + self.arg_parser.add_argument('--font', default = 'Roboto', help = 'Font Family') + self.arg_parser.add_argument('--fontweight', default = 'bold', help = 'Font Weight') + self.arg_parser.add_argument('--replaced', default = '', help = 'Text to replace') + self.arg_parser.add_argument('--replacewith', default = '', help = 'Replace with this text') + self.arg_parser.add_argument('--angle', type = float, dest = 'angle', default = 0, help = 'Rotation angle') + self.arg_parser.add_argument('--capitals', type = inkex.Boolean, default = False, help = 'Capitalize') + + def effect(self): + if len(self.svg.selected) == 0: + inkex.errormsg("Please select some paths first.") + exit() + + for id, node in self.svg.selected.items(): + id = node.get('id') + self.group = node.getparent().add(TextElement()) + csp = node.path.transform(node.composed_transform()).to_superpath() + bbox = node.bounding_box() + tx, ty = bbox.center + anchor = 'middle' + + node = self.group + new = node.add(Tspan()) + new.set('sodipodi:role', 'line') + s = {'text-align': 'center', 'vertical-align': 'bottom', + 'text-anchor': 'middle', 'font-size': str(self.options.fontsize) + 'px', + 'font-weight': self.options.fontweight, 'font-style': 'normal', 'font-family': self.options.font, 'fill': str(self.options.color)} + new.set('style', str(inkex.Style(s))) + new.set('dy', '0') + + if self.options.capitals: + id = id.upper() + + new.text = id.replace(self.options.replaced, self.options.replacewith) + node.set('x', str(tx)) + node.set('y', str(ty)) + node.set('transform', 'rotate(%s, %s, %s)' % (-int(self.options.angle), tx, ty)) + +IdsToText().run() \ No newline at end of file