Add exception missing from previous commit.
[libsub.git] / src / subrip_reader.cc
1 /*
2     Copyright (C) 2014-2020 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 "sub_assert.h"
28 #include "raw_convert.h"
29 #include "ssa_reader.h"
30 #include <boost/algorithm/string.hpp>
31 #include <boost/algorithm/string_regex.hpp>
32 #include <boost/lexical_cast.hpp>
33 #include <boost/regex.hpp>
34 #include <boost/bind.hpp>
35 #include <cstdio>
36 #include <vector>
37 #include <iostream>
38
39 using std::string;
40 using std::vector;
41 using std::cout;
42 using std::hex;
43 using boost::lexical_cast;
44 using boost::to_upper;
45 using boost::optional;
46 using boost::function;
47 using boost::algorithm::replace_all;
48 using namespace sub;
49
50 /** @param s Subtitle string encoded in UTF-8 */
51 SubripReader::SubripReader (string s)
52 {
53         this->read (boost::bind(&get_line_string, &s));
54 }
55
56 /** @param f Subtitle file encoded in UTF-8 */
57 SubripReader::SubripReader (FILE* f)
58 {
59         this->read (boost::bind (&get_line_file, f));
60 }
61
62 void
63 SubripReader::read (function<optional<string> ()> get_line)
64 {
65         enum {
66                 COUNTER,
67                 METADATA,
68                 CONTENT
69         } state = COUNTER;
70
71         RawSubtitle rs;
72
73         rs.vertical_position.line = 0;
74         rs.vertical_position.reference = TOP_OF_SUBTITLE;
75
76         while (true) {
77                 auto 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                         rs.vertical_position.reference = TOP_OF_SUBTITLE;
107                 }
108                 break;
109                 case METADATA:
110                 {
111                         vector<string> p;
112
113                         /* Further trim this line, removing spaces from the end */
114                         trim_right_if (*line, boost::is_any_of (" "));
115
116                         boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "), boost::token_compress_on);
117                         if (p.size() != 3 && p.size() != 7) {
118                                 for (int i = 0; i < 2; ++i) {
119                                         optional<string> ex = get_line ();
120                                         if (ex) {
121                                                 _context.push_back (*ex);
122                                         }
123                                 }
124                                 throw SubripError (*line, "a time/position line", _context);
125                         }
126
127                         string expected;
128                         auto from = convert_time(p[0], &expected);
129                         if (!from) {
130                                 throw SubripError(p[0], expected, _context);
131                         }
132                         rs.from = *from;
133
134                         auto to = convert_time(p[2], &expected);
135                         if (!to) {
136                                 throw SubripError(p[2], expected, _context);
137                         }
138                         rs.to = *to;
139
140                         /* XXX: should not ignore coordinate specifications */
141
142                         state = CONTENT;
143                         break;
144                 }
145                 case CONTENT:
146                         if (line->empty ()) {
147                                 state = COUNTER;
148                         } else {
149                                 vector<string> sub_lines;
150                                 /* Split up this line on unicode "LINE SEPARATOR".  This feels hacky but also
151                                  * the least unpleasant place to do it.
152                                  */
153                                 boost::algorithm::split_regex(sub_lines, *line, boost::regex("\xe2\x80\xa8"));
154                                 for (auto sub_line: sub_lines) {
155                                         convert_line(sub_line, rs);
156                                         rs.vertical_position.line = rs.vertical_position.line.get() + 1;
157                                         rs.text.clear();
158                                 }
159                         }
160                         break;
161                 }
162         }
163 }
164
165 optional<Time>
166 SubripReader::convert_time(string t, string* expected)
167 {
168         auto report_expected = [expected](string const& s) {
169                 if (expected) {
170                         *expected = s;
171                 }
172         };
173
174         vector<string> a;
175         boost::algorithm::split (a, t, boost::is_any_of (":"));
176         if (a.size() != 3) {
177                 report_expected("time in the format h:m:s,ms");
178                 return {};
179         }
180
181         vector<string> b;
182         boost::algorithm::split (b, a[2], boost::is_any_of (","));
183         if (b.size() != 2) {
184                 report_expected("time in the format h:m:s,ms");
185                 return {};
186         }
187
188         int h, m, s, ms;
189
190         try {
191                 h = lexical_cast<int>(a[0]);
192         } catch (boost::bad_lexical_cast &) {
193                 report_expected("integer hour value");
194                 return {};
195         }
196
197         try {
198                 m = lexical_cast<int>(a[1]);
199         } catch (boost::bad_lexical_cast &) {
200                 report_expected("integer minute value");
201                 return {};
202         }
203
204         try {
205                 s = lexical_cast<int>(b[0]);
206         } catch (boost::bad_lexical_cast &) {
207                 report_expected("integer second value");
208                 return {};
209         }
210
211         try {
212                 ms = lexical_cast<int>(b[1]);
213         } catch (boost::bad_lexical_cast &) {
214                 report_expected("integer millisecond value");
215                 return {};
216         }
217
218         return Time::from_hms (h, m, s, ms);
219 }
220
221 void
222 SubripReader::convert_line (string t, RawSubtitle& p)
223 {
224         vector<Colour> colours;
225         colours.push_back (Colour (1, 1, 1));
226
227         auto has_next = [](string line, size_t& index, string s) {
228                 boost::to_lower(s);
229                 auto next = line.substr(index, s.size());
230                 boost::to_lower(next);
231                 if (next != s) {
232                         return false;
233                 }
234
235                 index += s.size();
236                 return true;
237         };
238
239         size_t i = 0;
240         while (i < t.size()) {
241                 if (has_next(t, i, "<b>") || has_next(t, i, "{b}")) {
242                         maybe_content (p);
243                         p.bold = true;
244                 } else if (has_next(t, i, "</b>") || has_next(t, i, "{/b}")) {
245                         maybe_content (p);
246                         p.bold = false;
247                 } else if (has_next(t, i, "<i>") || has_next(t, i, "{i}")) {
248                         maybe_content (p);
249                         p.italic = true;
250                 } else if (has_next(t, i, "</i>") || has_next(t, i, "{/i}")) {
251                         maybe_content (p);
252                         p.italic = false;
253                 } else if (has_next(t, i, "<u>") || has_next(t, i, "{u}")) {
254                         maybe_content (p);
255                         p.underline = true;
256                 } else if (has_next(t, i, "</u>") || has_next(t, i, "{/u}")) {
257                         maybe_content (p);
258                         p.underline = false;
259                 } else if (has_next(t, i, "<font") || has_next(t, i, "<Font")) {
260                         maybe_content (p);
261                         boost::regex re (".*color=\"?#([[:xdigit:]]+)\"?");
262                         boost::smatch match;
263                         string tag;
264                         while (i < t.size() && t[i] != '>') {
265                                 tag += t[i];
266                                 ++i;
267                         }
268                         ++i;
269                         if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
270                                 p.colour = Colour::from_rgb_hex (match[1]);
271                                 colours.push_back (p.colour);
272                         } else {
273                                 re = boost::regex (
274                                         ".*color=\"rgba\\("
275                                         "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
276                                         "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
277                                         "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
278                                         "[[:space:]]*([[:digit:]]+)[[:space:]]*"
279                                         "\\)\""
280                                         );
281                                 if (boost::regex_search (tag, match, re) && match.size() == 5) {
282                                         p.colour.r = raw_convert<int>(string(match[1])) / 255.0;
283                                         p.colour.g = raw_convert<int>(string(match[2])) / 255.0;
284                                         p.colour.b = raw_convert<int>(string(match[3])) / 255.0;
285                                         colours.push_back (p.colour);
286                                 } else {
287                                         throw SubripError (tag, "a colour in the format #rrggbb or rgba(rr,gg,bb,aa)", _context);
288                                 }
289                         }
290                 } else if (has_next(t, i, "</font>")) {
291                         maybe_content (p);
292                         SUB_ASSERT (!colours.empty());
293                         colours.pop_back ();
294                         p.colour = colours.back ();
295                 } else if (has_next(t, i, "{\\")) {
296                         string ssa = "\\";
297                         while (i < t.size() && t[i] != '}') {
298                                 ssa += t[i];
299                                 ++i;
300                         }
301                         ++i;
302                         SSAReader::parse_style (p, ssa, 288, 288, Colour(1, 1, 1));
303                 } else {
304                         p.text += t[i];
305                         ++i;
306                 }
307         }
308
309         /* Strip Unicode U+202B (right-to-left embedding) as sometimes it is rendered
310            as a missing character.  This may be a hack.
311         */
312         replace_all (p.text, "\xe2\x80\xab", "");
313
314         maybe_content (p);
315 }
316
317 /* Push p into _subs if it has some text, and clear the text out of p */
318 void
319 SubripReader::maybe_content (RawSubtitle& p)
320 {
321         if (!p.text.empty ()) {
322                 _subs.push_back (p);
323                 p.text.clear ();
324         }
325 }