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