2006-02-26 23:45:34 +01:00
|
|
|
// Copyright (c) 2006, 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_srt.cpp
|
|
|
|
/// @brief Reading/writing SubRip format subtitles (.SRT)
|
|
|
|
/// @ingroup subtitle_io
|
|
|
|
///
|
2006-02-26 23:45:34 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-03-29 01:59:01 +02:00
|
|
|
#include "subtitle_format_srt.h"
|
|
|
|
|
|
|
|
#include "ass_attachment.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
2011-11-25 20:27:51 +01:00
|
|
|
#include "ass_style.h"
|
2012-03-29 01:59:01 +02:00
|
|
|
#include "utils.h"
|
2006-02-26 23:45:34 +01:00
|
|
|
#include "text_file_reader.h"
|
2006-02-27 22:57:10 +01:00
|
|
|
#include "text_file_writer.h"
|
2006-02-26 23:45:34 +01:00
|
|
|
|
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/case_conv.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <boost/regex.hpp>
|
2012-11-19 06:10:36 +01:00
|
|
|
#include <map>
|
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
DEFINE_SIMPLE_EXCEPTION(SRTParseError, SubtitleFormatParseError, "subtitle_io/parse/srt")
|
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
namespace {
|
|
|
|
class SrtTagParser {
|
|
|
|
struct FontAttribs {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string face;
|
|
|
|
std::string size;
|
|
|
|
std::string color;
|
2011-11-25 20:27:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum TagType {
|
2012-03-25 06:05:06 +02:00
|
|
|
// leave 0 unused so indexing an unknown tag in the map won't clash
|
2011-11-25 20:27:51 +01:00
|
|
|
TAG_BOLD_OPEN = 1,
|
|
|
|
TAG_BOLD_CLOSE,
|
|
|
|
TAG_ITALICS_OPEN,
|
|
|
|
TAG_ITALICS_CLOSE,
|
|
|
|
TAG_UNDERLINE_OPEN,
|
|
|
|
TAG_UNDERLINE_CLOSE,
|
|
|
|
TAG_STRIKEOUT_OPEN,
|
|
|
|
TAG_STRIKEOUT_CLOSE,
|
|
|
|
TAG_FONT_OPEN,
|
2011-12-28 22:27:06 +01:00
|
|
|
TAG_FONT_CLOSE
|
2011-11-25 20:27:51 +01:00
|
|
|
};
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
const boost::regex tag_matcher;
|
|
|
|
const boost::regex attrib_matcher;
|
|
|
|
const boost::regex is_quoted;
|
|
|
|
std::map<std::string, TagType> tag_name_cases;
|
2011-11-25 20:27:51 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
SrtTagParser()
|
2013-01-04 16:01:50 +01:00
|
|
|
: tag_matcher("^(.*?)<(/?b|/?i|/?u|/?s|/?font)([^>]*)>(.*)$", boost::regex::icase)
|
|
|
|
, attrib_matcher("^[[:space:]]+(face|size|color)=('[^']*'|\"[^\"]*\"|[^[:space:]]+)", boost::regex::icase)
|
|
|
|
, is_quoted("^(['\"]).*\\1$")
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
|
|
|
tag_name_cases["b"] = TAG_BOLD_OPEN;
|
|
|
|
tag_name_cases["/b"] = TAG_BOLD_CLOSE;
|
|
|
|
tag_name_cases["i"] = TAG_ITALICS_OPEN;
|
|
|
|
tag_name_cases["/i"] = TAG_ITALICS_CLOSE;
|
|
|
|
tag_name_cases["u"] = TAG_UNDERLINE_OPEN;
|
|
|
|
tag_name_cases["/u"] = TAG_UNDERLINE_CLOSE;
|
|
|
|
tag_name_cases["s"] = TAG_STRIKEOUT_OPEN;
|
|
|
|
tag_name_cases["/s"] = TAG_STRIKEOUT_CLOSE;
|
|
|
|
tag_name_cases["font"] = TAG_FONT_OPEN;
|
|
|
|
tag_name_cases["/font"] = TAG_FONT_CLOSE;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string ToAss(std::string srt)
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
|
|
|
int bold_level = 0;
|
|
|
|
int italics_level = 0;
|
|
|
|
int underline_level = 0;
|
|
|
|
int strikeout_level = 0;
|
|
|
|
std::vector<FontAttribs> font_stack;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string ass; // result to be built
|
2011-11-25 20:27:51 +01:00
|
|
|
|
|
|
|
while (!srt.empty())
|
|
|
|
{
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::smatch result;
|
|
|
|
if (!regex_match(srt, result, tag_matcher))
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
|
|
|
// no more tags could be matched, end of string
|
|
|
|
ass.append(srt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we found a tag, translate it
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string pre_text = result.str(1);
|
|
|
|
std::string tag_name = result.str(2);
|
|
|
|
std::string tag_attrs = result.str(3);
|
|
|
|
std::string post_text = result.str(4);
|
2011-11-25 20:27:51 +01:00
|
|
|
|
|
|
|
// the text before the tag goes through unchanged
|
|
|
|
ass.append(pre_text);
|
|
|
|
// the text after the tag is the input for next iteration
|
|
|
|
srt = post_text;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::to_lower(tag_name);
|
|
|
|
switch (tag_name_cases[tag_name])
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
|
|
|
case TAG_BOLD_OPEN:
|
|
|
|
if (bold_level == 0)
|
|
|
|
ass.append("{\\b1}");
|
|
|
|
bold_level++;
|
|
|
|
break;
|
|
|
|
case TAG_BOLD_CLOSE:
|
|
|
|
if (bold_level == 1)
|
|
|
|
ass.append("{\\b}");
|
|
|
|
if (bold_level > 0)
|
|
|
|
bold_level--;
|
|
|
|
break;
|
|
|
|
case TAG_ITALICS_OPEN:
|
|
|
|
if (italics_level == 0)
|
|
|
|
ass.append("{\\i1}");
|
|
|
|
italics_level++;
|
|
|
|
break;
|
|
|
|
case TAG_ITALICS_CLOSE:
|
|
|
|
if (italics_level == 1)
|
|
|
|
ass.append("{\\i}");
|
|
|
|
if (italics_level > 0)
|
|
|
|
italics_level--;
|
|
|
|
break;
|
|
|
|
case TAG_UNDERLINE_OPEN:
|
|
|
|
if (underline_level == 0)
|
|
|
|
ass.append("{\\u1}");
|
|
|
|
underline_level++;
|
|
|
|
break;
|
|
|
|
case TAG_UNDERLINE_CLOSE:
|
|
|
|
if (underline_level == 1)
|
|
|
|
ass.append("{\\u}");
|
|
|
|
if (underline_level > 0)
|
|
|
|
underline_level--;
|
|
|
|
break;
|
|
|
|
case TAG_STRIKEOUT_OPEN:
|
|
|
|
if (strikeout_level == 0)
|
|
|
|
ass.append("{\\s1}");
|
|
|
|
strikeout_level++;
|
|
|
|
break;
|
|
|
|
case TAG_STRIKEOUT_CLOSE:
|
|
|
|
if (strikeout_level == 1)
|
|
|
|
ass.append("{\\s}");
|
|
|
|
if (strikeout_level > 0)
|
|
|
|
strikeout_level--;
|
|
|
|
break;
|
|
|
|
case TAG_FONT_OPEN:
|
|
|
|
{
|
|
|
|
// new attributes to fill in
|
|
|
|
FontAttribs new_attribs;
|
|
|
|
FontAttribs old_attribs;
|
|
|
|
// start out with any previous ones on stack
|
|
|
|
if (font_stack.size() > 0)
|
|
|
|
old_attribs = font_stack.back();
|
|
|
|
new_attribs = old_attribs;
|
|
|
|
// now find all attributes on this font tag
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::smatch result;
|
|
|
|
while (regex_search(tag_attrs, result, attrib_matcher))
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
|
|
|
// get attribute name and values
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string attr_name = result.str(1);
|
|
|
|
std::string attr_value = result.str(2);
|
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
// clean them
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::to_lower(attr_name);
|
|
|
|
if (regex_match(attr_value, is_quoted))
|
|
|
|
attr_value = attr_value.substr(1, attr_value.size() - 2);
|
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
// handle the attributes
|
|
|
|
if (attr_name == "face")
|
2013-01-04 16:01:50 +01:00
|
|
|
new_attribs.face = str(boost::format("{\\fn%s}") % attr_value);
|
2011-11-25 20:27:51 +01:00
|
|
|
else if (attr_name == "size")
|
2013-01-04 16:01:50 +01:00
|
|
|
new_attribs.size = str(boost::format("{\\fs%s}") % attr_value);
|
2011-11-25 20:27:51 +01:00
|
|
|
else if (attr_name == "color")
|
2013-01-04 16:01:50 +01:00
|
|
|
new_attribs.color = str(boost::format("{\\c%s}") % agi::Color(attr_value).GetAssOverrideFormatted());
|
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
// remove this attribute to prepare for the next
|
2013-01-04 16:01:50 +01:00
|
|
|
tag_attrs = result.suffix().str();
|
2011-11-25 20:27:51 +01:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
// the attributes changed from old are then written out
|
|
|
|
if (new_attribs.face != old_attribs.face)
|
|
|
|
ass.append(new_attribs.face);
|
|
|
|
if (new_attribs.size != old_attribs.size)
|
|
|
|
ass.append(new_attribs.size);
|
|
|
|
if (new_attribs.color != old_attribs.color)
|
|
|
|
ass.append(new_attribs.color);
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
// lastly dump the new attributes state onto the stack
|
|
|
|
font_stack.push_back(new_attribs);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TAG_FONT_CLOSE:
|
|
|
|
{
|
|
|
|
// this requires a font stack entry
|
|
|
|
if (font_stack.empty())
|
|
|
|
break;
|
|
|
|
// get the current attribs
|
|
|
|
FontAttribs cur_attribs = font_stack.back();
|
|
|
|
// remove them from the stack
|
|
|
|
font_stack.pop_back();
|
|
|
|
// grab the old attributes if there are any
|
|
|
|
FontAttribs old_attribs;
|
|
|
|
if (font_stack.size() > 0)
|
|
|
|
old_attribs = font_stack.back();
|
|
|
|
// then restore the attributes to previous settings
|
|
|
|
if (cur_attribs.face != old_attribs.face)
|
|
|
|
{
|
|
|
|
if (old_attribs.face.empty())
|
|
|
|
ass.append("{\\fn}");
|
|
|
|
else
|
|
|
|
ass.append(old_attribs.face);
|
|
|
|
}
|
|
|
|
if (cur_attribs.size != old_attribs.size)
|
|
|
|
{
|
|
|
|
if (old_attribs.size.empty())
|
|
|
|
ass.append("{\\fs}");
|
|
|
|
else
|
|
|
|
ass.append(old_attribs.size);
|
|
|
|
}
|
|
|
|
if (cur_attribs.color != old_attribs.color)
|
|
|
|
{
|
|
|
|
if (old_attribs.color.empty())
|
|
|
|
ass.append("{\\c}");
|
|
|
|
else
|
|
|
|
ass.append(old_attribs.color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// unknown tag, replicate it in the output
|
|
|
|
ass.append("<").append(tag_name).append(tag_attrs).append(">");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make it a little prettier, join tag groups
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::replace_all(ass, "}{", "");
|
2011-11-25 20:27:51 +01:00
|
|
|
|
|
|
|
return ass;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
AssTime ReadSRTTime(std::string const& ts)
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
|
|
|
// For the sake of your sanity, please do not read this function.
|
|
|
|
|
|
|
|
int d, h, m, s, ms;
|
|
|
|
d = h = m = s = ms = 0;
|
|
|
|
|
|
|
|
size_t ci = 0;
|
|
|
|
int ms_chars = 0;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
for (; ci < ts.size(); ++ci)
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
2013-01-04 16:01:50 +01:00
|
|
|
switch (ts[ci])
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
2013-01-04 16:01:50 +01:00
|
|
|
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
|
|
|
|
s = s * 10 + (ts[ci] - '0');
|
2011-11-25 20:27:51 +01:00
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
d = h;
|
|
|
|
h = m;
|
|
|
|
m = s;
|
|
|
|
s = 0;
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
ci++;
|
|
|
|
goto milliseconds;
|
|
|
|
default:
|
2013-01-04 16:01:50 +01:00
|
|
|
ci = ts.size();
|
2011-11-25 20:27:51 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
milliseconds:
|
2013-01-04 16:01:50 +01:00
|
|
|
for (; ci < ts.size(); ++ci)
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
2013-01-04 16:01:50 +01:00
|
|
|
if (!isdigit(ts[ci])) break;
|
|
|
|
|
|
|
|
ms = ms * 10 + (ts[ci] - '0');
|
|
|
|
ms_chars++;
|
2011-11-25 20:27:51 +01:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
ms *= pow(10, 3 - ms_chars);
|
2011-11-25 20:27:51 +01:00
|
|
|
|
2011-12-22 22:28:51 +01:00
|
|
|
return ms + 1000*(s + 60*(m + 60*(h + d*24)));
|
2011-11-25 20:27:51 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string WriteSRTTime(AssTime const& ts)
|
2011-11-25 20:27:51 +01:00
|
|
|
{
|
2013-01-04 16:01:50 +01:00
|
|
|
return str(boost::format("%02d:%02d:%02d,%03d") % ts.GetTimeHours() % ts.GetTimeMinutes() % ts.GetTimeSeconds() % ts.GetTimeMiliseconds());
|
2011-11-25 20:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
SRTSubtitleFormat::SRTSubtitleFormat()
|
|
|
|
: SubtitleFormat("SubRip")
|
|
|
|
{
|
2006-12-26 19:26:13 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> SRTSubtitleFormat::GetReadWildcards() const {
|
|
|
|
std::vector<std::string> formats;
|
|
|
|
formats.push_back("srt");
|
2006-12-26 19:26:13 +01:00
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> SRTSubtitleFormat::GetWriteWildcards() const {
|
2006-12-26 19:26:13 +01:00
|
|
|
return GetReadWildcards();
|
|
|
|
}
|
|
|
|
|
2012-12-05 16:16:10 +01:00
|
|
|
enum ParseState {
|
|
|
|
STATE_INITIAL,
|
|
|
|
STATE_TIMESTAMP,
|
|
|
|
STATE_FIRST_LINE_OF_BODY,
|
|
|
|
STATE_REST_OF_BODY,
|
|
|
|
STATE_LAST_WAS_BLANK
|
|
|
|
};
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void SRTSubtitleFormat::ReadFile(AssFile *target, agi::fs::path const& filename, std::string const& encoding) const {
|
2006-02-26 23:45:34 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
TextFileReader file(filename, encoding);
|
2012-01-26 21:08:38 +01:00
|
|
|
target->LoadDefault(false);
|
2006-02-26 23:45:34 +01:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
// See parsing algorithm at <http://devel.aegisub.org/wiki/SubtitleFormats/SRT>
|
2007-01-15 22:35:34 +01:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
// "hh:mm:ss,fff --> hh:mm:ss,fff" (e.g. "00:00:04,070 --> 00:00:10,04")
|
2013-06-22 16:54:56 +02:00
|
|
|
const boost::regex timestamp_regex("^([0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2},[0-9]{1,}) --> ([0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2},[0-9]{1,})");
|
2007-01-15 22:35:34 +01:00
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
SrtTagParser tag_parser;
|
|
|
|
|
2012-12-05 16:16:10 +01:00
|
|
|
ParseState state = STATE_INITIAL;
|
2010-06-20 21:07:43 +02:00
|
|
|
int line_num = 0;
|
|
|
|
int linebreak_debt = 0;
|
|
|
|
AssDialogue *line = 0;
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string text;
|
2010-06-20 21:07:43 +02:00
|
|
|
while (file.HasMoreLines()) {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string text_line = file.ReadLineFromFile();
|
|
|
|
++line_num;
|
|
|
|
boost::trim(text_line);
|
2010-06-20 21:07:43 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::smatch timestamp_match;
|
2010-06-20 21:07:43 +02:00
|
|
|
switch (state) {
|
2012-12-05 16:16:10 +01:00
|
|
|
case STATE_INITIAL:
|
|
|
|
// ignore leading blank lines
|
|
|
|
if (text_line.empty()) break;
|
2013-01-04 16:01:50 +01:00
|
|
|
if (all(text_line, boost::is_digit())) {
|
2010-06-20 21:07:43 +02:00
|
|
|
// found the line number, throw it away and hope for timestamps
|
2012-12-05 16:16:10 +01:00
|
|
|
state = STATE_TIMESTAMP;
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-06-22 16:47:51 +02:00
|
|
|
if (regex_search(text_line, timestamp_match, timestamp_regex))
|
2010-06-20 21:07:43 +02:00
|
|
|
goto found_timestamps;
|
2011-11-25 20:27:51 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
throw SRTParseError(str(boost::format("Parsing SRT: Expected subtitle index at line %d") % line_num), 0);
|
|
|
|
|
2012-12-05 16:16:10 +01:00
|
|
|
case STATE_TIMESTAMP:
|
2013-06-22 16:47:51 +02:00
|
|
|
if (!regex_search(text_line, timestamp_match, timestamp_regex))
|
2013-01-04 16:01:50 +01:00
|
|
|
throw SRTParseError(str(boost::format("Parsing SRT: Expected timestamp pair at line %d") % line_num), 0);
|
2010-06-20 21:07:43 +02:00
|
|
|
found_timestamps:
|
2011-11-25 20:27:51 +01:00
|
|
|
if (line) {
|
|
|
|
// finalize active line
|
2012-12-04 23:35:59 +01:00
|
|
|
line->Text = tag_parser.ToAss(text);
|
|
|
|
text.clear();
|
2010-06-20 21:07:43 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
// create new subtitle
|
2011-11-25 20:27:51 +01:00
|
|
|
line = new AssDialogue;
|
2013-01-04 16:01:50 +01:00
|
|
|
line->Start = ReadSRTTime(timestamp_match.str(1));
|
|
|
|
line->End = ReadSRTTime(timestamp_match.str(2));
|
2010-06-20 21:07:43 +02:00
|
|
|
// store pointer to subtitle, we'll continue working on it
|
2012-10-12 19:16:39 +02:00
|
|
|
target->Line.push_back(*line);
|
2010-06-20 21:07:43 +02:00
|
|
|
// next we're reading the text
|
2012-12-05 16:16:10 +01:00
|
|
|
state = STATE_FIRST_LINE_OF_BODY;
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2012-12-05 16:16:10 +01:00
|
|
|
case STATE_FIRST_LINE_OF_BODY:
|
2011-11-25 20:27:51 +01:00
|
|
|
if (text_line.empty()) {
|
2010-06-20 21:07:43 +02:00
|
|
|
// that's not very interesting... blank subtitle?
|
2012-12-05 16:16:10 +01:00
|
|
|
state = STATE_LAST_WAS_BLANK;
|
2010-06-20 21:42:13 +02:00
|
|
|
// no previous line that needs a line break after
|
|
|
|
linebreak_debt = 0;
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
text.append(text_line);
|
2012-12-05 16:16:10 +01:00
|
|
|
state = STATE_REST_OF_BODY;
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2012-12-05 16:16:10 +01:00
|
|
|
case STATE_REST_OF_BODY:
|
2011-11-25 20:27:51 +01:00
|
|
|
if (text_line.empty()) {
|
2012-12-05 16:16:10 +01:00
|
|
|
// Might be either the gap between two subtitles or just a
|
|
|
|
// blank line in the middle of a subtitle, so defer adding
|
|
|
|
// the line break until we check what's on the next line
|
|
|
|
state = STATE_LAST_WAS_BLANK;
|
2010-06-20 21:07:43 +02:00
|
|
|
linebreak_debt = 1;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
text.append("\\N");
|
|
|
|
text.append(text_line);
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2012-12-05 16:16:10 +01:00
|
|
|
case STATE_LAST_WAS_BLANK:
|
|
|
|
++linebreak_debt;
|
|
|
|
if (text_line.empty()) break;
|
2013-01-04 16:01:50 +01:00
|
|
|
if (all(text_line, boost::is_digit())) {
|
2012-12-05 16:16:10 +01:00
|
|
|
// Hopefully it's the start of a new subtitle, and the
|
|
|
|
// previous blank line(s) were the gap between subtitles
|
|
|
|
state = STATE_TIMESTAMP;
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-06-22 16:47:51 +02:00
|
|
|
if (regex_search(text_line, timestamp_match, timestamp_regex))
|
2010-06-20 21:07:43 +02:00
|
|
|
goto found_timestamps;
|
2011-11-25 20:27:51 +01:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
// assume it's a continuation of the subtitle text
|
|
|
|
// resolve our line break debt and append the line text
|
|
|
|
while (linebreak_debt-- > 0)
|
2013-01-04 16:01:50 +01:00
|
|
|
text.append("\\N");
|
|
|
|
text.append(text_line);
|
2012-12-05 16:16:10 +01:00
|
|
|
state = STATE_REST_OF_BODY;
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
2006-02-26 23:45:34 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-06 06:04:57 +01:00
|
|
|
|
2012-12-04 23:35:59 +01:00
|
|
|
if (state == 1 || state == 2)
|
2011-09-28 21:44:53 +02:00
|
|
|
throw SRTParseError("Parsing SRT: Incomplete file", 0);
|
2010-06-20 21:07:43 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
if (line) // an unfinalized line
|
2012-12-04 23:35:59 +01:00
|
|
|
line->Text = tag_parser.ToAss(text);
|
2006-02-26 23:45:34 +01:00
|
|
|
}
|
2006-02-27 22:57:10 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void SRTSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& filename, std::string const& encoding) const {
|
2012-01-26 21:08:38 +01:00
|
|
|
TextFileWriter file(filename, encoding);
|
2006-02-27 22:57:10 +01:00
|
|
|
|
|
|
|
// Convert to SRT
|
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-07-24 04:39:32 +02:00
|
|
|
#ifdef _WIN32
|
2012-10-12 19:16:39 +02:00
|
|
|
ConvertNewlines(copy, "\r\n", false);
|
2012-07-24 04:39:32 +02:00
|
|
|
#else
|
2012-10-12 19:16:39 +02:00
|
|
|
ConvertNewlines(copy, "\n", false);
|
2012-07-24 04:39:32 +02:00
|
|
|
#endif
|
2006-02-27 22:57:10 +01:00
|
|
|
|
|
|
|
// Write lines
|
2012-11-05 17:20:58 +01:00
|
|
|
int i=0;
|
|
|
|
for (auto current : copy.Line | agi::of_type<AssDialogue>()) {
|
2013-01-04 16:01:50 +01:00
|
|
|
file.WriteLineToFile(std::to_string(++i));
|
2012-11-05 17:20:58 +01:00
|
|
|
file.WriteLineToFile(WriteSRTTime(current->Start) + " --> " + WriteSRTTime(current->End));
|
|
|
|
file.WriteLineToFile(ConvertTags(current));
|
|
|
|
file.WriteLineToFile("");
|
2006-02-27 22:57:10 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-25 20:28:19 +01:00
|
|
|
|
2012-03-29 01:59:01 +02:00
|
|
|
bool SRTSubtitleFormat::CanSave(const AssFile *file) const {
|
2012-12-30 00:53:56 +01:00
|
|
|
std::string supported_tags[] = { "\\b", "\\i", "\\s", "\\u" };
|
2012-03-29 01:59:01 +02:00
|
|
|
|
|
|
|
AssStyle defstyle;
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& line : file->Line) {
|
2012-03-29 01:59:01 +02:00
|
|
|
// Check style, if anything non-default is found, return false
|
2012-11-04 04:53:03 +01:00
|
|
|
if (const AssStyle *curstyle = dynamic_cast<const AssStyle*>(&line)) {
|
2012-03-29 01:59:01 +02:00
|
|
|
if (curstyle->GetEntryData() != defstyle.GetEntryData())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for attachments, if any is found, return false
|
2012-11-04 04:53:03 +01:00
|
|
|
if (dynamic_cast<const AssAttachment*>(&line)) return false;
|
2012-03-29 01:59:01 +02:00
|
|
|
|
|
|
|
// Check dialogue
|
2012-11-04 04:53:03 +01:00
|
|
|
if (const AssDialogue *curdiag = dynamic_cast<const AssDialogue*>(&line)) {
|
2012-12-02 19:24:49 +01:00
|
|
|
boost::ptr_vector<AssDialogueBlock> blocks(curdiag->ParseTags());
|
2012-11-05 17:20:58 +01:00
|
|
|
for (auto ovr : blocks | agi::of_type<AssDialogueBlockOverride>()) {
|
2012-03-29 01:59:01 +02:00
|
|
|
// Verify that all overrides used are supported
|
2012-12-11 00:32:36 +01:00
|
|
|
for (auto const& tag : ovr->Tags) {
|
|
|
|
if (!std::binary_search(supported_tags, std::end(supported_tags), tag.Name))
|
2012-03-29 01:59:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string SRTSubtitleFormat::ConvertTags(const AssDialogue *diag) const {
|
|
|
|
std::string final;
|
2011-11-25 20:28:19 +01:00
|
|
|
std::map<char, bool> tag_states;
|
|
|
|
tag_states['i'] = false;
|
|
|
|
tag_states['b'] = false;
|
|
|
|
tag_states['u'] = false;
|
|
|
|
tag_states['s'] = false;
|
|
|
|
|
2012-12-02 19:24:49 +01:00
|
|
|
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
|
2011-11-25 20:28:19 +01:00
|
|
|
|
2012-12-02 19:24:49 +01:00
|
|
|
for (auto& block : blocks) {
|
|
|
|
if (AssDialogueBlockOverride* ovr = dynamic_cast<AssDialogueBlockOverride*>(&block)) {
|
2011-11-25 20:28:19 +01:00
|
|
|
// Iterate through overrides
|
2012-12-11 00:32:36 +01:00
|
|
|
for (auto const& tag : ovr->Tags) {
|
|
|
|
if (tag.IsValid() && tag.Name.size() == 2) {
|
|
|
|
auto it = tag_states.find(tag.Name[1]);
|
2011-11-25 20:28:19 +01:00
|
|
|
if (it != tag_states.end()) {
|
2012-12-11 00:32:36 +01:00
|
|
|
bool temp = tag.Params[0].Get(false);
|
2011-11-25 20:28:19 +01:00
|
|
|
if (temp && !it->second)
|
2013-01-04 16:01:50 +01:00
|
|
|
final += str(boost::format("<%c>") % it->first);
|
2011-11-25 20:28:19 +01:00
|
|
|
if (!temp && it->second)
|
2013-01-04 16:01:50 +01:00
|
|
|
final += str(boost::format("</%c>") % it->first);
|
2011-11-25 20:28:19 +01:00
|
|
|
it->second = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Plain text
|
2013-01-04 16:01:50 +01:00
|
|
|
else if (AssDialogueBlockPlain *plain = dynamic_cast<AssDialogueBlockPlain*>(&block))
|
|
|
|
final += plain->GetText();
|
2011-11-25 20:28:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure all tags are closed
|
|
|
|
// Otherwise unclosed overrides might affect lines they shouldn't, see bug #809 for example
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto it : tag_states) {
|
|
|
|
if (it.second)
|
2013-01-04 16:01:50 +01:00
|
|
|
final += str(boost::format("</%c>") % it.first);
|
2011-11-25 20:28:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return final;
|
|
|
|
}
|