Merge branch '1.0' of git.carlh.net:git/libsub into 1.0
[libsub.git] / src / subrip_reader.cc
index 8bd200537200c4f2f9601d2f78e24a404fcec64c..e5f113da1d294c17d31792ccbd79b103dc9b742f 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 */
 
+/** @file  src/subrip_reader.cc
+ *  @brief SubripReader class.
+ */
+
 #include "subrip_reader.h"
 #include "exceptions.h"
+#include "util.h"
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
+#include <boost/regex.hpp>
+#include <boost/bind.hpp>
 #include <cstdio>
 #include <vector>
+#include <iostream>
 
 using std::string;
 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 namespace sub;
 
+/** @param s Subtitle string encoded in UTF-8 */
+SubripReader::SubripReader (string const & s)
+{
+       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 (&get_line_file, f));
+}
+
+void
+SubripReader::read (function<optional<string> ()> get_line)
 {
        enum {
                COUNTER,
@@ -37,64 +66,73 @@ SubripReader::SubripReader (FILE* f)
                CONTENT
        } state = COUNTER;
 
-       char buffer[256];
-
-       TimePair from;
-       TimePair 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 (!feof (f)) {
-               char* r = fgets (buffer, sizeof (buffer), f);
-               if (r == 0 || feof (f)) {
+       while (true) {
+               optional<string> line = get_line ();
+               if (!line) {
                        break;
                }
 
-               line = string (buffer);
-               trim_right_if (line, boost::is_any_of ("\n\r"));
+               trim_right_if (*line, boost::is_any_of ("\n\r"));
+               remove_unicode_bom (line);
 
                switch (state) {
                case COUNTER:
                {
-                       if (line.empty ()) {
+                       if (line->empty ()) {
                                /* a blank line at the start is ok */
                                break;
                        }
 
                        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 (" "));
                        if (p.size() != 3 && p.size() != 7) {
-                               throw SubripError (line, "a time/position line");
+                               throw SubripError (*line, "a time/position line");
                        }
 
-                       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 */
-                       
+
                        state = CONTENT;
                        break;
                }
                case CONTENT:
-                       if (line.empty ()) {
+                       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;
                }
        }
 }
 
-TimePair
+Time
 SubripReader::convert_time (string t)
 {
        vector<string> a;
@@ -106,34 +144,27 @@ SubripReader::convert_time (string t)
        vector<string> b;
        boost::algorithm::split (b, a[2], boost::is_any_of (","));
 
-       return TimePair (
-               MetricTime (
-                       lexical_cast<int> (a[0]),
-                       lexical_cast<int> (a[1]),
-                       lexical_cast<int> (b[0]),
-                       lexical_cast<int> (b[1])
-                       )
+       return Time::from_hms (
+               lexical_cast<int> (a[0]),
+               lexical_cast<int> (a[1]),
+               lexical_cast<int> (b[0]),
+               lexical_cast<int> (b[1])
                );
 }
 
 void
-SubripReader::convert_line (string t, int line_number, TimePair from, TimePair to)
+SubripReader::convert_line (string t, RawSubtitle& p)
 {
        enum {
                TEXT,
                TAG
        } state = TEXT;
-       
+
        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;
-       p.vertical_position.reference = TOP_OF_SUBTITLE;
-       
+       list<Colour> colours;
+       colours.push_back (Colour (1, 1, 1));
+
        /* XXX: missing <font> support */
        /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
           not work, I think.
@@ -168,6 +199,18 @@ SubripReader::convert_line (string t, int line_number, TimePair from, TimePair t
                                } else if (tag == "/u") {
                                        maybe_content (p);
                                        p.underline = false;
+                               } else if (boost::starts_with (tag, "font")) {
+                                       maybe_content (p);
+                                       boost::regex re (".*color=\"#([0123456789abcdef]+)\"");
+                                       boost::smatch match;
+                                       if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
+                                               p.colour = Colour::from_rgb_hex (match[1]);
+                                               colours.push_back (p.colour);
+                                       }
+                               } else if (tag == "/font") {
+                                       maybe_content (p);
+                                       colours.pop_back ();
+                                       p.colour = colours.back ();
                                }
                                tag.clear ();
                                state = TEXT;
@@ -181,6 +224,7 @@ SubripReader::convert_line (string t, int line_number, TimePair from, TimePair t
        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)
 {