Extract get_line_{file,stringstream} into standalone methods.
[libsub.git] / src / subrip_reader.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/subrip_reader.cc
21  *  @brief SubripReader class.
22  */
23
24 #include "subrip_reader.h"
25 #include "exceptions.h"
26 #include "util.h"
27 #include <boost/algorithm/string.hpp>
28 #include <boost/lexical_cast.hpp>
29 #include <boost/regex.hpp>
30 #include <boost/bind.hpp>
31 #include <cstdio>
32 #include <vector>
33 #include <iostream>
34
35 using std::string;
36 using std::vector;
37 using std::list;
38 using std::cout;
39 using std::hex;
40 using std::stringstream;
41 using boost::lexical_cast;
42 using boost::to_upper;
43 using boost::optional;
44 using boost::function;
45 using namespace sub;
46
47 /** @param s Subtitle string encoded in UTF-8 */
48 SubripReader::SubripReader (string const & s)
49 {
50         stringstream str (s);
51         this->read (boost::bind (&get_line_stringstream, &str));
52 }
53
54 /** @param f Subtitle file encoded in UTF-8 */
55 SubripReader::SubripReader (FILE* f)
56 {
57         this->read (boost::bind (&get_line_file, f));
58 }
59
60 void
61 SubripReader::read (function<optional<string> ()> get_line)
62 {
63         enum {
64                 COUNTER,
65                 METADATA,
66                 CONTENT
67         } state = COUNTER;
68
69         Time from;
70         Time to;
71
72         string line;
73         int line_number = 0;
74
75         while (true) {
76                 optional<string> line = get_line ();
77                 if (!line) {
78                         break;
79                 }
80
81                 trim_right_if (*line, boost::is_any_of ("\n\r"));
82
83                 if (
84                         line->length() >= 3 &&
85                         static_cast<unsigned char> (line.get()[0]) == 0xef &&
86                         static_cast<unsigned char> (line.get()[1]) == 0xbb &&
87                         static_cast<unsigned char> (line.get()[2]) == 0xbf
88                         ) {
89
90                         /* Skip Unicode byte order mark */
91                         line = line->substr (3);
92                 }
93
94                 switch (state) {
95                 case COUNTER:
96                 {
97                         if (line->empty ()) {
98                                 /* a blank line at the start is ok */
99                                 break;
100                         }
101
102                         state = METADATA;
103                 }
104                 break;
105                 case METADATA:
106                 {
107                         vector<string> p;
108                         boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "));
109                         if (p.size() != 3 && p.size() != 7) {
110                                 throw SubripError (*line, "a time/position line");
111                         }
112
113                         from = convert_time (p[0]);
114                         to = convert_time (p[2]);
115
116                         /* XXX: should not ignore coordinate specifications */
117
118                         state = CONTENT;
119                         break;
120                 }
121                 case CONTENT:
122                         if (line->empty ()) {
123                                 state = COUNTER;
124                                 line_number = 0;
125                         } else {
126                                 convert_line (*line, line_number, from, to);
127                                 line_number++;
128                         }
129                         break;
130                 }
131         }
132 }
133
134 Time
135 SubripReader::convert_time (string t)
136 {
137         vector<string> a;
138         boost::algorithm::split (a, t, boost::is_any_of (":"));
139         if (a.size() != 3) {
140                 throw SubripError (t, "time in the format h:m:s,ms");
141         }
142
143         vector<string> b;
144         boost::algorithm::split (b, a[2], boost::is_any_of (","));
145
146         return Time::from_hms (
147                 lexical_cast<int> (a[0]),
148                 lexical_cast<int> (a[1]),
149                 lexical_cast<int> (b[0]),
150                 lexical_cast<int> (b[1])
151                 );
152 }
153
154 void
155 SubripReader::convert_line (string t, int line_number, Time from, Time to)
156 {
157         enum {
158                 TEXT,
159                 TAG
160         } state = TEXT;
161
162         string tag;
163
164         RawSubtitle p;
165         p.font = "Arial";
166         p.font_size.set_points (48);
167         p.from = from;
168         p.to = to;
169         p.vertical_position.line = line_number;
170         /* XXX: arbitrary */
171         p.vertical_position.lines = 32;
172         p.vertical_position.reference = TOP_OF_SUBTITLE;
173
174         list<Colour> colours;
175         colours.push_back (Colour (1, 1, 1));
176
177         /* XXX: missing <font> support */
178         /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
179            not work, I think.
180         */
181
182         for (size_t i = 0; i < t.size(); ++i) {
183                 switch (state) {
184                 case TEXT:
185                         if (t[i] == '<' || t[i] == '{') {
186                                 state = TAG;
187                         } else {
188                                 p.text += t[i];
189                         }
190                         break;
191                 case TAG:
192                         if (t[i] == '>' || t[i] == '}') {
193                                 if (tag == "b") {
194                                         maybe_content (p);
195                                         p.bold = true;
196                                 } else if (tag == "/b") {
197                                         maybe_content (p);
198                                         p.bold = false;
199                                 } else if (tag == "i") {
200                                         maybe_content (p);
201                                         p.italic = true;
202                                 } else if (tag == "/i") {
203                                         maybe_content (p);
204                                         p.italic = false;
205                                 } else if (tag == "u") {
206                                         maybe_content (p);
207                                         p.underline = true;
208                                 } else if (tag == "/u") {
209                                         maybe_content (p);
210                                         p.underline = false;
211                                 } else if (boost::starts_with (tag, "font")) {
212                                         maybe_content (p);
213                                         boost::regex re (".*color=\"#([0123456789abcdef]+)\"");
214                                         boost::smatch match;
215                                         if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
216                                                 p.colour = Colour::from_rgb_hex (match[1]);
217                                                 colours.push_back (p.colour);
218                                         }
219                                 } else if (tag == "/font") {
220                                         maybe_content (p);
221                                         colours.pop_back ();
222                                         p.colour = colours.back ();
223                                 }
224                                 tag.clear ();
225                                 state = TEXT;
226                         } else {
227                                 tag += t[i];
228                         }
229                         break;
230                 }
231         }
232
233         maybe_content (p);
234 }
235
236 void
237 SubripReader::maybe_content (RawSubtitle& p)
238 {
239         if (!p.text.empty ()) {
240                 _subs.push_back (p);
241                 p.text.clear ();
242         }
243 }