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"));
61 /* a blank line at the start is ok */
71 boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
72 if (p.size() != 3 && p.size() != 7) {
73 throw SubripError (line, "a time/position line");
76 from = convert_time (p[0]);
77 to = convert_time (p[2]);
79 /* XXX: should not ignore coordinate specifications */
89 convert_line (line, line_number, from, to);
98 SubripReader::convert_time (string t)
101 boost::algorithm::split (a, t, boost::is_any_of (":"));
103 throw SubripError (t, "time in the format h:m:s,ms");
107 boost::algorithm::split (b, a[2], boost::is_any_of (","));
111 lexical_cast<int> (a[0]),
112 lexical_cast<int> (a[1]),
113 lexical_cast<int> (b[0]),
114 lexical_cast<int> (b[1])
120 SubripReader::convert_line (string t, int line_number, TimePair from, TimePair to)
131 p.font_size.set_points (48);
134 p.vertical_position.proportional = 0.7 + line_number * 0.1;
135 p.vertical_position.reference = TOP;
137 /* XXX: missing <font> support */
138 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
142 for (size_t i = 0; i < t.size(); ++i) {
145 if (t[i] == '<' || t[i] == '{') {
152 if (t[i] == '>' || t[i] == '}') {
156 } else if (tag == "/b") {
159 } else if (tag == "i") {
162 } else if (tag == "/i") {
165 } else if (tag == "u") {
168 } else if (tag == "/u") {
185 SubripReader::maybe_content (RawSubtitle& p)
187 if (!p.text.empty ()) {