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/
|
2007-06-18 03:17:03 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @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"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "main.h"
|
2007-06-18 03:17:03 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
wxArrayString TTXTSubtitleFormat::GetReadWildcards() const {
|
2007-06-18 03:17:03 +02:00
|
|
|
wxArrayString formats;
|
2011-09-28 21:43:11 +02:00
|
|
|
formats.Add("ttxt");
|
2007-06-18 03:17:03 +02:00
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
wxArrayString TTXTSubtitleFormat::GetWriteWildcards() const {
|
2007-06-19 00:20:50 +02:00
|
|
|
return GetReadWildcards();
|
2007-06-18 03:17:03 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
void TTXTSubtitleFormat::ReadFile(wxString const& filename, wxString const& forceEncoding) {
|
2007-06-18 03:17:03 +02:00
|
|
|
LoadDefault(false);
|
|
|
|
|
|
|
|
// Load XML document
|
|
|
|
wxXmlDocument doc;
|
2011-09-28 21:44:53 +02:00
|
|
|
if (!doc.Load(filename)) 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", "");
|
2007-06-19 00:20:50 +02:00
|
|
|
version = -1;
|
2011-09-28 21:43:11 +02:00
|
|
|
if (verStr == "1.0") version = 0;
|
|
|
|
else if (verStr == "1.1") version = 1;
|
2011-09-28 21:44:53 +02:00
|
|
|
else throw TTXTParseError("Unknown TTXT version: " + STD_STR(verStr), 0);
|
2007-06-18 08:56:10 +02:00
|
|
|
|
2007-06-18 03:17:03 +02:00
|
|
|
// Get children
|
2007-06-19 00:20:50 +02:00
|
|
|
diag = NULL;
|
2007-06-18 03:17:03 +02:00
|
|
|
wxXmlNode *child = doc.GetRoot()->GetChildren();
|
|
|
|
int lines = 0;
|
|
|
|
while (child) {
|
|
|
|
// Line
|
2011-09-28 21:43:11 +02:00
|
|
|
if (child->GetName() == "TextSample") {
|
2007-06-19 00:20:50 +02:00
|
|
|
if (ProcessLine(child)) lines++;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Proceed to next child
|
|
|
|
child = child->GetNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
// No lines?
|
|
|
|
if (lines == 0) {
|
|
|
|
AssDialogue *line = new AssDialogue();
|
2011-09-28 21:43:11 +02:00
|
|
|
line->group = "[Events]";
|
|
|
|
line->Style = "Default";
|
2011-12-22 22:28:51 +01:00
|
|
|
line->Start = 0;
|
|
|
|
line->End = 5000;
|
2007-06-18 03:17:03 +02:00
|
|
|
Line->push_back(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-19 00:20:50 +02:00
|
|
|
bool TTXTSubtitleFormat::ProcessLine(wxXmlNode *node) {
|
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");
|
2007-06-19 00:20:50 +02:00
|
|
|
AssTime time;
|
|
|
|
time.ParseASS(sampleTime);
|
|
|
|
|
|
|
|
// Set end time of last line
|
|
|
|
if (diag) diag->End = time;
|
|
|
|
diag = NULL;
|
|
|
|
|
2007-06-19 03:30:27 +02:00
|
|
|
// Get text
|
|
|
|
wxString text;
|
2011-09-28 21:44:53 +02:00
|
|
|
if (version == 0) text = node->GetAttribute("text", "");
|
2007-06-19 03:30:27 +02:00
|
|
|
else text = node->GetNodeContent();
|
|
|
|
|
2007-06-19 00:20:50 +02:00
|
|
|
// Create line
|
|
|
|
if (!text.IsEmpty()) {
|
|
|
|
// Create dialogue
|
|
|
|
diag = new AssDialogue();
|
2011-12-22 22:28:51 +01:00
|
|
|
diag->Start = time;
|
|
|
|
diag->End = 36000000-10;
|
2011-09-28 21:43:11 +02:00
|
|
|
diag->group = "[Events]";
|
|
|
|
diag->Style = "Default";
|
2007-06-19 00:20:50 +02:00
|
|
|
diag->Comment = false;
|
|
|
|
|
|
|
|
// Process text for 1.0
|
|
|
|
if (version == 0) {
|
|
|
|
wxString finalText;
|
|
|
|
finalText.Alloc(text.Length());
|
|
|
|
bool in = false;
|
|
|
|
bool first = true;
|
|
|
|
for (size_t i=0;i<text.Length();i++) {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (text[i] == '\'') {
|
|
|
|
if (!in && !first) finalText += "\\N";
|
2007-06-19 00:20:50 +02:00
|
|
|
first = false;
|
|
|
|
in = !in;
|
|
|
|
}
|
|
|
|
else if (in) finalText += text[i];
|
|
|
|
}
|
|
|
|
diag->Text = finalText;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process text for 1.1
|
|
|
|
else {
|
2011-09-28 21:44:53 +02:00
|
|
|
text.Replace("\r", "");
|
|
|
|
text.Replace("\n", "\\N");
|
2007-06-19 00:20:50 +02:00
|
|
|
diag->Text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert dialogue
|
|
|
|
Line->push_back(diag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTXTSubtitleFormat::ProcessHeader(wxXmlNode *node) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
void TTXTSubtitleFormat::WriteFile(wxString const& filename, wxString const& encoding) {
|
2007-06-19 00:20:50 +02:00
|
|
|
// Convert to TTXT
|
|
|
|
CreateCopy();
|
|
|
|
ConvertToTTXT();
|
|
|
|
|
|
|
|
// Create XML structure
|
|
|
|
wxXmlDocument doc;
|
2011-09-28 21:44:53 +02:00
|
|
|
wxXmlNode *root = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "TextStream");
|
|
|
|
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
|
|
|
|
int i=1;
|
|
|
|
using std::list;
|
2007-06-19 03:30:27 +02:00
|
|
|
prev = NULL;
|
2007-06-19 00:20:50 +02:00
|
|
|
for (list<AssEntry*>::iterator cur=Line->begin();cur!=Line->end();cur++) {
|
2010-05-19 02:44:52 +02:00
|
|
|
AssDialogue *current = dynamic_cast<AssDialogue*>(*cur);
|
2007-06-19 03:30:27 +02:00
|
|
|
if (current && !current->Comment) {
|
2011-09-28 21:44:53 +02:00
|
|
|
WriteLine(root, current);
|
2007-06-19 00:20:50 +02:00
|
|
|
i++;
|
|
|
|
}
|
2011-09-28 21:44:53 +02:00
|
|
|
else
|
|
|
|
throw TTXTParseError("Unexpected line type in TTXT file", 0);
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save XML
|
|
|
|
//prevNode->SetNext(NULL);
|
|
|
|
doc.Save(filename);
|
|
|
|
|
|
|
|
// Clear
|
|
|
|
ClearCopy();
|
|
|
|
}
|
|
|
|
|
2007-06-19 03:30:27 +02:00
|
|
|
void TTXTSubtitleFormat::WriteHeader(wxXmlNode *root) {
|
|
|
|
// 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-09-28 21:44:53 +02:00
|
|
|
node = new wxXmlNode(wxXML_ELEMENT_NODE, "FontTable");
|
|
|
|
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);
|
|
|
|
root->AddChild(node);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTXTSubtitleFormat::WriteLine(wxXmlNode *root, AssDialogue *line) {
|
|
|
|
// If it doesn't start at the end of previous, add blank
|
2011-09-28 21:44:53 +02:00
|
|
|
wxXmlNode *node, *subNode;
|
2007-06-19 03:30:27 +02:00
|
|
|
if (prev && prev->End != line->Start) {
|
2011-09-28 21:44:53 +02:00
|
|
|
node = new wxXmlNode(wxXML_ELEMENT_NODE, "TextSample");
|
|
|
|
node->AddAttribute("sampleTime", "0" + prev->End.GetASSFormated(true));
|
|
|
|
node->AddAttribute("xml:space", "preserve");
|
|
|
|
subNode = new wxXmlNode(wxXML_TEXT_NODE, "", "");
|
2007-06-19 03:30:27 +02:00
|
|
|
node->AddChild(subNode);
|
|
|
|
root->AddChild(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate and insert node
|
2011-09-28 21:44:53 +02:00
|
|
|
node = new wxXmlNode(wxXML_ELEMENT_NODE, "TextSample");
|
|
|
|
node->AddAttribute("sampleTime", "0" + line->Start.GetASSFormated(true));
|
|
|
|
node->AddAttribute("xml:space", "preserve");
|
|
|
|
subNode = new wxXmlNode(wxXML_TEXT_NODE, "", line->Text);
|
2007-06-19 03:30:27 +02:00
|
|
|
node->AddChild(subNode);
|
|
|
|
root->AddChild(node);
|
|
|
|
|
|
|
|
// Set as previous
|
|
|
|
prev = line;
|
|
|
|
}
|
|
|
|
|
2007-06-19 00:20:50 +02:00
|
|
|
void TTXTSubtitleFormat::ConvertToTTXT () {
|
2007-06-19 05:34:53 +02:00
|
|
|
SortLines();
|
2008-07-18 17:39:34 +02:00
|
|
|
StripComments();
|
|
|
|
RecombineOverlaps();
|
|
|
|
MergeIdentical();
|
2011-11-25 20:28:19 +01:00
|
|
|
StripTags();
|
|
|
|
ConvertNewlines("\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;
|
2007-06-19 05:34:53 +02:00
|
|
|
for (std::list<AssEntry*>::reverse_iterator cur=Line->rbegin();cur!=Line->rend();cur++) {
|
2011-12-22 22:29:09 +01:00
|
|
|
if (AssDialogue *prev = dynamic_cast<AssDialogue*>(*cur)) {
|
2007-06-19 05:34:53 +02:00
|
|
|
lastTime = prev->End;
|
|
|
|
break;
|
2007-06-19 00:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert blank line at the end
|
|
|
|
AssDialogue *diag = new AssDialogue();
|
2011-12-22 22:28:51 +01:00
|
|
|
diag->Start = lastTime;
|
|
|
|
diag->End = lastTime+OPT_GET("Timing/Default Duration")->GetInt();
|
2011-09-28 21:43:11 +02:00
|
|
|
diag->group = "[Events]";
|
|
|
|
diag->Style = "Default";
|
2007-06-19 00:20:50 +02:00
|
|
|
diag->Comment = false;
|
|
|
|
Line->push_back(diag);
|
2007-06-18 03:17:03 +02:00
|
|
|
}
|