/*
- Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "subrip_reader.h"
#include "exceptions.h"
#include "util.h"
-#include <locked_sstream.h>
+#include "sub_assert.h"
+#include "raw_convert.h"
+#include "ssa_reader.h"
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
using namespace sub;
/** @param s Subtitle string encoded in UTF-8 */
-SubripReader::SubripReader (string const & s)
+SubripReader::SubripReader (string s)
{
- locked_stringstream str (s);
- this->read (boost::bind (&get_line_stringstream, &str));
+ this->read (boost::bind(&get_line_string, &s));
}
/** @param f Subtitle file encoded in UTF-8 */
RawSubtitle rs;
- /* This reader extracts no information about where the subtitle
- should be on screen, so its reference is TOP_OF_SUBTITLE.
- */
rs.vertical_position.line = 0;
rs.vertical_position.reference = TOP_OF_SUBTITLE;
rs.italic = false;
rs.underline = false;
rs.vertical_position.line = 0;
+ rs.vertical_position.reference = TOP_OF_SUBTITLE;
}
break;
case METADATA:
vector<string> b;
boost::algorithm::split (b, a[2], boost::is_any_of (","));
+ if (b.size() != 2) {
+ throw SubripError (t, "time in the format h:m:s,ms", _context);
+ }
+
+ int h, m, s, ms;
+
+ try {
+ h = lexical_cast<int>(a[0]);
+ } catch (boost::bad_lexical_cast &) {
+ throw SubripError (t, "integer hour value", _context);
+ }
+
+ try {
+ m = lexical_cast<int>(a[1]);
+ } catch (boost::bad_lexical_cast &) {
+ throw SubripError (t, "integer minute value", _context);
+ }
+
+ try {
+ s = lexical_cast<int>(b[0]);
+ } catch (boost::bad_lexical_cast &) {
+ throw SubripError (t, "integer second value", _context);
+ }
+
+ try {
+ ms = lexical_cast<int>(b[1]);
+ } catch (boost::bad_lexical_cast &) {
+ throw SubripError (t, "integer millisecond value", _context);
+ }
- return Time::from_hms (
- lexical_cast<int> (a[0]),
- lexical_cast<int> (a[1]),
- lexical_cast<int> (b[0]),
- lexical_cast<int> (b[1])
- );
+ return Time::from_hms (h, m, s, ms);
}
void
p.underline = false;
} else if (boost::starts_with (tag, "font")) {
maybe_content (p);
- boost::regex re (".*color=\"#([0123456789abcdef]+)\"");
+ boost::regex re (".*color=\"#([[:xdigit:]]+)\"");
boost::smatch match;
if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
p.colour = Colour::from_rgb_hex (match[1]);
colours.push_back (p.colour);
+ } else {
+ re = boost::regex (
+ ".*color=\"rgba\\("
+ "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
+ "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
+ "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
+ "[[:space:]]*([[:digit:]]+)[[:space:]]*"
+ "\\)\""
+ );
+ if (boost::regex_search (tag, match, re) && match.size() == 5) {
+ p.colour.r = raw_convert<int>(string(match[1])) / 255.0;
+ p.colour.g = raw_convert<int>(string(match[2])) / 255.0;
+ p.colour.b = raw_convert<int>(string(match[3])) / 255.0;
+ colours.push_back (p.colour);
+ } else {
+ throw SubripError (tag, "a colour in the format #rrggbb or rgba(rr,gg,bb,aa)", _context);
+ }
}
} else if (tag == "/font") {
maybe_content (p);
+ SUB_ASSERT (!colours.empty());
colours.pop_back ();
p.colour = colours.back ();
+ } else if (tag.size() > 0 && tag[0] == '\\') {
+ SSAReader::parse_style (p, tag, 288, 288);
}
tag.clear ();
state = TEXT;