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