From 0fbfd67864583672d2c137038492f142e086cfc5 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Mon, 8 Jan 2007 01:54:02 +0000 Subject: [PATCH] Added support for reading v4.00++ (ASS2) files. Originally committed to SVN as r741. --- aegisub/ass_dialogue.cpp | 45 ++++++++++++++++++++++++++++----- aegisub/ass_file.cpp | 2 +- aegisub/ass_style.cpp | 25 +++++++++++++++--- aegisub/ass_style.h | 1 + aegisub/changelog.txt | 1 + aegisub/subtitle_format_ass.cpp | 12 ++++++--- 6 files changed, 73 insertions(+), 13 deletions(-) diff --git a/aegisub/ass_dialogue.cpp b/aegisub/ass_dialogue.cpp index 43d81541f..6235cf3e8 100644 --- a/aegisub/ass_dialogue.cpp +++ b/aegisub/ass_dialogue.cpp @@ -81,11 +81,25 @@ AssDialogue::AssDialogue(wxString _data,int version) { Movement = 0; #endif + // Set group group = _T("[Events]"); - Valid = Parse(_data,version); + + // Try parsing in different ways + int count = 0; + Valid = false; + while (!Valid && count < 3) { + Valid = Parse(_data,version); + count++; + version++; + if (version > 2) version = 0; + } + + // Not valid if (!Valid) { throw _T("Failed parsing line."); } + + // update UpdateData(); } @@ -145,9 +159,13 @@ bool AssDialogue::Parse(wxString rawData, int version) { else return false; wxStringTokenizer tkn(rawData.Mid(pos),_T(","),wxTOKEN_RET_EMPTY_ALL); - // Get layer number + // Get first token and see if it has "Marked=" in it if (!tkn.HasMoreTokens()) return false; temp = tkn.GetNextToken().Trim(false).Trim(true); + if (temp.Lower().Left(7) == _T("marked=")) version = 0; + else if (version == 0) version = 1; + + // Get layer number if (version == 0) Layer = 0; else { long templ; @@ -184,15 +202,30 @@ bool AssDialogue::Parse(wxString rawData, int version) { if (!tkn.HasMoreTokens()) return false; SetMarginString(tkn.GetNextToken().Trim(false).Trim(true),1); - // Get vertical margin + // Get top margin if (!tkn.HasMoreTokens()) return false; temp = tkn.GetNextToken().Trim(false).Trim(true); SetMarginString(temp,2); - SetMarginString(temp,3); + if (version == 1) SetMarginString(temp,3); + + // Get bottom margin + bool rollBack = false; + if (version == 2) { + if (!tkn.HasMoreTokens()) return false; + wxString oldTemp = temp; + temp = tkn.GetNextToken().Trim(false).Trim(true); + if (!temp.IsNumber()) { + version = 1; + rollBack = true; + } + } // Get effect - if (!tkn.HasMoreTokens()) return false; - Effect = tkn.GetNextToken(); + if (!rollBack) { + if (!tkn.HasMoreTokens()) return false; + temp = tkn.GetNextToken(); + } + Effect = temp; Effect.Trim(true); Effect.Trim(false); diff --git a/aegisub/ass_file.cpp b/aegisub/ass_file.cpp index ef678d7f9..711712474 100644 --- a/aegisub/ass_file.cpp +++ b/aegisub/ass_file.cpp @@ -368,7 +368,7 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,int &version,wxS else if (versionString == _T("v4.00++")) trueVersion = 2; else throw _T("Unknown file version"); if (trueVersion != version) { - wxLogMessage(_T("Warning: File has the wrong extension.")); + if (!(trueVersion == 2 && version == 1)) wxLogMessage(_T("Warning: File has the wrong extension.")); version = trueVersion; } } diff --git a/aegisub/ass_style.cpp b/aegisub/ass_style.cpp index 341ed3f08..cd374da23 100644 --- a/aegisub/ass_style.cpp +++ b/aegisub/ass_style.cpp @@ -189,6 +189,7 @@ AssStyle::AssStyle() { Margin[2] = 10; Margin[3] = 10; encoding = 0; + relativeTo = 1; UpdateData(); } @@ -214,6 +215,7 @@ AssStyle::~AssStyle() { ////////////////////////////// // Parses value from ASS data bool AssStyle::Parse(wxString rawData,int version) { + // Tokenize wxString temp; long templ; wxStringTokenizer tkn(rawData.Mid(6),_T(","),wxTOKEN_RET_EMPTY_ALL); @@ -381,12 +383,20 @@ bool AssStyle::Parse(wxString rawData,int version) { if (!tkn.HasMoreTokens()) return false; SetMarginString(tkn.GetNextToken(),1); - // Read vertical margin + // Read top margin if (!tkn.HasMoreTokens()) return false; - SetMarginString(tkn.GetNextToken(),2); + temp = tkn.GetNextToken(); + SetMarginString(temp,2); + // Read bottom margin + if (version == 2) { + if (!tkn.HasMoreTokens()) return false; + SetMarginString(tkn.GetNextToken(),3); + } + else SetMarginString(temp,3); + + // Read alpha level if (version == 0) { - // Read alpha level if (!tkn.HasMoreTokens()) return false; temp = tkn.GetNextToken(); } @@ -397,6 +407,14 @@ bool AssStyle::Parse(wxString rawData,int version) { temp.ToLong(&templ); encoding = templ; + // Read relative to + if (version == 2) { + if (!tkn.HasMoreTokens()) return false; + temp = tkn.GetNextToken(); + temp.ToLong(&templ); + relativeTo = templ; + } + // End if (tkn.HasMoreTokens()) return false; return true; @@ -551,6 +569,7 @@ AssEntry *AssStyle::Clone() { final->spacing = spacing; final->strikeout = strikeout; final->underline = underline; + final->relativeTo = relativeTo; final->SetEntryData(GetEntryData()); // Return diff --git a/aegisub/ass_style.h b/aegisub/ass_style.h index 950e18b30..773e32cb4 100644 --- a/aegisub/ass_style.h +++ b/aegisub/ass_style.h @@ -91,6 +91,7 @@ public: int alignment; int Margin[4]; int encoding; + int relativeTo; ASS_EntryType GetType() { return ENTRY_STYLE; } diff --git a/aegisub/changelog.txt b/aegisub/changelog.txt index 80fee2fa3..e5603b325 100644 --- a/aegisub/changelog.txt +++ b/aegisub/changelog.txt @@ -80,6 +80,7 @@ Please visit http://aegisub.net to download latest version - Destination of screenshots can now be set in the options dialog. (AMZ) - Moved karaoke syllable text in audio display to the top instead of bottom, since it often covers important information in spectrum mode (jfs) - Implemented a version checker that can automatically or manually check if there are any updates to Aegisub. (AMZ) +- Added support for reading v4.00++ (ASS2) files. (AMZ) = 1.10 beta - 2006.08.07 =========================== diff --git a/aegisub/subtitle_format_ass.cpp b/aegisub/subtitle_format_ass.cpp index 76311c841..41927b977 100644 --- a/aegisub/subtitle_format_ass.cpp +++ b/aegisub/subtitle_format_ass.cpp @@ -83,7 +83,8 @@ void ASSSubtitleFormat::ReadFile(wxString filename,wxString encoding) { // Reader TextFileReader file(filename,encoding); - int version = filename.Right(4).Lower() == _T(".ssa"); + int version = 1; + if (filename.Right(4).Lower() == _T(".ssa")) version = 0; // Parse file wxString curgroup; @@ -104,12 +105,17 @@ void ASSSubtitleFormat::ReadFile(wxString filename,wxString encoding) { curgroup = wxbuffer; version = 1; } + else if (wxbuffer.Lower() == _T("[v4++ styles]")) { + wxbuffer = _T("[V4+ Styles]"); + curgroup = wxbuffer; + version = 2; + } // Not-so-special case for other groups, just set it else if (wxbuffer[0] == _T('[')) { curgroup = wxbuffer; // default from extension in all other sections - version = 1; - if (filename.Right(4).Lower() == _T(".ssa")) version = 0; + //version = 1; + //if (filename.Right(4).Lower() == _T(".ssa")) version = 0; } // Add line