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