2 Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/subrip_reader.cc
21 * @brief SubripReader class.
24 #include "subrip_reader.h"
25 #include "exceptions.h"
27 #include "sub_assert.h"
28 #include "raw_convert.h"
29 #include "ssa_reader.h"
30 #include <boost/algorithm/string.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/regex.hpp>
33 #include <boost/bind.hpp>
42 using boost::lexical_cast;
43 using boost::to_upper;
44 using boost::optional;
45 using boost::function;
46 using boost::algorithm::replace_all;
49 /** @param s Subtitle string encoded in UTF-8 */
50 SubripReader::SubripReader (string s)
52 this->read (boost::bind(&get_line_string, &s));
55 /** @param f Subtitle file encoded in UTF-8 */
56 SubripReader::SubripReader (FILE* f)
58 this->read (boost::bind (&get_line_file, f));
62 SubripReader::read (function<optional<string> ()> get_line)
72 rs.vertical_position.line = 0;
73 rs.vertical_position.reference = TOP_OF_SUBTITLE;
76 auto line = get_line ();
81 trim_right_if (*line, boost::is_any_of ("\n\r"));
82 remove_unicode_bom (line);
84 /* Keep some history in case there is an error to report */
85 _context.push_back (*line);
86 if (_context.size() > 5) {
87 _context.pop_front ();
94 /* a blank line at the start is ok */
100 /* Reset stuff that should not persist across separate subtitles */
103 rs.underline = false;
104 rs.vertical_position.line = 0;
105 rs.vertical_position.reference = TOP_OF_SUBTITLE;
112 /* Further trim this line, removing spaces from the end */
113 trim_right_if (*line, boost::is_any_of (" "));
115 boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "), boost::token_compress_on);
116 if (p.size() != 3 && p.size() != 7) {
117 for (int i = 0; i < 2; ++i) {
118 optional<string> ex = get_line ();
120 _context.push_back (*ex);
123 throw SubripError (*line, "a time/position line", _context);
126 rs.from = convert_time (p[0]);
127 rs.to = convert_time (p[2]);
129 /* XXX: should not ignore coordinate specifications */
135 if (line->empty ()) {
138 convert_line (*line, rs);
139 rs.vertical_position.line = rs.vertical_position.line.get() + 1;
147 SubripReader::convert_time (string t)
150 boost::algorithm::split (a, t, boost::is_any_of (":"));
152 throw SubripError (t, "time in the format h:m:s,ms", _context);
156 boost::algorithm::split (b, a[2], boost::is_any_of (","));
158 throw SubripError (t, "time in the format h:m:s,ms", _context);
164 h = lexical_cast<int>(a[0]);
165 } catch (boost::bad_lexical_cast &) {
166 throw SubripError (t, "integer hour value", _context);
170 m = lexical_cast<int>(a[1]);
171 } catch (boost::bad_lexical_cast &) {
172 throw SubripError (t, "integer minute value", _context);
176 s = lexical_cast<int>(b[0]);
177 } catch (boost::bad_lexical_cast &) {
178 throw SubripError (t, "integer second value", _context);
182 ms = lexical_cast<int>(b[1]);
183 } catch (boost::bad_lexical_cast &) {
184 throw SubripError (t, "integer millisecond value", _context);
187 return Time::from_hms (h, m, s, ms);
191 SubripReader::convert_line (string t, RawSubtitle& p)
200 vector<Colour> colours;
201 colours.push_back (Colour (1, 1, 1));
203 /* XXX: missing <font> support */
204 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
208 for (size_t i = 0; i < t.size(); ++i) {
211 if (t[i] == '<' || t[i] == '{') {
218 if (t[i] == '>' || t[i] == '}') {
222 } else if (tag == "/b") {
225 } else if (tag == "i") {
228 } else if (tag == "/i") {
231 } else if (tag == "u") {
234 } else if (tag == "/u") {
237 } else if (boost::starts_with (tag, "font")) {
239 boost::regex re (".*color=\"?#([[:xdigit:]]+)\"?");
241 if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
242 p.colour = Colour::from_rgb_hex (match[1]);
243 colours.push_back (p.colour);
247 "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
248 "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
249 "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
250 "[[:space:]]*([[:digit:]]+)[[:space:]]*"
253 if (boost::regex_search (tag, match, re) && match.size() == 5) {
254 p.colour.r = raw_convert<int>(string(match[1])) / 255.0;
255 p.colour.g = raw_convert<int>(string(match[2])) / 255.0;
256 p.colour.b = raw_convert<int>(string(match[3])) / 255.0;
257 colours.push_back (p.colour);
259 throw SubripError (tag, "a colour in the format #rrggbb or rgba(rr,gg,bb,aa)", _context);
262 } else if (tag == "/font") {
264 SUB_ASSERT (!colours.empty());
266 p.colour = colours.back ();
267 } else if (tag.size() > 0 && tag[0] == '\\') {
268 SSAReader::parse_style (p, tag, 288, 288);
273 tag += tolower (t[i]);
279 /* Strip Unicode U+202B (right-to-left embedding) as sometimes it is rendered
280 as a missing character. This may be a hack.
282 replace_all (p.text, "\xe2\x80\xab", "");
287 /* Push p into _subs if it has some text, and clear the text out of p */
289 SubripReader::maybe_content (RawSubtitle& p)
291 if (!p.text.empty ()) {