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/
|
2006-02-26 23:45:34 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @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"
|
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <wx/regex.h>
|
|
|
|
#endif
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
2011-11-25 20:28:19 +01:00
|
|
|
#include "ass_override.h"
|
2011-11-25 20:27:51 +01:00
|
|
|
#include "ass_style.h"
|
|
|
|
#include "colorspace.h"
|
|
|
|
#include "compat.h"
|
2006-02-26 23:45:34 +01:00
|
|
|
#include "subtitle_format_srt.h"
|
|
|
|
#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
|
|
|
|
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 {
|
|
|
|
wxString face;
|
|
|
|
wxString size;
|
|
|
|
wxString color;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum TagType {
|
|
|
|
// leave 0 unused so indexing an unknown tag in the map won't clash
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
wxRegEx tag_matcher;
|
|
|
|
wxRegEx attrib_matcher;
|
|
|
|
std::map<wxString,TagType> tag_name_cases;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SrtTagParser()
|
|
|
|
: tag_matcher("^(.*?)<(/?b|/?i|/?u|/?s|/?font)([^>]*)>(.*)$", wxRE_ICASE|wxRE_ADVANCED)
|
|
|
|
, attrib_matcher("^[[:space:]]+(face|size|color)=('[^']*'|\"[^\"]*\"|[^[:space:]]+)", wxRE_ICASE|wxRE_ADVANCED)
|
|
|
|
{
|
|
|
|
if (!tag_matcher.IsValid())
|
|
|
|
throw agi::InternalError("Parsing SRT: Failed compiling tag matching regex", 0);
|
|
|
|
if (!attrib_matcher.IsValid())
|
|
|
|
throw agi::InternalError("Parsing SRT: Failed compiling tag attribute matching regex", 0);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString ToAss(wxString srt)
|
|
|
|
{
|
|
|
|
int bold_level = 0;
|
|
|
|
int italics_level = 0;
|
|
|
|
int underline_level = 0;
|
|
|
|
int strikeout_level = 0;
|
|
|
|
std::vector<FontAttribs> font_stack;
|
|
|
|
|
|
|
|
wxString ass; // result to be built
|
|
|
|
|
|
|
|
while (!srt.empty())
|
|
|
|
{
|
|
|
|
if (!tag_matcher.Matches(srt))
|
|
|
|
{
|
|
|
|
// no more tags could be matched, end of string
|
|
|
|
ass.append(srt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we found a tag, translate it
|
|
|
|
wxString pre_text = tag_matcher.GetMatch(srt, 1);
|
|
|
|
wxString tag_name = tag_matcher.GetMatch(srt, 2);
|
|
|
|
wxString tag_attrs = tag_matcher.GetMatch(srt, 3);
|
|
|
|
wxString post_text = tag_matcher.GetMatch(srt, 4);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
switch (tag_name_cases[tag_name.Lower()])
|
|
|
|
{
|
|
|
|
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
|
|
|
|
while (attrib_matcher.Matches(tag_attrs))
|
|
|
|
{
|
|
|
|
// get attribute name and values
|
|
|
|
wxString attr_name = attrib_matcher.GetMatch(tag_attrs, 1);
|
|
|
|
wxString attr_value = attrib_matcher.GetMatch(tag_attrs, 2);
|
|
|
|
// clean them
|
|
|
|
attr_name.MakeLower();
|
|
|
|
if ((attr_value.StartsWith("'") && attr_value.EndsWith("'")) ||
|
|
|
|
(attr_value.StartsWith("\"") && attr_value.EndsWith("\"")))
|
|
|
|
{
|
|
|
|
attr_value = attr_value.Mid(1, attr_value.Len()-2);
|
|
|
|
}
|
|
|
|
// handle the attributes
|
|
|
|
if (attr_name == "face")
|
|
|
|
{
|
|
|
|
new_attribs.face = wxString::Format("{\\fn%s}", attr_value);
|
|
|
|
}
|
|
|
|
else if (attr_name == "size")
|
|
|
|
{
|
|
|
|
new_attribs.size = wxString::Format("{\\fs%s}", attr_value);
|
|
|
|
}
|
|
|
|
else if (attr_name == "color")
|
|
|
|
{
|
|
|
|
wxColour wxcl = html_to_color(attr_value);
|
|
|
|
wxString colorstr = AssColor(wxcl).GetASSFormatted(false, false, false);
|
|
|
|
new_attribs.color = wxString::Format("{\\c%s}", colorstr);
|
|
|
|
}
|
|
|
|
// remove this attribute to prepare for the next
|
|
|
|
size_t attr_pos, attr_len;
|
|
|
|
attrib_matcher.GetMatch(&attr_pos, &attr_len, 0);
|
|
|
|
tag_attrs.erase(attr_pos, attr_len);
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
// 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
|
|
|
|
ass.Replace("}{", "", true);
|
|
|
|
|
|
|
|
return ass;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AssTime ReadSRTTime(wxString const& ts)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
for (; ci < ts.length(); ++ci)
|
|
|
|
{
|
|
|
|
char ch = ts[ci];
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
s = s * 10 + (ch - '0');
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
d = h;
|
|
|
|
h = m;
|
|
|
|
m = s;
|
|
|
|
s = 0;
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
ci++;
|
|
|
|
goto milliseconds;
|
|
|
|
default:
|
|
|
|
goto allparsed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto allparsed;
|
|
|
|
milliseconds:
|
|
|
|
for (; ci < ts.length(); ++ci)
|
|
|
|
{
|
|
|
|
char ch = ts[ci];
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
ms = ms * 10 + (ch - '0');
|
|
|
|
ms_chars++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto allparsed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allparsed:
|
|
|
|
while (ms_chars < 3) ms *= 10, ms_chars++;
|
|
|
|
while (ms_chars > 3) ms /= 10, ms_chars--;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
wxString WriteSRTTime(AssTime const& ts)
|
|
|
|
{
|
2011-12-22 22:29:09 +01:00
|
|
|
return wxString::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
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
wxArrayString SRTSubtitleFormat::GetReadWildcards() const {
|
2006-12-26 19:26:13 +01:00
|
|
|
wxArrayString formats;
|
2011-09-28 21:43:11 +02:00
|
|
|
formats.Add("srt");
|
2006-12-26 19:26:13 +01:00
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
wxArrayString SRTSubtitleFormat::GetWriteWildcards() const {
|
2006-12-26 19:26:13 +01:00
|
|
|
return GetReadWildcards();
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
void SRTSubtitleFormat::ReadFile(wxString const& filename, wxString const& encoding) {
|
2006-02-26 23:45:34 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
TextFileReader file(filename, encoding);
|
2007-01-06 06:04:57 +01:00
|
|
|
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")
|
2011-11-25 20:27:51 +01:00
|
|
|
wxRegEx timestamp_regex("^([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{1,}) --> ([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{1,})");
|
2010-06-20 21:07:43 +02:00
|
|
|
if (!timestamp_regex.IsValid())
|
|
|
|
throw agi::InternalError("Parsing SRT: Failed compiling regex", 0);
|
2007-01-15 22:35:34 +01:00
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
SrtTagParser tag_parser;
|
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
int state = 1;
|
|
|
|
int line_num = 0;
|
|
|
|
int linebreak_debt = 0;
|
|
|
|
AssDialogue *line = 0;
|
|
|
|
while (file.HasMoreLines()) {
|
|
|
|
wxString text_line = file.ReadLineFromFile();
|
|
|
|
line_num++;
|
|
|
|
text_line.Trim(true).Trim(false);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 1:
|
|
|
|
// start of file, no subtitles found yet
|
2011-11-25 20:27:51 +01:00
|
|
|
if (text_line.empty())
|
2010-06-20 21:07:43 +02:00
|
|
|
// ignore blank lines
|
|
|
|
break;
|
|
|
|
if (text_line.IsNumber()) {
|
|
|
|
// found the line number, throw it away and hope for timestamps
|
|
|
|
state = 2;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-25 20:27:51 +01:00
|
|
|
if (timestamp_regex.Matches(text_line))
|
2010-06-20 21:07:43 +02:00
|
|
|
goto found_timestamps;
|
2011-11-25 20:27:51 +01:00
|
|
|
|
|
|
|
throw SRTParseError(STD_STR(wxString::Format("Parsing SRT: Expected subtitle index at line %d", line_num)), 0);
|
2010-06-20 21:07:43 +02:00
|
|
|
case 2:
|
|
|
|
// want timestamps
|
2011-11-25 20:27:51 +01:00
|
|
|
if (timestamp_regex.Matches(text_line) == false)
|
2010-06-20 21:07:43 +02:00
|
|
|
// bad format
|
2011-11-25 20:27:51 +01:00
|
|
|
throw SRTParseError(STD_STR(wxString::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
|
|
|
|
line->Text = tag_parser.ToAss(line->Text);
|
2010-06-20 21:07:43 +02:00
|
|
|
line = 0;
|
|
|
|
}
|
|
|
|
// create new subtitle
|
2011-11-25 20:27:51 +01:00
|
|
|
line = new AssDialogue;
|
2011-09-28 21:43:11 +02:00
|
|
|
line->group = "[Events]";
|
|
|
|
line->Style = "Default";
|
2007-01-15 22:35:34 +01:00
|
|
|
line->Comment = false;
|
2011-11-25 20:27:51 +01:00
|
|
|
line->Start = ReadSRTTime(timestamp_regex.GetMatch(text_line, 1));
|
|
|
|
line->End = ReadSRTTime(timestamp_regex.GetMatch(text_line, 2));
|
2010-06-20 21:07:43 +02:00
|
|
|
// store pointer to subtitle, we'll continue working on it
|
2007-01-15 22:35:34 +01:00
|
|
|
Line->push_back(line);
|
2010-06-20 21:07:43 +02:00
|
|
|
// next we're reading the text
|
|
|
|
state = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// reading first line of subtitle text
|
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?
|
|
|
|
state = 5;
|
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;
|
|
|
|
}
|
|
|
|
line->Text.Append(text_line);
|
|
|
|
state = 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
// reading following line of subtitle text
|
2011-11-25 20:27:51 +01:00
|
|
|
if (text_line.empty()) {
|
2010-06-20 21:07:43 +02:00
|
|
|
// blank line, next may begin a new subtitle
|
|
|
|
state = 5;
|
2010-06-20 21:42:13 +02:00
|
|
|
// previous line needs a line break after
|
2010-06-20 21:07:43 +02:00
|
|
|
linebreak_debt = 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-09-28 21:43:11 +02:00
|
|
|
line->Text.Append("\\N").Append(text_line);
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
// blank line in subtitle text
|
|
|
|
linebreak_debt++;
|
2011-11-25 20:27:51 +01:00
|
|
|
if (text_line.empty())
|
2010-06-20 21:07:43 +02:00
|
|
|
// multiple blank lines in a row, just add a line break...
|
|
|
|
break;
|
|
|
|
if (text_line.IsNumber()) {
|
|
|
|
// must be a subtitle index!
|
|
|
|
// go for timestamps next
|
|
|
|
state = 2;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-25 20:27:51 +01:00
|
|
|
if (timestamp_regex.Matches(text_line))
|
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)
|
2011-09-28 21:43:11 +02:00
|
|
|
line->Text.Append("\\N");
|
2010-06-20 21:07:43 +02:00
|
|
|
line->Text.Append(text_line);
|
|
|
|
state = 4;
|
|
|
|
break;
|
|
|
|
default:
|
2011-11-25 20:27:51 +01:00
|
|
|
throw agi::InternalError(STD_STR(wxString::Format("Parsing SRT: Reached unexpected state %d", state)), 0);
|
2006-02-26 23:45:34 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-06 06:04:57 +01:00
|
|
|
|
2010-06-20 21:07:43 +02: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
|
|
|
}
|
|
|
|
|
2011-11-25 20:27:51 +01:00
|
|
|
if (line)
|
|
|
|
// an unfinalized line
|
|
|
|
line->Text = tag_parser.ToAss(line->Text);
|
2006-02-26 23:45:34 +01:00
|
|
|
}
|
2006-02-27 22:57:10 +01:00
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
void SRTSubtitleFormat::WriteFile(wxString const& filename, wxString const& encoding) {
|
|
|
|
TextFileWriter file(filename,encoding);
|
2006-02-27 22:57:10 +01:00
|
|
|
|
|
|
|
// Convert to SRT
|
|
|
|
CreateCopy();
|
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
|
|
|
ConvertNewlines("\r\n", false);
|
2006-02-27 22:57:10 +01:00
|
|
|
|
|
|
|
// Write lines
|
|
|
|
int i=1;
|
2011-11-25 20:28:19 +01:00
|
|
|
for (std::list<AssEntry*>::iterator cur = Line->begin(); cur != Line->end(); ++cur) {
|
2011-09-28 21:44:53 +02:00
|
|
|
if (AssDialogue *current = dynamic_cast<AssDialogue*>(*cur)) {
|
2011-11-25 20:27:51 +01:00
|
|
|
file.WriteLineToFile(wxString::Format("%d", i++));
|
|
|
|
file.WriteLineToFile(WriteSRTTime(current->Start) + " --> " + WriteSRTTime(current->End));
|
2011-11-25 20:28:19 +01:00
|
|
|
file.WriteLineToFile(ConvertTags(current));
|
2011-09-28 21:43:11 +02:00
|
|
|
file.WriteLineToFile("");
|
2006-02-27 22:57:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-19 05:34:53 +02:00
|
|
|
ClearCopy();
|
2006-02-27 22:57:10 +01:00
|
|
|
}
|
2011-11-25 20:28:19 +01:00
|
|
|
|
|
|
|
wxString SRTSubtitleFormat::ConvertTags(AssDialogue *diag) {
|
|
|
|
wxString final;
|
|
|
|
std::map<char, bool> tag_states;
|
|
|
|
tag_states['i'] = false;
|
|
|
|
tag_states['b'] = false;
|
|
|
|
tag_states['u'] = false;
|
|
|
|
tag_states['s'] = false;
|
|
|
|
|
|
|
|
diag->ParseASSTags();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < diag->Blocks.size(); ++i) {
|
|
|
|
if (AssDialogueBlockOverride* block = dynamic_cast<AssDialogueBlockOverride*>(diag->Blocks[i])) {
|
|
|
|
// Iterate through overrides
|
|
|
|
for (size_t j = 0; j < block->Tags.size(); j++) {
|
|
|
|
AssOverrideTag *tag = block->Tags[j];
|
|
|
|
if (tag->IsValid()) {
|
|
|
|
std::map<char, bool>::iterator it = tag_states.find(tag->Name[1]);
|
|
|
|
if (it != tag_states.end()) {
|
|
|
|
bool temp = tag->Params[0]->Get<bool>();
|
|
|
|
if (temp && !it->second)
|
|
|
|
final += wxString::Format("<%c>", it->first);
|
|
|
|
if (!temp && it->second)
|
|
|
|
final += wxString::Format("</%c>", it->first);
|
|
|
|
it->second = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Plain text
|
|
|
|
else if (AssDialogueBlockPlain *plain = dynamic_cast<AssDialogueBlockPlain*>(diag->Blocks[i])) {
|
|
|
|
final += plain->GetText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure all tags are closed
|
|
|
|
// Otherwise unclosed overrides might affect lines they shouldn't, see bug #809 for example
|
|
|
|
for (std::map<char, bool>::iterator it = tag_states.begin(); it != tag_states.end(); ++it) {
|
|
|
|
if (it->second)
|
|
|
|
final += wxString::Format("</%c>", it->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
diag->ClearBlocks();
|
|
|
|
|
|
|
|
return final;
|
|
|
|
}
|