From 26f2ca350e268e1d5a8d44ebfa5577318c9fdbf3 Mon Sep 17 00:00:00 2001 From: odrling Date: Fri, 9 Aug 2019 16:48:56 +0200 Subject: [PATCH] [deduetto meika] add fontsize and fontname support --- duetto-meika.lua | 67 +++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/duetto-meika.lua b/duetto-meika.lua index 2c7804a..e35a2c1 100644 --- a/duetto-meika.lua +++ b/duetto-meika.lua @@ -4,6 +4,8 @@ script_name = tr"Duetto Meika" script_description = tr"The ultimate tool for karaoke duets" script_author = "amoethyst" +include("utils.lua") + function replace_style(line, style_name, style_string) before_style, after_style = line.text:match("^(.-{[^}]-)\\?s:".. style_name .."(.*)$") @@ -61,19 +63,19 @@ function duetto(subs, sel) 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 +function test_colors(c1, c2) + return color_from_style(c1) == color_from_style(c2) +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 + +function get_script_style(style, styles) + for key, script_style in pairs(styles) do + if (test_colors(style.color1, script_style.color1) + and test_colors(style.color2, script_style.color2) + and test_colors(style.color3, script_style.color3) + and test_colors(style.color4, script_style.color4) + and tonumber(style.fontsize) == tonumber(script_style.fontsize) + and style.fontname == script_style.fontname) then return script_style end end @@ -88,7 +90,7 @@ function deduetto_meika(subs, sel) -- create the style map for i, line in ipairs(subs) do if line.class == "style" then - styles[line.name] = i + styles[line.name] = line last_style = i end end @@ -97,11 +99,10 @@ function deduetto_meika(subs, sel) for _, i in ipairs(sel) do local line = subs[i] - local current_style = subs[styles[line.style]] + local current_style = table.copy(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 @@ -109,24 +110,36 @@ function deduetto_meika(subs, sel) local bracketed = line.text:sub(match_start, match_end) local new_style = false - for tag, color in bracketed:gmatch("\\(c[1-4]?)([^}\\]*)") do + + -- change style's colors + for tag, value in bracketed:gmatch("\\([1-4]?c)([^}\\]*)") 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 + if tag == "c" or tag == "1c" then + current_style.color1 = value + elseif tag == "2c" then + current_style.color2 = value + elseif tag == "3c" then + current_style.color3 = value + elseif tag == "4c" then + current_style.color4 = value + end + end + + -- change style's font + for tag, value in bracketed:gmatch("\\(f[sn])([^}\\]*)") do + new_style = true + if tag == "fs" then + current_style.fontsize = value + elseif tag == "fn" then + current_style.fontname = value end end if new_style then - local script_style = get_script_style(current_style, styles, subs) + local script_style = get_script_style(current_style, styles) if script_style == nil then if get_script_style(current_style, new_styles) == nil then - new_styles[#new_styles+1] = current_style + new_styles[#new_styles+1] = table.copy(current_style) end else aegisub.log("changing line with style: " .. script_style.name .. "\n") @@ -144,9 +157,9 @@ function deduetto_meika(subs, sel) 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) + last_style = last_style + 1 aegisub.log("Created new style: " .. new_style.name .. "\n") end end