2007-06-19 07:04:15 +02:00
|
|
|
// Copyright (c) 2007, Rodrigo Braz Monteiro
|
2007-06-18 03:17:03 +02:00
|
|
|
// 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 subtitle_format_ttxt.cpp
|
|
|
|
/// @brief Reading/writing MPEG-4 Timed Text subtitles in TTXT XML format
|
|
|
|
/// @ingroup subtitle_io
|
|
|
|
///
|
2007-06-18 03:17:03 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
#include "subtitle_format_ttxt.h"
|
|
|
|
|
2012-01-26 21:08:38 +01:00
|
|
|
#include <wx/xml/xml.h>
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
#include "ass_dialogue.h"
|
2007-06-19 00:20:50 +02:00
|
|
|
#include "ass_file.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_time.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "compat.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2007-06-18 03:17:03 +02:00
|
|
|
|
2012-11-05 17:20:58 +01:00
|
|
|
#include <libaegisub/of_type_adaptor.h>
|
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
DEFINE_SIMPLE_EXCEPTION(TTXTParseError, SubtitleFormatParseError, "subtitle_io/parse/ttxt")
|
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
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
TTXTSubtitleFormat::TTXTSubtitleFormat()
|
|
|
|
: SubtitleFormat("MPEG-4 Streaming Text")
|
|
|
|
{
|
2007-06-18 03:17:03 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> TTXTSubtitleFormat::GetReadWildcards() const {
|
|
|
|
std::vector<std::string> formats;
|
|
|
|
formats.push_back("ttxt");
|
2007-06-18 03:17:03 +02:00
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> TTXTSubtitleFormat::GetWriteWildcards() const {
|
2007-06-19 00:20:50 +02:00
|
|
|
return GetReadWildcards();
|
2007-06-18 03:17:03 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void TTXTSubtitleFormat::ReadFile(AssFile *target, agi::fs::path const& filename, std::string const& encoding) const {
|
2012-01-26 21:08:38 +01:00
|
|
|
target->LoadDefault(false);
|
2007-06-18 03:17:03 +02:00
|
|
|
|
|
|
|
// Load XML document
|
|
|
|
wxXmlDocument doc;
|
2013-01-04 16:01:50 +01:00
|
|
|
if (!doc.Load(filename.wstring())) throw TTXTParseError("Failed loading TTXT XML file.", 0);
|
2007-06-18 03:17:03 +02:00
|
|
|
|
|
|
|
// Check root node name
|
2011-09-28 21:44:53 +02:00
|
|
|
if (doc.GetRoot()->GetName() != "TextStream") throw TTXTParseError("Invalid TTXT file.", 0);
|
2007-06-18 03:17:03 +02:00
|
|
|
|
2007-06-18 08:56:10 +02:00
|
|
|
// Check version
|
2011-09-28 21:44:53 +02:00
|
|
|
wxString verStr = doc.GetRoot()->GetAttribute("version", "");
|
2012-01-26 21:08:38 +01:00
|
|
|
int version = -1;
|
|
|
|
if (verStr == "1.0")
|
|
|
|
version = 0;
|
|
|
|
else if (verStr == "1.1")
|
|
|
|
version = 1;
|
|
|
|
else
|
2012-12-23 00:18:38 +01:00
|
|
|
throw TTXTParseError("Unknown TTXT version: " + from_wx(verStr), 0);
|
2007-06-18 08:56:10 +02:00
|
|
|
|
2007-06-18 03:17:03 +02:00
|
|
|
// Get children
|
2012-01-26 21:08:38 +01:00
|
|
|
AssDialogue *diag = 0;
|
2007-06-18 03:17:03 +02:00
|
|
|
int lines = 0;
|
2012-01-26 21:08:38 +01:00
|
|
|
for (wxXmlNode *child = doc.GetRoot()->GetChildren(); child; child = child->GetNext()) {
|
2007-06-18 03:17:03 +02:00
|
|
|
// Line
|
2011-09-28 21:43:11 +02:00
|
|
|
if (child->GetName() == "TextSample") {
|
2012-03-10 03:16:08 +01:00
|
|
|
if ((diag = ProcessLine(child, diag, version))) {
|
2012-01-26 21:08:38 +01:00
|
|
|
lines++;
|
2012-10-12 19:16:39 +02:00
|
|
|
target->Line.push_back(*diag);
|
2012-01-26 21:08:38 +01:00
|
|
|
}
|
2007-06-18 03:17:03 +02:00
|
|
|
}
|
|
|
|
// Header
|
2011-09-28 21:43:11 +02:00
|
|
|
else if (child->GetName() == "TextStreamHeader") {
|
2007-06-19 00:20:50 +02:00
|
|
|
ProcessHeader(child);
|
2007-06-18 03:17:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No lines?
|
2012-01-26 21:08:38 +01:00
|
|
|
if (lines == 0)
|
2012-10-12 19:16:39 +02:00
|
|
|
target->Line.push_back(*new AssDialogue);
|
2007-06-18 03:17:03 +02:00
|
|
|
}
|
|
|
|
|
2012-01-26 21:08:38 +01:00
|
|
|
AssDialogue *TTXTSubtitleFormat::ProcessLine(wxXmlNode *node, AssDialogue *prev, int version) const {
|
2007-06-19 03:30:27 +02:00
|
|
|
// Get time
|
2011-09-28 21:44:53 +02:00
|
|
|
wxString sampleTime = node->GetAttribute("sampleTime", "00:00:00.000");
|
2013-01-04 16:01:50 +01:00
|
|
|
AssTime time(from_wx(sampleTime));
|
2007-06-19 00:20:50 +02:00
|
|
|
|
|
|
|
// Set end time of last line
|
2012-01-26 21:08:38 +01:00
|
|
|
if (prev)
|
|
|
|
prev->End = time;
|
2007-06-19 00:20:50 +02:00
|
|
|
|
2007-06-19 03:30:27 +02:00
|
|
|
// Get text
|
|
|
|
wxString text;
|
2012-01-26 21:08:38 +01:00
|
|
|
if (version == 0)
|
|
|
|
text = node->GetAttribute("text", "");
|
|
|
|
else
|
|
|
|
text = node->GetNodeContent();
|
2007-06-19 03:30:27 +02:00
|
|
|
|
2007-06-19 00:20:50 +02:00
|
|
|
// Create line
|
2012-01-26 21:08:38 +01:00
|
|
|
if (text.empty()) return 0;
|
|
|
|
|
|
|
|
// Create dialogue
|
|
|
|
AssDialogue *diag = new AssDialogue;
|
|
|
|
diag->Start = time;
|
|
|
|
diag->End = 36000000-10;
|
|
|
|
|
|
|
|
// Process text for 1.0
|
|
|
|
if (version == 0) {
|
|
|
|
wxString finalText;
|
|
|
|
finalText.reserve(text.size());
|
|
|
|
bool in = false;
|
|
|
|
bool first = true;
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto chr : text) {
|
|
|
|
if (chr == '\'') {
|
2012-01-26 21:08:38 +01:00
|
|
|
if (!in && !first) finalText += "\\N";
|
|
|
|
first = false;
|
|
|
|
in = !in;
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
2012-11-04 04:53:03 +01:00
|
|
|
else if (in) finalText += chr;
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
diag->Text = from_wx(finalText);
|
2012-01-26 21:08:38 +01:00
|
|
|
}
|
2007-06-19 00:20:50 +02:00
|
|
|
|
2012-01-26 21:08:38 +01:00
|
|
|
// Process text for 1.1
|
|
|
|
else {
|
|
|
|
text.Replace("\r", "");
|
|
|
|
text.Replace("\n", "\\N");
|
2013-01-04 16:01:50 +01:00
|
|
|
diag->Text = from_wx(text);
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-26 21:08:38 +01:00
|
|
|
return diag;
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-26 21:08:38 +01:00
|
|
|
void TTXTSubtitleFormat::ProcessHeader(wxXmlNode *node) const {
|
2007-06-19 00:20:50 +02:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void TTXTSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& filename, std::string const& encoding) const {
|
2007-06-19 00:20:50 +02:00
|
|
|
// Convert to TTXT
|
2012-01-26 21:08:38 +01:00
|
|
|
AssFile copy(*src);
|
|
|
|
ConvertToTTXT(copy);
|
2007-06-19 00:20:50 +02:00
|
|
|
|
|
|
|
// Create XML structure
|
|
|
|
wxXmlDocument doc;
|
2012-11-13 17:51:01 +01:00
|
|
|
wxXmlNode *root = new wxXmlNode(nullptr, wxXML_ELEMENT_NODE, "TextStream");
|
2011-09-28 21:44:53 +02:00
|
|
|
root->AddAttribute("version", "1.1");
|
2007-06-19 00:20:50 +02:00
|
|
|
doc.SetRoot(root);
|
|
|
|
|
|
|
|
// Create header
|
2007-06-19 03:30:27 +02:00
|
|
|
WriteHeader(root);
|
2007-06-19 00:20:50 +02:00
|
|
|
|
|
|
|
// Create lines
|
2012-10-12 19:16:39 +02:00
|
|
|
const AssDialogue *prev = 0;
|
2012-11-05 17:20:58 +01:00
|
|
|
for (auto current : copy.Line | agi::of_type<AssDialogue>()) {
|
|
|
|
WriteLine(root, prev, current);
|
|
|
|
prev = current;
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save XML
|
2013-01-04 16:01:50 +01:00
|
|
|
doc.Save(filename.wstring());
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-26 21:08:38 +01:00
|
|
|
void TTXTSubtitleFormat::WriteHeader(wxXmlNode *root) const {
|
2007-06-19 03:30:27 +02:00
|
|
|
// Write stream header
|
2011-09-28 21:44:53 +02:00
|
|
|
wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, "TextStreamHeader");
|
|
|
|
node->AddAttribute("width", "400");
|
|
|
|
node->AddAttribute("height", "60");
|
|
|
|
node->AddAttribute("layer", "0");
|
|
|
|
node->AddAttribute("translation_x", "0");
|
|
|
|
node->AddAttribute("translation_y", "0");
|
2007-06-19 03:30:27 +02:00
|
|
|
root->AddChild(node);
|
|
|
|
root = node;
|
|
|
|
|
|
|
|
// Write sample description
|
2011-09-28 21:44:53 +02:00
|
|
|
node = new wxXmlNode(wxXML_ELEMENT_NODE, "TextSampleDescription");
|
|
|
|
node->AddAttribute("horizontalJustification", "center");
|
|
|
|
node->AddAttribute("verticalJustification", "bottom");
|
|
|
|
node->AddAttribute("backColor", "0 0 0 0");
|
|
|
|
node->AddAttribute("verticalText", "no");
|
|
|
|
node->AddAttribute("fillTextRegion", "no");
|
|
|
|
node->AddAttribute("continuousKaraoke", "no");
|
|
|
|
node->AddAttribute("scroll", "None");
|
2007-06-19 03:30:27 +02:00
|
|
|
root->AddChild(node);
|
|
|
|
root = node;
|
|
|
|
|
|
|
|
// Write font table
|
2011-12-26 23:20:49 +01:00
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
node = new wxXmlNode(wxXML_ELEMENT_NODE, "FontTable");
|
2011-12-26 23:20:49 +01:00
|
|
|
root->AddChild(node);
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
wxXmlNode *subNode = new wxXmlNode(wxXML_ELEMENT_NODE, "FontTableEntry");
|
|
|
|
subNode->AddAttribute("fontName", "Sans");
|
|
|
|
subNode->AddAttribute("fontID", "1");
|
2007-06-19 03:30:27 +02:00
|
|
|
node->AddChild(subNode);
|
2011-12-26 23:20:49 +01:00
|
|
|
|
2007-06-19 03:30:27 +02:00
|
|
|
// Write text box
|
2011-09-28 21:44:53 +02:00
|
|
|
node = new wxXmlNode(wxXML_ELEMENT_NODE, "TextBox");
|
|
|
|
node->AddAttribute("top", "0");
|
|
|
|
node->AddAttribute("left", "0");
|
|
|
|
node->AddAttribute("bottom", "60");
|
|
|
|
node->AddAttribute("right", "400");
|
2007-06-19 03:30:27 +02:00
|
|
|
root->AddChild(node);
|
|
|
|
|
|
|
|
// Write style
|
2011-09-28 21:44:53 +02:00
|
|
|
node = new wxXmlNode(wxXML_ELEMENT_NODE, "Style");
|
|
|
|
node->AddAttribute("styles", "Normal");
|
|
|
|
node->AddAttribute("fontID", "1");
|
|
|
|
node->AddAttribute("fontSize", "18");
|
|
|
|
node->AddAttribute("color", "ff ff ff ff");
|
2007-06-19 03:30:27 +02:00
|
|
|
root->AddChild(node);
|
|
|
|
}
|
|
|
|
|
2012-10-12 19:16:39 +02:00
|
|
|
void TTXTSubtitleFormat::WriteLine(wxXmlNode *root, const AssDialogue *prev, const AssDialogue *line) const {
|
2007-06-19 03:30:27 +02:00
|
|
|
// If it doesn't start at the end of previous, add blank
|
|
|
|
if (prev && prev->End != line->Start) {
|
2011-12-26 23:20:49 +01:00
|
|
|
wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, "TextSample");
|
2013-01-04 16:01:50 +01:00
|
|
|
node->AddAttribute("sampleTime", to_wx("0" + prev->End.GetAssFormated(true)));
|
2011-09-28 21:44:53 +02:00
|
|
|
node->AddAttribute("xml:space", "preserve");
|
2007-06-19 03:30:27 +02:00
|
|
|
root->AddChild(node);
|
2011-12-26 23:20:49 +01:00
|
|
|
node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "", ""));
|
2007-06-19 03:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate and insert node
|
2011-12-26 23:20:49 +01:00
|
|
|
wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, "TextSample");
|
2013-01-04 16:01:50 +01:00
|
|
|
node->AddAttribute("sampleTime", to_wx("0" + line->Start.GetAssFormated(true)));
|
2011-09-28 21:44:53 +02:00
|
|
|
node->AddAttribute("xml:space", "preserve");
|
2007-06-19 03:30:27 +02:00
|
|
|
root->AddChild(node);
|
2013-01-04 16:01:50 +01:00
|
|
|
node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "", to_wx(line->Text)));
|
2007-06-19 03:30:27 +02:00
|
|
|
}
|
|
|
|
|
2012-01-26 21:08:38 +01:00
|
|
|
void TTXTSubtitleFormat::ConvertToTTXT(AssFile &file) const {
|
|
|
|
file.Sort();
|
2012-10-12 19:16:39 +02:00
|
|
|
StripComments(file);
|
|
|
|
RecombineOverlaps(file);
|
|
|
|
MergeIdentical(file);
|
|
|
|
StripTags(file);
|
|
|
|
ConvertNewlines(file, "\r\n");
|
2007-06-19 00:20:50 +02:00
|
|
|
|
2007-06-19 05:34:53 +02:00
|
|
|
// Find last line
|
2007-06-19 00:20:50 +02:00
|
|
|
AssTime lastTime;
|
2012-11-05 17:20:58 +01:00
|
|
|
for (auto line : file.Line | boost::adaptors::reversed | agi::of_type<AssDialogue>()) {
|
|
|
|
lastTime = line->End;
|
|
|
|
break;
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert blank line at the end
|
2012-01-26 21:08:38 +01:00
|
|
|
AssDialogue *diag = new AssDialogue;
|
2011-12-22 22:28:51 +01:00
|
|
|
diag->Start = lastTime;
|
|
|
|
diag->End = lastTime+OPT_GET("Timing/Default Duration")->GetInt();
|
2012-10-12 19:16:39 +02:00
|
|
|
file.Line.push_back(*diag);
|
2007-06-18 03:17:03 +02:00
|
|
|
}
|