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/
|
|
|
|
|
|
|
|
/// @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"
|
|
|
|
|
2012-12-01 20:36:30 +01:00
|
|
|
#include <cstdio>
|
2009-09-10 15:06:40 +02:00
|
|
|
|
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"
|
|
|
|
|
2012-11-05 17:20:58 +01:00
|
|
|
#include <libaegisub/of_type_adaptor.h>
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2013-01-26 02:57:46 +01:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
TranStationSubtitleFormat::TranStationSubtitleFormat()
|
|
|
|
: SubtitleFormat("TranStation")
|
|
|
|
{
|
2008-07-18 03:36:20 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> TranStationSubtitleFormat::GetWriteWildcards() const {
|
|
|
|
std::vector<std::string> formats;
|
|
|
|
formats.push_back("transtation.txt");
|
2008-07-18 03:36:20 +02:00
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void TranStationSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& filename, std::string const& encoding) const {
|
2012-03-29 21:04:36 +02:00
|
|
|
agi::vfr::Framerate fps = AskForFPS(false, true);
|
|
|
|
if (!fps.IsLoaded()) return;
|
2008-07-18 03:36:20 +02:00
|
|
|
|
|
|
|
// Convert to TranStation
|
2012-01-26 21:08:38 +01:00
|
|
|
AssFile copy(*src);
|
|
|
|
copy.Sort();
|
2012-10-12 19:16:39 +02:00
|
|
|
StripComments(copy);
|
|
|
|
RecombineOverlaps(copy);
|
|
|
|
MergeIdentical(copy);
|
2012-12-04 23:35:59 +01:00
|
|
|
StripTags(copy);
|
|
|
|
ConvertNewlines(copy, "\r\n");
|
2008-07-18 03:36:20 +02:00
|
|
|
|
2012-03-29 21:04:36 +02:00
|
|
|
SmpteFormatter ft(fps);
|
|
|
|
TextFileWriter file(filename, encoding);
|
2013-11-21 18:13:36 +01:00
|
|
|
AssDialogue *prev = nullptr;
|
2014-03-07 18:02:24 +01:00
|
|
|
for (auto cur : copy.Events | agi::of_type<AssDialogue>()) {
|
2012-11-05 17:20:58 +01:00
|
|
|
if (prev) {
|
2012-03-29 21:04:36 +02:00
|
|
|
file.WriteLineToFile(ConvertLine(©, prev, fps, 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
|
|
|
|
2012-11-05 17:20:58 +01:00
|
|
|
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)
|
2012-03-29 21:04:36 +02:00
|
|
|
file.WriteLineToFile(ConvertLine(©, prev, fps, 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-18 03:36:20 +02:00
|
|
|
}
|
2009-05-13 22:24:21 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string TranStationSubtitleFormat::ConvertLine(AssFile *file, AssDialogue *current, agi::vfr::Framerate const& fps, SmpteFormatter const& ft, int nextl_start) const {
|
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
|
2013-01-04 16:01:50 +01:00
|
|
|
if (AssStyle *style = file->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
|
2013-01-04 16:01:50 +01:00
|
|
|
if (current->Text.get().find("\\i1") != std::string::npos) 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)
|
2012-03-29 21:04:36 +02:00
|
|
|
end = fps.TimeAtFrame(fps.FrameAtTime(end, agi::vfr::END) - 1, agi::vfr::END);
|
2009-05-13 22:24:21 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string header = str(boost::format("SUB[%i%s%s %s>%s]\r\n") % valign % halign % type % ft.ToSMPTE(current->Start) % ft.ToSMPTE(end));
|
|
|
|
return header + current->Text.get();
|
2009-07-29 07:43:02 +02:00
|
|
|
}
|