ass_style improvements - ParseASS and ParseSSA are now just Parse in AssColor

Originally committed to SVN as r829.
This commit is contained in:
Dan Donovan 2007-01-18 06:45:55 +00:00
parent 27a9927cf2
commit 7b78f6e37c
5 changed files with 86 additions and 148 deletions

View file

@ -33,7 +33,6 @@
// Contact: mailto:zeratul@cellosoft.com // Contact: mailto:zeratul@cellosoft.com
// //
//////////// ////////////
// Includes // Includes
#include <wx/tokenzr.h> #include <wx/tokenzr.h>
@ -54,65 +53,40 @@ AssColor::AssColor (wxColour &color) {
////////////////// //////////////////
// Parse from ASS // Parse from SSA/ASS
void AssColor::ParseASS (const wxString _value) { void AssColor::Parse(const wxString value) {
// Prepare // Prepare
wxString value = _value; char c,ostr[12];
value.Trim(false); unsigned long outval;
value.Trim(true); int oindex=11;
value.UpperCase(); bool ishex=false,isneg=false;
// Remove leading and ending crap ostr[11]=0;
if (value.Left(1) == _T("&")) value = value.Mid(1);
if (value.Left(1) == _T("H")) value = value.Mid(1);
if (value.Right(1) == _T("&")) value = value.Left(value.Length()-1);
// Read colours for(int i=value.Len()-1;i>=0&&oindex>=0;i--) {
long temp[4] = { 0, 0, 0, 0 }; c=value[i];
bool ok; if ((c >= 48 && c <= 57) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102)) {
for (int i=0;i<4;i++) { ostr[--oindex] = c;
if (value.Length() > 0) { if (c>=65) ishex = true;
ok = value.Right(2).ToLong(&temp[i],16); }
if (!ok) temp[i] = 0; else if (c == 'H' || c == 'h') ishex = true;
value.Truncate(value.Length()-2); else if (c==45) isneg=true;
}
else break;
} }
outval=strtoul(ostr+oindex,0,ishex?16:10);
if (isneg) outval+=2147483648; //2^31 (MSB)
// Copy r = outval & 0xFF;
r = temp[0]; g = (outval>>8) & 0xFF;
g = temp[1]; b = (outval>>16)& 0xFF;
b = temp[2]; a = (outval>>24)& 0xFF;
a = temp[3];
}
//////////////////
// Parse from SSA
void AssColor::ParseSSA (wxString value) {
value.Trim(true);
value.Trim(false);
// Check if the moron who wrote it used ASS style in SSA
if (value.Left(2) == _T("&H")) {
ParseASS(value);
return;
}
// Parse SSA
long val;
value.ToLong(&val);
b = (val >> 16) & 0xFF;
g = (val >> 8) & 0xFF;
r = val & 0xFF;
a = 0;
} }
/////////////////// ///////////////////
// Gets a wxColour // Gets a wxColour
wxColour AssColor::GetWXColor() { wxColour AssColor::GetWXColor() {
return wxColour(r,g,b); return wxColour(r,g,b,a);
} }
@ -122,6 +96,7 @@ void AssColor::SetWXColor(const wxColor &color) {
r = color.Red(); r = color.Red();
g = color.Green(); g = color.Green();
b = color.Blue(); b = color.Blue();
a = color.Alpha();
} }
@ -140,10 +115,14 @@ wxString AssColor::GetASSFormatted (bool alpha,bool stripped,bool isStyle) {
///////////////////////// /////////////////////////
// Get decimal formatted // Get decimal formatted
wxString AssColor::GetSSAFormatted () { wxString AssColor::GetSSAFormatted () {
return wxString::Format(_T("%i"),(b<<16)+(g<<8)+r); long color = ((a&127)<<24)+(b<<16)+(g<<8)+r;
if ((a&128)!=0) color = 0-color;
wxString output=wxString::Format(_T("%i"),(long)color);
return output;
} }
///////////////////////// AssStyle ///////////////////////// ///////////////////////// AssStyle /////////////////////////
/////////////////////// ///////////////////////
// Default Constructor // Default Constructor
@ -241,29 +220,29 @@ bool AssStyle::Parse(wxString rawData,int version) {
if (version != 0) { if (version != 0) {
// Read primary color // Read primary color
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
primary.ParseASS(tkn.GetNextToken()); primary.Parse(tkn.GetNextToken());
// Read secondary color // Read secondary color
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
secondary.ParseASS(tkn.GetNextToken()); secondary.Parse(tkn.GetNextToken());
// Read outline color // Read outline color
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
outline.ParseASS(tkn.GetNextToken()); outline.Parse(tkn.GetNextToken());
// Read shadow color // Read shadow color
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
shadow.ParseASS(tkn.GetNextToken()); shadow.Parse(tkn.GetNextToken());
} }
else { else {
// Read primary color // Read primary color
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
primary.ParseSSA(tkn.GetNextToken()); primary.Parse(tkn.GetNextToken());
// Read secondary color // Read secondary color
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
secondary.ParseSSA(tkn.GetNextToken()); secondary.Parse(tkn.GetNextToken());
// Read and discard tertiary color // Read and discard tertiary color
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
@ -271,7 +250,7 @@ bool AssStyle::Parse(wxString rawData,int version) {
// Read shadow/outline color // Read shadow/outline color
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
outline.ParseSSA(tkn.GetNextToken()); outline.Parse(tkn.GetNextToken());
shadow = outline; shadow = outline;
} }
@ -279,30 +258,25 @@ bool AssStyle::Parse(wxString rawData,int version) {
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken(); temp = tkn.GetNextToken();
temp.ToLong(&templ); temp.ToLong(&templ);
bold = true; bold = (templ==0)?false:true;
if (templ == 0) bold = false;
// Read italics // Read italics
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken(); temp.ToLong(&templ); temp = tkn.GetNextToken(); temp.ToLong(&templ);
italic = true; italic = (templ==0)?false:true;
if (templ == 0) italic = false;
if (version != 0) { if (version != 0) {
// Read underline // Read underline
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken(); temp = tkn.GetNextToken();
temp.ToLong(&templ); temp.ToLong(&templ);
underline = true; underline = (templ==0)?false:true;
if (templ == 0) underline = false;
// Read strikeout // Read strikeout
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken(); temp = tkn.GetNextToken();
temp.ToLong(&templ); temp.ToLong(&templ);
strikeout = true; strikeout = (templ==0)?false:true;
if (templ == 0) strikeout = false;
// Read scale x // Read scale x
if (!tkn.HasMoreTokens()) return false; if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken(); temp = tkn.GetNextToken();
@ -329,7 +303,7 @@ bool AssStyle::Parse(wxString rawData,int version) {
else { else {
// SSA defaults // SSA defaults
shadow.a = 128; //shadow.a = 128; //Parsed
underline = false; underline = false;
strikeout = false; strikeout = false;
@ -421,43 +395,27 @@ bool AssStyle::Parse(wxString rawData,int version) {
} }
////////////////////////////////// ///////////////////////////////////////
// Writes data back to ASS format // Writes data back to ASS (v4+) format
void AssStyle::UpdateData() { void AssStyle::UpdateData() {
// Prepare wxString final;
wxString final = _T("Style: ");
// Write all final
name.Replace(_T(","),_T(";")); name.Replace(_T(","),_T(";"));
font.Replace(_T(","),_T(";")); font.Replace(_T(","),_T(";"));
final += name + _T(",");
final += font + _T(",");
final += FloatToString(fontsize) + _T(",");
final += primary.GetASSFormatted(true,false,true) + _T(",");
final += secondary.GetASSFormatted(true,false,true) + _T(",");
final += outline.GetASSFormatted(true,false,true) + _T(",");
final += shadow.GetASSFormatted(true,false,true) + _T(",");
final += IntegerToString(bold?-1:0) + _T(","); final = wxString::Format(_T("Style: %s,%s,%.0f,%s,%s,%s,%s,%d,%d,%d,%d,%.0f,%.0f,%.0f,%.0f,%d,%.0f,%.0f,%i,%i,%i,%i,%i"),
final += IntegerToString(italic?-1:0) + _T(","); name, font, fontsize,
final += IntegerToString(underline?-1:0) + _T(","); primary.GetASSFormatted(true,false,true),
final += IntegerToString(strikeout?-1:0) + _T(","); secondary.GetASSFormatted(true,false,true),
outline.GetASSFormatted(true,false,true),
shadow.GetASSFormatted(true,false,true),
(bold? -1 : 0), (italic ? -1 : 0),
(underline?-1:0),(strikeout?-1:0),
scalex,scaley,spacing,angle,
borderstyle,outline_w,shadow_w,alignment,
Margin[0],Margin[1],Margin[2],encoding);
final += FloatToString(scalex) + _T(",");
final += FloatToString(scaley) + _T(",");
final += FloatToString(spacing) + _T(",");
final += FloatToString(angle) + _T(",");
final += IntegerToString(borderstyle) + _T(",");
final += FloatToString(outline_w) + _T(",");
final += FloatToString(shadow_w) + _T(",");
final += IntegerToString(alignment) + _T(",");
final += IntegerToString(Margin[0]) + _T(",");
final += IntegerToString(Margin[1]) + _T(",");
final += IntegerToString(Margin[2]) + _T(",");
final += IntegerToString(encoding);
SetEntryData(final); SetEntryData(final);
} }
@ -465,15 +423,13 @@ void AssStyle::UpdateData() {
///////////////////////////// /////////////////////////////
// Sets margin from a string // Sets margin from a string
void AssStyle::SetMarginString(const wxString str,int which) { void AssStyle::SetMarginString(const wxString str,int which) {
wxString work = str;
work.Trim(false);
work.Trim(true);
if (!work.IsNumber()) throw _T("Invalid margin value");
long value;
work.ToLong(&value);
if (value < 0) value = 0;
if (value > 9999) value = 9999;
if (which < 0 || which >= 4) throw _T("Invalid margin"); if (which < 0 || which >= 4) throw _T("Invalid margin");
if (!str.IsNumber()) throw _T("Invalid margin value");
long value;
str.ToLong(&value);
if (value < 0) value = 0;
else if (value > 9999) value = 9999;
Margin[which] = value; Margin[which] = value;
} }
@ -481,10 +437,8 @@ void AssStyle::SetMarginString(const wxString str,int which) {
////////////////////////// //////////////////////////
// Gets string for margin // Gets string for margin
wxString AssStyle::GetMarginString(int which) { wxString AssStyle::GetMarginString(int which) {
int value;
if (which < 0 || which >= 4) throw _T("Invalid margin"); if (which < 0 || which >= 4) throw _T("Invalid margin");
value = Margin[which]; wxString result = wxString::Format(_T("%04i"),Margin[which]);
wxString result = wxString::Format(_T("%04i"),value);
return result; return result;
} }
@ -492,28 +446,7 @@ wxString AssStyle::GetMarginString(int which) {
/////////////////////////////// ///////////////////////////////
// Convert style to SSA string // Convert style to SSA string
wxString AssStyle::GetSSAText() { wxString AssStyle::GetSSAText() {
// Prepare wxString output;
wxString output = _T("Style: ");
// Write all data
name.Replace(_T(","),_T(";"));
font.Replace(_T(","),_T(";"));
output += name + _T(",");
output += font + _T(",");
output += FloatToString(fontsize) + _T(",");
output += primary.GetSSAFormatted() + _T(",");
output += secondary.GetSSAFormatted() + _T(",");
output += _T("0,");
output += shadow.GetSSAFormatted() + _T(",");
output += IntegerToString(bold?-1:0) + _T(",");
output += IntegerToString(italic?-1:0) + _T(",");
output += IntegerToString(borderstyle) + _T(",");
output += FloatToString(outline_w) + _T(",");
output += FloatToString(shadow_w) + _T(",");
int align = 0; int align = 0;
switch (alignment) { switch (alignment) {
case 1: align = 1; break; case 1: align = 1; break;
@ -526,13 +459,17 @@ wxString AssStyle::GetSSAText() {
case 8: align = 6; break; case 8: align = 6; break;
case 9: align = 7; break; case 9: align = 7; break;
} }
output += IntegerToString(align) + _T(","); name.Replace(_T(","),_T(";"));
font.Replace(_T(","),_T(";"));
output += IntegerToString(Margin[0]) + _T(","); output = wxString::Format(_T("Style: %s,%s,%.0f,%s,%s,0,%s,%d,%d,%d,%.0f,%.0f,%d,%d,%d,%d,0,%i"),
output += IntegerToString(Margin[1]) + _T(","); name, font, fontsize,
output += IntegerToString(Margin[2]) + _T(","); primary.GetSSAFormatted(),
output += _T("0,"); secondary.GetSSAFormatted(),
output += IntegerToString(encoding); shadow.GetSSAFormatted(),
(bold? -1 : 0), (italic ? -1 : 0),
borderstyle,outline_w,shadow_w,align,
Margin[0],Margin[1],Margin[2],encoding);
return output; return output;
} }
@ -556,7 +493,10 @@ AssEntry *AssStyle::Clone() {
final->font = font; final->font = font;
final->fontsize = fontsize; final->fontsize = fontsize;
final->italic = italic; final->italic = italic;
for (int i=0;i<4;i++) final->Margin[i] = Margin[i]; final->Margin[0] = Margin[0];
final->Margin[1] = Margin[1];
final->Margin[2] = Margin[2];
final->Margin[3] = Margin[3];
final->name = name; final->name = name;
final->outline = outline; final->outline = outline;
final->outline_w = outline_w; final->outline_w = outline_w;

View file

@ -36,7 +36,6 @@
#pragma once #pragma once
/////////// ///////////
// Headers // Headers
#include "ass_entry.h" #include "ass_entry.h"
@ -56,8 +55,7 @@ public:
wxColor GetWXColor(); // Return as a wxColor wxColor GetWXColor(); // Return as a wxColor
void SetWXColor(const wxColor &color); // Sets from a wxColor void SetWXColor(const wxColor &color); // Sets from a wxColor
void ParseASS(const wxString value); // Parse ASS-style color void Parse(const wxString value); // Parse SSA or ASS-style color
void ParseSSA(const wxString value); // Parse SSA-style color
wxString GetASSFormatted(bool alpha,bool stripped=false,bool isStyle=false); // Gets color formated in ASS format wxString GetASSFormatted(bool alpha,bool stripped=false,bool isStyle=false); // Gets color formated in ASS format
wxString GetSSAFormatted(); wxString GetSSAFormatted();
}; };

View file

@ -337,10 +337,10 @@ namespace Automation4 {
sty->name = name; sty->name = name;
sty->font = fontname; sty->font = fontname;
sty->fontsize = fontsize; sty->fontsize = fontsize;
sty->primary.ParseASS(color1); sty->primary.Parse(color1);
sty->secondary.ParseASS(color2); sty->secondary.Parse(color2);
sty->outline.ParseASS(color3); sty->outline.Parse(color3);
sty->shadow.ParseASS(color4); sty->shadow.Parse(color4);
sty->bold = bold; sty->bold = bold;
sty->italic = italic; sty->italic = italic;
sty->underline = underline; sty->underline = underline;

View file

@ -165,7 +165,7 @@ void ColorPickerRecent::LoadFromString(const wxString &recent_string)
wxStringTokenizer toker(recent_string, _T(" "), false); wxStringTokenizer toker(recent_string, _T(" "), false);
while (toker.HasMoreTokens()) { while (toker.HasMoreTokens()) {
AssColor color; AssColor color;
color.ParseASS(toker.NextToken()); color.Parse(toker.NextToken());
colors.push_back(color.GetWXColor()); colors.push_back(color.GetWXColor());
} }
while ((int)colors.size() < rows*cols) { while ((int)colors.size() < rows*cols) {
@ -695,7 +695,7 @@ void DialogColorPicker::UpdateFromASS()
unsigned char r, g, b, h, s, l, h2, s2, v2; unsigned char r, g, b, h, s, l, h2, s2, v2;
AssColor ass; AssColor ass;
ass.ParseASS(ass_input->GetValue()); ass.Parse(ass_input->GetValue());
r = ass.r; r = ass.r;
g = ass.g; g = ass.g;
b = ass.b; b = ass.b;
@ -1106,7 +1106,7 @@ void DialogColorPicker::OnSliderChange(wxCommandEvent &evt)
void DialogColorPicker::OnRecentSelect(wxCommandEvent &evt) void DialogColorPicker::OnRecentSelect(wxCommandEvent &evt)
{ {
AssColor color; AssColor color;
color.ParseASS(evt.GetString()); color.Parse(evt.GetString());
SetColor(color.GetWXColor()); SetColor(color.GetWXColor());
} }

View file

@ -204,7 +204,7 @@ wxColour VariableData::AsColour() const {
if (type == VARDATA_COLOUR) return *value_colour; if (type == VARDATA_COLOUR) return *value_colour;
else if (type == VARDATA_TEXT) { else if (type == VARDATA_TEXT) {
AssColor color; AssColor color;
color.ParseASS(*value_text); color.Parse(*value_text);
return color.GetWXColor(); return color.GetWXColor();
} }
else throw _T("Wrong parameter type, should be colour"); else throw _T("Wrong parameter type, should be colour");