2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file ass_style.cpp
|
|
|
|
/// @brief Class for style definitions in subtitles
|
|
|
|
/// @ingroup subs_storage
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-04-04 22:41:09 +02:00
|
|
|
#include "ass_style.h"
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2012-10-25 15:45:06 +02:00
|
|
|
#include "subtitle_format.h"
|
2006-04-04 22:41:09 +02:00
|
|
|
#include "utils.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2014-05-29 00:19:05 +02:00
|
|
|
#include <libaegisub/format.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/split.h>
|
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <wx/intl.h>
|
|
|
|
|
2013-12-12 01:29:48 +01:00
|
|
|
AssStyle::AssStyle() {
|
2012-10-26 16:29:52 +02:00
|
|
|
std::fill(Margin.begin(), Margin.end(), 10);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
UpdateData();
|
|
|
|
}
|
|
|
|
|
2014-12-29 05:31:33 +01:00
|
|
|
AssEntryGroup AssStyle::Group() const { return AssEntryGroup::STYLE; }
|
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
namespace {
|
|
|
|
class parser {
|
2015-01-04 23:56:27 +01:00
|
|
|
agi::split_iterator<agi::StringRange::const_iterator> pos;
|
2012-12-30 01:32:36 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string next_tok() {
|
|
|
|
if (pos.eof())
|
2014-05-29 14:57:27 +02:00
|
|
|
throw SubtitleFormatParseError("Malformed style: not enough fields");
|
2013-01-04 16:01:50 +01:00
|
|
|
return agi::str(trim_copy(*pos++));
|
2012-12-30 01:32:36 +01:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
public:
|
2013-01-04 16:01:50 +01:00
|
|
|
parser(std::string const& str) {
|
|
|
|
auto colon = find(str.begin(), str.end(), ':');
|
|
|
|
if (colon != str.end())
|
|
|
|
pos = agi::Split(agi::StringRange(colon + 1, str.end()), ',');
|
2012-12-30 01:32:36 +01:00
|
|
|
}
|
|
|
|
|
2013-01-02 05:54:50 +01:00
|
|
|
void check_done() const {
|
2013-01-04 16:01:50 +01:00
|
|
|
if (!pos.eof())
|
2014-05-29 14:57:27 +02:00
|
|
|
throw SubtitleFormatParseError("Malformed style: too many fields");
|
2012-12-30 01:32:36 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string next_str() { return next_tok(); }
|
|
|
|
agi::Color next_color() { return next_tok(); }
|
2012-12-30 01:32:36 +01:00
|
|
|
|
|
|
|
int next_int() {
|
|
|
|
try {
|
|
|
|
return boost::lexical_cast<int>(next_tok());
|
|
|
|
}
|
|
|
|
catch (boost::bad_lexical_cast const&) {
|
2014-05-29 14:57:27 +02:00
|
|
|
throw SubtitleFormatParseError("Malformed style: bad int field");
|
2012-12-30 01:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
double next_double() {
|
|
|
|
try {
|
|
|
|
return boost::lexical_cast<double>(next_tok());
|
|
|
|
}
|
|
|
|
catch (boost::bad_lexical_cast const&) {
|
2014-05-29 14:57:27 +02:00
|
|
|
throw SubtitleFormatParseError("Malformed style: bad double field");
|
2012-12-30 01:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void skip_token() {
|
2013-01-04 16:01:50 +01:00
|
|
|
if (!pos.eof())
|
|
|
|
++pos;
|
2012-12-30 01:32:36 +01:00
|
|
|
}
|
|
|
|
};
|
2011-12-22 22:15:19 +01:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
AssStyle::AssStyle(std::string const& str, int version) {
|
2013-01-02 05:54:50 +01:00
|
|
|
parser p(str);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
name = p.next_str();
|
|
|
|
font = p.next_str();
|
|
|
|
fontsize = p.next_double();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:15:19 +01:00
|
|
|
if (version != 0) {
|
2012-12-30 01:32:36 +01:00
|
|
|
primary = p.next_color();
|
|
|
|
secondary = p.next_color();
|
|
|
|
outline = p.next_color();
|
|
|
|
shadow = p.next_color();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-12-30 01:32:36 +01:00
|
|
|
primary = p.next_color();
|
|
|
|
secondary = p.next_color();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
// Skip tertiary color
|
|
|
|
p.skip_token();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Read shadow/outline color
|
2012-12-30 01:32:36 +01:00
|
|
|
outline = p.next_color();
|
2006-01-16 22:02:54 +01:00
|
|
|
shadow = outline;
|
|
|
|
}
|
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
bold = !!p.next_int();
|
|
|
|
italic = !!p.next_int();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2007-01-08 02:00:44 +01:00
|
|
|
if (version != 0) {
|
2012-12-30 01:32:36 +01:00
|
|
|
underline = !!p.next_int();
|
|
|
|
strikeout = !!p.next_int();
|
2011-12-22 22:15:19 +01:00
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
scalex = p.next_double();
|
|
|
|
scaley = p.next_double();
|
|
|
|
spacing = p.next_double();
|
|
|
|
angle = p.next_double();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// SSA defaults
|
|
|
|
underline = false;
|
|
|
|
strikeout = false;
|
|
|
|
|
|
|
|
scalex = 100;
|
|
|
|
scaley = 100;
|
|
|
|
spacing = 0;
|
|
|
|
angle = 0.0;
|
|
|
|
}
|
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
borderstyle = p.next_int();
|
|
|
|
outline_w = p.next_double();
|
|
|
|
shadow_w = p.next_double();
|
|
|
|
alignment = p.next_int();
|
2011-12-22 22:15:19 +01:00
|
|
|
|
2012-03-25 06:04:54 +02:00
|
|
|
if (version == 0)
|
|
|
|
alignment = SsaToAss(alignment);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
Margin[0] = mid(0, p.next_int(), 9999);
|
|
|
|
Margin[1] = mid(0, p.next_int(), 9999);
|
|
|
|
Margin[2] = mid(0, p.next_int(), 9999);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:15:19 +01:00
|
|
|
// Skip alpha level
|
|
|
|
if (version == 0)
|
2012-12-30 01:32:36 +01:00
|
|
|
p.skip_token();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
encoding = p.next_int();
|
2011-12-22 22:15:19 +01:00
|
|
|
|
2013-01-02 05:54:50 +01:00
|
|
|
p.check_done();
|
|
|
|
|
2011-12-22 22:15:19 +01:00
|
|
|
UpdateData();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AssStyle::UpdateData() {
|
2012-12-30 01:32:36 +01:00
|
|
|
replace(name.begin(), name.end(), ',', ';');
|
|
|
|
replace(font.begin(), font.end(), ',', ';');
|
2012-12-01 05:02:46 +01:00
|
|
|
|
2014-05-29 00:19:05 +02:00
|
|
|
data = agi::format("Style: %s,%s,%g,%s,%s,%s,%s,%d,%d,%d,%d,%g,%g,%g,%g,%d,%g,%g,%i,%i,%i,%i,%i",
|
|
|
|
name, font, fontsize,
|
|
|
|
primary.GetAssStyleFormatted(),
|
|
|
|
secondary.GetAssStyleFormatted(),
|
|
|
|
outline.GetAssStyleFormatted(),
|
|
|
|
shadow.GetAssStyleFormatted(),
|
|
|
|
(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);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2007-04-17 00:13:09 +02:00
|
|
|
void AssStyle::GetEncodings(wxArrayString &encodingStrings) {
|
|
|
|
encodingStrings.Clear();
|
2011-09-28 21:43:11 +02:00
|
|
|
encodingStrings.Add(wxString("0 - ") + _("ANSI"));
|
|
|
|
encodingStrings.Add(wxString("1 - ") + _("Default"));
|
|
|
|
encodingStrings.Add(wxString("2 - ") + _("Symbol"));
|
|
|
|
encodingStrings.Add(wxString("77 - ") + _("Mac"));
|
|
|
|
encodingStrings.Add(wxString("128 - ") + _("Shift_JIS"));
|
|
|
|
encodingStrings.Add(wxString("129 - ") + _("Hangeul"));
|
|
|
|
encodingStrings.Add(wxString("130 - ") + _("Johab"));
|
|
|
|
encodingStrings.Add(wxString("134 - ") + _("GB2312"));
|
|
|
|
encodingStrings.Add(wxString("136 - ") + _("Chinese BIG5"));
|
|
|
|
encodingStrings.Add(wxString("161 - ") + _("Greek"));
|
|
|
|
encodingStrings.Add(wxString("162 - ") + _("Turkish"));
|
|
|
|
encodingStrings.Add(wxString("163 - ") + _("Vietnamese"));
|
|
|
|
encodingStrings.Add(wxString("177 - ") + _("Hebrew"));
|
|
|
|
encodingStrings.Add(wxString("178 - ") + _("Arabic"));
|
|
|
|
encodingStrings.Add(wxString("186 - ") + _("Baltic"));
|
|
|
|
encodingStrings.Add(wxString("204 - ") + _("Russian"));
|
|
|
|
encodingStrings.Add(wxString("222 - ") + _("Thai"));
|
|
|
|
encodingStrings.Add(wxString("238 - ") + _("East European"));
|
|
|
|
encodingStrings.Add(wxString("255 - ") + _("OEM"));
|
2007-04-17 00:13:09 +02:00
|
|
|
}
|
2012-03-25 06:04:54 +02:00
|
|
|
|
|
|
|
int AssStyle::AssToSsa(int ass_align) {
|
|
|
|
switch (ass_align) {
|
|
|
|
case 1: return 1;
|
|
|
|
case 2: return 2;
|
|
|
|
case 3: return 3;
|
|
|
|
case 4: return 9;
|
|
|
|
case 5: return 10;
|
|
|
|
case 6: return 11;
|
|
|
|
case 7: return 5;
|
|
|
|
case 8: return 6;
|
|
|
|
case 9: return 7;
|
|
|
|
default: return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AssStyle::SsaToAss(int ssa_align) {
|
|
|
|
switch(ssa_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;
|
|
|
|
}
|
|
|
|
}
|