Added Ids to Text

This commit is contained in:
Mario Voigt 2020-08-23 15:35:34 +02:00
parent 07096bb201
commit 0860b1b80f
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
<name>Ids To Text</name>
<id>org.inkscape.render.ids_to_text</id>
<param name="fontsize" type="int" min="1" max="1000" gui-text="Font size (px):">10</param>
<param name="color" type="color" appearance="colorbutton" gui-text="Color (in hex)">255</param>
<param name="font" type="string" gui-text="Font">Roboto</param>
<param name="fontweight" appearance="combo" gui-text="Font Weight" type="optiongroup">
<option value="light">Light</option>
<option value="normal">Normal</option>
<option value="italic">Italic</option>
<option value="medium">Medium</option>
<option value="bold">Bold</option>
</param>
<param name="replaced" type="string" gui-text="Text to replace:" />
<param name="replacewith" type="string" gui-text="Replace with:" />
<param name="angle" type="float" min="-360" max="360" gui-text="Angle (°):">0</param>
<param name="capitals" type="bool" gui-text="Capitalize all text">false</param>
<label appearance="header">Help</label>
<label>A simple Inkscape extension that lets you extract the ids from all selected paths and show them as elements inside the paths.
Useful for when you want to have all paths' ids shown on the SVG document as text nodes.
Examples and more info:</label>
<label appearance="url">https://github.com/whiplashoo/ids_to_text_inkscape</label>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="FabLab Chemnitz">
<submenu name="Various"/>
</submenu>
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">fablabchemnitz_ids_to_text.py</command>
</script>
</inkscape-extension>

View File

@ -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()