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