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_dialogue.cpp
|
|
|
|
/// @brief Class for dialogue lines in subtitles
|
|
|
|
/// @ingroup subs_storage
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2007-01-08 22:11:06 +01:00
|
|
|
#include "ass_dialogue.h"
|
2012-10-25 15:45:06 +02:00
|
|
|
#include "subtitle_format.h"
|
2007-01-08 22:11:06 +01:00
|
|
|
#include "utils.h"
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2012-12-02 19:24:49 +01:00
|
|
|
#include <libaegisub/of_type_adaptor.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/split.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2012-12-02 19:24:49 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2012-12-30 00:53:56 +01:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2012-12-30 00:53:56 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-04-22 19:21:00 +02:00
|
|
|
#include <boost/regex.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/spirit/include/karma_generate.hpp>
|
|
|
|
#include <boost/spirit/include/karma_int.hpp>
|
2012-12-30 00:53:56 +01:00
|
|
|
|
2012-12-07 17:06:03 +01:00
|
|
|
using namespace boost::adaptors;
|
|
|
|
|
2012-11-03 05:06:37 +01:00
|
|
|
static int next_id = 0;
|
|
|
|
|
2014-03-04 17:32:29 +01:00
|
|
|
AssDialogue::AssDialogue() {
|
|
|
|
Id = ++next_id;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2014-04-04 17:11:09 +02:00
|
|
|
AssDialogue::AssDialogue(AssDialogue const& that)
|
|
|
|
: AssDialogueBase(that)
|
|
|
|
, AssEntryListHook(that)
|
|
|
|
{
|
2014-03-04 17:32:29 +01:00
|
|
|
Id = ++next_id;
|
2010-06-22 02:03:11 +02:00
|
|
|
}
|
|
|
|
|
2014-03-05 02:36:02 +01:00
|
|
|
AssDialogue::AssDialogue(AssDialogueBase const& that) : AssDialogueBase(that) { }
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2014-03-04 17:32:29 +01:00
|
|
|
AssDialogue::AssDialogue(std::string const& data) {
|
|
|
|
Id = ++next_id;
|
|
|
|
Parse(data);
|
2006-02-21 04:13:35 +01:00
|
|
|
}
|
|
|
|
|
2014-03-04 17:32:29 +01:00
|
|
|
AssDialogue::~AssDialogue () { }
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
class tokenizer {
|
|
|
|
agi::StringRange str;
|
2015-01-04 23:56:27 +01:00
|
|
|
agi::split_iterator<agi::StringRange::const_iterator> pos;
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
tokenizer(agi::StringRange const& str) : str(str) , pos(agi::Split(str, ',')) { }
|
|
|
|
|
|
|
|
agi::StringRange next_tok() {
|
|
|
|
if (pos.eof())
|
2014-05-29 14:57:27 +02:00
|
|
|
throw SubtitleFormatParseError("Failed parsing line: " + std::string(str.begin(), str.end()));
|
2013-01-04 16:01:50 +01:00
|
|
|
return *pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string next_str() { return agi::str(next_tok()); }
|
|
|
|
std::string next_str_trim() { return agi::str(boost::trim_copy(next_tok())); }
|
|
|
|
};
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void AssDialogue::Parse(std::string const& raw) {
|
|
|
|
agi::StringRange str;
|
|
|
|
if (boost::starts_with(raw, "Dialogue:")) {
|
2006-01-16 22:02:54 +01:00
|
|
|
Comment = false;
|
2013-01-04 16:01:50 +01:00
|
|
|
str = agi::StringRange(raw.begin() + 10, raw.end());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else if (boost::starts_with(raw, "Comment:")) {
|
2006-01-16 22:02:54 +01:00
|
|
|
Comment = true;
|
2013-01-04 16:01:50 +01:00
|
|
|
str = agi::StringRange(raw.begin() + 9, raw.end());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else
|
2014-05-29 14:57:27 +02:00
|
|
|
throw SubtitleFormatParseError("Failed parsing line: " + raw);
|
2007-06-03 03:54:39 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
tokenizer tkn(str);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2007-01-08 02:54:02 +01:00
|
|
|
// Get first token and see if it has "Marked=" in it
|
2013-01-04 16:01:50 +01:00
|
|
|
auto tmp = tkn.next_str_trim();
|
|
|
|
bool ssa = boost::istarts_with(tmp, "marked=");
|
2007-01-08 02:54:02 +01:00
|
|
|
|
|
|
|
// Get layer number
|
2012-10-10 16:04:44 +02:00
|
|
|
if (ssa)
|
|
|
|
Layer = 0;
|
2013-01-04 16:01:50 +01:00
|
|
|
else
|
|
|
|
Layer = boost::lexical_cast<int>(tmp);
|
|
|
|
|
|
|
|
Start = tkn.next_str_trim();
|
|
|
|
End = tkn.next_str_trim();
|
|
|
|
Style = tkn.next_str_trim();
|
|
|
|
Actor = tkn.next_str_trim();
|
|
|
|
for (int& margin : Margin)
|
|
|
|
margin = mid(0, boost::lexical_cast<int>(tkn.next_str()), 9999);
|
|
|
|
Effect = tkn.next_str_trim();
|
2014-04-22 19:21:00 +02:00
|
|
|
|
|
|
|
std::string text{tkn.next_tok().begin(), str.end()};
|
|
|
|
|
2015-01-03 23:56:20 +01:00
|
|
|
if (text.size() > 1 && text[0] == '{' && text[1] == '=') {
|
|
|
|
static const boost::regex extradata_test("^\\{(=\\d+)+\\}");
|
|
|
|
boost::match_results<std::string::iterator> rematch;
|
|
|
|
if (boost::regex_search(text.begin(), text.end(), rematch, extradata_test)) {
|
|
|
|
std::string extradata_str = rematch.str(0);
|
|
|
|
text = rematch.suffix().str();
|
|
|
|
|
|
|
|
static const boost::regex idmatcher("=(\\d+)");
|
|
|
|
auto start = extradata_str.begin();
|
|
|
|
auto end = extradata_str.end();
|
|
|
|
std::vector<uint32_t> ids;
|
|
|
|
while (boost::regex_search(start, end, rematch, idmatcher)) {
|
|
|
|
auto id = boost::lexical_cast<uint32_t>(rematch.str(1));
|
|
|
|
ids.push_back(id);
|
|
|
|
start = rematch.suffix().first;
|
|
|
|
}
|
|
|
|
ExtradataIds = ids;
|
2014-04-22 19:21:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Text = text;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2014-07-09 00:16:49 +02:00
|
|
|
static void append_int(std::string &str, int v) {
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::spirit::karma::generate(back_inserter(str), boost::spirit::karma::int_, v);
|
2013-01-09 05:50:44 +01:00
|
|
|
str += ',';
|
|
|
|
}
|
|
|
|
|
2014-07-09 00:16:49 +02:00
|
|
|
static void append_str(std::string &out, std::string const& str) {
|
2013-01-09 05:50:44 +01:00
|
|
|
out += str;
|
|
|
|
out += ',';
|
|
|
|
}
|
|
|
|
|
2014-07-09 00:16:49 +02:00
|
|
|
static void append_unsafe_str(std::string &out, std::string const& str) {
|
2014-07-07 16:11:01 +02:00
|
|
|
for (auto c : str) {
|
|
|
|
if (c == ',')
|
|
|
|
out += ';';
|
|
|
|
else
|
|
|
|
out += c;
|
|
|
|
}
|
2013-01-09 05:50:44 +01:00
|
|
|
out += ',';
|
|
|
|
}
|
|
|
|
|
2014-04-29 18:33:22 +02:00
|
|
|
std::string AssDialogue::GetEntryData() const {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string str = Comment ? "Comment: " : "Dialogue: ";
|
2013-01-09 05:50:44 +01:00
|
|
|
str.reserve(51 + Style.get().size() + Actor.get().size() + Effect.get().size() + Text.get().size());
|
|
|
|
|
2014-04-29 18:33:22 +02:00
|
|
|
append_int(str, Layer);
|
2014-07-06 16:28:58 +02:00
|
|
|
append_str(str, Start.GetAssFormatted());
|
|
|
|
append_str(str, End.GetAssFormatted());
|
2013-01-09 05:50:44 +01:00
|
|
|
append_unsafe_str(str, Style);
|
|
|
|
append_unsafe_str(str, Actor);
|
2013-11-21 18:13:36 +01:00
|
|
|
for (auto margin : Margin)
|
|
|
|
append_int(str, margin);
|
2013-01-09 05:50:44 +01:00
|
|
|
append_unsafe_str(str, Effect);
|
2014-04-22 19:21:00 +02:00
|
|
|
|
|
|
|
if (ExtradataIds.get().size() > 0) {
|
2014-07-07 16:11:01 +02:00
|
|
|
str += '{';
|
2014-04-22 19:21:00 +02:00
|
|
|
for (auto id : ExtradataIds.get()) {
|
2014-07-07 16:11:01 +02:00
|
|
|
str += '=';
|
2014-04-22 19:21:00 +02:00
|
|
|
boost::spirit::karma::generate(back_inserter(str), boost::spirit::karma::int_, id);
|
|
|
|
}
|
2014-07-07 16:11:01 +02:00
|
|
|
str += '}';
|
2014-04-22 19:21:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 16:11:01 +02:00
|
|
|
for (auto c : Text.get()) {
|
|
|
|
if (c != '\n' && c != '\r')
|
|
|
|
str += c;
|
2013-01-09 05:50:44 +01:00
|
|
|
}
|
2007-04-19 17:22:47 +02:00
|
|
|
|
2010-07-08 09:15:04 +02:00
|
|
|
return str;
|
2006-02-27 05:18:00 +01:00
|
|
|
}
|
|
|
|
|
2014-04-14 19:58:46 +02:00
|
|
|
std::vector<std::unique_ptr<AssDialogueBlock>> AssDialogue::ParseTags() const {
|
|
|
|
std::vector<std::unique_ptr<AssDialogueBlock>> Blocks;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-26 23:21:02 +01:00
|
|
|
// Empty line, make an empty block
|
2012-12-04 23:35:59 +01:00
|
|
|
if (Text.get().empty()) {
|
2014-04-23 22:53:24 +02:00
|
|
|
Blocks.push_back(agi::make_unique<AssDialogueBlockPlain>());
|
2014-04-14 19:58:46 +02:00
|
|
|
return Blocks;
|
2011-12-26 23:21:02 +01:00
|
|
|
}
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
int drawingLevel = 0;
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string const& text(Text.get());
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-30 00:53:56 +01:00
|
|
|
for (size_t len = text.size(), cur = 0; cur < len; ) {
|
2006-01-16 22:02:54 +01:00
|
|
|
// Overrides block
|
2012-12-30 00:53:56 +01:00
|
|
|
if (text[cur] == '{') {
|
|
|
|
size_t end = text.find('}', cur);
|
2012-11-22 17:54:17 +01:00
|
|
|
|
|
|
|
// VSFilter requires that override blocks be closed, while libass
|
|
|
|
// does not. We match VSFilter here.
|
2012-12-30 00:53:56 +01:00
|
|
|
if (end == std::string::npos)
|
2012-11-22 17:54:17 +01:00
|
|
|
goto plain;
|
|
|
|
|
2011-12-26 23:21:02 +01:00
|
|
|
++cur;
|
2006-01-16 22:02:54 +01:00
|
|
|
// Get contents of block
|
2012-12-30 00:53:56 +01:00
|
|
|
std::string work = text.substr(cur, end - cur);
|
2012-11-22 17:54:17 +01:00
|
|
|
cur = end + 1;
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2012-12-30 00:53:56 +01:00
|
|
|
if (work.size() && work.find('\\') == std::string::npos) {
|
2007-06-03 03:54:39 +02:00
|
|
|
//We've found an override block with no backslashes
|
|
|
|
//We're going to assume it's a comment and not consider it an override block
|
2014-04-23 22:53:24 +02:00
|
|
|
Blocks.push_back(agi::make_unique<AssDialogueBlockComment>(work));
|
2007-06-03 03:54:39 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Create block
|
2014-04-23 22:53:24 +02:00
|
|
|
auto block = agi::make_unique<AssDialogueBlockOverride>(work);
|
2007-06-03 03:54:39 +02:00
|
|
|
block->ParseTags();
|
|
|
|
|
|
|
|
// Look for \p in block
|
2012-12-11 00:32:36 +01:00
|
|
|
for (auto const& tag : block->Tags) {
|
|
|
|
if (tag.Name == "\\p")
|
|
|
|
drawingLevel = tag.Params[0].Get<int>(0);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2014-04-14 19:58:46 +02:00
|
|
|
Blocks.push_back(std::move(block));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2012-11-22 17:54:17 +01:00
|
|
|
|
|
|
|
continue;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2012-11-22 17:54:17 +01:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
// Plain-text/drawing block
|
2012-11-22 17:54:17 +01:00
|
|
|
plain:
|
2012-12-30 00:53:56 +01:00
|
|
|
std::string work;
|
|
|
|
size_t end = text.find('{', cur + 1);
|
|
|
|
if (end == std::string::npos) {
|
|
|
|
work = text.substr(cur);
|
2012-11-22 17:54:17 +01:00
|
|
|
cur = len;
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
else {
|
2012-12-30 00:53:56 +01:00
|
|
|
work = text.substr(cur, end - cur);
|
2012-11-22 17:54:17 +01:00
|
|
|
cur = end;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2012-11-22 17:54:17 +01:00
|
|
|
|
|
|
|
if (drawingLevel == 0)
|
2014-04-23 22:53:24 +02:00
|
|
|
Blocks.push_back(agi::make_unique<AssDialogueBlockPlain>(work));
|
2012-11-22 17:54:17 +01:00
|
|
|
else
|
2014-04-23 22:53:24 +02:00
|
|
|
Blocks.push_back(agi::make_unique<AssDialogueBlockDrawing>(work, drawingLevel));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2012-03-29 01:58:50 +02:00
|
|
|
|
2014-04-14 19:58:46 +02:00
|
|
|
return Blocks;
|
2012-03-29 01:58:50 +02:00
|
|
|
}
|
|
|
|
|
2012-12-02 19:24:49 +01:00
|
|
|
void AssDialogue::StripTags() {
|
2010-06-24 03:24:43 +02:00
|
|
|
Text = GetStrippedText();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2014-04-14 19:58:46 +02:00
|
|
|
static std::string get_text(std::unique_ptr<AssDialogueBlock> &d) { return d->GetText(); }
|
|
|
|
void AssDialogue::UpdateText(std::vector<std::unique_ptr<AssDialogueBlock>>& blocks) {
|
2012-12-02 19:24:49 +01:00
|
|
|
if (blocks.empty()) return;
|
2013-01-04 16:01:50 +01:00
|
|
|
Text = join(blocks | transformed(get_text), "");
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-20 01:39:10 +01:00
|
|
|
bool AssDialogue::CollidesWith(const AssDialogue *target) const {
|
2006-01-16 22:02:54 +01:00
|
|
|
if (!target) return false;
|
2011-12-22 22:28:51 +01:00
|
|
|
return ((Start < target->Start) ? (target->Start < End) : (Start < target->End));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-12-30 00:53:56 +01:00
|
|
|
static std::string get_text_p(AssDialogueBlock *d) { return d->GetText(); }
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string AssDialogue::GetStrippedText() const {
|
2014-04-14 19:58:46 +02:00
|
|
|
auto blocks = ParseTags();
|
2013-01-04 16:01:50 +01:00
|
|
|
return join(blocks | agi::of_type<AssDialogueBlockPlain>() | transformed(get_text_p), "");
|
2007-01-13 03:22:28 +01:00
|
|
|
}
|