2008-07-18 03:36:20 +02:00
|
|
|
// Copyright (c) 2007, 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/
|
2008-07-18 03:36:20 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file subtitle_format_transtation.cpp
|
|
|
|
/// @brief Reading/writing Transtation-compatible subtitles
|
|
|
|
/// @ingroup subtitle_io
|
|
|
|
///
|
2008-07-18 03:36:20 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
#include "subtitle_format_transtation.h"
|
|
|
|
|
2008-07-18 03:36:20 +02:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
|
|
|
#include "ass_style.h"
|
2009-05-10 05:50:58 +02:00
|
|
|
#include "ass_time.h"
|
2008-07-18 03:36:20 +02:00
|
|
|
#include "text_file_writer.h"
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
TranStationSubtitleFormat::TranStationSubtitleFormat()
|
|
|
|
: SubtitleFormat("TranStation")
|
|
|
|
{
|
2008-07-18 03:36:20 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
wxArrayString TranStationSubtitleFormat::GetWriteWildcards() const {
|
2008-07-18 03:36:20 +02:00
|
|
|
wxArrayString formats;
|
2011-09-28 21:43:11 +02:00
|
|
|
formats.Add("transtation.txt");
|
2008-07-18 03:36:20 +02:00
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
void TranStationSubtitleFormat::WriteFile(wxString const& filename, wxString const& encoding) {
|
2011-12-22 22:27:53 +01:00
|
|
|
FractionalTime ft = AskForFPS(true);
|
2011-12-22 22:28:13 +01:00
|
|
|
if (!ft.FPS().IsLoaded()) return;
|
2008-07-18 03:36:20 +02:00
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
TextFileWriter file(filename, encoding);
|
2008-07-18 03:36:20 +02:00
|
|
|
|
|
|
|
// Convert to TranStation
|
|
|
|
CreateCopy();
|
|
|
|
SortLines();
|
2008-07-18 17:39:34 +02:00
|
|
|
StripComments();
|
|
|
|
RecombineOverlaps();
|
|
|
|
MergeIdentical();
|
2008-07-18 03:36:20 +02:00
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
AssDialogue *prev = 0;
|
|
|
|
for (std::list<AssEntry*>::iterator it = Line->begin(); it != Line->end(); ++it) {
|
|
|
|
AssDialogue *cur = dynamic_cast<AssDialogue*>(*it);
|
|
|
|
|
|
|
|
if (prev && cur) {
|
2011-12-22 22:28:51 +01:00
|
|
|
file.WriteLineToFile(ConvertLine(prev, &ft, cur->Start));
|
2011-09-28 21:43:11 +02:00
|
|
|
file.WriteLineToFile("");
|
2008-07-18 03:36:20 +02:00
|
|
|
}
|
2011-09-28 21:44:53 +02:00
|
|
|
|
|
|
|
if (cur)
|
|
|
|
prev = cur;
|
2008-07-18 03:36:20 +02:00
|
|
|
}
|
2011-09-28 21:44:53 +02:00
|
|
|
|
2009-05-13 22:24:21 +02:00
|
|
|
// flush last line
|
2011-09-28 21:44:53 +02:00
|
|
|
if (prev)
|
2011-12-22 22:27:53 +01:00
|
|
|
file.WriteLineToFile(ConvertLine(prev, &ft, -1));
|
2008-07-18 03:36:20 +02:00
|
|
|
|
2008-07-20 15:34:42 +02:00
|
|
|
// Every file must end with this line
|
2011-09-28 21:43:11 +02:00
|
|
|
file.WriteLineToFile("SUB[");
|
2008-07-20 15:34:42 +02:00
|
|
|
|
2008-07-18 03:36:20 +02:00
|
|
|
ClearCopy();
|
|
|
|
}
|
2009-05-13 22:24:21 +02:00
|
|
|
|
2011-12-22 22:27:53 +01:00
|
|
|
wxString TranStationSubtitleFormat::ConvertLine(AssDialogue *current, FractionalTime *ft, int nextl_start) {
|
2009-05-13 22:24:21 +02:00
|
|
|
int valign = 0;
|
2011-09-28 21:43:11 +02:00
|
|
|
const char *halign = " "; // default is centered
|
|
|
|
const char *type = "N"; // no special style
|
2011-09-28 21:44:53 +02:00
|
|
|
if (AssStyle *style = GetAssFile()->GetStyle(current->Style)) {
|
2009-05-13 22:24:21 +02:00
|
|
|
if (style->alignment >= 4) valign = 4;
|
|
|
|
if (style->alignment >= 7) valign = 9;
|
2011-09-28 21:43:11 +02:00
|
|
|
if (style->alignment == 1 || style->alignment == 4 || style->alignment == 7) halign = "L";
|
|
|
|
if (style->alignment == 3 || style->alignment == 6 || style->alignment == 9) halign = "R";
|
|
|
|
if (style->italic) type = "I";
|
2009-05-13 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hack: If an italics-tag (\i1) appears anywhere in the line,
|
|
|
|
// make it all italics
|
2011-09-28 21:43:11 +02:00
|
|
|
if (current->Text.Find("\\i1") != wxNOT_FOUND) type = "I";
|
2009-05-13 22:24:21 +02:00
|
|
|
|
|
|
|
// Write header
|
|
|
|
AssTime end = current->End;
|
|
|
|
|
|
|
|
// Subtract one frame if the end time of the current line is equal to the
|
|
|
|
// start of next one, since the end timestamp is inclusive and the lines
|
|
|
|
// would overlap if left as is.
|
2011-12-22 22:28:51 +01:00
|
|
|
if (nextl_start > 0 && end == nextl_start)
|
|
|
|
end = ft->FPS().TimeAtFrame(ft->FPS().FrameAtTime(end, agi::vfr::END) - 1, agi::vfr::END);
|
2009-05-13 22:24:21 +02:00
|
|
|
|
2011-12-22 22:29:09 +01:00
|
|
|
wxString header = wxString::Format("SUB[%i%s%s ", valign, halign, type) + ft->ToSMPTE(current->Start) + ">" + ft->ToSMPTE(end) + "]\r\n";
|
2009-05-13 22:24:21 +02:00
|
|
|
|
|
|
|
// Process text
|
2011-09-28 21:43:11 +02:00
|
|
|
wxString lineEnd = "\r\n";
|
2009-05-13 22:24:21 +02:00
|
|
|
current->StripTags();
|
2011-09-28 21:44:53 +02:00
|
|
|
current->Text.Replace("\\h", " ", true);
|
|
|
|
current->Text.Replace("\\n", lineEnd, true);
|
|
|
|
current->Text.Replace("\\N", lineEnd, true);
|
|
|
|
while (current->Text.Replace(lineEnd + lineEnd, lineEnd, true));
|
2009-05-13 22:24:21 +02:00
|
|
|
|
|
|
|
return header + current->Text;
|
2009-07-29 07:43:02 +02:00
|
|
|
}
|