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 */
86 /* XXX: I think this line_number should be set to some sensible value and TOP_OF_SUBTITLE
92 convert_line (line, line_number, from, to);
101 SubripReader::convert_time (string t)
104 boost::algorithm::split (a, t, boost::is_any_of (":"));
106 throw SubripError (t, "time in the format h:m:s,ms");
110 boost::algorithm::split (b, a[2], boost::is_any_of (","));
114 lexical_cast<int> (a[0]),
115 lexical_cast<int> (a[1]),
116 lexical_cast<int> (b[0]),
117 lexical_cast<int> (b[1])
123 SubripReader::convert_line (string t, int line_number, TimePair from, TimePair to)
134 p.font_size.set_points (48);
137 p.vertical_position.line = line_number;
138 p.vertical_position.reference = TOP_OF_SUBTITLE;
140 /* XXX: missing <font> support */
141 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
145 for (size_t i = 0; i < t.size(); ++i) {
148 if (t[i] == '<' || t[i] == '{') {
155 if (t[i] == '>' || t[i] == '}') {
159 } else if (tag == "/b") {
162 } else if (tag == "i") {
165 } else if (tag == "/i") {
168 } else if (tag == "u") {
171 } else if (tag == "/u") {
188 SubripReader::maybe_content (RawSubtitle& p)
190 if (!p.text.empty ()) {