Make AssStyle::Clone ~15 times faster.

Originally committed to SVN as r4664.
This commit is contained in:
Thomas Goyne 2010-07-08 07:14:55 +00:00
parent a59d2a8e2e
commit 1da57a4be5
8 changed files with 116 additions and 202 deletions

View File

@ -214,7 +214,7 @@ bool AssDialogue::Parse(wxString rawData, int version) {
return true;
}
wxString AssDialogue::GetData(bool ssa) {
wxString AssDialogue::GetData(bool ssa) const {
wxString final;
if (Comment) final = L"Comment: ";
@ -228,10 +228,13 @@ wxString AssDialogue::GetData(bool ssa) {
final += Start.GetASSFormated() + L",";
final += End.GetASSFormated() + L",";
Style.Replace(L",",L";");
Actor.Replace(L",",L";");
final += Style + L",";
final += Actor + L",";
wxString s = Style;
wxString a = Actor;
wxString e = Effect;
s.Replace(L",",L";");
a.Replace(L",",L";");
final += s + L",";
final += a + L",";
final += GetMarginString(0);
final += L",";
@ -240,8 +243,8 @@ wxString AssDialogue::GetData(bool ssa) {
final += GetMarginString(2);
final += L",";
Effect.Replace(L",",L";");
final += Effect + L",";
e.Replace(L",",L";");
final += e + L",";
final += Text;
// Make sure that final has no line breaks
@ -251,11 +254,11 @@ wxString AssDialogue::GetData(bool ssa) {
return final;
}
const wxString AssDialogue::GetEntryData() {
const wxString AssDialogue::GetEntryData() const {
return GetData(false);
}
wxString AssDialogue::GetSSAText () {
wxString AssDialogue::GetSSAText () const {
return GetData(true);
}
@ -643,7 +646,7 @@ void AssDialogue::SetMarginString(const wxString origvalue,int which) {
Margin[which] = value;
}
wxString AssDialogue::GetMarginString(int which,bool pad) {
wxString AssDialogue::GetMarginString(int which,bool pad) const {
if (which < 0 || which >= 4) throw Aegisub::InvalidMarginIdError();
int value = Margin[which];
if (pad) return wxString::Format(_T("%04i"),value);

View File

@ -163,7 +163,7 @@ public:
///
/// DOCME
class AssDialogue : public AssEntry {
wxString GetData(bool ssa);
wxString GetData(bool ssa) const;
public:
/// Contains information about each block of text
@ -188,7 +188,7 @@ public:
/// Raw text data
wxString Text;
ASS_EntryType GetType() { return ENTRY_DIALOGUE; }
ASS_EntryType GetType() const { return ENTRY_DIALOGUE; }
/// @brief Parse raw ASS data into everything else
/// @param data ASS line
@ -217,7 +217,7 @@ public:
/// If blocks have been parsed, update the text from their current value
void UpdateText();
const wxString GetEntryData();
const wxString GetEntryData() const;
/// Do nothing
void SetEntryData(wxString) { }
/// Synonym for ClearBlocks
@ -231,9 +231,9 @@ public:
/// @brief Get a margin
/// @param which 0 = left, 1 = right, 2 = vertical/top, 3 = bottom
/// @param pad Pad the number to four digits
wxString GetMarginString(int which,bool pad=true);
wxString GetMarginString(int which,bool pad=true) const;
/// Get the line as SSA rather than ASS
wxString GetSSAText();
wxString GetSSAText() const;
/// Does this line collide with the passed line?
bool CollidesWith(AssDialogue *target);

View File

@ -66,7 +66,7 @@ AssEntry::~AssEntry() {
/// @brief Get SSA conversion
/// @return
///
wxString AssEntry::GetSSAText() {
wxString AssEntry::GetSSAText() const {
// Special cases
if (data.Lower() == _T("[v4+ styles]")) return wxString(_T("[V4 Styles]"));
if (data.Lower() == _T("scripttype: v4.00+")) return wxString(_T("ScriptType: v4.00"));

View File

@ -125,17 +125,17 @@ public:
/// @brief DOCME
/// @return
///
virtual ASS_EntryType GetType() { return ENTRY_BASE; }
virtual ASS_EntryType GetType() const { return ENTRY_BASE; }
/// @brief DOCME
/// @return
///
virtual const wxString GetEntryData() { return data; }
virtual const wxString GetEntryData() const { return data; }
/// @brief DOCME
/// @param newData
///
virtual void SetEntryData(wxString newData) { if (newData.IsEmpty()) data.Clear(); else data = newData; }
virtual void SetEntryData(wxString newData) { data = newData; }
virtual wxString GetSSAText();
virtual wxString GetSSAText() const;
};

View File

@ -34,8 +34,6 @@
/// @ingroup subs_storage
///
////////////
// Includes
#include "config.h"
#ifndef AGI_PRE
@ -48,27 +46,26 @@
#include "ass_style.h"
#include "utils.h"
/// @brief Constructors AssColor //////////////////////////
///
AssColor::AssColor () {
r=g=b=a=0;
}
AssColor::AssColor(int r, int g, int b, int a)
: r(r)
, g(g)
, b(b)
, a(a)
{
}
/// @brief DOCME
/// @param color
///
AssColor::AssColor (wxColour &color) {
AssColor::AssColor (const wxColour &color) {
SetWXColor(color);
}
/// @brief Parse from SSA/ASS
/// @param value
/// @return
///
void AssColor::Parse(const wxString value) {
if (value.Len() > 0 && value[0] == _T('#')) {
// HTML colour
@ -101,20 +98,14 @@ void AssColor::Parse(const wxString value) {
a = (outval>>24)& 0xFF;
}
/// @brief Gets a wxColour
/// @return
///
wxColour AssColor::GetWXColor() {
return wxColour(r,g,b,255-a);
}
/// @brief Sets color from wx
/// @param color
///
void AssColor::SetWXColor(const wxColor &color) {
r = color.Red();
g = color.Green();
@ -122,15 +113,12 @@ void AssColor::SetWXColor(const wxColor &color) {
//a = color.Alpha();
}
/// @brief Get formatted in ASS format
/// @param alpha
/// @param stripped
/// @param isStyle
/// @return
///
wxString AssColor::GetASSFormatted (bool alpha,bool stripped,bool isStyle) {
wxString AssColor::GetASSFormatted(bool alpha,bool stripped,bool isStyle) const {
wxString work;
if (!stripped) work += _T("&H");
if (alpha) work += wxString::Format(_T("%02X"),a);
@ -139,93 +127,80 @@ wxString AssColor::GetASSFormatted (bool alpha,bool stripped,bool isStyle) {
return work;
}
/// @brief Get decimal formatted
/// @return
///
wxString AssColor::GetSSAFormatted () {
wxString AssColor::GetSSAFormatted() const {
long color = (a<<24)+(b<<16)+(g<<8)+r;
wxString output=wxString::Format(_T("%i"),(long)color);
return output;
}
/// @brief Operators
/// @param col
/// @return
///
bool AssColor::operator==(AssColor &col) const {
bool AssColor::operator==(const AssColor &col) const {
return r==col.r && g==col.g && b==col.b && a==col.a;
}
/// @brief DOCME
/// @param col
/// @return
///
bool AssColor::operator!=(AssColor &col) const {
return r!=col.r || g!=col.g || b!=col.b || a!=col.a;
bool AssColor::operator!=(const AssColor &col) const {
return !(*this == col);
}
/// @brief Default Constructor AssStyle /////////////////////////
///
AssStyle::AssStyle() {
group = _T("[V4+ Styles]");
name = _T("Default");
font = _T("Arial");
fontsize = 20;
primary.r = 255;
primary.g = 255;
primary.b = 255;
primary.a = 0;
secondary.r = 255;
secondary.g = 0;
secondary.b = 0;
secondary.a = 0;
outline.r = 0;
outline.g = 0;
outline.b = 0;
outline.a = 0;
shadow.r = 0;
shadow.g = 0;
shadow.b = 0;
shadow.a = 0;
bold = false;
italic = false;
underline = false;
strikeout = false;
scalex = 100;
scaley = 100;
spacing = 0;
angle = 0.0;
borderstyle = 1;
outline_w = 2.0;
shadow_w = 2.0;
alignment = 2;
Margin[0] = 10;
Margin[1] = 10;
Margin[2] = 10;
Margin[3] = 10;
encoding = 1;
relativeTo = 1;
AssStyle::AssStyle()
: name(L"Default")
, font(L"Arial")
, fontsize(20.)
, primary(255, 255, 255)
, secondary(255, 0, 0)
, outline(0, 0, 0)
, shadow(0, 0, 0)
, bold(false)
, italic(false)
, underline(false)
, strikeout(false)
, scalex(100.)
, scaley(100.)
, spacing(0.)
, angle(0.)
, borderstyle(1)
, outline_w(2.)
, shadow_w(2.)
, alignment(2)
, encoding(1)
, relativeTo(1)
{
group = L"[V4+ Styles]";
for (int i = 0; i < 4; i++)
Margin[i] = 10;
UpdateData();
}
AssStyle::AssStyle(const AssStyle& s)
: name(s.name)
, font(s.font)
, fontsize(s.fontsize)
, primary(s.primary)
, secondary(s.secondary)
, outline(s.outline)
, shadow(s.shadow)
, bold(s.bold)
, italic(s.italic)
, underline(s.underline)
, strikeout(s.strikeout)
, scalex(s.scalex)
, scaley(s.scaley)
, spacing(s.spacing)
, angle(s.angle)
, borderstyle(s.borderstyle)
, outline_w(s.outline_w)
, shadow_w(s.outline_w)
, alignment(s.alignment)
, encoding(s.encoding)
, relativeTo(s.relativeTo)
{
group = L"[V4+ Styles]";
memcpy(Margin, s.Margin, sizeof(Margin));
SetEntryData(s.GetEntryData());
}
/// @brief Constructor
/// @param _data
/// @param version
///
AssStyle::AssStyle(wxString _data,int version) {
Valid = Parse(_data,version);
if (!Valid) {
@ -234,15 +209,9 @@ AssStyle::AssStyle(wxString _data,int version) {
UpdateData();
}
/// @brief Destructor
///
AssStyle::~AssStyle() {
}
/// @brief Parses value from ASS data
/// @param rawData
/// @param version
@ -449,8 +418,6 @@ bool AssStyle::Parse(wxString rawData,int version) {
return true;
}
/// @brief Writes data back to ASS (v4+) format
///
void AssStyle::UpdateData() {
@ -475,8 +442,6 @@ void AssStyle::UpdateData() {
SetEntryData(final);
}
/// @brief Sets margin from a string
/// @param str
/// @param which
@ -492,24 +457,20 @@ void AssStyle::SetMarginString(const wxString str,int which) {
Margin[which] = value;
}
/// @brief Gets string for margin
/// @param which
/// @return
///
wxString AssStyle::GetMarginString(int which) {
wxString AssStyle::GetMarginString(int which) const {
if (which < 0 || which >= 4) throw Aegisub::InvalidMarginIdError();
wxString result = wxString::Format(_T("%04i"),Margin[which]);
return result;
}
/// @brief Convert style to SSA string
/// @return
///
wxString AssStyle::GetSSAText() {
wxString AssStyle::GetSSAText() const {
wxString output;
int align = 0;
switch (alignment) {
@ -523,11 +484,13 @@ wxString AssStyle::GetSSAText() {
case 8: align = 6; break;
case 9: align = 7; break;
}
name.Replace(_T(","),_T(";"));
font.Replace(_T(","),_T(";"));
wxString n = name;
n.Replace(L",", L";");
wxString f = font;
f.Replace(L",", L";");
output = wxString::Format(_T("Style: %s,%s,%g,%s,%s,0,%s,%d,%d,%d,%g,%g,%d,%d,%d,%d,0,%i"),
name.c_str(), font.c_str(), fontsize,
n.c_str(), f.c_str(), fontsize,
primary.GetSSAFormatted().c_str(),
secondary.GetSSAFormatted().c_str(),
shadow.GetSSAFormatted().c_str(),
@ -538,47 +501,11 @@ wxString AssStyle::GetSSAText() {
return output;
}
/// @brief Clone
/// @return
///
AssEntry *AssStyle::Clone() const {
// Create clone
AssStyle *final = new AssStyle();
// Copy data
final->group = group;
final->Valid = Valid;
final->alignment = alignment;
final->angle = angle;
final->bold = bold;
final->borderstyle = borderstyle;
final->encoding = encoding;
final->font = font;
final->fontsize = fontsize;
final->italic = italic;
final->Margin[0] = Margin[0];
final->Margin[1] = Margin[1];
final->Margin[2] = Margin[2];
final->Margin[3] = Margin[3];
final->name = name;
final->outline = outline;
final->outline_w = outline_w;
final->primary = primary;
final->scalex = scalex;
final->scaley = scaley;
final->secondary = secondary;
final->shadow = shadow;
final->shadow_w = shadow_w;
final->spacing = spacing;
final->strikeout = strikeout;
final->underline = underline;
final->relativeTo = relativeTo;
final->SetEntryData(const_cast<AssStyle*>(this)->GetEntryData());
// Return
return final;
return new AssStyle(*this);
}
@ -587,7 +514,7 @@ AssEntry *AssStyle::Clone() const {
/// @param style
/// @return
///
bool AssStyle::IsEqualTo(AssStyle *style) {
bool AssStyle::IsEqualTo(AssStyle *style) const {
// memcmp won't work because strings won't match
if (style->alignment != alignment ||
style->angle != angle ||

View File

@ -34,53 +34,37 @@
/// @ingroup subs_storage
///
///////////
// Headers
#ifndef AGI_PRE
#include <wx/colour.h>
#endif
#include "ass_entry.h"
/// DOCME
/// @class AssColor
/// @brief DOCME
///
/// DOCME
class AssColor {
public:
/// DOCME
int r; // Red component
/// DOCME
int g; // Green component
/// DOCME
int b; // Blue component
/// DOCME
int a; // Alpha component
struct AssColor {
int r; ///< Red component
int g; ///< Green component
int b; ///< Blue component
int a; ///< Alpha component
AssColor();
AssColor(wxColour &color);
AssColor(int r, int g, int b, int a = 0);
AssColor(const wxColour &color);
bool operator==(AssColor &col) const;
bool operator!=(AssColor &col) const;
bool operator==(const AssColor &col) const;
bool operator!=(const AssColor &col) const;
wxColor GetWXColor(); // Return as a wxColor
void SetWXColor(const wxColor &color); // Sets from a wxColor
void Parse(const wxString value); // Parse SSA or ASS-style color
wxString GetASSFormatted(bool alpha,bool stripped=false,bool isStyle=false); // Gets color formated in ASS format
wxString GetSSAFormatted();
wxString GetASSFormatted(bool alpha,bool stripped=false,bool isStyle=false) const; // Gets color formated in ASS format
wxString GetSSAFormatted() const;
};
/// DOCME
/// @class AssStyle
/// @brief DOCME
@ -88,7 +72,6 @@ public:
/// DOCME
class AssStyle : public AssEntry {
public:
/// DOCME
wxString name;
@ -161,20 +144,21 @@ public:
/// @brief DOCME
///
ASS_EntryType GetType() { return ENTRY_STYLE; }
ASS_EntryType GetType() const { return ENTRY_STYLE; }
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 GetSSAText() const; // 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) const; // Returns the margin value as a string (0 = left, 1 = right, 2 = vertical/top, 3 = bottom)
void SetMarginString(const wxString value,int which); // Sets margin value from a string (0 = left, 1 = right, 2 = vertical/top, 3 = bottom)
static void GetEncodings(wxArrayString &encodingStrings);
AssEntry *Clone() const;
bool IsEqualTo(AssStyle *style);
bool IsEqualTo(AssStyle *style) const;
AssStyle();
AssStyle(AssStyle const&);
AssStyle(wxString data,int version=1);
~AssStyle();
};

View File

@ -171,7 +171,7 @@ void AssTime::SetMS (int _ms) {
/// @param msPrecision
/// @return
///
wxString AssTime::GetASSFormated (bool msPrecision) {
wxString AssTime::GetASSFormated (bool msPrecision) const {
int h,m,s,ms;
int _ms = time;

View File

@ -76,7 +76,7 @@ public:
void SetMS(int ms); // Sets values to miliseconds
void ParseASS(const wxString text); // Sets value to text-form time, in ASS format
void ParseSRT(const wxString text); // Sets value to text-form time, in SRT format
wxString GetASSFormated(bool ms=false); // Returns the ASS representation of time
wxString GetASSFormated(bool ms=false) const; // Returns the ASS representation of time
wxString GetSRTFormated(); // Returns the SRT representation of time
};