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>
29 using boost::lexical_cast;
32 SubripReader::SubripReader (FILE* f)
49 char* r = fgets (buffer, sizeof (buffer), f);
50 if (r == 0 || feof (f)) {
54 line = string (buffer);
55 trim_right_if (line, boost::is_any_of ("\n\r"));
59 static_cast<unsigned char> (line[0]) == 0xef &&
60 static_cast<unsigned char> (line[1]) == 0xbb &&
61 static_cast<unsigned char> (line[2]) == 0xbf
64 /* Skip Unicode byte order mark */
65 line = line.substr (3);
72 /* a blank line at the start is ok */
82 boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
83 if (p.size() != 3 && p.size() != 7) {
84 throw SubripError (line, "a time/position line");
87 from = convert_time (p[0]);
88 to = convert_time (p[2]);
90 /* XXX: should not ignore coordinate specifications */
100 convert_line (line, line_number, from, to);
109 SubripReader::convert_time (string t)
112 boost::algorithm::split (a, t, boost::is_any_of (":"));
114 throw SubripError (t, "time in the format h:m:s,ms");
118 boost::algorithm::split (b, a[2], boost::is_any_of (","));
120 return Time::from_hms (
121 lexical_cast<int> (a[0]),
122 lexical_cast<int> (a[1]),
123 lexical_cast<int> (b[0]),
124 lexical_cast<int> (b[1])
129 SubripReader::convert_line (string t, int line_number, Time from, Time to)
140 p.font_size.set_points (48);
143 p.vertical_position.line = line_number;
145 p.vertical_position.lines = 32;
146 p.vertical_position.reference = TOP_OF_SUBTITLE;
148 /* XXX: missing <font> support */
149 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
153 for (size_t i = 0; i < t.size(); ++i) {
156 if (t[i] == '<' || t[i] == '{') {
163 if (t[i] == '>' || t[i] == '}') {
167 } else if (tag == "/b") {
170 } else if (tag == "i") {
173 } else if (tag == "/i") {
176 } else if (tag == "u") {
179 } else if (tag == "/u") {
196 SubripReader::maybe_content (RawSubtitle& p)
198 if (!p.text.empty ()) {