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