2 Copyright (C) 2014 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 #include "subrip_reader.h"
21 #include "exceptions.h"
22 #include <boost/algorithm/string.hpp>
23 #include <boost/lexical_cast.hpp>
24 #include <boost/regex.hpp>
33 using boost::lexical_cast;
34 using boost::to_upper;
37 SubripReader::SubripReader (FILE* f)
54 char* r = fgets (buffer, sizeof (buffer), f);
55 if (r == 0 || feof (f)) {
59 line = string (buffer);
60 trim_right_if (line, boost::is_any_of ("\n\r"));
64 static_cast<unsigned char> (line[0]) == 0xef &&
65 static_cast<unsigned char> (line[1]) == 0xbb &&
66 static_cast<unsigned char> (line[2]) == 0xbf
69 /* Skip Unicode byte order mark */
70 line = line.substr (3);
77 /* a blank line at the start is ok */
87 boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
88 if (p.size() != 3 && p.size() != 7) {
89 throw SubripError (line, "a time/position line");
92 from = convert_time (p[0]);
93 to = convert_time (p[2]);
95 /* XXX: should not ignore coordinate specifications */
105 convert_line (line, line_number, from, to);
114 SubripReader::convert_time (string t)
117 boost::algorithm::split (a, t, boost::is_any_of (":"));
119 throw SubripError (t, "time in the format h:m:s,ms");
123 boost::algorithm::split (b, a[2], boost::is_any_of (","));
125 return Time::from_hms (
126 lexical_cast<int> (a[0]),
127 lexical_cast<int> (a[1]),
128 lexical_cast<int> (b[0]),
129 lexical_cast<int> (b[1])
134 SubripReader::convert_line (string t, int line_number, Time from, Time to)
145 p.font_size.set_points (48);
148 p.vertical_position.line = line_number;
150 p.vertical_position.lines = 32;
151 p.vertical_position.reference = TOP_OF_SUBTITLE;
153 list<Colour> colours;
154 colours.push_back (Colour (1, 1, 1));
156 /* XXX: missing <font> support */
157 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
161 for (size_t i = 0; i < t.size(); ++i) {
164 if (t[i] == '<' || t[i] == '{') {
171 if (t[i] == '>' || t[i] == '}') {
175 } else if (tag == "/b") {
178 } else if (tag == "i") {
181 } else if (tag == "/i") {
184 } else if (tag == "u") {
187 } else if (tag == "/u") {
190 } else if (boost::starts_with (tag, "font")) {
192 boost::regex re (".*color=\"#([0123456789abcdef]+)\"");
194 if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
195 p.colour = Colour::from_rgb_hex (match[1]);
196 colours.push_back (p.colour);
198 } else if (tag == "/font") {
201 p.colour = colours.back ();
216 SubripReader::maybe_content (RawSubtitle& p)
218 if (!p.text.empty ()) {