-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrounded_corners.py
135 lines (105 loc) · 4.4 KB
/
rounded_corners.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
"""
Generates a rectangle with rounded corners as a path.
The user can specify each corner's radius in css corner order.
Also allows selecting multiple rectangles, and each gets processed in order.
Quadratic Bezier curves are not yet supported, otherwise they would make the task easier.
And SVG rects do not support different radii for different corners (cite proposal).
"""
__author__ = "Mois Moshev"
__email__ = "[email protected]"
__copyright__ = "Copyright (C) 2017 Mois Moshev"
__license__ = "GPL"
import gettext
import inkex
import inkex.localization
from lxml import etree
inkex.localization.localize()
class RoundCorners(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument("--top-left", dest="tl",
action="store", type=float,
default=0.0, help="top-left")
self.arg_parser.add_argument("--top-right", dest="tr",
action="store", type=float,
default=0.0, help="top-right")
self.arg_parser.add_argument("--bottom-right", dest="br",
action="store", type=float,
default=0.0, help="bottom-right")
self.arg_parser.add_argument("--bottom-left", dest="bl",
action="store", type=float,
default=0.0, help="bottom-left")
self.arg_parser.add_argument("--remove-original", dest="remove",
action="store", type=inkex.Boolean,
default=False, help="Remove original rect")
self.arg_parser.add_argument("--inherit-style", dest="inheritstyle",
action="store", type=inkex.Boolean,
default=True, help="Inherit style from rect")
@staticmethod
def createRoundedRect(parent, bbox, corner_radii):
x, y, w, h = bbox
tl, tr, br, bl = corner_radii
pathEl = etree.SubElement(parent, inkex.addNS("path", "svg"))
d = []
if tl > 0:
d.append(["M", [x + tl, y]])
else:
d.append(["M", [x, y]])
if tr > 0:
d.append(["L", [x + w - tr, y]])
d.append(["Q", [x + w, y, x + w, y + tr]])
else:
d.append(["L", [x + w, y]])
if br > 0:
d.append(["L", [x + w, y + h - br]])
d.append(["Q", [x + w, y + h, x + w - br, y + h]])
else:
d.append(["L", [x + w, y + h]])
if bl > 0:
d.append(["L", [x + bl, y + h]])
d.append(["Q", [x, y + h, x, y + h - bl]])
else:
d.append(["L", [x, y + h]])
if tl > 0:
d.append(["L", [x, y + tl]])
d.append(["Q", [x, y, x + tl, y]])
else:
d.append(["L", [x, y]])
d.append(["z", []])
pathEl.set("d", str(inkex.Path(d)))
return pathEl
def effect(self):
tl = float(self.options.tl)
tr = float(self.options.tr)
br = float(self.options.br)
bl = float(self.options.bl)
paths = []
print(self.svg.selected)
for nodeName in self.svg.selected:
node = self.svg.selected[nodeName]
# print(dir(self.svg.selected[node]))
if node.tag == inkex.addNS("rect", "svg"):
x = float(node.get("x", 0))
y = float(node.get("y", 0))
w = float(node.get("width"))
h = float(node.get("height"))
pathEl = RoundCorners.createRoundedRect(
node.getparent(),
(x, y, w, h),
(tl, tr, br, bl)
)
if self.options.inheritstyle:
# copy the styles
pathEl.set("style", node.get("style"))
# set the same css class if there is one
if node.get("class"):
pathEl.set("class", node.get("class"))
paths.append(pathEl)
if self.options.remove:
node.getparent().remove(node)
if len(paths) == 0:
inkex.errormsg(_("Please select at least one rectangle."))
if __name__ == "__main__":
e = RoundCorners()
e.run()