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