Tweak ISO6937 mapping to put $ sign on 0xa4 (164) (from master).
[libsub.git] / src / ssa_reader.cc
index 74d497a87d85049a456ddc7a25b25baea17713bc..147f1bad75691a7c2bddd9caee6b9da2b3a27e0d 100644 (file)
 #include "sub_assert.h"
 #include "raw_convert.h"
 #include "subtitle.h"
+#include <locked_sstream.h>
 #include <boost/algorithm/string.hpp>
 #include <boost/bind.hpp>
 #include <boost/foreach.hpp>
-#include <sstream>
 #include <iostream>
 #include <vector>
 
 using std::string;
-using std::stringstream;
 using std::vector;
 using std::map;
 using std::cout;
@@ -43,7 +42,7 @@ using namespace sub;
 /** @param s Subtitle string encoded in UTF-8 */
 SSAReader::SSAReader (string const & s)
 {
-       stringstream str (s);
+       locked_stringstream str (s);
        this->read (boost::bind (&get_line_stringstream, &str));
 }
 
@@ -61,6 +60,7 @@ public:
                , primary_colour (255, 255, 255)
                , bold (false)
                , italic (false)
+               , underline (false)
                , vertical_reference (BOTTOM_OF_SCREEN)
                , vertical_margin (0)
        {}
@@ -70,6 +70,7 @@ public:
                , primary_colour (255, 255, 255)
                , bold (false)
                , italic (false)
+               , underline (false)
                , vertical_reference (BOTTOM_OF_SCREEN)
                , vertical_margin (0)
        {
@@ -99,6 +100,8 @@ public:
                                bold = style[i] == "-1";
                        } else if (keys[i] == "Italic") {
                                italic = style[i] == "-1";
+                       } else if (keys[i] == "Underline") {
+                               underline = style[i] == "-1";
                        } else if (keys[i] == "BorderStyle") {
                                if (style[i] == "1") {
                                        effect = SHADOW;
@@ -130,6 +133,7 @@ public:
        optional<Colour> back_colour;
        bool bold;
        bool italic;
+       bool underline;
        optional<Effect> effect;
        VerticalReference vertical_reference;
        int vertical_margin;
@@ -230,7 +234,7 @@ SSAReader::parse_line (RawSubtitle base, string line)
                        }
                        break;
                case STYLE:
-                       if (c == '}') {
+                       if (c == '}' || c == '\\') {
                                if (!current.text.empty ()) {
                                        subs.push_back (current);
                                        current.text = "";
@@ -239,6 +243,14 @@ SSAReader::parse_line (RawSubtitle base, string line)
                                        current.italic = true;
                                } else if (style == "\\i0" || style == "\\i") {
                                        current.italic = false;
+                               } else if (style == "\\b1") {
+                                       current.bold = true;
+                               } else if (style == "\\b0") {
+                                       current.bold = false;
+                               } else if (style == "\\u1") {
+                                       current.underline = true;
+                               } else if (style == "\\u0") {
+                                       current.underline = false;
                                } else if (style == "\\an1" || style == "\\an2" || style == "\\an3") {
                                        current.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
                                } else if (style == "\\an4" || style == "\\an5" || style == "\\an6") {
@@ -246,16 +258,22 @@ SSAReader::parse_line (RawSubtitle base, string line)
                                } else if (style == "\\an7" || style == "\\an8" || style == "\\an9") {
                                        current.vertical_position.reference = sub::TOP_OF_SCREEN;
                                }
+
                                style = "";
+                       }
+
+                       if (c == '}') {
                                state = TEXT;
                        } else {
                                style += c;
                        }
                        break;
                case BACKSLASH:
-                       if ((c == 'n' || c == 'N') && !current.text.empty ()) {
-                               subs.push_back (current);
-                               current.text = "";
+                       if (c == 'n' || c == 'N') {
+                               if (!current.text.empty ()) {
+                                       subs.push_back (current);
+                                       current.text = "";
+                               }
                                /* Move down one line (1.2 times the font size) */
                                if (current.vertical_position.reference.get() == BOTTOM_OF_SCREEN) {
                                        current.vertical_position.proportional = current.vertical_position.proportional.get() - line_size;
@@ -306,7 +324,7 @@ SSAReader::read (function<optional<string> ()> get_line)
                        /* Section heading */
                        if (line.get() == "[Script Info]") {
                                part = INFO;
-                       } else if (line.get() == "[V4 Styles]") {
+                       } else if (line.get() == "[V4 Styles]" || line.get() == "[V4+ Styles]") {
                                part = STYLES;
                        } else if (line.get() == "[Events]") {
                                part = EVENTS;
@@ -379,6 +397,7 @@ SSAReader::read (function<optional<string> ()> get_line)
                                                sub.effect_colour = style.back_colour;
                                                sub.bold = style.bold;
                                                sub.italic = style.italic;
+                                               sub.underline = style.underline;
                                                sub.effect = style.effect;
                                                sub.vertical_position.reference = style.vertical_reference;
                                                sub.vertical_position.proportional = float(style.vertical_margin) / play_res_y;