Fix pretty broken SSA alignment code.
[libsub.git] / src / ssa_reader.cc
index c507ddbd84f71aaba4323c48499f5c778efc17ad..03798034b3cdfe0435c7a54382d78bcea894d23e 100644 (file)
@@ -24,8 +24,7 @@
 #include "subtitle.h"
 #include "compose.hpp"
 #include <boost/algorithm/string.hpp>
-#include <boost/bind.hpp>
-#include <boost/foreach.hpp>
+#include <boost/bind/bind.hpp>
 #include <iostream>
 #include <vector>
 
@@ -33,10 +32,12 @@ using std::string;
 using std::vector;
 using std::map;
 using std::cout;
-using std::list;
 using boost::optional;
 using boost::function;
 using namespace boost::algorithm;
+#if BOOST_VERSION >= 106100
+using namespace boost::placeholders;
+#endif
 using namespace sub;
 
 /** @param s Subtitle string encoded in UTF-8 */
@@ -124,28 +125,19 @@ public:
                                        effect = SHADOW;
                                }
                        } else if (keys[i] == "Alignment") {
-                               /* These values from libass' source code */
-                               switch ((raw_convert<int> (style[i]) - 1) % 3) {
-                               case 0:
-                                       horizontal_reference = LEFT_OF_SCREEN;
-                                       break;
-                               case 1:
-                                       horizontal_reference = HORIZONTAL_CENTRE_OF_SCREEN;
-                                       break;
-                               case 2:
-                                       horizontal_reference = RIGHT_OF_SCREEN;
-                                       break;
-                               }
-                               switch (raw_convert<int> (style[i]) & 12) {
-                               case 4:
+                               if (style[i] == "7" || style[i] == "8" || style[i] == "9") {
                                        vertical_reference = TOP_OF_SCREEN;
-                                       break;
-                               case 8:
+                               } else if (style[i] == "4" || style[i] == "5" || style[i] == "6") {
                                        vertical_reference = VERTICAL_CENTRE_OF_SCREEN;
-                                       break;
-                               case 0:
+                               } else {
                                        vertical_reference = BOTTOM_OF_SCREEN;
-                                       break;
+                               }
+                               if (style[i] == "1" || style[i] == "4" || style[i] == "7") {
+                                       horizontal_reference = LEFT_OF_SCREEN;
+                               } else if (style[i] == "3" || style[i] == "6" || style[i] == "9") {
+                                       horizontal_reference = RIGHT_OF_SCREEN;
+                               } else {
+                                       horizontal_reference = HORIZONTAL_CENTRE_OF_SCREEN;
                                }
                        } else if (keys[i] == "MarginV") {
                                vertical_margin = raw_convert<int> (style[i]);
@@ -265,7 +257,7 @@ SSAReader::parse_style (RawSubtitle& sub, string style, int play_res_x, int play
  *  @param line SSA line string (i.e. just the subtitle, possibly with embedded stuff)
  *  @return List of RawSubtitles to represent line with vertical reference TOP_OF_SUBTITLE.
  */
-list<RawSubtitle>
+vector<RawSubtitle>
 SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_res_y)
 {
        enum {
@@ -274,7 +266,7 @@ SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_r
                BACKSLASH
        } state = TEXT;
 
-       list<RawSubtitle> subs;
+       vector<RawSubtitle> subs;
        RawSubtitle current = base;
        string style;
 
@@ -440,7 +432,7 @@ SSAReader::read (function<optional<string> ()> get_line)
                case EVENTS:
                        if (type == "Format") {
                                split (event_format, body, is_any_of (","));
-                               BOOST_FOREACH (string& i, event_format) {
+                               for (auto& i: event_format) {
                                        trim (i);
                                }
                        } else if (type == "Dialogue") {
@@ -489,7 +481,7 @@ SSAReader::read (function<optional<string> ()> get_line)
                                        } else if (event_format[i] == "MarginV") {
                                                sub.vertical_position.proportional = raw_convert<float>(event[i]) / play_res_y;
                                        } else if (event_format[i] == "Text") {
-                                               BOOST_FOREACH (sub::RawSubtitle j, parse_line (sub, event[i], play_res_x, play_res_y)) {
+                                               for (auto j: parse_line (sub, event[i], play_res_x, play_res_y)) {
                                                        _subs.push_back (j);
                                                }
                                        }