-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_ass.py
executable file
·292 lines (240 loc) · 10.3 KB
/
convert_to_ass.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2024 PhosCity
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = "1.0.7"
import inkex
def round_number(num, decimals=3):
rounded = round(float(num), decimals)
return int(rounded) if rounded.is_integer() else rounded
class ShapeProcessor:
def __init__(self, element, options, svg):
self.element = element
self.path = None
self.style = element.specified_style()
self.ass_tags = {
"an": 7,
"bord": 0,
"shad": 0,
"fscx": 100,
"fscy": 100,
"pos": [0, 0],
}
self.options = options
self.svg = svg
# self.notes = []
def create_ass_tags(self):
"""Create tags based on the attributes of the element."""
if opacity := self.get_opacity("opacity"):
self.ass_tags["alpha"] = opacity
fill_color = self.get_color("fill")
if fill_color:
self.ass_tags["c"] = fill_color
if fill_opacity := self.get_opacity("fill-opacity"):
self.ass_tags["1a"] = fill_opacity
else:
self.ass_tags["1a"] = "&HFF&"
stroke_color = self.get_color("stroke")
stroke_width = self.get_stroke_width()
if stroke_width and stroke_color:
self.ass_tags["bord"] = stroke_width
self.ass_tags["3c"] = stroke_color
if stroke_opacity := self.get_opacity("stroke-opacity"):
self.ass_tags["3a"] = stroke_opacity
self.ass_tags["p"] = 1
def get_opacity(self, attrib):
"""Extract the opacity attribute from the element and convert to ass hex"""
opacity = float(self.style.get(attrib, 1.0))
if opacity != 1.0:
return f"&H{int((1 - opacity) * 255):02X}&"
return False
def get_color(self, attrib):
"""Extract the fill color attribute from the element."""
color_attr = self.style(attrib)
if color_attr is None:
return False
if isinstance(color_attr, inkex.LinearGradient):
# Get first color of the gradient for now
first_stop = color_attr.href.stops[0]
fisrt_stop_style = first_stop.specified_style()
color_str = fisrt_stop_style.get("stop-color")
# # TODO: Add support for linear gradients.
# # Retrieve gradient attributes
# x1 = color_attr.get("x1", "0%")
# y1 = color_attr.get("y1", "0%")
# x2 = color_attr.get("x2", "100%")
# y2 = color_attr.get("y2", "0%")
# inkex.errormsg(f"x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2}")
# # Process stops
# for stop in color_attr.href.stops:
# stop_style = stop.specified_style()
# color = stop_style.get("stop-color")
# opacity = stop_style.get("stop-opacity")
# offset = stop.attrib.get("offset")
# inkex.errormsg(f"color: {color}, opacity: {opacity}, offset: {offset}")
# elif isinstance(color_attr, inkex.RadialGradient):
# inkex.utils.debug("It's radial gradient.")
else:
color_str = color_attr
color = inkex.Color(color_str)
return f"&H{color.blue:02X}{color.green:02X}{color.red:02X}&"
def get_stroke_width(self):
"""Extract the stroke attribute from the element."""
stroke_width = self.style.get("stroke-width")
if not stroke_width:
return False
paint_order = self.style.get("paint-order", "normal")
if paint_order == "normal":
paint_order = "fill stroke markers"
paint_order = paint_order.split()
stroke_index = paint_order.index("stroke")
fill_index = paint_order.index("fill")
if self.options.stroke_preservation == "strict" and stroke_index > fill_index:
inkex.errormsg(
f'Error:\n\nThe stroke order of object "{self.element.get_id()}" has stroke above fill which will have wrong output in ASS.\nYou see this message because have chosen strict stroke preservation in the GUI.\n\nFor accurate output, either change stroke order in Inkscape to have fill above stroke or change the stroke preservation in GUI if you don\'t mind applying path effect to your object.'
)
exit()
elif (
self.options.stroke_preservation == "use_path_effects"
and stroke_index > fill_index
):
paint_order[stroke_index] = "fill"
paint_order[fill_index] = "stroke"
offset_param_dict = {
"update_on_knot_move": "true",
"attempt_force_join": "false",
"miter_limit": "4",
"offset": -float(stroke_width) * 0.5,
"unit": "px",
"linejoin_type": "round",
"lpeversion": "1.3",
"is_visible": "true",
"effect": "offset",
}
effect = inkex.PathEffect()
for key in offset_param_dict:
effect.set(key, offset_param_dict[key])
self.svg.defs.add(effect)
self.element.set("inkscape:original-d", self.element.attrib["d"])
self.element.set("inkscape:path-effect", effect.get_id(as_url=1))
self.element.style.update(
{
"paint-order": " ".join(paint_order),
"stroke-width": float(stroke_width) * 2,
}
)
return round_number(float(stroke_width) * 2, 2)
else:
return round_number(float(stroke_width) * 0.5, 2)
def handle_clip_path(self):
"""Process the clip-path attribute of the element if present."""
clip_path = self.element.get("clip-path")
if clip_path is None:
return
clip_path = clip_path[5:-1] # Extract the ID between 'url(#' and ')'
clip_elem = self.svg.getElementById(clip_path)
self.ass_tags["clip"] = f"({self.convert_path(clip_elem.to_path_element())})"
def convert_path(self, shape_elem=None):
"""Convert the path of the shape element to the ass format."""
if shape_elem is None:
shape_elem = self.element
# Apply any transformations and viewBox scaling
shape_elem.apply_transform()
# Convert commands like A, S, Q, and T to cubic bezier
elem = shape_elem.path.to_superpath().to_path()
# Convert all commands to absolute positions
elem = elem.to_absolute()
viewBoxScale = self.svg.scale
if viewBoxScale != 1:
elem.scale(viewBoxScale, viewBoxScale, True)
# After this, path will now contain only M, L, C, and Z commands
path = []
prev_cmd = None
for idx, segment in enumerate(elem):
cmd = (segment.letter).lower()
if cmd == "z":
continue
cmd = "b" if cmd == "c" else cmd
if cmd != prev_cmd:
path.append(cmd)
prev_cmd = cmd
path.extend([round_number(num) for num in segment.args])
return " ".join(map(str, path))
def generate_lines(self):
"""Combine tags, clips, and path to generate final output lines."""
tags = []
for key, value in self.ass_tags.items():
if isinstance(value, list):
value_str = f"({value[0]},{value[1]})"
else:
value_str = str(value)
tags.append(f"\\{key}{value_str}")
tag_string = "{" + "".join(tags) + "}"
match self.options.output_format:
case "drawing":
line = tag_string + self.path
case "clip":
line = "\\clip(" + self.path + ")"
case "iclip":
line = "\\iclip(" + self.path + ")"
case "line":
line = (
"Dialogue: 0,0:00:00.00,0:00:00.02,Default,,0,0,0,,"
+ tag_string
+ self.path
)
return line
def process(self):
"""Perform all steps in sequence to process the shape element."""
self.create_ass_tags()
self.handle_clip_path()
if self.element.TAG != "path":
self.element = self.element.to_path_element()
self.path = self.convert_path()
return self.generate_lines()
class ConvertToASS(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument("--output_format", type=str, help="types of output")
pars.add_argument("--stroke_preservation", type=str, help="types of output")
def process_element(self, element):
"""Processes a single SVG element, handling groups recursively."""
if isinstance(element, inkex.Group):
element.bake_transforms_recursively() # Apply transformations to the group
for child in element:
self.process_element(child) # Recurse into group elements
elif isinstance(element, inkex.ShapeElement):
if element.TAG in {
"path",
"rect",
"circle",
"ellipse",
"line",
"polyline",
"polygon",
}:
processor = ShapeProcessor(element, self.options, self.svg)
line = processor.process()
inkex.utils.debug(line)
def effect(self):
# This grabs selected objects by z-order, ordered from bottom to top
selection_list = self.svg.selection.rendering_order()
if not selection_list:
inkex.errormsg("No object was selected!")
return
for elem in selection_list:
self.process_element(elem)
if __name__ == "__main__":
ConvertToASS().run()