This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbaguettisation.lua
279 lines (230 loc) · 7.74 KB
/
baguettisation.lua
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
script_name = "Baguettisation"
script_description = "Remplace les tirets courts des dialogues en tirets longs et gère la marge auto"
script_version = "1.6"
script_author = "Vardë"
-- karaskel = require('karaskel')
include("karaskel.lua")
---@class Line
---@field start_time integer
---@field end_time integer
---@field duration number
---@field text string
---@field effect string
---@field style string
---@field margin_l integer
---@class Meta
---@field res_x integer|nil
---@field res_y integer|nil
---@class Style
---@field align integer
---@field name string
---@class Styles
---@field n integer
---@field [string] Style
TIRET = "–"
DEFAULT = "Default"
DEFAULT_ITA = "Italique"
DEFAULT_DIALOGUE = "TiretsDefault"
DEFAULT_DIALOGUE_ITA = "TiretsItalique"
---Split string s by delimiter
---@param self string
---@param delimiter string
---@return string[]
string.split = function(self, delimiter)
local result = {}
for match in (self .. delimiter):gmatch("(.-)" .. delimiter) do
table.insert(result, match)
end
return result
end
---@param num number
---@param ndigits integer|nil
---@return number
local function round(num, ndigits)
local mult = 10^(ndigits or 0)
return math.floor(num * mult + 0.5) / mult
end
---@param gui table
---@param result_table table
local function save_config(gui, result_table)
local bag_conf = "# Baguettisation Config\n\n"
for _, val in ipairs(gui) do
if val.class == "edit" or val.class == "dropdown" then
bag_conf = bag_conf .. val.name .. "=" .. result_table[val.name] .. "\n"
end
end
local path = aegisub.decode_path("?user") .."\\baguettetisation.conf"
local file = io.open(path, "w")
if file then
file:write(bag_conf)
file:close()
end
aegisub.dialog.display(
{{class="label", label="Configuration sauvegardé à :\n" .. path}},
{"OK"},
{close="OK"}
)
end
---@param gui table
local function load_config(gui)
local path = aegisub.decode_path("?user") .."\\baguettetisation.conf"
local file = io.open(path, "r")
if file then
---@type string
local bag_conf = file:read("*all")
for _, val in ipairs(gui) do
if val.class == "edit" or val.class == "dropdown" then
if bag_conf:match(val.name) then
val.value = bag_conf:match(val.name .. "=(.-)\n")
end
end
end
io.close(file)
end
end
---@param styles Styles
---@return table, table
local function gui_config(styles)
---@type string[]
local styles_s = {}
for _, value in ipairs(styles) do
table.insert(styles_s, value.name)
end
local conf = {
-- Label
{
class="label", label="Type de tiret :",
x=0, y=0
},
{
class="label", label="Nom du Default :",
x=0, y=1
},
{
class="label", label="Nom du Default italique :",
x=0, y=2
},
{
class="label", label="Nom du Default dialogue :",
x=0, y=3
},
{
class="label", label="Nom du Default italique dialogue :",
x=0, y=4
},
-- User input
{
class="edit", name="tiret", value=TIRET,
x=1, y=0
},
{
class="dropdown", name="sn_default", value=DEFAULT,
items=styles_s,
x=1, y=1
},
{
class="dropdown", name="sn_default_ita", value=DEFAULT_ITA,
items=styles_s,
x=1, y=2
},
{
class="dropdown", name="sn_default_dia", value=DEFAULT_DIALOGUE,
items=styles_s,
x=1, y=3
},
{
class="dropdown", name="sn_default_dia_ita", value=DEFAULT_DIALOGUE_ITA,
items=styles_s,
x=1, y=4
},
-- Save checkbox
{
class="checkbox", name="save", label="Sauvegarder configuration",
x=5, y=5
}
}
local buttons = {"OK", "Annuler"}
return conf, buttons
end
---@param subs Line[]
---@param sel integer[]
---@param conf table
---@param meta Meta
---@param styles Styles
local function baguettetisation(subs, sel, conf, meta, styles)
---@type integer|nil, integer|nil, integer|nil, integer|nil
local xres, _, _, _ = aegisub.video_size()
video_x = meta.res_x or xres
if not video_x then
aegisub.log("Erreur : Définir la résolution d\'image au script ou ouvrir une vidéo !")
aegisub.cancel()
end
for _, i in ipairs(sel) do
local line = subs[i]
-- Vérifications si CorrectPonc a déjà été utilisé
if line.text:find("– ") then
replace_space = true
else
replace_space = false
end
-- Réinitialisation des lignes
line.text = line.text
:gsub("–", "-") -- Semi quadratin -> Tiret
:gsub("—", "-") -- Quadratin -> Tiret
:gsub("- ", "- ") -- Espace insécable fine -> Tiret
:gsub("- ", conf.tiret .. " ") --
:gsub("-{", conf.tiret .. "{") --
:gsub("\\an8", "\\an7")
:gsub("\\an2", "\\an1")
local line_clean = line.text:gsub("{[^}]+}", "")
-- Modifier si et seulement si la ligne commence par un tiret et contient un retour à la ligne suivi d'un 2e tiret
if line_clean:find("^–%s") and (line_clean:find("\\N–%s") or line_clean:find("\\N –%s")) then
local split_line = line_clean:split("\\N")
if split_line[2] then
if split_line[1]:len() >= split_line[2]:len() then
longest_line = split_line[1]
else
longest_line = split_line[2]
end
---@type integer, integer, integer, integer
local width, _, _, _ = aegisub.text_extents(styles[line.style], longest_line)
line.margin_l = video_x / 2 - round(width / 2, 0)
local style = styles[line.style]
if style.align == 8 then
line.text = "{\\an7}" .. line.text
if line.style == conf.sn_default then
line.style = conf.sn_default_dia
elseif line.style == conf.sn_default_ita then
line.style = conf.sn_default_dia_ita
end
elseif line.style == conf.sn_default_ita or line.style == conf.sn_default_dia_ita then
line.style = conf.sn_default_dia_ita
else
line.style = conf.sn_default_dia
end
if replace_space then
line.text = line.text:gsub("– ", "– ")
end
subs[i] = line
end
end
end
end
---@param subs Line[]
---@param sel integer[]
local function main(subs, sel)
---@type Meta, Styles
local meta, styles = karaskel.collect_head(subs)
local gui_conf, buttons = gui_config(styles)
load_config(gui_conf)
---@type string, table
local button, result_table = aegisub.dialog.display(gui_conf, buttons, {ok=buttons["OK"], cancel=buttons["Annuler"]})
if button == "Annuler" then
aegisub.cancel()
end
if result_table.save then
save_config(gui_conf, result_table)
end
baguettetisation(subs, sel, result_table, meta, styles)
end
aegisub.register_macro(script_name, script_description, main)