Changes to prepare support for ASS2

Originally committed to SVN as r739.
This commit is contained in:
Rodrigo Braz Monteiro 2007-01-08 01:00:44 +00:00
parent 91ac4bef10
commit b96c7932b9
10 changed files with 70 additions and 66 deletions

View file

@ -75,14 +75,14 @@ AssDialogue::AssDialogue() {
}
AssDialogue::AssDialogue(wxString _data,bool IsSSA) {
AssDialogue::AssDialogue(wxString _data,int version) {
#if USE_FEXTRACKER == 1
Tracker = 0;
Movement = 0;
#endif
group = _T("[Events]");
Valid = Parse(_data,IsSSA);
Valid = Parse(_data,version);
if (!Valid) {
throw _T("Failed parsing line.");
}
@ -129,7 +129,7 @@ void AssDialogue::ClearBlocks() {
//////////////////
// Parse ASS Data
bool AssDialogue::Parse(wxString rawData, bool IsSSA) {
bool AssDialogue::Parse(wxString rawData, int version) {
size_t pos = 0;
wxString temp;
@ -148,7 +148,7 @@ bool AssDialogue::Parse(wxString rawData, bool IsSSA) {
// Get layer number
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken().Trim(false).Trim(true);
if (IsSSA) Layer = 0;
if (version == 0) Layer = 0;
else {
long templ;
temp.ToLong(&templ);

View file

@ -178,7 +178,7 @@ public:
ASS_EntryType GetType() { return ENTRY_DIALOGUE; }
bool Parse(wxString data,bool IsSSA=false); // Parses raw ASS data into everything else
bool Parse(wxString data,int version=1); // Parses raw ASS data into everything else
void ParseASSTags(); // Parses text to generate block information (doesn't update data)
void ParseSRTTags(); // Converts tags to ass format and calls ParseASSTags+UpdateData
@ -200,6 +200,6 @@ public:
AssEntry *Clone();
AssDialogue();
AssDialogue(wxString data,bool IsSSA=false);
AssDialogue(wxString data,int version=1);
~AssDialogue();
};

View file

@ -252,7 +252,7 @@ wxString AssFile::GetString() {
// I strongly advice you against touching this function unless you know what you're doing;
// even moving things out of order might break ASS parsing - AMZ.
//
int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA,wxString *outGroup) {
int AssFile::AddLine (wxString data,wxString group,int lasttime,int &version,wxString *outGroup) {
// Group
AssEntry *entry = NULL;
wxString origGroup = group;
@ -318,7 +318,7 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA,wxSt
// Dialogue
if (group.Lower() == _T("[events]")) {
if ((data.Left(9) == _T("Dialogue:") || data.Left(8) == _T("Comment:"))) {
AssDialogue *diag = new AssDialogue(data,IsSSA);
AssDialogue *diag = new AssDialogue(data,version);
lasttime = diag->Start.GetMS();
//diag->ParseASSTags();
entry = diag;
@ -335,7 +335,7 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA,wxSt
// Style
else if (group.Lower() == _T("[v4+ styles]")) {
if (data.Left(6) == _T("Style:")) {
AssStyle *style = new AssStyle(data,IsSSA);
AssStyle *style = new AssStyle(data,version);
entry = style;
entry->StartMS = lasttime;
entry->group = group;
@ -358,17 +358,18 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA,wxSt
// Version
if (data.Left(11) == _T("ScriptType:")) {
wxString version = data.Mid(11);
version.Trim(true);
version.Trim(false);
version.MakeLower();
bool trueSSA;
if (version == _T("v4.00")) trueSSA = true;
else if (version == _T("v4.00+")) trueSSA = false;
wxString versionString = data.Mid(11);
versionString.Trim(true);
versionString.Trim(false);
versionString.MakeLower();
int trueVersion;
if (versionString == _T("v4.00")) trueVersion = 0;
else if (versionString == _T("v4.00+")) trueVersion = 1;
else if (versionString == _T("v4.00++")) trueVersion = 2;
else throw _T("Unknown file version");
if (trueSSA != IsSSA) {
if (trueVersion != version) {
wxLogMessage(_T("Warning: File has the wrong extension."));
IsSSA = trueSSA;
version = trueVersion;
}
}
@ -413,24 +414,24 @@ void AssFile::LoadDefault (bool defline) {
// Write headers
AssStyle defstyle;
bool IsSSA = false;
AddLine(_T("[Script Info]"),_T("[Script Info]"),-1,IsSSA);
AddLine(_T("Title: Default Aegisub file"),_T("[Script Info]"),-1,IsSSA);
AddLine(_T("ScriptType: v4.00+"),_T("[Script Info]"),-1,IsSSA);
AddLine(_T("WrapStyle: 0"), _T("[Script Info]"),-1,IsSSA);
AddLine(_T("PlayResX: 640"),_T("[Script Info]"),-1,IsSSA);
AddLine(_T("PlayResY: 480"),_T("[Script Info]"),-1,IsSSA);
AddLine(_T(""),_T("[Script Info]"),-1,IsSSA);
AddLine(_T("[V4+ Styles]"),_T("[V4+ Styles]"),-1,IsSSA);
AddLine(_T("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding"),_T("[V4+ Styles]"),-1,IsSSA);
AddLine(defstyle.GetEntryData(),_T("[V4+ Styles]"),-1,IsSSA);
AddLine(_T(""),_T("[V4+ Styles]"),-1,IsSSA);
AddLine(_T("[Events]"),_T("[Events]"),-1,IsSSA);
AddLine(_T("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"),_T("[Events]"),-1,IsSSA);
int version = 1;
AddLine(_T("[Script Info]"),_T("[Script Info]"),-1,version);
AddLine(_T("Title: Default Aegisub file"),_T("[Script Info]"),-1,version);
AddLine(_T("ScriptType: v4.00+"),_T("[Script Info]"),-1,version);
AddLine(_T("WrapStyle: 0"), _T("[Script Info]"),-1,version);
AddLine(_T("PlayResX: 640"),_T("[Script Info]"),-1,version);
AddLine(_T("PlayResY: 480"),_T("[Script Info]"),-1,version);
AddLine(_T(""),_T("[Script Info]"),-1,version);
AddLine(_T("[V4+ Styles]"),_T("[V4+ Styles]"),-1,version);
AddLine(_T("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding"),_T("[V4+ Styles]"),-1,version);
AddLine(defstyle.GetEntryData(),_T("[V4+ Styles]"),-1,version);
AddLine(_T(""),_T("[V4+ Styles]"),-1,version);
AddLine(_T("[Events]"),_T("[Events]"),-1,version);
AddLine(_T("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"),_T("[Events]"),-1,version);
if (defline) {
AssDialogue def;
AddLine(def.GetEntryData(),_T("[Events]"),0,IsSSA);
AddLine(def.GetEntryData(),_T("[Events]"),0,version);
}
loaded = true;
@ -446,7 +447,7 @@ AssFile::AssFile (AssFile &from) {
filename = from.filename;
loaded = from.loaded;
Modified = from.Modified;
bool IsSSA = false;
int version = 1;
// Copy lines
int lasttime = -1;
@ -541,13 +542,13 @@ void AssFile::InsertAttachment (AssAttachment *attach) {
// Otherwise, create the [Fonts] group and insert
else {
bool IsSSA=false;
int version=1;
int StartMS = Line.back()->StartMS;
AddLine(_T(""),Line.back()->group,StartMS,IsSSA);
AddLine(attach->group,attach->group,StartMS,IsSSA);
AddLine(_T(""),Line.back()->group,StartMS,version);
AddLine(attach->group,attach->group,StartMS,version);
attach->StartMS = StartMS;
Line.push_back(attach);
AddLine(_T(""),attach->group,StartMS,IsSSA);
AddLine(_T(""),attach->group,StartMS,version);
}
}

View file

@ -102,7 +102,7 @@ public:
wxString GetScriptInfo(const wxString key); // Returns the value in a [Script Info] key as string.
void SetScriptInfo(const wxString key,const wxString value); // Sets the value of a [Script Info] key. Adds it if it doesn't exist.
void AddComment(const wxString comment); // Adds a ";" comment under [Script Info].
int AddLine(wxString data,wxString group,int lasttime,bool &IsSSA,wxString *outGroup=NULL);
int AddLine(wxString data,wxString group,int lasttime,int &version,wxString *outGroup=NULL);
static void StackPop(); // Pop subs from stack and sets 'top' to it
static void StackRedo(); // Redoes action on stack

View file

@ -195,8 +195,8 @@ AssStyle::AssStyle() {
///////////////
// Constructor
AssStyle::AssStyle(wxString _data,bool IsSSA) {
Valid = Parse(_data,IsSSA);
AssStyle::AssStyle(wxString _data,int version) {
Valid = Parse(_data,version);
if (!Valid) {
throw _T("[Error] Failed parsing line.");
}
@ -212,7 +212,7 @@ AssStyle::~AssStyle() {
//////////////////////////////
// Parses value from ASS data
bool AssStyle::Parse(wxString rawData,bool IsSSA) {
bool AssStyle::Parse(wxString rawData,int version) {
wxString temp;
long templ;
wxStringTokenizer tkn(rawData.Mid(6),_T(","),wxTOKEN_RET_EMPTY_ALL);
@ -235,7 +235,7 @@ bool AssStyle::Parse(wxString rawData,bool IsSSA) {
temp.ToLong(&templ);
fontsize = templ;
if (!IsSSA) {
if (version != 0) {
// Read primary color
if (!tkn.HasMoreTokens()) return false;
primary.ParseASS(tkn.GetNextToken());
@ -285,7 +285,7 @@ bool AssStyle::Parse(wxString rawData,bool IsSSA) {
italic = true;
if (templ == 0) italic = false;
if (!IsSSA) {
if (version != 0) {
// Read underline
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
@ -356,7 +356,7 @@ bool AssStyle::Parse(wxString rawData,bool IsSSA) {
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
temp.ToLong(&templ);
if (IsSSA) {
if (version == 0) {
switch(templ) {
case 1: alignment = 1; break;
case 2: alignment = 2; break;
@ -384,7 +384,7 @@ bool AssStyle::Parse(wxString rawData,bool IsSSA) {
if (!tkn.HasMoreTokens()) return false;
SetMarginString(tkn.GetNextToken(),2);
if (IsSSA) {
if (version == 0) {
// Read alpha level
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
@ -458,6 +458,7 @@ void AssStyle::SetMarginString(const wxString str,int which) {
case 0: MarginL = value; break;
case 1: MarginR = value; break;
case 2: MarginV = value; break;
case 3: MarginV = value; break;
default: throw _T("Invalid margin");
}
}
@ -468,8 +469,9 @@ void AssStyle::SetMarginString(const wxString str,int which) {
wxString AssStyle::GetMarginString(int which) {
int value;
switch (which) {
case 1: value = MarginL; break;
case 2: value = MarginR; break;
case 0: value = MarginL; break;
case 1: value = MarginR; break;
case 2: value = MarginV; break;
case 3: value = MarginV; break;
default: throw _T("Invalid margin");
}

View file

@ -96,7 +96,7 @@ public:
ASS_EntryType GetType() { return ENTRY_STYLE; }
bool Parse(wxString data,bool IsSSA=false); // Parses raw ASS/SSA data into everything else
bool Parse(wxString data,int version=1); // Parses raw ASS/SSA data into everything else
void UpdateData(); // Updates raw data
wxString GetSSAText(); // Retrieves SSA-formatted style
wxString GetMarginString(int which); // Returns the margin value as a string (0 = left, 1 = right, 2 = vertical/top, 3 = bottom)
@ -105,6 +105,6 @@ public:
AssEntry *Clone();
AssStyle();
AssStyle(wxString data,bool IsSSA=false);
AssStyle(wxString data,int version=1);
~AssStyle();
};

View file

@ -354,18 +354,18 @@ void MatroskaWrapper::GetSubtitles(AssFile *target) {
// Load into file
wxString group = _T("[Script Info]");
int lasttime = 0;
bool IsSSA = (CodecID == _T("S_TEXT/SSA"));
int version = (CodecID == _T("S_TEXT/SSA"));
wxStringTokenizer token(privString,_T("\r\n"),wxTOKEN_STRTOK);
while (token.HasMoreTokens()) {
wxString next = token.GetNextToken();
if (next[0] == _T('[')) group = next;
lasttime = target->AddLine(next,group,lasttime,IsSSA,&group);
lasttime = target->AddLine(next,group,lasttime,version,&group);
}
// Insert "[Events]"
//target->AddLine(_T(""),group,lasttime,IsSSA,&group);
//target->AddLine(_T("[Events]"),group,lasttime,IsSSA,&group);
//target->AddLine(_T("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"),group,lasttime,IsSSA,&group);
//target->AddLine(_T(""),group,lasttime,version,&group);
//target->AddLine(_T("[Events]"),group,lasttime,version,&group);
//target->AddLine(_T("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"),group,lasttime,version,&group);
}
// Load default if it's SRT
@ -457,9 +457,9 @@ void MatroskaWrapper::GetSubtitles(AssFile *target) {
// Insert into file
wxString group = _T("[Events]");
int lasttime = 0;
bool IsSSA = (CodecID == _T("S_TEXT/SSA"));
int version = (CodecID == _T("S_TEXT/SSA"));
for (unsigned int i=0;i<subList.size();i++) {
lasttime = target->AddLine(subList[i],group,lasttime,IsSSA,&group);
lasttime = target->AddLine(subList[i],group,lasttime,version,&group);
}
// Close progress bar

View file

@ -115,8 +115,8 @@ void SubtitleFormat::LoadDefault(bool defline) {
////////////
// Add line
int SubtitleFormat::AddLine(wxString data,wxString group,int lasttime,bool &IsSSA,wxString *outgroup) {
return assFile->AddLine(data,group,lasttime,IsSSA,outgroup);
int SubtitleFormat::AddLine(wxString data,wxString group,int lasttime,int &version,wxString *outgroup) {
return assFile->AddLine(data,group,lasttime,version,outgroup);
}

View file

@ -70,7 +70,7 @@ protected:
void Clear();
void LoadDefault(bool defline=true);
AssFile *GetAssFile() { return assFile; }
int AddLine(wxString data,wxString group,int lasttime,bool &IsSSA,wxString *outgroup=NULL);
int AddLine(wxString data,wxString group,int lasttime,int &version,wxString *outgroup=NULL);
virtual wxString GetName()=0;
virtual wxArrayString GetReadWildcards();

View file

@ -83,7 +83,7 @@ void ASSSubtitleFormat::ReadFile(wxString filename,wxString encoding) {
// Reader
TextFileReader file(filename,encoding);
bool IsSSA = filename.Right(4).Lower() == _T(".ssa");
int version = filename.Right(4).Lower() == _T(".ssa");
// Parse file
wxString curgroup;
@ -98,22 +98,23 @@ void ASSSubtitleFormat::ReadFile(wxString filename,wxString encoding) {
if (wxbuffer.Lower() == _T("[v4 styles]")) {
wxbuffer = _T("[V4+ Styles]");
curgroup = wxbuffer;
IsSSA = true;
version = 0;
}
else if (wxbuffer.Lower() == _T("[v4+ styles]")) {
curgroup = wxbuffer;
IsSSA = false;
version = 1;
}
// Not-so-special case for other groups, just set it
else if (wxbuffer[0] == _T('[')) {
curgroup = wxbuffer;
// default from extension in all other sections
IsSSA = filename.Right(4).Lower() == _T(".ssa");
version = 1;
if (filename.Right(4).Lower() == _T(".ssa")) version = 0;
}
// Add line
try {
lasttime = AddLine(wxbuffer,curgroup,lasttime,IsSSA,&curgroup);
lasttime = AddLine(wxbuffer,curgroup,lasttime,version,&curgroup);
}
catch (const wchar_t *err) {
Clear();
@ -126,7 +127,7 @@ void ASSSubtitleFormat::ReadFile(wxString filename,wxString encoding) {
}
// Add one last empty line in case it didn't end with one
if (!wxbuffer.IsEmpty()) AddLine(_T(""),curgroup,lasttime,IsSSA);
if (!wxbuffer.IsEmpty()) AddLine(_T(""),curgroup,lasttime,version);
}