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