It seems likely that SSA font sizes should be interpreted as
[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                                 if (style[i] == "7" || style[i] == "8" || style[i] == "9") {
129                                         vertical_reference = TOP_OF_SCREEN;
130                                 } else if (style[i] == "4" || style[i] == "5" || style[i] == "6") {
131                                         vertical_reference = VERTICAL_CENTRE_OF_SCREEN;
132                                 } else {
133                                         vertical_reference = BOTTOM_OF_SCREEN;
134                                 }
135                                 if (style[i] == "1" || style[i] == "4" || style[i] == "7") {
136                                         horizontal_reference = LEFT_OF_SCREEN;
137                                 } else if (style[i] == "3" || style[i] == "6" || style[i] == "9") {
138                                         horizontal_reference = RIGHT_OF_SCREEN;
139                                 } else {
140                                         horizontal_reference = HORIZONTAL_CENTRE_OF_SCREEN;
141                                 }
142                         } else if (keys[i] == "MarginV") {
143                                 vertical_margin = raw_convert<int> (style[i]);
144                         }
145                 }
146         }
147
148         string name;
149         optional<string> font_name;
150         int font_size; ///< points
151         Colour primary_colour;
152         /** outline colour */
153         optional<Colour> back_colour;
154         bool bold;
155         bool italic;
156         bool underline;
157         optional<Effect> effect;
158         HorizontalReference horizontal_reference;
159         VerticalReference vertical_reference;
160         int vertical_margin;
161
162 private:
163         Colour colour (string c) const
164         {
165                 if (c.length() > 0 && c[0] == '&') {
166                         /* &Hbbggrr or &Haabbggrr */
167                         return h_colour (c);
168                 } else {
169                         /* integer */
170                         int i = raw_convert<int>(c);
171                         return Colour (
172                                 ((i & 0x0000ff) >>  0) / 255.0,
173                                 ((i & 0x00ff00) >>  8) / 255.0,
174                                 ((i & 0xff0000) >> 16) / 255.0
175                                 );
176                 }
177         }
178 };
179
180 Time
181 SSAReader::parse_time (string t) const
182 {
183         vector<string> bits;
184         split (bits, t, is_any_of (":."));
185         SUB_ASSERT (bits.size() == 4);
186         return Time::from_hms (
187                 raw_convert<int> (bits[0]),
188                 raw_convert<int> (bits[1]),
189                 raw_convert<int> (bits[2]),
190                 raw_convert<int> (bits[3]) * 10
191                 );
192 }
193
194 void
195 SSAReader::parse_style (RawSubtitle& sub, string style, int play_res_x, int play_res_y)
196 {
197         if (style == "\\i1") {
198                 sub.italic = true;
199         } else if (style == "\\i0" || style == "\\i") {
200                 sub.italic = false;
201         } else if (style == "\\b1") {
202                 sub.bold = true;
203         } else if (style == "\\b0") {
204                 sub.bold = false;
205         } else if (style == "\\u1") {
206                 sub.underline = true;
207         } else if (style == "\\u0") {
208                 sub.underline = false;
209         } else if (style == "\\an1") {
210                 sub.horizontal_position.reference = sub::LEFT_OF_SCREEN;
211                 sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
212         } else if (style == "\\an2") {
213                 sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN;
214                 sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
215         } else if (style == "\\an3") {
216                 sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN;
217                 sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
218         } else if (style == "\\an4") {
219                 sub.horizontal_position.reference = sub::LEFT_OF_SCREEN;
220                 sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN;
221         } else if (style == "\\an5") {
222                 sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN;
223                 sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN;
224         } else if (style == "\\an6") {
225                 sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN;
226                 sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN;
227         } else if (style == "\\an7") {
228                 sub.horizontal_position.reference = sub::LEFT_OF_SCREEN;
229                 sub.vertical_position.reference = sub::TOP_OF_SCREEN;
230         } else if (style == "\\an8") {
231                 sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN;
232                 sub.vertical_position.reference = sub::TOP_OF_SCREEN;
233         } else if (style == "\\an9") {
234                 sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN;
235                 sub.vertical_position.reference = sub::TOP_OF_SCREEN;
236         } else if (boost::starts_with(style, "\\pos")) {
237                 vector<string> bits;
238                 boost::algorithm::split (bits, style, boost::is_any_of("(,"));
239                 SUB_ASSERT (bits.size() == 3);
240                 sub.horizontal_position.reference = sub::LEFT_OF_SCREEN;
241                 sub.horizontal_position.proportional = raw_convert<float>(bits[1]) / play_res_x;
242                 sub.vertical_position.reference = sub::TOP_OF_SCREEN;
243                 sub.vertical_position.proportional = raw_convert<float>(bits[2]) / play_res_y;
244         } else if (boost::starts_with(style, "\\fs")) {
245                 SUB_ASSERT (style.length() > 3);
246                 sub.font_size.set_proportional(raw_convert<float>(style.substr(3)) / play_res_y);
247         } else if (boost::starts_with(style, "\\c")) {
248                 /* \c&Hbbggrr& */
249                 if (style.length() <= 2) {
250                         throw SSAError(String::compose("Badly formatted colour tag %1", style));
251                 }
252                 sub.colour = h_colour (style.substr(2, style.length() - 3));
253         }
254 }
255
256 /** @param base RawSubtitle filled in with any required common values.
257  *  @param line SSA line string (i.e. just the subtitle, possibly with embedded stuff)
258  *  @return List of RawSubtitles to represent line with vertical reference TOP_OF_SUBTITLE.
259  */
260 vector<RawSubtitle>
261 SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_res_y)
262 {
263         enum {
264                 TEXT,
265                 STYLE,
266                 BACKSLASH
267         } state = TEXT;
268
269         vector<RawSubtitle> subs;
270         RawSubtitle current = base;
271         string style;
272
273         if (!current.vertical_position.reference) {
274                 current.vertical_position.reference = BOTTOM_OF_SCREEN;
275         }
276
277         /* Any vertical_position that is set in base (and therefore current) is a margin, which
278          * we need to ignore if we end up vertically centering this subtitle.
279          * Clear out vertical_position from current; we'll re-add it from base later
280          * if required.
281          */
282         current.vertical_position.proportional = 0;
283
284         /* We must have a font size, as there could be a margin specified
285            in pixels and in that case we must know how big the subtitle
286            lines are to work out the position on screen.
287         */
288         if (!current.font_size.proportional()) {
289                 current.font_size.set_proportional(72.0 / play_res_y);
290         }
291
292         /* Count the number of line breaks */
293         int line_breaks = 0;
294         if (line.length() > 0) {
295                 for (size_t i = 0; i < line.length() - 1; ++i) {
296                         if (line[i] == '\\' && (line[i+1] == 'n' || line[i+1] == 'N')) {
297                                 ++line_breaks;
298                         }
299                 }
300         }
301
302         /* There are vague indications that with ASS 1 point should equal 1 pixel */
303         double const line_size = current.font_size.proportional(play_res_y) * 1.2;
304
305         for (size_t i = 0; i < line.length(); ++i) {
306                 char const c = line[i];
307                 switch (state) {
308                 case TEXT:
309                         if (c == '{') {
310                                 state = STYLE;
311                         } else if (c == '\\') {
312                                 state = BACKSLASH;
313                         } else if (c != '\r' && c != '\n') {
314                                 current.text += c;
315                         }
316                         break;
317                 case STYLE:
318                         if (c == '}' || c == '\\') {
319                                 if (!current.text.empty ()) {
320                                         subs.push_back (current);
321                                         current.text = "";
322                                 }
323                                 parse_style (current, style, play_res_x, play_res_y);
324                                 style = "";
325                         }
326
327                         if (c == '}') {
328                                 state = TEXT;
329                         } else {
330                                 style += c;
331                         }
332                         break;
333                 case BACKSLASH:
334                         if (c == 'n' || c == 'N') {
335                                 if (!current.text.empty ()) {
336                                         subs.push_back (current);
337                                         current.text = "";
338                                 }
339                                 /* Move down one line (1.2 times the font size) */
340                                 if (current.vertical_position.reference.get() == BOTTOM_OF_SCREEN) {
341                                         current.vertical_position.proportional = current.vertical_position.proportional.get() - line_size;
342                                 } else {
343                                         current.vertical_position.proportional = current.vertical_position.proportional.get() + line_size;
344                                 }
345                         }
346                         state = TEXT;
347                         break;
348                 }
349         }
350
351         if (!current.text.empty ()) {
352                 subs.push_back (current);
353         }
354
355         /* Now we definitely know the vertical position reference we can finish off the position */
356         for (auto& sub: subs) {
357                 switch (sub.vertical_position.reference.get()) {
358                 case TOP_OF_SCREEN:
359                 case TOP_OF_SUBTITLE:
360                         /* Just re-add any margins we came in with */
361                         sub.vertical_position.proportional = sub.vertical_position.proportional.get() + base.vertical_position.proportional.get_value_or(0);
362                         break;
363                 case VERTICAL_CENTRE_OF_SCREEN:
364                         /* Margins are ignored, but we need to centre */
365                         sub.vertical_position.proportional = sub.vertical_position.proportional.get() - ((line_breaks + 1) * line_size) / 2;
366                         break;
367                 case BOTTOM_OF_SCREEN:
368                         /* Re-add margins and account for each line */
369                         sub.vertical_position.proportional =
370                                 sub.vertical_position.proportional.get()
371                                 + base.vertical_position.proportional.get_value_or(0)
372                                 + line_breaks * line_size;
373                         break;
374                 }
375         }
376
377         return subs;
378 }
379
380 void
381 SSAReader::read (function<optional<string> ()> get_line)
382 {
383         enum {
384                 INFO,
385                 STYLES,
386                 EVENTS
387         } part = INFO;
388
389         int play_res_x = 288;
390         int play_res_y = 288;
391         map<string, Style> styles;
392         string style_format_line;
393         vector<string> event_format;
394
395         while (true) {
396                 optional<string> line = get_line ();
397                 if (!line) {
398                         break;
399                 }
400
401                 trim (*line);
402                 remove_unicode_bom (line);
403
404                 if (starts_with (*line, ";") || line->empty ()) {
405                         continue;
406                 }
407
408                 if (starts_with (*line, "[")) {
409                         /* Section heading */
410                         if (line.get() == "[Script Info]") {
411                                 part = INFO;
412                         } else if (line.get() == "[V4 Styles]" || line.get() == "[V4+ Styles]") {
413                                 part = STYLES;
414                         } else if (line.get() == "[Events]") {
415                                 part = EVENTS;
416                         }
417                         continue;
418                 }
419
420                 size_t const colon = line->find (":");
421                 SUB_ASSERT (colon != string::npos);
422                 string const type = line->substr (0, colon);
423                 string body = line->substr (colon + 1);
424                 trim (body);
425
426                 switch (part) {
427                 case INFO:
428                         if (type == "PlayResX") {
429                                 play_res_x = raw_convert<int> (body);
430                         } else if (type == "PlayResY") {
431                                 play_res_y = raw_convert<int> (body);
432                         }
433                         break;
434                 case STYLES:
435                         if (type == "Format") {
436                                 style_format_line = body;
437                         } else if (type == "Style") {
438                                 SUB_ASSERT (!style_format_line.empty ());
439                                 Style s (style_format_line, body);
440                                 styles[s.name] = s;
441                         }
442                         break;
443                 case EVENTS:
444                         if (type == "Format") {
445                                 split (event_format, body, is_any_of (","));
446                                 for (auto& i: event_format) {
447                                         trim (i);
448                                 }
449                         } else if (type == "Dialogue") {
450                                 SUB_ASSERT (!event_format.empty ());
451                                 vector<string> event;
452                                 split (event, body, is_any_of (","));
453
454                                 /* There may be commas in the subtitle part; reassemble any extra parts
455                                    from when we just split it.
456                                 */
457                                 while (event.size() > event_format.size()) {
458                                         string const ex = event.back ();
459                                         event.pop_back ();
460                                         event.back() += "," + ex;
461                                 }
462
463                                 SUB_ASSERT (!event.empty());
464                                 SUB_ASSERT (event_format.size() == event.size());
465
466                                 RawSubtitle sub;
467
468                                 for (size_t i = 0; i < event.size(); ++i) {
469                                         trim (event[i]);
470                                         if (event_format[i] == "Start") {
471                                                 sub.from = parse_time (event[i]);
472                                         } else if (event_format[i] == "End") {
473                                                 sub.to = parse_time (event[i]);
474                                         } else if (event_format[i] == "Style") {
475                                                 /* libass trims leading '*'s from style names, commenting that
476                                                    "they seem to mean literally nothing".  Go figure...
477                                                 */
478                                                 trim_left_if (event[i], boost::is_any_of ("*"));
479                                                 SUB_ASSERT (styles.find(event[i]) != styles.end());
480                                                 Style style = styles[event[i]];
481                                                 sub.font = style.font_name;
482                                                 sub.font_size = FontSize::from_proportional(static_cast<float>(style.font_size) / play_res_y);
483                                                 sub.colour = style.primary_colour;
484                                                 sub.effect_colour = style.back_colour;
485                                                 sub.bold = style.bold;
486                                                 sub.italic = style.italic;
487                                                 sub.underline = style.underline;
488                                                 sub.effect = style.effect;
489                                                 sub.horizontal_position.reference = style.horizontal_reference;
490                                                 sub.vertical_position.reference = style.vertical_reference;
491                                                 if (sub.vertical_position.reference != sub::VERTICAL_CENTRE_OF_SCREEN) {
492                                                         sub.vertical_position.proportional = float(style.vertical_margin) / play_res_y;
493                                                 }
494                                         } else if (event_format[i] == "MarginV") {
495                                                 if (event[i] != "0" && sub.vertical_position.reference != sub::VERTICAL_CENTRE_OF_SCREEN) {
496                                                         /* Override the style if its non-zero */
497                                                         sub.vertical_position.proportional = raw_convert<float>(event[i]) / play_res_y;
498                                                 }
499                                         } else if (event_format[i] == "Text") {
500                                                 for (auto j: parse_line (sub, event[i], play_res_x, play_res_y)) {
501                                                         _subs.push_back (j);
502                                                 }
503                                         }
504                                 }
505                         }
506                 }
507
508         }
509 }