2019-08-02 20:14:28 +00:00
|
|
|
local tr = aegisub.gettext
|
|
|
|
|
2019-08-02 20:45:47 +00:00
|
|
|
script_name = tr"Duetto Meika"
|
|
|
|
script_description = tr"The ultimate tool for karaoke duets"
|
2019-08-02 20:14:28 +00:00
|
|
|
script_author = "amoethyst"
|
|
|
|
|
2019-08-09 09:34:28 +00:00
|
|
|
|
|
|
|
function replace_style(line, style_name, style_string)
|
|
|
|
before_style, after_style = line.text:match("^(.-{[^}]-)\\?s:".. style_name .."(.*)$")
|
|
|
|
return before_style .. style_string .. after_style
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-08-02 20:14:28 +00:00
|
|
|
function duetto(subs, sel)
|
|
|
|
styles = {}
|
|
|
|
|
|
|
|
-- create the style map
|
|
|
|
for _, line in ipairs(subs) do
|
|
|
|
if line.class == "style" then
|
|
|
|
styles[line.name] = line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- duetto~
|
|
|
|
for _, i in ipairs(sel) do
|
|
|
|
line = subs[i]
|
|
|
|
|
|
|
|
current_style = styles[line.style]
|
|
|
|
-- match every `s:` marker
|
2019-08-09 09:34:28 +00:00
|
|
|
for style_name in line.text:gmatch("{[^}]*s:([^}\\]*)[^}]*}") do
|
2019-08-02 20:14:28 +00:00
|
|
|
if style_name ~= current_style.name then
|
|
|
|
|
|
|
|
style = styles[style_name]
|
|
|
|
-- build the tags to use the new style
|
2019-08-09 09:34:28 +00:00
|
|
|
style_string = ""
|
2019-08-02 20:14:28 +00:00
|
|
|
if current_style.color1 ~= style.color1 then
|
|
|
|
style_string = style_string .. "\\c" .. style.color1
|
|
|
|
end
|
|
|
|
if current_style.color2 ~= style.color2 then
|
|
|
|
style_string = style_string .. "\\2c" .. style.color2
|
|
|
|
end
|
|
|
|
if current_style.color3 ~= style.color3 then
|
|
|
|
style_string = style_string .. "\\3c" .. style.color3
|
|
|
|
end
|
|
|
|
if current_style.color4 ~= style.color4 then
|
|
|
|
style_string = style_string .. "\\4c" .. style.color4
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set style
|
2019-08-09 09:34:28 +00:00
|
|
|
line.text = replace_style(line, style_name, style_string)
|
2019-08-02 20:14:28 +00:00
|
|
|
current_style = style
|
|
|
|
else
|
|
|
|
-- remove marker to not break everything
|
2019-08-09 09:34:28 +00:00
|
|
|
line.text = replace_style(line, style_name, "")
|
2019-08-02 20:14:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
subs[i] = line
|
|
|
|
end
|
|
|
|
|
|
|
|
aegisub.set_undo_point(script_name)
|
|
|
|
end
|
|
|
|
|
2019-08-09 13:50:13 +00:00
|
|
|
|
|
|
|
function get_script_style(style, styles, ...)
|
|
|
|
subs = ({...})[1]
|
|
|
|
for key, i in pairs(styles) do
|
|
|
|
if subs == nil then
|
|
|
|
script_style = i
|
|
|
|
else
|
|
|
|
script_style = subs[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
if (style.color1 == script_style.color1
|
|
|
|
and style.color2 == script_style.color2
|
|
|
|
and style.color3 == script_style.color3
|
|
|
|
and style.color4 == script_style.color4) then
|
|
|
|
return script_style
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function deduetto_meika(subs, sel)
|
|
|
|
local styles = {}
|
|
|
|
local last_style = -1
|
|
|
|
|
|
|
|
-- create the style map
|
|
|
|
for i, line in ipairs(subs) do
|
|
|
|
if line.class == "style" then
|
|
|
|
styles[line.name] = i
|
|
|
|
last_style = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local new_styles = {}
|
|
|
|
|
|
|
|
for _, i in ipairs(sel) do
|
|
|
|
local line = subs[i]
|
|
|
|
local current_style = subs[styles[line.style]]
|
|
|
|
|
|
|
|
local search_index = 1
|
|
|
|
while search_index < #line.text do
|
|
|
|
aegisub.log("remaining: " .. line.text:sub(search_index, #line.text) .. "\n")
|
|
|
|
local match_start, match_end = line.text:find("{[^}]*}", search_index)
|
|
|
|
if match_start == nil then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
local bracketed = line.text:sub(match_start, match_end)
|
|
|
|
local new_style = false
|
|
|
|
for tag, color in bracketed:gmatch("\\(c[1-4]?)([^}\\]*)") do
|
|
|
|
new_style = true
|
|
|
|
if tag == "c" or tag == "c1" then
|
|
|
|
current_style.color1 = color
|
|
|
|
elseif tag == "c2" then
|
|
|
|
current_style.color2 = color
|
|
|
|
elseif tag == "c3" then
|
|
|
|
current_style.color3 = color
|
|
|
|
elseif tag == "c4" then
|
|
|
|
current_style.color4 = color
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_style then
|
|
|
|
local script_style = get_script_style(current_style, styles, subs)
|
|
|
|
if script_style == nil then
|
|
|
|
if get_script_style(current_style, new_styles) == nil then
|
|
|
|
new_styles[#new_styles+1] = current_style
|
|
|
|
end
|
|
|
|
else
|
|
|
|
aegisub.log("changing line with style: " .. script_style.name .. "\n")
|
|
|
|
bracketed = bracketed:gsub("\\c[1-4]?[^\\}]*", "")
|
|
|
|
bracketed = "{s:" .. script_style.name .. bracketed:sub(2, #bracketed)
|
|
|
|
line.text = line.text:sub(1, match_start-1) .. bracketed .. line.text:sub(match_end + 1, #line.text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
search_index = match_start + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
subs[i] = line
|
|
|
|
end
|
|
|
|
|
|
|
|
if #new_styles > 0 then
|
|
|
|
for i, new_style in ipairs(new_styles) do
|
|
|
|
last_style = last_style + 1
|
|
|
|
new_style.name = "Deduetto style " .. i
|
|
|
|
subs.insert(last_style, new_style)
|
|
|
|
aegisub.log("Created new style: " .. new_style.name .. "\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-08-02 20:14:28 +00:00
|
|
|
aegisub.register_macro(script_name, script_description, duetto)
|
2019-08-09 13:50:13 +00:00
|
|
|
aegisub.register_macro(tr"Deduetto Meika", tr"Create styles from inline color tags", deduetto_meika)
|