2 Copyright (C) 2014-2015 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 <locked_sstream.h>
28 #include <boost/algorithm/string.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <boost/regex.hpp>
31 #include <boost/bind.hpp>
41 using boost::lexical_cast;
42 using boost::to_upper;
43 using boost::optional;
44 using boost::function;
45 using boost::algorithm::replace_all;
48 /** @param s Subtitle string encoded in UTF-8 */
49 SubripReader::SubripReader (string const & s)
51 locked_stringstream str (s);
52 this->read (boost::bind (&get_line_stringstream, &str));
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 /* This reader extracts no information about where the subtitle
73 should be on screen, so its reference is TOP_OF_SUBTITLE.
75 rs.vertical_position.line = 0;
76 rs.vertical_position.reference = TOP_OF_SUBTITLE;
79 optional<string> line = get_line ();
84 trim_right_if (*line, boost::is_any_of ("\n\r"));
85 remove_unicode_bom (line);
87 /* Keep some history in case there is an error to report */
88 _context.push_back (*line);
89 if (_context.size() > 5) {
90 _context.pop_front ();
97 /* a blank line at the start is ok */
103 /* Reset stuff that should not persist across separate subtitles */
106 rs.underline = false;
107 rs.vertical_position.line = 0;
114 /* Further trim this line, removing spaces from the end */
115 trim_right_if (*line, boost::is_any_of (" "));
117 boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "), boost::token_compress_on);
118 if (p.size() != 3 && p.size() != 7) {
119 for (int i = 0; i < 2; ++i) {
120 optional<string> ex = get_line ();
122 _context.push_back (*ex);
125 throw SubripError (*line, "a time/position line", _context);
128 rs.from = convert_time (p[0]);
129 rs.to = convert_time (p[2]);
131 /* XXX: should not ignore coordinate specifications */
137 if (line->empty ()) {
140 convert_line (*line, rs);
141 rs.vertical_position.line = rs.vertical_position.line.get() + 1;
149 SubripReader::convert_time (string t)
152 boost::algorithm::split (a, t, boost::is_any_of (":"));
154 throw SubripError (t, "time in the format h:m:s,ms", _context);
158 boost::algorithm::split (b, a[2], boost::is_any_of (","));
160 throw SubripError (t, "time in the format h:m:s,ms", _context);
163 return Time::from_hms (
164 lexical_cast<int> (a[0]),
165 lexical_cast<int> (a[1]),
166 lexical_cast<int> (b[0]),
167 lexical_cast<int> (b[1])
172 SubripReader::convert_line (string t, RawSubtitle& p)
181 list<Colour> colours;
182 colours.push_back (Colour (1, 1, 1));
184 /* XXX: missing <font> support */
185 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
189 for (size_t i = 0; i < t.size(); ++i) {
192 if (t[i] == '<' || t[i] == '{') {
199 if (t[i] == '>' || t[i] == '}') {
203 } else if (tag == "/b") {
206 } else if (tag == "i") {
209 } else if (tag == "/i") {
212 } else if (tag == "u") {
215 } else if (tag == "/u") {
218 } else if (boost::starts_with (tag, "font")) {
220 boost::regex re (".*color=\"#([0123456789abcdef]+)\"");
222 if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
223 p.colour = Colour::from_rgb_hex (match[1]);
224 colours.push_back (p.colour);
226 } else if (tag == "/font") {
229 p.colour = colours.back ();
234 tag += tolower (t[i]);
240 /* Strip Unicode U+202B (right-to-left embedding) as sometimes it is rendered
241 as a missing character. This may be a hack.
243 replace_all (p.text, "\xe2\x80\xab", "");
248 /* Push p into _subs if it has some text, and clear the text out of p */
250 SubripReader::maybe_content (RawSubtitle& p)
252 if (!p.text.empty ()) {