Merge master.
[dcpomatic.git] / src / lib / subrip.cc
1 /*
2     Copyright (C) 2014 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 #include <boost/algorithm/string.hpp>
21 #include <boost/lexical_cast.hpp>
22 #include "subrip.h"
23 #include "subrip_content.h"
24 #include "subrip_subtitle.h"
25 #include "cross.h"
26 #include "exceptions.h"
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::list;
32 using std::vector;
33 using std::cout;
34 using boost::shared_ptr;
35 using boost::lexical_cast;
36 using boost::algorithm::trim;
37
38 SubRip::SubRip (shared_ptr<const SubRipContent> content)
39 {
40         FILE* f = fopen_boost (content->path (0), "r");
41         if (!f) {
42                 throw OpenFileError (content->path (0));
43         }
44
45         enum {
46                 COUNTER,
47                 METADATA,
48                 CONTENT
49         } state = COUNTER;
50
51         char buffer[256];
52         int next_count = 1;
53
54         boost::optional<SubRipSubtitle> current;
55         list<string> lines;
56         
57         while (!feof (f)) {
58                 fgets (buffer, sizeof (buffer), f);
59                 if (feof (f)) {
60                         break;
61                 }
62                 
63                 string line (buffer);
64                 trim_right_if (line, boost::is_any_of ("\n\r"));
65                 
66                 switch (state) {
67                 case COUNTER:
68                 {
69                         if (line.empty ()) {
70                                 /* a blank line at the start is ok */
71                                 break;
72                         }
73                         
74                         int x = 0;
75                         try {
76                                 x = lexical_cast<int> (line);
77                         } catch (...) {
78
79                         }
80
81                         if (x == next_count) {
82                                 state = METADATA;
83                                 ++next_count;
84                                 current = SubRipSubtitle ();
85                         } else {
86                                 throw SubRipError (line, _("a subtitle count"), content->path (0));
87                         }
88                 }
89                 break;
90                 case METADATA:
91                 {
92                         vector<string> p;
93                         boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
94                         if (p.size() != 3 && p.size() != 7) {
95                                 throw SubRipError (line, _("a time/position line"), content->path (0));
96                         }
97
98                         current->period = ContentTimePeriod (convert_time (p[0]), convert_time (p[2]));
99
100                         if (p.size() > 3) {
101                                 current->x1 = convert_coordinate (p[3]);
102                                 current->x2 = convert_coordinate (p[4]);
103                                 current->y1 = convert_coordinate (p[5]);
104                                 current->y2 = convert_coordinate (p[6]);
105                         }
106                         state = CONTENT;
107                         break;
108                 }
109                 case CONTENT:
110                         if (line.empty ()) {
111                                 state = COUNTER;
112                                 current->pieces = convert_content (lines);
113                                 _subtitles.push_back (current.get ());
114                                 current.reset ();
115                                 lines.clear ();
116                         } else {
117                                 lines.push_back (line);
118                         }
119                         break;
120                 }
121         }
122
123         if (state == CONTENT) {
124                 current->pieces = convert_content (lines);
125                 _subtitles.push_back (current.get ());
126         }
127
128         fclose (f);
129 }
130
131 ContentTime
132 SubRip::convert_time (string t)
133 {
134         ContentTime r;
135
136         vector<string> a;
137         boost::algorithm::split (a, t, boost::is_any_of (":"));
138         assert (a.size() == 3);
139         r += ContentTime::from_seconds (lexical_cast<int> (a[0]) * 60 * 60);
140         r += ContentTime::from_seconds (lexical_cast<int> (a[1]) * 60);
141
142         vector<string> b;
143         boost::algorithm::split (b, a[2], boost::is_any_of (","));
144         r += ContentTime::from_seconds (lexical_cast<int> (b[0]));
145         r += ContentTime::from_seconds (lexical_cast<double> (b[1]) / 1000);
146
147         return r;
148 }
149
150 int
151 SubRip::convert_coordinate (string t)
152 {
153         vector<string> a;
154         boost::algorithm::split (a, t, boost::is_any_of (":"));
155         assert (a.size() == 2);
156         return lexical_cast<int> (a[1]);
157 }
158
159 void
160 SubRip::maybe_content (list<SubRipSubtitlePiece>& pieces, SubRipSubtitlePiece& p)
161 {
162         if (!p.text.empty ()) {
163                 pieces.push_back (p);
164                 p.text.clear ();
165         }
166 }
167
168 list<SubRipSubtitlePiece>
169 SubRip::convert_content (list<string> t)
170 {
171         list<SubRipSubtitlePiece> pieces;
172         
173         SubRipSubtitlePiece p;
174
175         enum {
176                 TEXT,
177                 TAG
178         } state = TEXT;
179
180         string tag;
181
182         /* XXX: missing <font> support */
183         /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
184            not work, I think.
185         */
186
187         for (list<string>::const_iterator i = t.begin(); i != t.end(); ++i) {
188                 for (size_t j = 0; j < i->size(); ++j) {
189                         switch (state) {
190                         case TEXT:
191                                 if ((*i)[j] == '<' || (*i)[j] == '{') {
192                                         state = TAG;
193                                 } else {
194                                         p.text += (*i)[j];
195                                 }
196                                 break;
197                         case TAG:
198                                 if ((*i)[j] == '>' || (*i)[j] == '}') {
199                                         if (tag == "b") {
200                                                 maybe_content (pieces, p);
201                                                 p.bold = true;
202                                         } else if (tag == "/b") {
203                                                 maybe_content (pieces, p);
204                                                 p.bold = false;
205                                         } else if (tag == "i") {
206                                                 maybe_content (pieces, p);
207                                                 p.italic = true;
208                                         } else if (tag == "/i") {
209                                                 maybe_content (pieces, p);
210                                                 p.italic = false;
211                                         } else if (tag == "u") {
212                                                 maybe_content (pieces, p);
213                                                 p.underline = true;
214                                         } else if (tag == "/u") {
215                                                 maybe_content (pieces, p);
216                                                 p.underline = false;
217                                         }
218                                         tag.clear ();
219                                         state = TEXT;
220                                 } else {
221                                         tag += (*i)[j];
222                                 }
223                                 break;
224                         }
225                 }
226         }
227
228         maybe_content (pieces, p);
229
230         return pieces;
231 }
232
233 ContentTime
234 SubRip::length () const
235 {
236         if (_subtitles.empty ()) {
237                 return ContentTime ();
238         }
239
240         return _subtitles.back().period.to;
241 }