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_file.cpp
|
|
|
|
/// @brief Overall storage of subtitle files, undo management and more
|
|
|
|
/// @ingroup subs_storage
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-10-12 19:16:39 +02:00
|
|
|
#include "ass_file.h"
|
|
|
|
|
2006-06-30 23:59:20 +02:00
|
|
|
#include "ass_attachment.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_dialogue.h"
|
2012-12-08 03:51:09 +01:00
|
|
|
#include "ass_info.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_style.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2010-06-24 03:23:43 +02: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
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/dispatch.h>
|
2012-11-05 17:20:58 +01:00
|
|
|
#include <libaegisub/of_type_adaptor.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2013-01-26 02:57:46 +01:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2012-11-05 17:20:58 +01:00
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
namespace std {
|
|
|
|
template<>
|
|
|
|
void swap(AssFile &lft, AssFile &rgt) {
|
|
|
|
lft.swap(rgt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
AssFile::~AssFile() {
|
2013-01-04 16:01:50 +01:00
|
|
|
auto copy = new EntryList;
|
|
|
|
copy->swap(Line);
|
2013-09-16 01:58:04 +02:00
|
|
|
agi::dispatch::Background().Async([=]{
|
|
|
|
copy->clear_and_dispose([](AssEntry *e) { delete e; });
|
|
|
|
delete copy;
|
|
|
|
});
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 08:16:33 +01:00
|
|
|
void AssFile::LoadDefault(bool defline) {
|
2012-12-08 03:51:09 +01:00
|
|
|
Line.push_back(*new AssInfo("Title", "Default Aegisub file"));
|
|
|
|
Line.push_back(*new AssInfo("ScriptType", "v4.00+"));
|
|
|
|
Line.push_back(*new AssInfo("WrapStyle", "0"));
|
|
|
|
Line.push_back(*new AssInfo("ScaledBorderAndShadow", "yes"));
|
2012-01-08 02:04:37 +01:00
|
|
|
if (!OPT_GET("Subtitle/Default Resolution/Auto")->GetBool()) {
|
2013-01-04 16:01:50 +01:00
|
|
|
Line.push_back(*new AssInfo("PlayResX", std::to_string(OPT_GET("Subtitle/Default Resolution/Width")->GetInt())));
|
|
|
|
Line.push_back(*new AssInfo("PlayResY", std::to_string(OPT_GET("Subtitle/Default Resolution/Height")->GetInt())));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2012-12-08 03:51:09 +01:00
|
|
|
Line.push_back(*new AssInfo("YCbCr Matrix", "None"));
|
2012-02-16 22:21:35 +01:00
|
|
|
|
2012-11-22 17:14:34 +01:00
|
|
|
Line.push_back(*new AssStyle);
|
2012-01-08 02:34:30 +01:00
|
|
|
|
|
|
|
if (defline)
|
2012-10-12 19:16:39 +02:00
|
|
|
Line.push_back(*new AssDialogue);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
void AssFile::swap(AssFile &that) throw() {
|
2013-01-26 02:57:46 +01:00
|
|
|
Line.swap(that.Line);
|
2010-07-09 09:31:34 +02:00
|
|
|
}
|
|
|
|
|
2013-01-26 02:57:46 +01:00
|
|
|
AssFile::AssFile(const AssFile &from) {
|
2013-10-27 15:15:39 +01:00
|
|
|
Line.clone_from(from.Line, std::mem_fun_ref(&AssEntry::Clone), [](AssEntry *e) { delete e; });
|
2010-06-22 02:03:33 +02:00
|
|
|
}
|
2013-10-27 15:15:39 +01:00
|
|
|
|
2010-06-22 02:03:33 +02:00
|
|
|
AssFile& AssFile::operator=(AssFile from) {
|
2010-07-09 09:31:34 +02:00
|
|
|
std::swap(*this, from);
|
2010-06-22 02:03:33 +02:00
|
|
|
return *this;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-12-08 03:51:09 +01:00
|
|
|
void AssFile::InsertLine(AssEntry *entry) {
|
2012-11-22 17:14:34 +01:00
|
|
|
if (Line.empty()) {
|
|
|
|
Line.push_back(*entry);
|
|
|
|
return;
|
|
|
|
}
|
2012-02-16 22:21:35 +01:00
|
|
|
|
2012-01-08 02:34:30 +01:00
|
|
|
// Search for insertion point
|
2012-11-22 17:14:34 +01:00
|
|
|
entryIter it = Line.end();
|
2012-01-08 02:34:30 +01:00
|
|
|
do {
|
|
|
|
--it;
|
2012-12-10 00:44:03 +01:00
|
|
|
if (it->Group() <= entry->Group()) {
|
2012-11-22 17:14:34 +01:00
|
|
|
Line.insert(++it, *entry);
|
|
|
|
return;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2012-11-22 17:14:34 +01:00
|
|
|
} while (it != Line.begin());
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-10 00:44:03 +01:00
|
|
|
Line.push_front(*entry);
|
2006-07-01 06:08:01 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void AssFile::InsertAttachment(agi::fs::path const& filename) {
|
2013-06-13 00:54:19 +02:00
|
|
|
AssEntryGroup group = AssEntryGroup::GRAPHIC;
|
2006-07-01 06:35:50 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
auto ext = boost::to_lower_copy(filename.extension().string());
|
2012-01-08 02:34:30 +01:00
|
|
|
if (ext == ".ttf" || ext == ".ttc" || ext == ".pfb")
|
2013-06-13 00:54:19 +02:00
|
|
|
group = AssEntryGroup::FONT;
|
2012-02-01 01:47:38 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
InsertLine(new AssAttachment(filename, group));
|
2012-02-16 22:21:35 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string AssFile::GetScriptInfo(std::string const& key) const {
|
2012-12-08 03:51:09 +01:00
|
|
|
for (const auto info : Line | agi::of_type<AssInfo>()) {
|
2013-01-04 16:01:50 +01:00
|
|
|
if (boost::iequals(key, info->Key()))
|
2012-12-08 03:51:09 +01:00
|
|
|
return info->Value();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-06-16 08:20:14 +02:00
|
|
|
return "";
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
int AssFile::GetScriptInfoAsInt(std::string const& key) const {
|
|
|
|
return atoi(GetScriptInfo(key).c_str());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-08 02:42:09 +02:00
|
|
|
std::string AssFile::GetUIState(std::string const& key) const {
|
|
|
|
auto value = GetScriptInfo("Aegisub " + key);
|
|
|
|
if (value.empty())
|
|
|
|
value = GetScriptInfo(key);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AssFile::GetUIStateAsInt(std::string const& key) const {
|
|
|
|
return atoi(GetUIState(key).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssFile::SaveUIState(std::string const& key, std::string const& value) {
|
2013-10-08 02:43:53 +02:00
|
|
|
if (OPT_GET("App/Save UI State")->GetBool())
|
|
|
|
SetScriptInfo("Aegisub " + key, value);
|
2013-10-08 02:42:09 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void AssFile::SetScriptInfo(std::string const& key, std::string const& value) {
|
2012-12-08 03:51:09 +01:00
|
|
|
for (auto info : Line | agi::of_type<AssInfo>()) {
|
2013-01-04 16:01:50 +01:00
|
|
|
if (boost::iequals(key, info->Key())) {
|
2012-12-08 03:51:09 +01:00
|
|
|
if (value.empty())
|
|
|
|
delete info;
|
|
|
|
else
|
|
|
|
info->SetValue(value);
|
2006-01-16 22:02:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-02-16 22:21:55 +01:00
|
|
|
|
2012-12-10 02:07:05 +01:00
|
|
|
if (!value.empty())
|
|
|
|
InsertLine(new AssInfo(key, value));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-20 01:39:10 +01:00
|
|
|
void AssFile::GetResolution(int &sw,int &sh) const {
|
2011-10-25 03:16:56 +02:00
|
|
|
sw = GetScriptInfoAsInt("PlayResX");
|
|
|
|
sh = GetScriptInfoAsInt("PlayResY");
|
2007-05-03 19:19:50 +02:00
|
|
|
|
|
|
|
// Gabest logic?
|
|
|
|
if (sw == 0 && sh == 0) {
|
|
|
|
sw = 384;
|
|
|
|
sh = 288;
|
|
|
|
} else if (sw == 0) {
|
|
|
|
if (sh == 1024)
|
|
|
|
sw = 1280;
|
|
|
|
else
|
|
|
|
sw = sh * 4 / 3;
|
|
|
|
} else if (sh == 0) {
|
2010-06-16 08:20:14 +02:00
|
|
|
// you are not crazy; this doesn't make any sense
|
2007-05-03 19:19:50 +02:00
|
|
|
if (sw == 1280)
|
|
|
|
sh = 1024;
|
|
|
|
else
|
|
|
|
sh = sw * 3 / 4;
|
|
|
|
}
|
2006-07-01 09:22:57 +02:00
|
|
|
}
|
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
std::vector<std::string> AssFile::GetStyles() const {
|
|
|
|
std::vector<std::string> styles;
|
2012-11-05 17:20:58 +01:00
|
|
|
for (auto style : Line | agi::of_type<AssStyle>())
|
|
|
|
styles.push_back(style->name);
|
2006-01-16 22:02:54 +01:00
|
|
|
return styles;
|
|
|
|
}
|
|
|
|
|
2012-12-30 01:32:36 +01:00
|
|
|
AssStyle *AssFile::GetStyle(std::string const& name) {
|
2012-11-05 17:20:58 +01:00
|
|
|
for (auto style : Line | agi::of_type<AssStyle>()) {
|
2012-12-30 01:32:36 +01:00
|
|
|
if (boost::iequals(style->name, name))
|
2012-11-05 17:20:58 +01:00
|
|
|
return style;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2012-11-05 17:20:58 +01:00
|
|
|
return nullptr;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2013-01-26 02:57:46 +01:00
|
|
|
int AssFile::Commit(wxString const& desc, int type, int amend_id, AssEntry *single_line) {
|
2014-03-04 17:32:29 +01:00
|
|
|
PushState({desc, &amend_id, single_line});
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-17 16:54:19 +01:00
|
|
|
std::set<const AssEntry*> changed_lines;
|
|
|
|
if (single_line)
|
|
|
|
changed_lines.insert(single_line);
|
|
|
|
|
|
|
|
AnnounceCommit(type, changed_lines);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-26 02:57:46 +01:00
|
|
|
return amend_id;
|
2010-05-19 02:44:44 +02:00
|
|
|
}
|
2007-01-26 01:47:42 +01:00
|
|
|
|
2010-05-19 02:44:44 +02:00
|
|
|
bool AssFile::CompStart(const AssDialogue* lft, const AssDialogue* rgt) {
|
|
|
|
return lft->Start < rgt->Start;
|
|
|
|
}
|
|
|
|
bool AssFile::CompEnd(const AssDialogue* lft, const AssDialogue* rgt) {
|
|
|
|
return lft->End < rgt->End;
|
|
|
|
}
|
|
|
|
bool AssFile::CompStyle(const AssDialogue* lft, const AssDialogue* rgt) {
|
|
|
|
return lft->Style < rgt->Style;
|
2007-01-26 01:47:42 +01:00
|
|
|
}
|
2012-01-31 01:44:34 +01:00
|
|
|
bool AssFile::CompActor(const AssDialogue* lft, const AssDialogue* rgt) {
|
|
|
|
return lft->Actor < rgt->Actor;
|
|
|
|
}
|
|
|
|
bool AssFile::CompEffect(const AssDialogue* lft, const AssDialogue* rgt) {
|
|
|
|
return lft->Effect < rgt->Effect;
|
|
|
|
}
|
2012-07-23 02:44:44 +02:00
|
|
|
bool AssFile::CompLayer(const AssDialogue* lft, const AssDialogue* rgt) {
|
|
|
|
return lft->Layer < rgt->Layer;
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-03-07 23:41:12 +01:00
|
|
|
void AssFile::Sort(CompFunc comp, std::set<AssDialogue*> const& limit) {
|
|
|
|
Sort(Line, comp, limit);
|
2010-05-19 02:44:44 +02:00
|
|
|
}
|
2010-05-19 05:24:07 +02:00
|
|
|
namespace {
|
2012-03-07 23:41:12 +01:00
|
|
|
inline bool is_dialogue(AssEntry *e, std::set<AssDialogue*> const& limit) {
|
|
|
|
AssDialogue *d = dynamic_cast<AssDialogue*>(e);
|
|
|
|
return d && (limit.empty() || limit.count(d));
|
|
|
|
}
|
2010-05-19 05:24:07 +02:00
|
|
|
}
|
2012-10-12 19:16:39 +02:00
|
|
|
|
|
|
|
void AssFile::Sort(EntryList &lst, CompFunc comp, std::set<AssDialogue*> const& limit) {
|
2013-01-26 02:57:46 +01:00
|
|
|
auto compE = [&](AssEntry const& a, AssEntry const& b) {
|
|
|
|
return comp(static_cast<const AssDialogue*>(&a), static_cast<const AssDialogue*>(&b));
|
|
|
|
};
|
|
|
|
|
2010-05-19 02:44:44 +02:00
|
|
|
// Sort each block of AssDialogues separately, leaving everything else untouched
|
|
|
|
for (entryIter begin = lst.begin(); begin != lst.end(); ++begin) {
|
2012-10-12 19:16:39 +02:00
|
|
|
if (!is_dialogue(&*begin, limit)) continue;
|
2010-05-19 02:44:44 +02:00
|
|
|
entryIter end = begin;
|
2012-10-12 19:16:39 +02:00
|
|
|
while (end != lst.end() && is_dialogue(&*end, limit)) ++end;
|
2010-05-19 02:44:44 +02:00
|
|
|
|
2010-07-20 05:11:11 +02:00
|
|
|
// used instead of std::list::sort for partial list sorting
|
2012-10-12 19:16:39 +02:00
|
|
|
EntryList tmp;
|
2010-05-19 02:44:44 +02:00
|
|
|
tmp.splice(tmp.begin(), lst, begin, end);
|
|
|
|
tmp.sort(compE);
|
|
|
|
lst.splice(end, tmp);
|
|
|
|
|
|
|
|
begin = --end;
|
|
|
|
}
|
|
|
|
}
|