From 164c24d962651d39bbf76146ad5bf01446653e29 Mon Sep 17 00:00:00 2001 From: odrling Date: Fri, 9 Aug 2019 15:50:13 +0200 Subject: [PATCH] [duetto meika] add deduetto meika macro As implied by the name it should reverse what duetto meika does in a subtitle file --- duetto-meika.lua | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/duetto-meika.lua b/duetto-meika.lua index 0d6c610..2c7804a 100644 --- a/duetto-meika.lua +++ b/duetto-meika.lua @@ -60,4 +60,98 @@ function duetto(subs, sel) aegisub.set_undo_point(script_name) end + +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 + aegisub.register_macro(script_name, script_description, duetto) +aegisub.register_macro(tr"Deduetto Meika", tr"Create styles from inline color tags", deduetto_meika)