Fix boost bind warning.
[libsub.git] / src / ssa_reader.cc
1 /*
2     Copyright (C) 2016-2019 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 "ssa_reader.h"
21 #include "util.h"
22 #include "sub_assert.h"
23 #include "raw_convert.h"
24 #include "subtitle.h"
25 #include "compose.hpp"
26 #include <boost/algorithm/string.hpp>
27 #include <boost/bind/bind.hpp>
28 #include <iostream>
29 #include <vector>
30
31 using std::string;
32 using std::vector;
33 using std::map;
34 using std::cout;
35 using boost::optional;
36 using boost::function;
37 using namespace boost::algorithm;
38 #if BOOST_VERSION >= 106100
39 using namespace boost::placeholders;
40 #endif
41 using namespace sub;
42
43 /** @param s Subtitle string encoded in UTF-8 */
44 SSAReader::SSAReader (string s)
45 {
46         this->read (boost::bind(&get_line_string, &s));
47 }
48
49 /** @param f Subtitle file encoded in UTF-8 */
50 SSAReader::SSAReader (FILE* f)
51 {
52         this->read (boost::bind (&get_line_file, f));
53 }
54
55 Colour
56 h_colour (string s)
57 {
58         /* There are both BGR and ABGR versions of these colours */
59         if ((s.length() != 8 && s.length() != 10) || s[0] != '&' || s[1] != 'H') {
60                 throw SSAError(String::compose("Badly formatted colour tag %1", s));
61         }
62         int ir, ig, ib;
63         /* XXX: ignoring alpha channel here; note that 00 is opaque and FF is transparent */
64         int const off = s.length() == 10 ? 4 : 2;
65         if (sscanf(s.c_str() + off, "%2x%2x%2x", &ib, &ig, &ir) < 3) {
66                 throw SSAError(String::compose("Badly formatted colour tag %1", s));
67         }
68         return sub::Colour(ir / 255.0, ig / 255.0, ib / 255.0);
69 }
70
71 class Style
72 {
73 public:
74         Style ()
75                 : font_size (72)
76                 , primary_colour (255, 255, 255)
77                 , bold (false)
78                 , italic (false)
79                 , underline (false)
80                 , horizontal_reference (HORIZONTAL_CENTRE_OF_SCREEN)
81                 , vertical_reference (BOTTOM_OF_SCREEN)
82                 , vertical_margin (0)
83         {}
84
85         Style (string format_line, string style_line)
86                 : font_size (72)
87                 , primary_colour (255, 255, 255)
88                 , bold (false)
89                 , italic (false)
90                 , underline (false)
91                 , horizontal_reference (HORIZONTAL_CENTRE_OF_SCREEN)
92                 , vertical_reference (BOTTOM_OF_SCREEN)
93                 , vertical_margin (0)
94         {
95                 vector<string> keys;
96                 split (keys, format_line, boost::is_any_of (","));
97                 vector<string> style;
98                 split (style, style_line, boost::is_any_of (","));
99
100                 SUB_ASSERT (!keys.empty());
101                 SUB_ASSERT (!style.empty());
102                 SUB_ASSERT (keys.size() == style.size());
103
104                 for (size_t i = 0; i < style.size(); ++i) {
105                         trim (keys[i]);
106                         trim (style[i]);
107                         if (keys[i] == "Name") {
108                                 name = style[i];
109                         } else if (keys[i] == "Fontname") {
110                                 font_name = style[i];
111                         } else if (keys[i] == "Fontsize") {
112                                 font_size = raw_convert<int> (style[i]);
113                         } else if (keys[i] == "PrimaryColour") {
114                                 primary_colour = colour (style[i]);
115                         } else if (keys[i] == "BackColour") {
116                                 back_colour = colour (style[i]);
117                         } else if (keys[i] == "Bold") {
118                                 bold = style[i] == "-1";
119                         } else if (keys[i] == "Italic") {
120                                 italic = style[i] == "-1";
121                         } else if (keys[i] == "Underline") {
122                                 underline = style[i] == "-1";
123                         } else if (keys[i] == "BorderStyle") {
124                                 if (style[i] == "1") {
125                                         effect = SHADOW;
126                                 }
127                         } else if (keys[i] == "Alignment") {
128                                 /* These values from libass' source code */
129                                 switch ((raw_convert<int> (style[i]) - 1) % 3) {
130                                 case 0:
131                                         horizontal_reference = LEFT_OF_SCREEN;
132                                         break;
133                                 case 1:
134                                         horizontal_reference = HORIZONTAL_CENTRE_OF_SCREEN;
135                                         break;
136                                 case 2:
137                                         horizontal_reference = RIGHT_OF_SCREEN;
138                                         break;
139                                 }
140                                 switch (raw_convert<int> (style[i]) & 12) {
141                                 case 4:
142                                         vertical_reference = TOP_OF_SCREEN;
143                                         break;
144                                 case 8:
145                                         vertical_reference = VERTICAL_CENTRE_OF_SCREEN;
146                                         break;
147                                 case 0:
148                                         vertical_reference = BOTTOM_OF_SCREEN;
149                                         break;
150                                 }
151                         } else if (keys[i] == "MarginV") {
152                                 vertical_margin = raw_convert<int> (style[i]);
153                         }
154                 }
155         }
156
157         string name;
158         optional<string> font_name;
159         int font_size;
160         Colour primary_colour;
161         /** outline colour */
162         optional<Colour> back_colour;
163         bool bold;
164         bool italic;
165         bool underline;
166         optional<Effect> effect;
167         HorizontalReference horizontal_reference;
168         VerticalReference vertical_reference;
169         int vertical_margin;
170
171 private:
172         Colour colour (string c) const
173         {
174                 if (c.length() > 0 && c[0] == '&') {
175                         /* &Hbbggrr or &Haabbggrr */
176                         return h_colour (c);
177                 } else {
178                         /* integer */
179                         int i = raw_convert<int>(c);
180                         return Colour (
181                                 ((i & 0x0000ff) >>  0) / 255.0,
182                                 ((i & 0x00ff00) >>  8) / 255.0,
183                                 ((i & 0xff0000) >> 16) / 255.0
184                                 );
185                 }
186         }
187 };
188
189 Time
190 SSAReader::parse_time (string t) const
191 {
192         vector<string> bits;
193         split (bits, t, is_any_of (":."));
194         SUB_ASSERT (bits.size() == 4);
195         return Time::from_hms (
196                 raw_convert<int> (bits[0]),
197                 raw_convert<int> (bits[1]),
198                 raw_convert<int> (bits[2]),
199                 raw_convert<int> (bits[3]) * 10
200                 );
201 }
202
203 void
204 SSAReader::parse_style (RawSubtitle& sub, string style, int play_res_x, int play_res_y)
205 {
206         if (style == "\\i1") {
207                 sub.italic = true;
208         } else if (style == "\\i0" || style == "\\i") {
209                 sub.italic = false;
210         } else if (style == "\\b1") {
211                 sub.bold = true;
212         } else if (style == "\\b0") {
213                 sub.bold = false;
214         } else if (style == "\\u1") {
215                 sub.underline = true;
216         } else if (style == "\\u0") {
217                 sub.underline = false;
218         } else if (style == "\\an1") {
219                 sub.horizontal_position.reference = sub::LEFT_OF_SCREEN;
220                 sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
221         } else if (style == "\\an2") {
222                 sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN;
223                 sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
224         } else if (style == "\\an3") {
225                 sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN;
226                 sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
227         } else if (style == "\\an4") {
228                 sub.horizontal_position.reference = sub::LEFT_OF_SCREEN;
229                 sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN;
230         } else if (style == "\\an5") {
231                 sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN;
232                 sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN;
233         } else if (style == "\\an6") {
234                 sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN;
235                 sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN;
236         } else if (style == "\\an7") {
237                 sub.horizontal_position.reference = sub::LEFT_OF_SCREEN;
238                 sub.vertical_position.reference = sub::TOP_OF_SCREEN;
239         } else if (style == "\\an8") {
240                 sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN;
241                 sub.vertical_position.reference = sub::TOP_OF_SCREEN;
242         } else if (style == "\\an9") {
243                 sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN;
244                 sub.vertical_position.reference = sub::TOP_OF_SCREEN;
245         } else if (boost::starts_with(style, "\\pos")) {
246                 vector<string> bits;
247                 boost::algorithm::split (bits, style, boost::is_any_of("(,"));
248                 SUB_ASSERT (bits.size() == 3);
249                 sub.horizontal_position.reference = sub::LEFT_OF_SCREEN;
250                 sub.horizontal_position.proportional = raw_convert<float>(bits[1]) / play_res_x;
251                 sub.vertical_position.reference = sub::TOP_OF_SCREEN;
252                 sub.vertical_position.proportional = raw_convert<float>(bits[2]) / play_res_y;
253         } else if (boost::starts_with(style, "\\fs")) {
254                 SUB_ASSERT (style.length() > 3);
255                 sub.font_size.set_points (raw_convert<int>(style.substr(3)));
256         } else if (boost::starts_with(style, "\\c")) {
257                 /* \c&Hbbggrr& */
258                 if (style.length() <= 2) {
259                         throw SSAError(String::compose("Badly formatted colour tag %1", style));
260                 }
261                 sub.colour = h_colour (style.substr(2, style.length() - 3));
262         }
263 }
264
265 /** @param base RawSubtitle filled in with any required common values.
266  *  @param line SSA line string (i.e. just the subtitle, possibly with embedded stuff)
267  *  @return List of RawSubtitles to represent line with vertical reference TOP_OF_SUBTITLE.
268  */
269 vector<RawSubtitle>
270 SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_res_y)
271 {
272         enum {
273                 TEXT,
274                 STYLE,
275                 BACKSLASH
276         } state = TEXT;
277
278         vector<RawSubtitle> subs;
279         RawSubtitle current = base;
280         string style;
281
282         if (!current.vertical_position.reference) {
283                 current.vertical_position.reference = BOTTOM_OF_SCREEN;
284         }
285
286         if (!current.vertical_position.proportional) {
287                 current.vertical_position.proportional = 0;
288         }
289
290         /* We must have a font size, as there could be a margin specified
291            in pixels and in that case we must know how big the subtitle
292            lines are to work out the position on screen.
293         */
294         if (!current.font_size.points()) {
295                 current.font_size.set_points (72);
296         }
297
298         /* Count the number of line breaks */
299         int line_breaks = 0;
300         if (line.length() > 0) {
301                 for (size_t i = 0; i < line.length() - 1; ++i) {
302                         if (line[i] == '\\' && (line[i+1] == 'n' || line[i+1] == 'N')) {
303                                 ++line_breaks;
304                         }
305                 }
306         }
307
308         /* Imagine that the screen is 792 points (i.e. 11 inches) high (as with DCP) */
309         double const line_size = current.font_size.proportional(792) * 1.2;
310
311         /* Tweak vertical_position accordingly */
312         switch (current.vertical_position.reference.get()) {
313         case TOP_OF_SCREEN:
314         case TOP_OF_SUBTITLE:
315                 /* Nothing to do */
316                 break;
317         case VERTICAL_CENTRE_OF_SCREEN:
318                 current.vertical_position.proportional = current.vertical_position.proportional.get() - ((line_breaks + 1) * line_size) / 2;
319                 break;
320         case BOTTOM_OF_SCREEN:
321                 current.vertical_position.proportional = current.vertical_position.proportional.get() + line_breaks * line_size;
322                 break;
323         }
324
325         for (size_t i = 0; i < line.length(); ++i) {
326                 char const c = line[i];
327                 switch (state) {
328                 case TEXT:
329                         if (c == '{') {
330                                 state = STYLE;
331                         } else if (c == '\\') {
332                                 state = BACKSLASH;
333                         } else if (c != '\r' && c != '\n') {
334                                 current.text += c;
335                         }
336                         break;
337                 case STYLE:
338                         if (c == '}' || c == '\\') {
339                                 if (!current.text.empty ()) {
340                                         subs.push_back (current);
341                                         current.text = "";
342                                 }
343                                 parse_style (current, style, play_res_x, play_res_y);
344                                 style = "";
345                         }
346
347                         if (c == '}') {
348                                 state = TEXT;
349                         } else {
350                                 style += c;
351                         }
352                         break;
353                 case BACKSLASH:
354                         if (c == 'n' || c == 'N') {
355                                 if (!current.text.empty ()) {
356                                         subs.push_back (current);
357                                         current.text = "";
358                                 }
359                                 /* Move down one line (1.2 times the font size) */
360                                 if (current.vertical_position.reference.get() == BOTTOM_OF_SCREEN) {
361                                         current.vertical_position.proportional = current.vertical_position.proportional.get() - line_size;
362                                 } else {
363                                         current.vertical_position.proportional = current.vertical_position.proportional.get() + line_size;
364                                 }
365                         }
366                         state = TEXT;
367                         break;
368                 }
369         }
370
371         if (!current.text.empty ()) {
372                 subs.push_back (current);
373         }
374
375         return subs;
376 }
377
378 void
379 SSAReader::read (function<optional<string> ()> get_line)
380 {
381         enum {
382                 INFO,
383                 STYLES,
384                 EVENTS
385         } part = INFO;
386
387         int play_res_x = 288;
388         int play_res_y = 288;
389         map<string, Style> styles;
390         string style_format_line;
391         vector<string> event_format;
392
393         while (true) {
394                 optional<string> line = get_line ();
395                 if (!line) {
396                         break;
397                 }
398
399                 trim (*line);
400                 remove_unicode_bom (line);
401
402                 if (starts_with (*line, ";") || line->empty ()) {
403                         continue;
404                 }
405
406                 if (starts_with (*line, "[")) {
407                         /* Section heading */
408                         if (line.get() == "[Script Info]") {
409                                 part = INFO;
410                         } else if (line.get() == "[V4 Styles]" || line.get() == "[V4+ Styles]") {
411                                 part = STYLES;
412                         } else if (line.get() == "[Events]") {
413                                 part = EVENTS;
414                         }
415                         continue;
416                 }
417
418                 size_t const colon = line->find (":");
419                 SUB_ASSERT (colon != string::npos);
420                 string const type = line->substr (0, colon);
421                 string body = line->substr (colon + 1);
422                 trim (body);
423
424                 switch (part) {
425                 case INFO:
426                         if (type == "PlayResX") {
427                                 play_res_x = raw_convert<int> (body);
428                         } else if (type == "PlayResY") {
429                                 play_res_y = raw_convert<int> (body);
430                         }
431                         break;
432                 case STYLES:
433                         if (type == "Format") {
434                                 style_format_line = body;
435                         } else if (type == "Style") {
436                                 SUB_ASSERT (!style_format_line.empty ());
437                                 Style s (style_format_line, body);
438                                 styles[s.name] = s;
439                         }
440                         break;
441                 case EVENTS:
442                         if (type == "Format") {
443                                 split (event_format, body, is_any_of (","));
444                                 for (auto& i: event_format) {
445                                         trim (i);
446                                 }
447                         } else if (type == "Dialogue") {
448                                 SUB_ASSERT (!event_format.empty ());
449                                 vector<string> event;
450                                 split (event, body, is_any_of (","));
451
452                                 /* There may be commas in the subtitle part; reassemble any extra parts
453                                    from when we just split it.
454                                 */
455                                 while (event.size() > event_format.size()) {
456                                         string const ex = event.back ();
457                                         event.pop_back ();
458                                         event.back() += "," + ex;
459                                 }
460
461                                 SUB_ASSERT (!event.empty());
462                                 SUB_ASSERT (event_format.size() == event.size());
463
464                                 RawSubtitle sub;
465
466                                 for (size_t i = 0; i < event.size(); ++i) {
467                                         trim (event[i]);
468                                         if (event_format[i] == "Start") {
469                                                 sub.from = parse_time (event[i]);
470                                         } else if (event_format[i] == "End") {
471                                                 sub.to = parse_time (event[i]);
472                                         } else if (event_format[i] == "Style") {
473                                                 /* libass trims leading '*'s from style names, commenting that
474                                                    "they seem to mean literally nothing".  Go figure...
475                                                 */
476                                                 trim_left_if (event[i], boost::is_any_of ("*"));
477                                                 SUB_ASSERT (styles.find(event[i]) != styles.end());
478                                                 Style style = styles[event[i]];
479                                                 sub.font = style.font_name;
480                                                 sub.font_size = FontSize::from_points (style.font_size);
481                                                 sub.colour = style.primary_colour;
482                                                 sub.effect_colour = style.back_colour;
483                                                 sub.bold = style.bold;
484                                                 sub.italic = style.italic;
485                                                 sub.underline = style.underline;
486                                                 sub.effect = style.effect;
487                                                 sub.horizontal_position.reference = style.horizontal_reference;
488                                                 sub.vertical_position.reference = style.vertical_reference;
489                                                 sub.vertical_position.proportional = float(style.vertical_margin) / play_res_y;
490                                         } else if (event_format[i] == "MarginV") {
491                                                 sub.vertical_position.proportional = raw_convert<float>(event[i]) / play_res_y;
492                                         } else if (event_format[i] == "Text") {
493                                                 for (auto j: parse_line (sub, event[i], play_res_x, play_res_y)) {
494                                                         _subs.push_back (j);
495                                                 }
496                                         }
497                                 }
498                         }
499                 }
500
501         }
502 }