From 81b86603480255656d5c09446a8677dda8d0de8a Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Fri, 4 May 2007 01:43:00 +0000 Subject: [PATCH] Fix for styles table in auto3, broken karaoke-splitter... Originally committed to SVN as r1163. --- aegisub/auto4_auto3.h | 2 + aegisub/auto4_lua.h | 2 + auto3/auto3.c | 4 +- automation/demos/auto3/2-dump.lua | 4 + automation/include/karaskel-base.auto3 | 102 +++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 2 deletions(-) diff --git a/aegisub/auto4_auto3.h b/aegisub/auto4_auto3.h index 999980f1b..cff77ad45 100644 --- a/aegisub/auto4_auto3.h +++ b/aegisub/auto4_auto3.h @@ -105,6 +105,8 @@ namespace Automation4 { Auto3Filter(const wxString &_name, const wxString &_description, Auto3Interpreter *_script); void ProcessSubs(AssFile *subs, wxWindow *export_dialog); + + virtual ~Auto3Filter() { } }; diff --git a/aegisub/auto4_lua.h b/aegisub/auto4_lua.h index 7832710e3..dbaf3c1f0 100644 --- a/aegisub/auto4_lua.h +++ b/aegisub/auto4_lua.h @@ -241,6 +241,8 @@ namespace Automation4 { public: static int LuaRegister(lua_State *L); + virtual ~LuaFeatureFilter() { } + void ProcessSubs(AssFile *subs, wxWindow *export_dialog); }; diff --git a/auto3/auto3.c b/auto3/auto3.c index 7c7e90196..d62ee27da 100644 --- a/auto3/auto3.c +++ b/auto3/auto3.c @@ -674,8 +674,8 @@ static void MakeStylesTable(lua_State *L, struct Auto3Interpreter *script) lua_settable(L, -3); } - // Finally, make 'n' key in table - lua_pushstring(L, "n"); + // Finally, make -1 key in table (because the name 'n' might clash with a style name) + lua_pushnumber(L, -1); lua_pushnumber(L, n); lua_settable(L, -3); } diff --git a/automation/demos/auto3/2-dump.lua b/automation/demos/auto3/2-dump.lua index 5074c328b..1e624debc 100644 --- a/automation/demos/auto3/2-dump.lua +++ b/automation/demos/auto3/2-dump.lua @@ -9,7 +9,11 @@ name = "Reading data demonstration" description = "This is a demonstration of how to access the various data passed to an Automation script. It loops over the data structures provided, and dumps them to the debug console." configuration = {} +include "karaskel-base.auto3" + function process_lines(meta, styles, lines, config) + karaskel.parse_syllable_data(meta, styles, lines) + -- This makes an alias for the aegisub.output_debug function. Remember that functions are first-class values in Lua. -- The 'local' keyword makes the 'out' variable local to this function local out = aegisub.output_debug diff --git a/automation/include/karaskel-base.auto3 b/automation/include/karaskel-base.auto3 index 711f2996d..8580a1b77 100644 --- a/automation/include/karaskel-base.auto3 +++ b/automation/include/karaskel-base.auto3 @@ -67,6 +67,107 @@ function karaskel.trace(s) end end +-- parse_syllable_data +-- Generates the line.karaoke table +-- This was moved to Lua code from the C++ code for various technical reasons +function karaskel.parse_syllable_data(meta, styles, lines) + for i = 0, lines.n-1 do + local l = lines[i] + local ltext = l.text + local indrawing = false + + l.karaoke = { n = 0 } + + local cursyl = { + duration = 0, + kind = "", + text = "", + text_stripped = "" + } + + l.text_stripped = "" + + while ltext ~= "" do + -- Find text part up until next tag start + local tagstart = string.find(ltext, "{") + + local textpart + if not tstart then + -- No tag start was found, rest of line is text + textpart = ltext + ltext = "" + else + -- Tag start was found, cut text part out + textpart = string.sub(ltext, 1, tagstart) + ltext = string.sub(ltext, tagstart+1) -- skip over opening brace + end + + -- Append text to relevant strings + if not indrawing then + l.text_stripped = l.text_stripped .. textpart + cursyl.text_stripped = cursyl.text_stripped .. textpart + end + cursyl.text = cursyl.text .. textpart + + -- If we're out of line text, we're done + if ltext == "" then + break + end + + -- Now find the tag group end + local tagend = string.find(ltext, "}") + + local tagpart + if not tagend then + tagpart = string.sub(ltext, tagstart+1) + ltext = "" + else + tagpart = string.sub(ltext, 1, tagend) + ltext = string.sub(ltext, tagend+1) + end + + -- Look for interesting tags (karaoke and drawing) + while tagpart ~= "" do + local tagstart, tagend, tag, param = string.find(tagpart, "\\([kKp]%a*)(%d+)") + + if tag and string.find(tag, "^[kK]") then + -- Karaoke tag, split stuff up + + -- If the kara tag wasn't the first thing in the group, prepend previous stuff + if tagstart > 1 then + cursyl.text = cursyl.text .. "{" .. string.sub(tagpart, 1, tagstart) .. "}" + end + + -- Store last tag to table and prepare new one + l.karaoke[l.karaoke.n] = cursyl + l.karaoke.n = l.karaoke.n + 1 + cursyl = { + duration = param, + kind = tag, + text = "", + text_stripped = "" + } + + -- Remove up to and including this tag from the tagpart + tagpart = string.sub(tagpart, tagend) + + elseif tag and tag == "p" then + -- Switch drawing-mode on/off + indrawing = param > 0 + + else + -- No more interesting tags here + cursyl.text = cursyl.text .. "{" .. tagpart .. "}" + break + end + end + end + + l.karaoke[l.karaoke.n] = cursyl + l.karaoke.n = l.karaoke.n + 1 + end +end + -- precalc_syllable_data -- Adds various extra fields to the line and syllable tables -- See the implementation and/or Aegisub help file for more information @@ -277,6 +378,7 @@ function karaskel.process_lines(meta, styles, lines, config) karaskel.trace("new skeleton") karaskel.trace("skel_process_lines") -- Do a little pre-calculation for each line and syllable + karaskel.parse_syllable_data(meta, styles, lines) karaskel.precalc_syllable_data(meta, styles, lines) karaskel.trace("skel_process_lines:2") -- A var for the new output