Remove sstream dependency.
[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 boost::lexical_cast;
41 using boost::to_upper;
42 using boost::optional;
43 using boost::function;
44 using boost::algorithm::replace_all;
45 using namespace sub;
46
47 /** @param s Subtitle string encoded in UTF-8 */
48 SubripReader::SubripReader (string s)
49 {
50         this->read (boost::bind(&get_line_string, &s));
51 }
52
53 /** @param f Subtitle file encoded in UTF-8 */
54 SubripReader::SubripReader (FILE* f)
55 {
56         this->read (boost::bind (&get_line_file, f));
57 }
58
59 void
60 SubripReader::read (function<optional<string> ()> get_line)
61 {
62         enum {
63                 COUNTER,
64                 METADATA,
65                 CONTENT
66         } state = COUNTER;
67
68         RawSubtitle rs;
69
70         /* This reader extracts no information about where the subtitle
71            should be on screen, so its reference is TOP_OF_SUBTITLE.
72         */
73         rs.vertical_position.line = 0;
74         rs.vertical_position.reference = TOP_OF_SUBTITLE;
75
76         while (true) {
77                 optional<string> line = get_line ();
78                 if (!line) {
79                         break;
80                 }
81
82                 trim_right_if (*line, boost::is_any_of ("\n\r"));
83                 remove_unicode_bom (line);
84
85                 /* Keep some history in case there is an error to report */
86                 _context.push_back (*line);
87                 if (_context.size() > 5) {
88                         _context.pop_front ();
89                 }
90
91                 switch (state) {
92                 case COUNTER:
93                 {
94                         if (line->empty ()) {
95                                 /* a blank line at the start is ok */
96                                 break;
97                         }
98
99                         state = METADATA;
100
101                         /* Reset stuff that should not persist across separate subtitles */
102                         rs.bold = false;
103                         rs.italic = false;
104                         rs.underline = false;
105                         rs.vertical_position.line = 0;
106                 }
107                 break;
108                 case METADATA:
109                 {
110                         vector<string> p;
111
112                         /* Further trim this line, removing spaces from the end */
113                         trim_right_if (*line, boost::is_any_of (" "));
114
115                         boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "), boost::token_compress_on);
116                         if (p.size() != 3 && p.size() != 7) {
117                                 for (int i = 0; i < 2; ++i) {
118                                         optional<string> ex = get_line ();
119                                         if (ex) {
120                                                 _context.push_back (*ex);
121                                         }
122                                 }
123                                 throw SubripError (*line, "a time/position line", _context);
124                         }
125
126                         rs.from = convert_time (p[0]);
127                         rs.to = convert_time (p[2]);
128
129                         /* XXX: should not ignore coordinate specifications */
130
131                         state = CONTENT;
132                         break;
133                 }
134                 case CONTENT:
135                         if (line->empty ()) {
136                                 state = COUNTER;
137                         } else {
138                                 convert_line (*line, rs);
139                                 rs.vertical_position.line = rs.vertical_position.line.get() + 1;
140                         }
141                         break;
142                 }
143         }
144 }
145
146 Time
147 SubripReader::convert_time (string t)
148 {
149         vector<string> a;
150         boost::algorithm::split (a, t, boost::is_any_of (":"));
151         if (a.size() != 3) {
152                 throw SubripError (t, "time in the format h:m:s,ms", _context);
153         }
154
155         vector<string> b;
156         boost::algorithm::split (b, a[2], boost::is_any_of (","));
157         if (b.size() != 2) {
158                 throw SubripError (t, "time in the format h:m:s,ms", _context);
159         }
160
161         return Time::from_hms (
162                 lexical_cast<int> (a[0]),
163                 lexical_cast<int> (a[1]),
164                 lexical_cast<int> (b[0]),
165                 lexical_cast<int> (b[1])
166                 );
167 }
168
169 void
170 SubripReader::convert_line (string t, RawSubtitle& p)
171 {
172         enum {
173                 TEXT,
174                 TAG
175         } state = TEXT;
176
177         string tag;
178
179         list<Colour> colours;
180         colours.push_back (Colour (1, 1, 1));
181
182         /* XXX: missing <font> support */
183         /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
184            not work, I think.
185         */
186
187         for (size_t i = 0; i < t.size(); ++i) {
188                 switch (state) {
189                 case TEXT:
190                         if (t[i] == '<' || t[i] == '{') {
191                                 state = TAG;
192                         } else {
193                                 p.text += t[i];
194                         }
195                         break;
196                 case TAG:
197                         if (t[i] == '>' || t[i] == '}') {
198                                 if (tag == "b") {
199                                         maybe_content (p);
200                                         p.bold = true;
201                                 } else if (tag == "/b") {
202                                         maybe_content (p);
203                                         p.bold = false;
204                                 } else if (tag == "i") {
205                                         maybe_content (p);
206                                         p.italic = true;
207                                 } else if (tag == "/i") {
208                                         maybe_content (p);
209                                         p.italic = false;
210                                 } else if (tag == "u") {
211                                         maybe_content (p);
212                                         p.underline = true;
213                                 } else if (tag == "/u") {
214                                         maybe_content (p);
215                                         p.underline = false;
216                                 } else if (boost::starts_with (tag, "font")) {
217                                         maybe_content (p);
218                                         boost::regex re (".*color=\"#([0123456789abcdef]+)\"");
219                                         boost::smatch match;
220                                         if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
221                                                 p.colour = Colour::from_rgb_hex (match[1]);
222                                                 colours.push_back (p.colour);
223                                         }
224                                 } else if (tag == "/font") {
225                                         maybe_content (p);
226                                         colours.pop_back ();
227                                         p.colour = colours.back ();
228                                 }
229                                 tag.clear ();
230                                 state = TEXT;
231                         } else {
232                                 tag += tolower (t[i]);
233                         }
234                         break;
235                 }
236         }
237
238         /* Strip Unicode U+202B (right-to-left embedding) as sometimes it is rendered
239            as a missing character.  This may be a hack.
240         */
241         replace_all (p.text, "\xe2\x80\xab", "");
242
243         maybe_content (p);
244 }
245
246 /* Push p into _subs if it has some text, and clear the text out of p */
247 void
248 SubripReader::maybe_content (RawSubtitle& p)
249 {
250         if (!p.text.empty ()) {
251                 _subs.push_back (p);
252                 p.text.clear ();
253         }
254 }