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/algorithm/string_regex.hpp>
32 #include <boost/lexical_cast.hpp>
33 #include <boost/regex.hpp>
34 #include <boost/bind.hpp>
43 using boost::lexical_cast;
44 using boost::to_upper;
45 using boost::optional;
46 using boost::function;
47 using boost::algorithm::replace_all;
50 /** @param s Subtitle string encoded in UTF-8 */
51 SubripReader::SubripReader (string s)
53 this->read (boost::bind(&get_line_string, &s));
56 /** @param f Subtitle file encoded in UTF-8 */
57 SubripReader::SubripReader (FILE* f)
59 this->read (boost::bind (&get_line_file, f));
63 SubripReader::read (function<optional<string> ()> get_line)
73 rs.vertical_position.line = 0;
74 rs.vertical_position.reference = TOP_OF_SUBTITLE;
77 auto line = get_line ();
82 trim_right_if (*line, boost::is_any_of ("\n\r"));
83 remove_unicode_bom (line);
85 /* Keep some history in case there is an error to report */
86 _context.push_back (*line);
87 if (_context.size() > 5) {
88 _context.pop_front ();
95 /* a blank line at the start is ok */
101 /* Reset stuff that should not persist across separate subtitles */
104 rs.underline = false;
105 rs.vertical_position.line = 0;
106 rs.vertical_position.reference = TOP_OF_SUBTITLE;
113 /* Further trim this line, removing spaces from the end */
114 trim_right_if (*line, boost::is_any_of (" "));
116 boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "), boost::token_compress_on);
117 if (p.size() != 3 && p.size() != 7) {
118 for (int i = 0; i < 2; ++i) {
119 optional<string> ex = get_line ();
121 _context.push_back (*ex);
124 throw SubripError (*line, "a time/position line", _context);
127 rs.from = convert_time (p[0]);
128 rs.to = convert_time (p[2]);
130 /* XXX: should not ignore coordinate specifications */
136 if (line->empty ()) {
139 vector<string> sub_lines;
140 /* Split up this line on unicode "LINE SEPARATOR". This feels hacky but also
141 * the least unpleasant place to do it.
143 boost::algorithm::split_regex(sub_lines, *line, boost::regex("\xe2\x80\xa8"));
144 for (auto sub_line: sub_lines) {
145 convert_line(sub_line, rs);
146 rs.vertical_position.line = rs.vertical_position.line.get() + 1;
156 SubripReader::convert_time (string t)
159 boost::algorithm::split (a, t, boost::is_any_of (":"));
161 throw SubripError (t, "time in the format h:m:s,ms", _context);
165 boost::algorithm::split (b, a[2], boost::is_any_of (","));
167 throw SubripError (t, "time in the format h:m:s,ms", _context);
173 h = lexical_cast<int>(a[0]);
174 } catch (boost::bad_lexical_cast &) {
175 throw SubripError (t, "integer hour value", _context);
179 m = lexical_cast<int>(a[1]);
180 } catch (boost::bad_lexical_cast &) {
181 throw SubripError (t, "integer minute value", _context);
185 s = lexical_cast<int>(b[0]);
186 } catch (boost::bad_lexical_cast &) {
187 throw SubripError (t, "integer second value", _context);
191 ms = lexical_cast<int>(b[1]);
192 } catch (boost::bad_lexical_cast &) {
193 throw SubripError (t, "integer millisecond value", _context);
196 return Time::from_hms (h, m, s, ms);
200 SubripReader::convert_line (string t, RawSubtitle& p)
202 vector<Colour> colours;
203 colours.push_back (Colour (1, 1, 1));
205 auto has_next = [](string line, size_t& index, string s) {
207 auto next = line.substr(index, s.size());
208 boost::to_lower(next);
218 while (i < t.size()) {
219 if (has_next(t, i, "<b>") || has_next(t, i, "{b}")) {
222 } else if (has_next(t, i, "</b>") || has_next(t, i, "{/b}")) {
225 } else if (has_next(t, i, "<i>") || has_next(t, i, "{i}")) {
228 } else if (has_next(t, i, "</i>") || has_next(t, i, "{/i}")) {
231 } else if (has_next(t, i, "<u>") || has_next(t, i, "{u}")) {
234 } else if (has_next(t, i, "</u>") || has_next(t, i, "{/u}")) {
237 } else if (has_next(t, i, "<font") || has_next(t, i, "<Font")) {
239 boost::regex re (".*color=\"?#([[:xdigit:]]+)\"?");
242 while (i < t.size() && t[i] != '>') {
247 if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
248 p.colour = Colour::from_rgb_hex (match[1]);
249 colours.push_back (p.colour);
253 "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
254 "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
255 "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
256 "[[:space:]]*([[:digit:]]+)[[:space:]]*"
259 if (boost::regex_search (tag, match, re) && match.size() == 5) {
260 p.colour.r = raw_convert<int>(string(match[1])) / 255.0;
261 p.colour.g = raw_convert<int>(string(match[2])) / 255.0;
262 p.colour.b = raw_convert<int>(string(match[3])) / 255.0;
263 colours.push_back (p.colour);
265 throw SubripError (tag, "a colour in the format #rrggbb or rgba(rr,gg,bb,aa)", _context);
268 } else if (has_next(t, i, "</font>")) {
270 SUB_ASSERT (!colours.empty());
272 p.colour = colours.back ();
273 } else if (has_next(t, i, "{\\")) {
275 while (i < t.size() && t[i] != '}') {
280 SSAReader::parse_style (p, ssa, 288, 288, Colour(1, 1, 1));
287 /* Strip Unicode U+202B (right-to-left embedding) as sometimes it is rendered
288 as a missing character. This may be a hack.
290 replace_all (p.text, "\xe2\x80\xab", "");
295 /* Push p into _subs if it has some text, and clear the text out of p */
297 SubripReader::maybe_content (RawSubtitle& p)
299 if (!p.text.empty ()) {