-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathids_to_text.py
113 lines (99 loc) · 4.64 KB
/
ids_to_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
import re
import inkex
class IdsToText(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument(
'--path_attribute', default='id', help='Path attribute to show')
self.arg_parser.add_argument(
'--fontsize', type=int, default='10', help='Font Size')
self.arg_parser.add_argument(
'--color', type=inkex.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(
'--matchre', default='', help='Match regular expression')
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')
self.arg_parser.add_argument(
'--group', type=inkex.Boolean, default=False, help='Group paths with generated text elements')
def extract_path_attribute(self, attr, node):
ret = ''
if attr == 'id':
ret = node.get(attr)
elif attr == 'label':
value = node.get(attr)
ret = str(value) if value else (node.get("inkscape:label") or "")
elif attr == 'width':
ret = format(node.bounding_box().width, '.2f')
elif attr == 'height':
ret = format(node.bounding_box().height, '.2f')
elif attr == 'fill' or attr == 'stroke':
if 'style' in node.attrib:
style = node.attrib.get('style')
style = dict(inkex.styles.Style.parse_str(style))
if attr in style:
ret = style.get(attr)
elif attr in node.attrib:
ret = node.attrib.get(attr)
return ret
def effect(self):
if len(self.svg.selected) == 0:
inkex.errormsg("Please select some paths first.")
exit()
path_attribute = self.options.path_attribute
is_text_attribute = path_attribute in ['id', 'label']
for id, node in self.svg.selection.filter(inkex.PathElement).items():
to_show = self.extract_path_attribute(path_attribute, node)
node.path.transform(node.composed_transform()).to_superpath()
bbox = node.bounding_box()
tx, ty = bbox.center
if self.options.group:
group_element = node.getparent().add(inkex.Group())
group_element.add(node)
group_element.set('id', node.get('id') + "_group")
text_element = group_element.add(inkex.TextElement())
else:
text_element = node.getparent().add(inkex.TextElement())
tspan_element = text_element.add(inkex.Tspan())
tspan_element.set('sodipodi:role', 'line')
styles = {'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)
}
tspan_element.set('style', str(inkex.Style(styles)))
tspan_element.set('dy', '0')
if is_text_attribute:
if self.options.capitals:
to_show = to_show.upper()
if self.options.matchre != '':
matches = re.findall(self.options.matchre, to_show)
if len(matches) > 0:
to_show = matches[0]
if self.options.replaced != '':
to_show = to_show.replace(
self.options.replaced, self.options.replacewith)
tspan_element.text = to_show
tspan_element.set('id', node.get('id') + "_tspan")
text_element.set('id', node.get('id') + "_text")
text_element.set('x', str(tx))
text_element.set('y', str(ty))
text_element.set('transform', 'rotate(%s, %s, %s)' %
(-int(self.options.angle), tx, ty))
if __name__ == '__main__':
IdsToText().run()