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/
|
|
|
|
|
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"
|
|
|
|
#include "text_file_writer.h"
|
|
|
|
|
2014-07-06 16:28:58 +02:00
|
|
|
#include <libaegisub/ass/smpte.h>
|
|
|
|
#include <libaegisub/ass/time.h>
|
2014-05-29 00:19:05 +02:00
|
|
|
#include <libaegisub/format.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
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 {
|
2014-05-23 00:40:16 +02:00
|
|
|
return {"transtation.txt"};
|
2008-07-18 03:36:20 +02:00
|
|
|
}
|
|
|
|
|
2014-03-26 16:14:08 +01:00
|
|
|
void TranStationSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& filename, agi::vfr::Framerate const& vfps, std::string const& encoding) const {
|
2014-07-06 16:28:58 +02:00
|
|
|
auto fps = AskForFPS(false, true, vfps);
|
2012-03-29 21:04:36 +02:00
|
|
|
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
|
|
|
|
2014-07-06 16:28:58 +02:00
|
|
|
agi::SmpteFormatter ft(fps);
|
2012-03-29 21:04:36 +02:00
|
|
|
TextFileWriter file(filename, encoding);
|
2014-03-07 19:58:51 +01:00
|
|
|
const AssDialogue *prev = nullptr;
|
|
|
|
for (auto const& cur : copy.Events) {
|
2012-11-05 17:20:58 +01:00
|
|
|
if (prev) {
|
2014-03-07 19:58:51 +01: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
|
|
|
|
2014-03-07 19:58:51 +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
|
|
|
|
2014-07-06 16:28:58 +02:00
|
|
|
std::string TranStationSubtitleFormat::ConvertLine(AssFile *file, const AssDialogue *current, agi::vfr::Framerate const& fps, agi::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
|
2014-07-06 16:28:58 +02:00
|
|
|
agi::Time end = current->End;
|
2009-05-13 22:24:21 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-05-29 00:19:05 +02:00
|
|
|
std::string header = agi::format("SUB[%i%s%s %s>%s]\r\n", valign, halign, type, ft.ToSMPTE(current->Start), ft.ToSMPTE(end));
|
2013-01-04 16:01:50 +01:00
|
|
|
return header + current->Text.get();
|
2009-07-29 07:43:02 +02:00
|
|
|
}
|