Handle angle brackets / tags better in SubRip files.
[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/lexical_cast.hpp>
32 #include <boost/regex.hpp>
33 #include <boost/bind.hpp>
34 #include <cstdio>
35 #include <vector>
36 #include <iostream>
37
38 using std::string;
39 using std::vector;
40 using std::cout;
41 using std::hex;
42 using boost::lexical_cast;
43 using boost::to_upper;
44 using boost::optional;
45 using boost::function;
46 using boost::algorithm::replace_all;
47 using namespace sub;
48
49 /** @param s Subtitle string encoded in UTF-8 */
50 SubripReader::SubripReader (string s)
51 {
52         this->read (boost::bind(&get_line_string, &s));
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         rs.vertical_position.line = 0;
73         rs.vertical_position.reference = TOP_OF_SUBTITLE;
74
75         while (true) {
76                 auto line = get_line ();
77                 if (!line) {
78                         break;
79                 }
80
81                 trim_right_if (*line, boost::is_any_of ("\n\r"));
82                 remove_unicode_bom (line);
83
84                 /* Keep some history in case there is an error to report */
85                 _context.push_back (*line);
86                 if (_context.size() > 5) {
87                         _context.pop_front ();
88                 }
89
90                 switch (state) {
91                 case COUNTER:
92                 {
93                         if (line->empty ()) {
94                                 /* a blank line at the start is ok */
95                                 break;
96                         }
97
98                         state = METADATA;
99
100                         /* Reset stuff that should not persist across separate subtitles */
101                         rs.bold = false;
102                         rs.italic = false;
103                         rs.underline = false;
104                         rs.vertical_position.line = 0;
105                         rs.vertical_position.reference = TOP_OF_SUBTITLE;
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         int h, m, s, ms;
162
163         try {
164                 h = lexical_cast<int>(a[0]);
165         } catch (boost::bad_lexical_cast &) {
166                 throw SubripError (t, "integer hour value", _context);
167         }
168
169         try {
170                 m = lexical_cast<int>(a[1]);
171         } catch (boost::bad_lexical_cast &) {
172                 throw SubripError (t, "integer minute value", _context);
173         }
174
175         try {
176                 s = lexical_cast<int>(b[0]);
177         } catch (boost::bad_lexical_cast &) {
178                 throw SubripError (t, "integer second value", _context);
179         }
180
181         try {
182                 ms = lexical_cast<int>(b[1]);
183         } catch (boost::bad_lexical_cast &) {
184                 throw SubripError (t, "integer millisecond value", _context);
185         }
186
187         return Time::from_hms (h, m, s, ms);
188 }
189
190 void
191 SubripReader::convert_line (string t, RawSubtitle& p)
192 {
193         vector<Colour> colours;
194         colours.push_back (Colour (1, 1, 1));
195
196         auto has_next = [](string line, size_t& index, string s) {
197                 boost::to_lower(s);
198                 auto next = line.substr(index, s.size());
199                 boost::to_lower(next);
200                 if (next != s) {
201                         return false;
202                 }
203
204                 index += s.size();
205                 return true;
206         };
207
208         size_t i = 0;
209         while (i < t.size()) {
210                 if (has_next(t, i, "<b>") || has_next(t, i, "{b}")) {
211                         maybe_content (p);
212                         p.bold = true;
213                 } else if (has_next(t, i, "</b>") || has_next(t, i, "{/b}")) {
214                         maybe_content (p);
215                         p.bold = false;
216                 } else if (has_next(t, i, "<i>") || has_next(t, i, "{i}")) {
217                         maybe_content (p);
218                         p.italic = true;
219                 } else if (has_next(t, i, "</i>") || has_next(t, i, "{/i}")) {
220                         maybe_content (p);
221                         p.italic = false;
222                 } else if (has_next(t, i, "<u>") || has_next(t, i, "{u}")) {
223                         maybe_content (p);
224                         p.underline = true;
225                 } else if (has_next(t, i, "</u>") || has_next(t, i, "{/u}")) {
226                         maybe_content (p);
227                         p.underline = false;
228                 } else if (has_next(t, i, "<font") || has_next(t, i, "<Font")) {
229                         maybe_content (p);
230                         boost::regex re (".*color=\"?#([[:xdigit:]]+)\"?");
231                         boost::smatch match;
232                         string tag;
233                         while (i < t.size() && t[i] != '>') {
234                                 tag += t[i];
235                                 ++i;
236                         }
237                         ++i;
238                         if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
239                                 p.colour = Colour::from_rgb_hex (match[1]);
240                                 colours.push_back (p.colour);
241                         } else {
242                                 re = boost::regex (
243                                         ".*color=\"rgba\\("
244                                         "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
245                                         "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
246                                         "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
247                                         "[[:space:]]*([[:digit:]]+)[[:space:]]*"
248                                         "\\)\""
249                                         );
250                                 if (boost::regex_search (tag, match, re) && match.size() == 5) {
251                                         p.colour.r = raw_convert<int>(string(match[1])) / 255.0;
252                                         p.colour.g = raw_convert<int>(string(match[2])) / 255.0;
253                                         p.colour.b = raw_convert<int>(string(match[3])) / 255.0;
254                                         colours.push_back (p.colour);
255                                 } else {
256                                         throw SubripError (tag, "a colour in the format #rrggbb or rgba(rr,gg,bb,aa)", _context);
257                                 }
258                         }
259                 } else if (has_next(t, i, "</font>")) {
260                         maybe_content (p);
261                         SUB_ASSERT (!colours.empty());
262                         colours.pop_back ();
263                         p.colour = colours.back ();
264                 } else if (has_next(t, i, "{\\")) {
265                         string ssa = "\\";
266                         while (i < t.size() && t[i] != '}') {
267                                 ssa += t[i];
268                                 ++i;
269                         }
270                         ++i;
271                         SSAReader::parse_style (p, ssa, 288, 288);
272                 } else {
273                         p.text += t[i];
274                         ++i;
275                 }
276         }
277
278         /* Strip Unicode U+202B (right-to-left embedding) as sometimes it is rendered
279            as a missing character.  This may be a hack.
280         */
281         replace_all (p.text, "\xe2\x80\xab", "");
282
283         maybe_content (p);
284 }
285
286 /* Push p into _subs if it has some text, and clear the text out of p */
287 void
288 SubripReader::maybe_content (RawSubtitle& p)
289 {
290         if (!p.text.empty ()) {
291                 _subs.push_back (p);
292                 p.text.clear ();
293         }
294 }