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

View File

@ -178,7 +178,7 @@ public:
ASS_EntryType GetType() { return ENTRY_DIALOGUE; } 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 ParseASSTags(); // Parses text to generate block information (doesn't update data)
void ParseSRTTags(); // Converts tags to ass format and calls ParseASSTags+UpdateData void ParseSRTTags(); // Converts tags to ass format and calls ParseASSTags+UpdateData
@ -200,6 +200,6 @@ public:
AssEntry *Clone(); AssEntry *Clone();
AssDialogue(); AssDialogue();
AssDialogue(wxString data,bool IsSSA=false); AssDialogue(wxString data,int version=1);
~AssDialogue(); ~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; // 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. // 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 // Group
AssEntry *entry = NULL; AssEntry *entry = NULL;
wxString origGroup = group; wxString origGroup = group;
@ -318,7 +318,7 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA,wxSt
// Dialogue // Dialogue
if (group.Lower() == _T("[events]")) { if (group.Lower() == _T("[events]")) {
if ((data.Left(9) == _T("Dialogue:") || data.Left(8) == _T("Comment:"))) { 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(); lasttime = diag->Start.GetMS();
//diag->ParseASSTags(); //diag->ParseASSTags();
entry = diag; entry = diag;
@ -335,7 +335,7 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA,wxSt
// Style // Style
else if (group.Lower() == _T("[v4+ styles]")) { else if (group.Lower() == _T("[v4+ styles]")) {
if (data.Left(6) == _T("Style:")) { if (data.Left(6) == _T("Style:")) {
AssStyle *style = new AssStyle(data,IsSSA); AssStyle *style = new AssStyle(data,version);
entry = style; entry = style;
entry->StartMS = lasttime; entry->StartMS = lasttime;
entry->group = group; entry->group = group;
@ -358,17 +358,18 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA,wxSt
// Version // Version
if (data.Left(11) == _T("ScriptType:")) { if (data.Left(11) == _T("ScriptType:")) {
wxString version = data.Mid(11); wxString versionString = data.Mid(11);
version.Trim(true); versionString.Trim(true);
version.Trim(false); versionString.Trim(false);
version.MakeLower(); versionString.MakeLower();
bool trueSSA; int trueVersion;
if (version == _T("v4.00")) trueSSA = true; if (versionString == _T("v4.00")) trueVersion = 0;
else if (version == _T("v4.00+")) trueSSA = false; else if (versionString == _T("v4.00+")) trueVersion = 1;
else if (versionString == _T("v4.00++")) trueVersion = 2;
else throw _T("Unknown file version"); else throw _T("Unknown file version");
if (trueSSA != IsSSA) { if (trueVersion != version) {
wxLogMessage(_T("Warning: File has the wrong extension.")); wxLogMessage(_T("Warning: File has the wrong extension."));
IsSSA = trueSSA; version = trueVersion;
} }
} }
@ -413,24 +414,24 @@ void AssFile::LoadDefault (bool defline) {
// Write headers // Write headers
AssStyle defstyle; AssStyle defstyle;
bool IsSSA = false; int version = 1;
AddLine(_T("[Script Info]"),_T("[Script Info]"),-1,IsSSA); AddLine(_T("[Script Info]"),_T("[Script Info]"),-1,version);
AddLine(_T("Title: Default Aegisub file"),_T("[Script Info]"),-1,IsSSA); AddLine(_T("Title: Default Aegisub file"),_T("[Script Info]"),-1,version);
AddLine(_T("ScriptType: v4.00+"),_T("[Script Info]"),-1,IsSSA); AddLine(_T("ScriptType: v4.00+"),_T("[Script Info]"),-1,version);
AddLine(_T("WrapStyle: 0"), _T("[Script Info]"),-1,IsSSA); AddLine(_T("WrapStyle: 0"), _T("[Script Info]"),-1,version);
AddLine(_T("PlayResX: 640"),_T("[Script Info]"),-1,IsSSA); AddLine(_T("PlayResX: 640"),_T("[Script Info]"),-1,version);
AddLine(_T("PlayResY: 480"),_T("[Script Info]"),-1,IsSSA); AddLine(_T("PlayResY: 480"),_T("[Script Info]"),-1,version);
AddLine(_T(""),_T("[Script Info]"),-1,IsSSA); AddLine(_T(""),_T("[Script Info]"),-1,version);
AddLine(_T("[V4+ Styles]"),_T("[V4+ Styles]"),-1,IsSSA); 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,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,version);
AddLine(defstyle.GetEntryData(),_T("[V4+ Styles]"),-1,IsSSA); AddLine(defstyle.GetEntryData(),_T("[V4+ Styles]"),-1,version);
AddLine(_T(""),_T("[V4+ Styles]"),-1,IsSSA); AddLine(_T(""),_T("[V4+ Styles]"),-1,version);
AddLine(_T("[Events]"),_T("[Events]"),-1,IsSSA); AddLine(_T("[Events]"),_T("[Events]"),-1,version);
AddLine(_T("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"),_T("[Events]"),-1,IsSSA); AddLine(_T("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"),_T("[Events]"),-1,version);
if (defline) { if (defline) {
AssDialogue def; AssDialogue def;
AddLine(def.GetEntryData(),_T("[Events]"),0,IsSSA); AddLine(def.GetEntryData(),_T("[Events]"),0,version);
} }
loaded = true; loaded = true;
@ -446,7 +447,7 @@ AssFile::AssFile (AssFile &from) {
filename = from.filename; filename = from.filename;
loaded = from.loaded; loaded = from.loaded;
Modified = from.Modified; Modified = from.Modified;
bool IsSSA = false; int version = 1;
// Copy lines // Copy lines
int lasttime = -1; int lasttime = -1;
@ -541,13 +542,13 @@ void AssFile::InsertAttachment (AssAttachment *attach) {
// Otherwise, create the [Fonts] group and insert // Otherwise, create the [Fonts] group and insert
else { else {
bool IsSSA=false; int version=1;
int StartMS = Line.back()->StartMS; int StartMS = Line.back()->StartMS;
AddLine(_T(""),Line.back()->group,StartMS,IsSSA); AddLine(_T(""),Line.back()->group,StartMS,version);
AddLine(attach->group,attach->group,StartMS,IsSSA); AddLine(attach->group,attach->group,StartMS,version);
attach->StartMS = StartMS; attach->StartMS = StartMS;
Line.push_back(attach); 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. 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 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]. 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 StackPop(); // Pop subs from stack and sets 'top' to it
static void StackRedo(); // Redoes action on stack static void StackRedo(); // Redoes action on stack

View File

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

View File

@ -96,7 +96,7 @@ public:
ASS_EntryType GetType() { return ENTRY_STYLE; } 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 void UpdateData(); // Updates raw data
wxString GetSSAText(); // Retrieves SSA-formatted style 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) 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(); AssEntry *Clone();
AssStyle(); AssStyle();
AssStyle(wxString data,bool IsSSA=false); AssStyle(wxString data,int version=1);
~AssStyle(); ~AssStyle();
}; };

View File

@ -354,18 +354,18 @@ void MatroskaWrapper::GetSubtitles(AssFile *target) {
// Load into file // Load into file
wxString group = _T("[Script Info]"); wxString group = _T("[Script Info]");
int lasttime = 0; 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); wxStringTokenizer token(privString,_T("\r\n"),wxTOKEN_STRTOK);
while (token.HasMoreTokens()) { while (token.HasMoreTokens()) {
wxString next = token.GetNextToken(); wxString next = token.GetNextToken();
if (next[0] == _T('[')) group = next; if (next[0] == _T('[')) group = next;
lasttime = target->AddLine(next,group,lasttime,IsSSA,&group); lasttime = target->AddLine(next,group,lasttime,version,&group);
} }
// Insert "[Events]" // Insert "[Events]"
//target->AddLine(_T(""),group,lasttime,IsSSA,&group); //target->AddLine(_T(""),group,lasttime,version,&group);
//target->AddLine(_T("[Events]"),group,lasttime,IsSSA,&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,IsSSA,&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 // Load default if it's SRT
@ -457,9 +457,9 @@ void MatroskaWrapper::GetSubtitles(AssFile *target) {
// Insert into file // Insert into file
wxString group = _T("[Events]"); wxString group = _T("[Events]");
int lasttime = 0; 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++) { 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 // Close progress bar

View File

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

View File

@ -70,7 +70,7 @@ protected:
void Clear(); void Clear();
void LoadDefault(bool defline=true); void LoadDefault(bool defline=true);
AssFile *GetAssFile() { return assFile; } 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 wxString GetName()=0;
virtual wxArrayString GetReadWildcards(); virtual wxArrayString GetReadWildcards();

View File

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