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                         int x = 0;
70                         try {
71                                 x = lexical_cast<int> (line);
72                         } catch (...) {
73
74                         }
75                         
76                         if (x == next_count) {
77                                 state = METADATA;
78                                 ++next_count;
79                                 current = SubRipSubtitle ();
80                         } else {
81                                 throw SubRipError (line, _("a subtitle count"), content->path (0));
82                         }
83                 }
84                 break;
85                 case METADATA:
86                 {
87                         vector<string> p;
88                         boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
89                         if (p.size() != 3 && p.size() != 7) {
90                                 throw SubRipError (line, _("a time/position line"), content->path (0));
91                         }
92
93                         current->from = convert_time (p[0]);
94                         current->to = convert_time (p[2]);
95
96                         if (p.size() > 3) {
97                                 current->x1 = convert_coordinate (p[3]);
98                                 current->x2 = convert_coordinate (p[4]);
99                                 current->y1 = convert_coordinate (p[5]);
100                                 current->y2 = convert_coordinate (p[6]);
101                         }
102                         state = CONTENT;
103                         break;
104                 }
105                 case CONTENT:
106                         if (line.empty ()) {
107                                 state = COUNTER;
108                                 current->pieces = convert_content (lines);
109                                 _subtitles.push_back (current.get ());
110                                 current.reset ();
111                                 lines.clear ();
112                         } else {
113                                 lines.push_back (line);
114                         }
115                         break;
116                 }
117         }
118
119         if (state == CONTENT) {
120                 current->pieces = convert_content (lines);
121                 _subtitles.push_back (current.get ());
122         }
123
124         fclose (f);
125 }
126
127 ContentTime
128 SubRip::convert_time (string t)
129 {
130         ContentTime r;
131
132         vector<string> a;
133         boost::algorithm::split (a, t, boost::is_any_of (":"));
134         assert (a.size() == 3);
135         r += ContentTime::from_seconds (lexical_cast<int> (a[0]) * 60 * 60);
136         r += ContentTime::from_seconds (lexical_cast<int> (a[1]) * 60);
137
138         vector<string> b;
139         boost::algorithm::split (b, a[2], boost::is_any_of (","));
140         r += ContentTime::from_seconds (lexical_cast<int> (b[0]));
141         r += ContentTime::from_seconds (lexical_cast<double> (b[1]) / 1000);
142
143         return r;
144 }
145
146 int
147 SubRip::convert_coordinate (string t)
148 {
149         vector<string> a;
150         boost::algorithm::split (a, t, boost::is_any_of (":"));
151         assert (a.size() == 2);
152         return lexical_cast<int> (a[1]);
153 }
154
155 void
156 SubRip::maybe_content (list<SubRipSubtitlePiece>& pieces, SubRipSubtitlePiece& p)
157 {
158         if (!p.text.empty ()) {
159                 pieces.push_back (p);
160                 p.text.clear ();
161         }
162 }
163
164 list<SubRipSubtitlePiece>
165 SubRip::convert_content (list<string> t)
166 {
167         list<SubRipSubtitlePiece> pieces;
168         
169         SubRipSubtitlePiece p;
170
171         enum {
172                 TEXT,
173                 TAG
174         } state = TEXT;
175
176         string tag;
177
178         /* XXX: missing <font> support */
179         /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
180            not work, I think.
181         */
182
183         for (list<string>::const_iterator i = t.begin(); i != t.end(); ++i) {
184                 for (size_t j = 0; j < i->size(); ++j) {
185                         switch (state) {
186                         case TEXT:
187                                 if ((*i)[j] == '<' || (*i)[j] == '{') {
188                                         state = TAG;
189                                 } else {
190                                         p.text += (*i)[j];
191                                 }
192                                 break;
193                         case TAG:
194                                 if ((*i)[j] == '>' || (*i)[j] == '}') {
195                                         if (tag == "b") {
196                                                 maybe_content (pieces, p);
197                                                 p.bold = true;
198                                         } else if (tag == "/b") {
199                                                 maybe_content (pieces, p);
200                                                 p.bold = false;
201                                         } else if (tag == "i") {
202                                                 maybe_content (pieces, p);
203                                                 p.italic = true;
204                                         } else if (tag == "/i") {
205                                                 maybe_content (pieces, p);
206                                                 p.italic = false;
207                                         } else if (tag == "u") {
208                                                 maybe_content (pieces, p);
209                                                 p.underline = true;
210                                         } else if (tag == "/u") {
211                                                 maybe_content (pieces, p);
212                                                 p.underline = false;
213                                         }
214                                         tag.clear ();
215                                         state = TEXT;
216                                 } else {
217                                         tag += (*i)[j];
218                                 }
219                                 break;
220                         }
221                 }
222         }
223
224         maybe_content (pieces, p);
225
226         return pieces;
227 }
228
229 ContentTime
230 SubRip::length () const
231 {
232         if (_subtitles.empty ()) {
233                 return ContentTime ();
234         }
235
236         return _subtitles.back().to;
237 }