Merge branch '1.0' of git.carlh.net:git/libsub into 1.0
[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 <boost/algorithm/string.hpp>
28 #include <boost/lexical_cast.hpp>
29 #include <boost/regex.hpp>
30 #include <boost/bind.hpp>
31 #include <cstdio>
32 #include <vector>
33 #include <iostream>
34
35 using std::string;
36 using std::vector;
37 using std::list;
38 using std::cout;
39 using std::hex;
40 using std::stringstream;
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         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         rs.font = "Arial";
71         rs.font_size.set_points (48);
72         rs.vertical_position.line = 0;
73         /* XXX: arbitrary */
74         rs.vertical_position.lines = 32;
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                 switch (state) {
87                 case COUNTER:
88                 {
89                         if (line->empty ()) {
90                                 /* a blank line at the start is ok */
91                                 break;
92                         }
93
94                         state = METADATA;
95
96                         /* Reset stuff that should not persist across separate subtitles */
97                         rs.bold = false;
98                         rs.italic = false;
99                         rs.underline = false;
100                         rs.vertical_position.line = 0;
101                 }
102                 break;
103                 case METADATA:
104                 {
105                         vector<string> p;
106
107                         /* Further trim this line, removing spaces from the end */
108                         trim_right_if (*line, boost::is_any_of (" "));
109
110                         boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "));
111                         if (p.size() != 3 && p.size() != 7) {
112                                 throw SubripError (*line, "a time/position line");
113                         }
114
115                         rs.from = convert_time (p[0]);
116                         rs.to = convert_time (p[2]);
117
118                         /* XXX: should not ignore coordinate specifications */
119
120                         state = CONTENT;
121                         break;
122                 }
123                 case CONTENT:
124                         if (line->empty ()) {
125                                 state = COUNTER;
126                         } else {
127                                 convert_line (*line, rs);
128                                 rs.vertical_position.line = rs.vertical_position.line.get() + 1;
129                         }
130                         break;
131                 }
132         }
133 }
134
135 Time
136 SubripReader::convert_time (string t)
137 {
138         vector<string> a;
139         boost::algorithm::split (a, t, boost::is_any_of (":"));
140         if (a.size() != 3) {
141                 throw SubripError (t, "time in the format h:m:s,ms");
142         }
143
144         vector<string> b;
145         boost::algorithm::split (b, a[2], boost::is_any_of (","));
146
147         return Time::from_hms (
148                 lexical_cast<int> (a[0]),
149                 lexical_cast<int> (a[1]),
150                 lexical_cast<int> (b[0]),
151                 lexical_cast<int> (b[1])
152                 );
153 }
154
155 void
156 SubripReader::convert_line (string t, RawSubtitle& p)
157 {
158         enum {
159                 TEXT,
160                 TAG
161         } state = TEXT;
162
163         string tag;
164
165         list<Colour> colours;
166         colours.push_back (Colour (1, 1, 1));
167
168         /* XXX: missing <font> support */
169         /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
170            not work, I think.
171         */
172
173         for (size_t i = 0; i < t.size(); ++i) {
174                 switch (state) {
175                 case TEXT:
176                         if (t[i] == '<' || t[i] == '{') {
177                                 state = TAG;
178                         } else {
179                                 p.text += t[i];
180                         }
181                         break;
182                 case TAG:
183                         if (t[i] == '>' || t[i] == '}') {
184                                 if (tag == "b") {
185                                         maybe_content (p);
186                                         p.bold = true;
187                                 } else if (tag == "/b") {
188                                         maybe_content (p);
189                                         p.bold = false;
190                                 } else if (tag == "i") {
191                                         maybe_content (p);
192                                         p.italic = true;
193                                 } else if (tag == "/i") {
194                                         maybe_content (p);
195                                         p.italic = false;
196                                 } else if (tag == "u") {
197                                         maybe_content (p);
198                                         p.underline = true;
199                                 } else if (tag == "/u") {
200                                         maybe_content (p);
201                                         p.underline = false;
202                                 } else if (boost::starts_with (tag, "font")) {
203                                         maybe_content (p);
204                                         boost::regex re (".*color=\"#([0123456789abcdef]+)\"");
205                                         boost::smatch match;
206                                         if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
207                                                 p.colour = Colour::from_rgb_hex (match[1]);
208                                                 colours.push_back (p.colour);
209                                         }
210                                 } else if (tag == "/font") {
211                                         maybe_content (p);
212                                         colours.pop_back ();
213                                         p.colour = colours.back ();
214                                 }
215                                 tag.clear ();
216                                 state = TEXT;
217                         } else {
218                                 tag += t[i];
219                         }
220                         break;
221                 }
222         }
223
224         maybe_content (p);
225 }
226
227 /* Push p into _subs if it has some text, and clear the text out of p */
228 void
229 SubripReader::maybe_content (RawSubtitle& p)
230 {
231         if (!p.text.empty ()) {
232                 _subs.push_back (p);
233                 p.text.clear ();
234         }
235 }