Lua export filters didn't work when there was no configuration dialog function. Really stupid fix. Also, Lua include files are now more usable and karaskel pre-calculation actually works.

Originally committed to SVN as r940.
This commit is contained in:
Niels Martin Hansen 2007-02-20 02:50:40 +00:00
parent 9dcae98180
commit 3a1d4ee18e
6 changed files with 67 additions and 27 deletions

View File

@ -55,7 +55,8 @@ namespace Automation4 {
// LuaStackcheck // LuaStackcheck
#ifdef _DEBUG //#ifdef _DEBUG
#if 0
struct LuaStackcheck { struct LuaStackcheck {
lua_State *L; lua_State *L;
int startstack; int startstack;
@ -241,7 +242,7 @@ namespace Automation4 {
// load user script // load user script
LuaScriptReader script_reader(GetFilename()); LuaScriptReader script_reader(GetFilename());
if (lua_load(L, script_reader.reader_func, &script_reader, GetFilename().mb_str(wxConvUTF8))) { if (lua_load(L, script_reader.reader_func, &script_reader, GetPrettyFilename().mb_str(wxConvUTF8))) {
wxString *err = new wxString(lua_tostring(L, -1), wxConvUTF8); wxString *err = new wxString(lua_tostring(L, -1), wxConvUTF8);
err->Prepend(_T("Error loading Lua script \"") + GetPrettyFilename() + _T("\":\n\n")); err->Prepend(_T("Error loading Lua script \"") + GetPrettyFilename() + _T("\":\n\n"));
throw err->c_str(); throw err->c_str();
@ -412,7 +413,7 @@ namespace Automation4 {
} }
LuaScriptReader script_reader(fname.GetFullPath()); LuaScriptReader script_reader(fname.GetFullPath());
if (lua_load(L, script_reader.reader_func, &script_reader, s->GetFilename().mb_str(wxConvUTF8))) { if (lua_load(L, script_reader.reader_func, &script_reader, fname.GetFullName().mb_str(wxConvUTF8))) {
lua_pushfstring(L, "Error loading Lua include \"%s\":\n\n%s", fname.GetFullPath().mb_str(wxConvUTF8).data(), lua_tostring(L, -1)); lua_pushfstring(L, "Error loading Lua include \"%s\":\n\n%s", fname.GetFullPath().mb_str(wxConvUTF8).data(), lua_tostring(L, -1));
lua_error(L); lua_error(L);
return 0; return 0;
@ -475,6 +476,15 @@ namespace Automation4 {
lua_getfield(L, LUA_REGISTRYINDEX, "progress_sink"); lua_getfield(L, LUA_REGISTRYINDEX, "progress_sink");
if (lua_isuserdata(L, -1)) { if (lua_isuserdata(L, -1)) {
LuaProgressSink *ps = LuaProgressSink::GetObjPointer(L, -1); LuaProgressSink *ps = LuaProgressSink::GetObjPointer(L, -1);
if (result) {
// if the call failed, log the error here
wxString errmsg(lua_tostring(L, -2), wxConvUTF8);
ps->AddDebugOutput(_T("\n\nLua reported a runtime error:\n"));
ps->AddDebugOutput(errmsg);
lua_pop(L, 1);
}
// don't bother protecting this with a mutex, it should be safe enough like this // don't bother protecting this with a mutex, it should be safe enough like this
ps->script_finished = true; ps->script_finished = true;
// tell wx to run its idle-events now, just to make the progress window notice earlier that we're done // tell wx to run its idle-events now, just to make the progress window notice earlier that we're done
@ -628,7 +638,7 @@ namespace Automation4 {
ps->ShowModal(); ps->ShowModal();
wxThread::ExitCode code = call.Wait(); wxThread::ExitCode code = call.Wait();
if (code) ThrowError(); //if (code) ThrowError();
delete ps; delete ps;
} }
@ -676,6 +686,7 @@ namespace Automation4 {
void LuaFeatureFilter::ProcessSubs(AssFile *subs, wxWindow *export_dialog) void LuaFeatureFilter::ProcessSubs(AssFile *subs, wxWindow *export_dialog)
{ {
GetFeatureFunction(1); // 1 = processing function GetFeatureFunction(1); // 1 = processing function
assert(lua_isfunction(L, -1));
// prepare function call // prepare function call
// subtitles (undo doesn't make sense in exported subs, in fact it'll totally break the undo system) // subtitles (undo doesn't make sense in exported subs, in fact it'll totally break the undo system)
@ -685,6 +696,9 @@ namespace Automation4 {
if (has_config && config_dialog) { if (has_config && config_dialog) {
assert(config_dialog->LuaReadBack(L) == 1); assert(config_dialog->LuaReadBack(L) == 1);
// TODO, write back stored options here // TODO, write back stored options here
} else {
// no config so put an empty table instead
lua_newtable(L);
} }
LuaProgressSink *ps = new LuaProgressSink(L, export_dialog, false); LuaProgressSink *ps = new LuaProgressSink(L, export_dialog, false);
@ -695,7 +709,7 @@ namespace Automation4 {
ps->ShowModal(); ps->ShowModal();
wxThread::ExitCode code = call.Wait(); wxThread::ExitCode code = call.Wait();
if (code) ThrowError(); //if (code) ThrowError();
delete ps; delete ps;
} }
@ -722,12 +736,16 @@ namespace Automation4 {
ps->ShowModal(); ps->ShowModal();
wxThread::ExitCode code = call.Wait(); wxThread::ExitCode code = call.Wait();
if (code) ThrowError(); //if (code) ThrowError();
delete ps; delete ps;
// The config dialog table should now be on stack // The config dialog table should now be on stack
return config_dialog = new LuaConfigDialog(L, false); if (!code) {
return config_dialog = new LuaConfigDialog(L, false);
} else {
return config_dialog = 0;
}
} }

View File

@ -865,7 +865,9 @@ void BaseGrid::SetColumnWidths() {
// Gets dialogue from map // Gets dialogue from map
AssDialogue *BaseGrid::GetDialogue(int n) { AssDialogue *BaseGrid::GetDialogue(int n) {
try { try {
return AssEntry::GetAsDialogue(*(diagMap.at(n))); AssEntry *e = *diagMap.at(n);
if (e->GetType() != ENTRY_DIALOGUE) return NULL;
return AssEntry::GetAsDialogue(e);
} }
catch (...) { catch (...) {
return NULL; return NULL;

View File

@ -998,11 +998,13 @@ void FrameMain::OnOpenAutomation (wxCommandEvent &event) {
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// General handler for all Automation-generated menu items // General handler for all Automation-generated menu items
void FrameMain::OnAutomationMacro (wxCommandEvent &event) { void FrameMain::OnAutomationMacro (wxCommandEvent &event) {
// Clear all maps from the subs grid before running the macro
// The stuff done by the macro might invalidate some of the iterators held by the grid, which will cause great crashing
SubsBox->Clear();
// Run the macro...
activeMacroItems[event.GetId()-Menu_Automation_Macro]->Process(SubsBox->ass, SubsBox->GetAbsoluteSelection(), SubsBox->GetFirstSelRow(), this); activeMacroItems[event.GetId()-Menu_Automation_Macro]->Process(SubsBox->ass, SubsBox->GetAbsoluteSelection(), SubsBox->GetFirstSelRow(), this);
// check if modifications were made and put on undo stack // Have the grid update its maps, this properly refreshes it to reflect the changed subs
AssFile::Popping = true; // HACK to avoid getting an additional undo point on stack SubsBox->UpdateMaps();
SubsBox->LoadFromAss(AssFile::top, true, true);
AssFile::Popping = false;
} }

View File

@ -52,7 +52,7 @@ function karaskel.collect_head(subs)
local l = subs[i] local l = subs[i]
if l.class == "style" then if l.class == "style" then
styles.n = styles.n + 1 styles.n = styles.n + 1
styles[n] = l styles[styles.n] = l
styles[l.name] = l styles[l.name] = l
elseif l.class == "info" then elseif l.class == "info" then
local k = l.key:lower() local k = l.key:lower()
@ -91,23 +91,23 @@ end
-- Modifies the line parameter -- Modifies the line parameter
function karaskel.preproc_line(subs, meta, styles, line) function karaskel.preproc_line(subs, meta, styles, line)
-- Assume line is class=dialogue -- Assume line is class=dialogue
local kara = aegisub.parse_karaoke_data(line.text) local kara = aegisub.parse_karaoke_data(line)
line.kara = { n = 0 } line.kara = { n = 0 }
line.furi = { n = 0 } line.furi = { n = 0 }
if styles[line.style] then if styles[line.style] then
line.style = styles[line.style] line.styleref = styles[line.style]
else else
aegisub.debug.out("WARNING: Style not found: " .. line.style .. "\n") aegisub.debug.out(2, "WARNING: Style not found: " .. line.style .. "\n")
line.style = styles[1] line.styleref = styles[1]
end end
line.text_stripped = "" line.text_stripped = ""
local curx = 0 local curx = 0
local worksyl = { } local worksyl = { }
for i = 0, #line.kara do for i = 0, #kara do
local syl = line.kara[i] local syl = kara[i]
-- Spaces at the start and end of the syllable are best ignored -- Spaces at the start and end of the syllable are best ignored
local prespace, syltext, postspace = syl.text_stripped:match("^([ \t]*)(.-)([ \t]*)$") local prespace, syltext, postspace = syl.text_stripped:match("^([ \t]*)(.-)([ \t]*)$")
@ -155,7 +155,7 @@ function karaskel.preproc_line(subs, meta, styles, line)
-- If this is the start of a highlight group, do regular processing -- If this is the start of a highlight group, do regular processing
if prefix ~= "#" and prefix ~= "" then if prefix ~= "#" and prefix ~= "" then
-- Update stripped line-text -- Update stripped line-text
line.text_stripped = line.text_stripped + syl.text_stripped line.text_stripped = line.text_stripped .. syl.text_stripped
-- Copy data from syl to worksyl -- Copy data from syl to worksyl
worksyl.text = syl.text worksyl.text = syl.text
@ -165,16 +165,16 @@ function karaskel.preproc_line(subs, meta, styles, line)
worksyl.end_time = syl.end_time worksyl.end_time = syl.end_time
worksyl.tag = syl.tag worksyl.tag = syl.tag
worksyl.line = line worksyl.line = line
worksyl.style = line.style worksyl.style = line.styleref
-- And add new data to worksyl -- And add new data to worksyl
worksyl.text_stripped = syltext worksyl.text_stripped = syltext
worksyl.width = aegisub.text_extents(line.style, syltext) worksyl.width = aegisub.text_extents(line.styleref, syltext)
curx = curx + aegisub.text_extents(line.style, prespace) curx = curx + aegisub.text_extents(line.styleref, prespace)
worksyl.left = curx worksyl.left = curx
worksyl.center = curx + worksyl.width/2 worksyl.center = curx + worksyl.width/2
worksyl.right = curx + worksyl.width worksyl.right = curx + worksyl.width
curx = curx + worksyl.width + aegisub.text_extents(line.style, postspace) curx = curx + worksyl.width + aegisub.text_extents(line.styleref, postspace)
-- TODO: inlinefx here -- TODO: inlinefx here
end end
@ -192,10 +192,12 @@ function karaskel.preproc_line(subs, meta, styles, line)
line.kara[line.kara.n] = worksyl line.kara[line.kara.n] = worksyl
-- Full line sizes -- Full line sizes
line.width, line.height, line.descent, line.extlead = aegisub.text_extents(line.style, line.text_stripped) line.width, line.height, line.descent, line.extlead = aegisub.text_extents(line.styleref, line.text_stripped)
-- And positioning (FIXME: assume centering for now)
line.left = (meta.res_x - line.styleref.margin_l - line.styleref.margin_r - line.width) / 2 + line.styleref.margin_l
-- Generate furigana style -- Generate furigana style
local furistyle = table.copy(line.style) local furistyle = table.copy(line.styleref)
furistyle.fontsize = furistyle.fontsize / 2 furistyle.fontsize = furistyle.fontsize / 2
furistyle.outline = furistyle.outline / 2 furistyle.outline = furistyle.outline / 2

View File

@ -35,7 +35,12 @@ module("unicode")
-- Return the number of bytes occupied by the character starting at the i'th byte in s -- Return the number of bytes occupied by the character starting at the i'th byte in s
function charwidth(s, i) function charwidth(s, i)
local b = s:byte(i) local b = s:byte(i)
if b < 128 then if not b then
--aegisub.debug.out(3, "unicode.charwidth of '%s' @ %d, nil byte\n", s, i)
-- FIXME, something in karaskel results in this case, shouldn't happen
-- What would "proper" behaviour be? Zero? Or just explode?
return 1
elseif b < 128 then
return 1 return 1
elseif b < 224 then elseif b < 224 then
return 2 return 2

View File

@ -143,3 +143,14 @@ function string.headtail(s)
return s, "" return s, ""
end end
end end
-- Clamp a number value to a range
function clamp(val, min, max)
if val < min then
return min
elseif val > max then
return max
else
return val
end
end