Some code simplification on the subs lib.

Originally committed to SVN as r2028.
This commit is contained in:
Rodrigo Braz Monteiro 2008-03-12 19:28:24 +00:00
parent 0e3333e4f8
commit 3b7dce9c8e
9 changed files with 92 additions and 71 deletions

View file

@ -35,7 +35,7 @@
#pragma once
#include <list>
#include <wx/stream.h>
#include <wx/wfstream.h>
#include "manipulator.h"
#include "section.h"
@ -68,8 +68,10 @@ namespace Aegilib {
const Format& GetFormat() const;
void AddListener(View *listener);
void LoadFile(wxInputStream &input,const Format *format=NULL,const String encoding=L"");
void SaveFile(wxOutputStream &output,const Format *format=NULL,const String encoding=L"UTF-8");
void Load(wxInputStream &input,const Format *format=NULL,const String encoding=L"");
void Save(wxOutputStream &output,const Format *format=NULL,const String encoding=L"UTF-8");
void LoadFile(const String filename,const String encoding=L"");
void SaveFile(const String filename,const String encoding=L"UTF-8");
Section* GetSection(String name) const;
void AddSection(String name);

View file

@ -53,7 +53,7 @@ namespace Aegilib {
bool HasMore();
int GetPosition();
String GetString();
String GetString(bool trim=false);
int GetInt();
long GetLong();
float GetFloat();

View file

@ -150,6 +150,8 @@ namespace Aegilib {
float shadow_w;
bool Parse(String data,int version);
int AlignSSAtoASS(int ssaAlignment);
int AlignASStoSSA(int assAlignment);
public:
// Constructors

View file

@ -81,13 +81,15 @@ bool DialogueASS::Parse(wxString rawData, int version)
Tokenizer tkn(rawData.Mid(pos),_T(","));
// Get first token and see if it has "Marked=" in it
temp = tkn.GetString().Trim(false).Trim(true);
if (temp.Lower().StartsWith(_T("marked="))) version = 0;
else if (version == 0) version = 1;
temp = tkn.GetString(true);
if (temp.Lower().StartsWith(_T("marked="))) {
version = 0;
layer = 0;
}
// Get layer number
if (version == 0) layer = 0;
// Not SSA, so read layer number
else {
if (version == 0) version = 1; // Only do it for SSA, not ASS2
long templ;
temp.ToLong(&templ);
layer = templ;
@ -97,15 +99,9 @@ bool DialogueASS::Parse(wxString rawData, int version)
start.Parse(tkn.GetString());
end.Parse(tkn.GetString());
// Get style
style = tkn.GetString();
style.Trim(true);
style.Trim(false);
// Get actor
actor = tkn.GetString();
actor.Trim(true);
actor.Trim(false);
// Get style and actor
style = tkn.GetString(true);
actor = tkn.GetString(true);
// Get margins
for (int i=0;i<3;i++) margin[i] = tkn.GetInt();

View file

@ -65,23 +65,15 @@ bool StyleASS::Parse(String data,int version)
wxString temp;
Tokenizer tkn(data.Trim(false).Mid(6),_T(","));
// Read name
name = tkn.GetString();
name.Trim(true);
name.Trim(false);
// Read font name and size
font = tkn.GetString();
font.Trim(true);
font.Trim(false);
// Read name, font name and size
name = tkn.GetString(true);
font = tkn.GetString(true);
fontSize = tkn.GetFloat();
// Read colours
for (int i=0;i<5;i++) {
if ((i == 4 && version == 0) || (i == 2 && version != 0)) colour[i] = colour[i-1];
else {
colour[i].Parse(tkn.GetString(),true);
}
else colour[i].Parse(tkn.GetString(),true);
}
// Read bold and italics
@ -115,43 +107,24 @@ bool StyleASS::Parse(String data,int version)
shadow_w = tkn.GetFloat();
// Read alignment
int align = tkn.GetInt();
if (version == 0) {
switch(align) {
case 1: alignment = 1; break;
case 2: alignment = 2; break;
case 3: alignment = 3; break;
case 5: alignment = 7; break;
case 6: alignment = 8; break;
case 7: alignment = 9; break;
case 9: alignment = 4; break;
case 10: alignment = 5; break;
case 11: alignment = 6; break;
default: alignment = 2; break;
}
}
else alignment = align;
alignment = tkn.GetInt();
if (version == 0) alignment = AlignSSAtoASS(alignment);
// Read margins
for (int i=0;i<4;i++) {
if (i == 3 && version < 2) margin[i] = margin[i-1];
else {
margin[i] = tkn.GetInt();
}
else margin[i] = tkn.GetInt();
}
// Read and discard alpha level
if (version == 0) {
tkn.GetString();
}
// Read and discard alpha level on SSA
// TODO: do something with this?
if (version == 0) tkn.GetString();
// Read encoding
encoding = tkn.GetInt();
// Read relative to
if (version == 2) {
relativeTo = tkn.GetInt();
}
if (version == 2) relativeTo = tkn.GetInt();
// End
if (tkn.HasMore()) return false;
@ -162,3 +135,31 @@ bool StyleASS::Parse(String data,int version)
return false;
}
}
////////////////////////////////
// Convert SSA alignment to ASS
int StyleASS::AlignSSAtoASS(int align)
{
switch(align) {
case 1: return 1;
case 2: return 2;
case 3: return 3;
case 5: return 7;
case 6: return 8;
case 7: return 9;
case 9: return 4;
case 10: return 5;
case 11: return 6;
default: return 2;
}
}
////////////////////////////////
// Convert ASS alignment to SSA
int StyleASS::AlignASStoSSA(int assAlignment)
{
// TODO
return assAlignment;
}

View file

@ -84,9 +84,9 @@ Manipulator Model::CreateAntiManipulator(const Manipulator &src)
}
///////////////
// Load a file
void Model::LoadFile(wxInputStream &input,const Format *format,const String encoding)
//////////////////
// Load subtitles
void Model::Load(wxInputStream &input,const Format *format,const String encoding)
{
// Autodetect format
if (format == NULL) {
@ -109,8 +109,8 @@ void Model::LoadFile(wxInputStream &input,const Format *format,const String enco
//////////////////
// Save to a file
void Model::SaveFile(wxOutputStream &output,const Format *format,const String encoding)
// Save subtitles
void Model::Save(wxOutputStream &output,const Format *format,const String encoding)
{
(void) output;
(void) format;
@ -119,6 +119,26 @@ void Model::SaveFile(wxOutputStream &output,const Format *format,const String en
}
///////////////
// Load a file
void Model::LoadFile(const String filename,const String encoding)
{
const Format *handler = FormatManager::GetFormatFromFilename(filename,true);
wxFileInputStream stream(filename);
Load(stream,handler,encoding);
}
///////////////
// Save a file
void Model::SaveFile(const String filename,const String encoding)
{
const Format *handler = FormatManager::GetFormatFromFilename(filename,true);
wxFileOutputStream stream(filename);
Save(stream,handler,encoding);
}
//////////////////
// Gets a section
Section* Model::GetSection(String name) const

View file

@ -68,9 +68,13 @@ int Tokenizer::GetPosition()
/////////////
// Get token
String Tokenizer::GetString()
String Tokenizer::GetString(bool trim)
{
return tkn->GetNextToken();
wxString str = tkn->GetNextToken();
if (trim) {
str.Trim(true).Trim(false);
}
return str;
}
int Tokenizer::GetInt()
{

View file

@ -54,9 +54,7 @@ int main () {
// Load subtitles
cout << "Loading file... ";
String filename = L"subs_in.ass";
const Format *handler = FormatManager::GetFormatFromFilename(filename,true);
subs.LoadFile(wxFileInputStream(filename),handler,L"UTF-8");
subs.LoadFile(L"subs_in.ass",L"UTF-8");
cout << "Done.\n";
// Modify subtitles
@ -65,9 +63,7 @@ int main () {
// Save subtitles
cout << "Saving file... ";
filename = L"subs_out.ass";
handler = FormatManager::GetFormatFromFilename(filename,false);
subs.SaveFile(wxFileOutputStream(filename),handler);
subs.SaveFile(L"subs_out.ass",L"UTF-8");
cout << "Done.\n";
}

View file

@ -46,7 +46,7 @@
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
WarningLevel="4"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="4"
DisableSpecificWarnings="4996"
@ -121,7 +121,7 @@
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
UsePrecompiledHeader="0"
WarningLevel="3"
WarningLevel="4"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
DisableSpecificWarnings="4996"