Strip Unicode U+202B (right-to-left-embedding) code; it looks like DoM does RTL ...
[libsub.git] / src / subrip_reader.cc
index ddaa11239c7c8284c2a94f89e3da471bf8d2e1d3..b5d04466bc7bb3e1f9cac167f75f204735b2642b 100644 (file)
@@ -23,6 +23,8 @@
 
 #include "subrip_reader.h"
 #include "exceptions.h"
+#include "util.h"
+#include <locked_sstream.h>
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/regex.hpp>
@@ -36,48 +38,24 @@ using std::vector;
 using std::list;
 using std::cout;
 using std::hex;
-using std::stringstream;
 using boost::lexical_cast;
 using boost::to_upper;
 using boost::optional;
 using boost::function;
+using boost::algorithm::replace_all;
 using namespace sub;
 
 /** @param s Subtitle string encoded in UTF-8 */
 SubripReader::SubripReader (string const & s)
 {
-       stringstream str (s);
-       this->read (boost::bind (&SubripReader::get_line_stringstream, this, &str));
+       locked_stringstream str (s);
+       this->read (boost::bind (&get_line_stringstream, &str));
 }
 
 /** @param f Subtitle file encoded in UTF-8 */
 SubripReader::SubripReader (FILE* f)
 {
-       this->read (boost::bind (&SubripReader::get_line_file, this, f));
-}
-
-optional<string>
-SubripReader::get_line_stringstream (stringstream* str) const
-{
-       string s;
-       getline (*str, s);
-       if (!str->good ()) {
-               return optional<string> ();
-       }
-
-       return s;
-}
-
-optional<string>
-SubripReader::get_line_file (FILE* f) const
-{
-       char buffer[256];
-       char* r = fgets (buffer, sizeof (buffer), f);
-       if (r == 0 || feof (f)) {
-               return optional<string> ();
-       }
-
-       return string (buffer);
+       this->read (boost::bind (&get_line_file, f));
 }
 
 void
@@ -89,11 +67,13 @@ SubripReader::read (function<optional<string> ()> get_line)
                CONTENT
        } state = COUNTER;
 
-       Time from;
-       Time to;
+       RawSubtitle rs;
 
-       string line;
-       int line_number = 0;
+       /* This reader extracts no information about where the subtitle
+          should be on screen, so its reference is TOP_OF_SUBTITLE.
+       */
+       rs.vertical_position.line = 0;
+       rs.vertical_position.reference = TOP_OF_SUBTITLE;
 
        while (true) {
                optional<string> line = get_line ();
@@ -102,16 +82,12 @@ SubripReader::read (function<optional<string> ()> get_line)
                }
 
                trim_right_if (*line, boost::is_any_of ("\n\r"));
+               remove_unicode_bom (line);
 
-               if (
-                       line->length() >= 3 &&
-                       static_cast<unsigned char> (line.get()[0]) == 0xef &&
-                       static_cast<unsigned char> (line.get()[1]) == 0xbb &&
-                       static_cast<unsigned char> (line.get()[2]) == 0xbf
-                       ) {
-
-                       /* Skip Unicode byte order mark */
-                       line = line->substr (3);
+               /* Keep some history in case there is an error to report */
+               _context.push_back (*line);
+               if (_context.size() > 5) {
+                       _context.pop_front ();
                }
 
                switch (state) {
@@ -123,18 +99,34 @@ SubripReader::read (function<optional<string> ()> get_line)
                        }
 
                        state = METADATA;
+
+                       /* Reset stuff that should not persist across separate subtitles */
+                       rs.bold = false;
+                       rs.italic = false;
+                       rs.underline = false;
+                       rs.vertical_position.line = 0;
                }
                break;
                case METADATA:
                {
                        vector<string> p;
-                       boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "));
+
+                       /* Further trim this line, removing spaces from the end */
+                       trim_right_if (*line, boost::is_any_of (" "));
+
+                       boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "), boost::token_compress_on);
                        if (p.size() != 3 && p.size() != 7) {
-                               throw SubripError (*line, "a time/position line");
+                               for (int i = 0; i < 2; ++i) {
+                                       optional<string> ex = get_line ();
+                                       if (ex) {
+                                               _context.push_back (*ex);
+                                       }
+                               }
+                               throw SubripError (*line, "a time/position line", _context);
                        }
 
-                       from = convert_time (p[0]);
-                       to = convert_time (p[2]);
+                       rs.from = convert_time (p[0]);
+                       rs.to = convert_time (p[2]);
 
                        /* XXX: should not ignore coordinate specifications */
 
@@ -144,10 +136,9 @@ SubripReader::read (function<optional<string> ()> get_line)
                case CONTENT:
                        if (line->empty ()) {
                                state = COUNTER;
-                               line_number = 0;
                        } else {
-                               convert_line (*line, line_number, from, to);
-                               line_number++;
+                               convert_line (*line, rs);
+                               rs.vertical_position.line = rs.vertical_position.line.get() + 1;
                        }
                        break;
                }
@@ -160,7 +151,7 @@ SubripReader::convert_time (string t)
        vector<string> a;
        boost::algorithm::split (a, t, boost::is_any_of (":"));
        if (a.size() != 3) {
-               throw SubripError (t, "time in the format h:m:s,ms");
+               throw SubripError (t, "time in the format h:m:s,ms", _context);
        }
 
        vector<string> b;
@@ -175,7 +166,7 @@ SubripReader::convert_time (string t)
 }
 
 void
-SubripReader::convert_line (string t, int line_number, Time from, Time to)
+SubripReader::convert_line (string t, RawSubtitle& p)
 {
        enum {
                TEXT,
@@ -184,16 +175,6 @@ SubripReader::convert_line (string t, int line_number, Time from, Time to)
 
        string tag;
 
-       RawSubtitle p;
-       p.font = "Arial";
-       p.font_size.set_points (48);
-       p.from = from;
-       p.to = to;
-       p.vertical_position.line = line_number;
-       /* XXX: arbitrary */
-       p.vertical_position.lines = 32;
-       p.vertical_position.reference = TOP_OF_SUBTITLE;
-
        list<Colour> colours;
        colours.push_back (Colour (1, 1, 1));
 
@@ -247,15 +228,21 @@ SubripReader::convert_line (string t, int line_number, Time from, Time to)
                                tag.clear ();
                                state = TEXT;
                        } else {
-                               tag += t[i];
+                               tag += tolower (t[i]);
                        }
                        break;
                }
        }
 
+       /* Strip Unicode U+202B (right-to-left embedding) as sometimes it is rendered
+          as a missing character.  This may be a hack.
+       */
+       replace_all (p.text, "\xe2\x80\xab", "");
+
        maybe_content (p);
 }
 
+/* Push p into _subs if it has some text, and clear the text out of p */
 void
 SubripReader::maybe_content (RawSubtitle& p)
 {