Allow SubripReader::convert_time to take a milliseconds separator.
[libsub.git] / src / subrip_reader.cc
index 8bd200537200c4f2f9601d2f78e24a404fcec64c..a59b43fbedfe8642f836d3e95160696ef60dd864 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2020 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
 
 */
 
-#include "subrip_reader.h"
+/** @file  src/subrip_reader.cc
+ *  @brief SubripReader class.
+ */
+
+
+#include "compose.hpp"
 #include "exceptions.h"
+#include "raw_convert.h"
+#include "ssa_reader.h"
+#include "sub_assert.h"
+#include "subrip_reader.h"
+#include "util.h"
 #include <boost/algorithm/string.hpp>
+#include <boost/algorithm/string_regex.hpp>
+#include <boost/bind.hpp>
 #include <boost/lexical_cast.hpp>
+#include <boost/regex.hpp>
 #include <cstdio>
+#include <iostream>
 #include <vector>
 
+
 using std::string;
 using std::vector;
+using std::cout;
+using std::hex;
 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 s)
+{
+       this->read (boost::bind(&get_line_string, &s));
+}
+
+/** @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,150 +71,253 @@ SubripReader::SubripReader (FILE* f)
                CONTENT
        } state = COUNTER;
 
-       char buffer[256];
-
-       TimePair from;
-       TimePair to;
+       RawSubtitle rs;
 
-       string line;
-       int line_number = 0;
+       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) {
+               auto 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);
+
+               /* Keep some history in case there is an error to report */
+               _context.push_back (*line);
+               if (_context.size() > 5) {
+                       _context.pop_front ();
+               }
 
                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;
+                       rs.vertical_position.reference = TOP_OF_SUBTITLE;
                }
                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]);
+                       string expected;
+                       auto from = convert_time(p[0], ",", &expected);
+                       if (!from) {
+                               throw SubripError(p[0], expected, _context);
+                       }
+                       rs.from = *from;
+
+                       auto to = convert_time(p[2], ",", &expected);
+                       if (!to) {
+                               throw SubripError(p[2], expected, _context);
+                       }
+                       rs.to = *to;
 
                        /* 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++;
+                               vector<string> sub_lines;
+                               /* Split up this line on unicode "LINE SEPARATOR".  This feels hacky but also
+                                * the least unpleasant place to do it.
+                                */
+                               boost::algorithm::split_regex(sub_lines, *line, boost::regex("\xe2\x80\xa8"));
+                               for (auto sub_line: sub_lines) {
+                                       convert_line(sub_line, rs);
+                                       rs.vertical_position.line = rs.vertical_position.line.get() + 1;
+                                       rs.text.clear();
+                               }
                        }
                        break;
                }
        }
 }
 
-TimePair
-SubripReader::convert_time (string t)
+optional<Time>
+SubripReader::convert_time(string t, string milliseconds_separator, string* expected)
 {
+       auto report_expected = [expected](string const& s) {
+               if (expected) {
+                       *expected = s;
+               }
+       };
+
        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");
+               report_expected("time in the format h:m:s,ms");
+               return {};
        }
 
        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])
-                       )
-               );
+       boost::algorithm::split(b, a[2], boost::is_any_of(milliseconds_separator));
+       if (b.size() != 2) {
+               report_expected(String::compose("time in the format h:m:s%1ms", milliseconds_separator));
+               return {};
+       }
+
+       int h, m, s, ms;
+
+       try {
+               h = lexical_cast<int>(a[0]);
+       } catch (boost::bad_lexical_cast &) {
+               report_expected("integer hour value");
+               return {};
+       }
+
+       try {
+               m = lexical_cast<int>(a[1]);
+       } catch (boost::bad_lexical_cast &) {
+               report_expected("integer minute value");
+               return {};
+       }
+
+       try {
+               s = lexical_cast<int>(b[0]);
+       } catch (boost::bad_lexical_cast &) {
+               report_expected("integer second value");
+               return {};
+       }
+
+       try {
+               ms = lexical_cast<int>(b[1]);
+       } catch (boost::bad_lexical_cast &) {
+               report_expected("integer millisecond value");
+               return {};
+       }
+
+       return Time::from_hms (h, m, s, ms);
 }
 
 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;
-       
-       /* 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.
-       */
+       vector<Colour> colours;
+       colours.push_back (Colour (1, 1, 1));
 
-       for (size_t i = 0; i < t.size(); ++i) {
-               switch (state) {
-               case TEXT:
-                       if (t[i] == '<' || t[i] == '{') {
-                               state = TAG;
-                       } else {
-                               p.text += t[i];
+       auto has_next = [](string line, size_t& index, string s) {
+               boost::to_lower(s);
+               auto next = line.substr(index, s.size());
+               boost::to_lower(next);
+               if (next != s) {
+                       return false;
+               }
+
+               index += s.size();
+               return true;
+       };
+
+       size_t i = 0;
+       while (i < t.size()) {
+               if (has_next(t, i, "<b>") || has_next(t, i, "{b}")) {
+                       maybe_content (p);
+                       p.bold = true;
+               } else if (has_next(t, i, "</b>") || has_next(t, i, "{/b}")) {
+                       maybe_content (p);
+                       p.bold = false;
+               } else if (has_next(t, i, "<i>") || has_next(t, i, "{i}")) {
+                       maybe_content (p);
+                       p.italic = true;
+               } else if (has_next(t, i, "</i>") || has_next(t, i, "{/i}")) {
+                       maybe_content (p);
+                       p.italic = false;
+               } else if (has_next(t, i, "<u>") || has_next(t, i, "{u}")) {
+                       maybe_content (p);
+                       p.underline = true;
+               } else if (has_next(t, i, "</u>") || has_next(t, i, "{/u}")) {
+                       maybe_content (p);
+                       p.underline = false;
+               } else if (has_next(t, i, "<font") || has_next(t, i, "<Font")) {
+                       maybe_content (p);
+                       boost::regex re (".*color=\"?#([[:xdigit:]]+)\"?");
+                       boost::smatch match;
+                       string tag;
+                       while (i < t.size() && t[i] != '>') {
+                               tag += t[i];
+                               ++i;
                        }
-                       break;
-               case TAG:
-                       if (t[i] == '>' || t[i] == '}') {
-                               if (tag == "b") {
-                                       maybe_content (p);
-                                       p.bold = true;
-                               } else if (tag == "/b") {
-                                       maybe_content (p);
-                                       p.bold = false;
-                               } else if (tag == "i") {
-                                       maybe_content (p);
-                                       p.italic = true;
-                               } else if (tag == "/i") {
-                                       maybe_content (p);
-                                       p.italic = false;
-                               } else if (tag == "u") {
-                                       maybe_content (p);
-                                       p.underline = true;
-                               } else if (tag == "/u") {
-                                       maybe_content (p);
-                                       p.underline = false;
-                               }
-                               tag.clear ();
-                               state = TEXT;
+                       ++i;
+                       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 {
-                               tag += t[i];
+                               re = boost::regex (
+                                       ".*color=\"rgba\\("
+                                       "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
+                                       "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
+                                       "[[:space:]]*([[:digit:]]+)[[:space:]]*,"
+                                       "[[:space:]]*([[:digit:]]+)[[:space:]]*"
+                                       "\\)\""
+                                       );
+                               if (boost::regex_search (tag, match, re) && match.size() == 5) {
+                                       p.colour.r = raw_convert<int>(string(match[1])) / 255.0;
+                                       p.colour.g = raw_convert<int>(string(match[2])) / 255.0;
+                                       p.colour.b = raw_convert<int>(string(match[3])) / 255.0;
+                                       colours.push_back (p.colour);
+                               } else {
+                                       throw SubripError (tag, "a colour in the format #rrggbb or rgba(rr,gg,bb,aa)", _context);
+                               }
                        }
-                       break;
+               } else if (has_next(t, i, "</font>")) {
+                       maybe_content (p);
+                       SUB_ASSERT (!colours.empty());
+                       colours.pop_back ();
+                       p.colour = colours.back ();
+               } else if (has_next(t, i, "{\\")) {
+                       string ssa = "\\";
+                       while (i < t.size() && t[i] != '}') {
+                               ssa += t[i];
+                               ++i;
+                       }
+                       ++i;
+                       SSAReader::parse_style (p, ssa, 288, 288, Colour(1, 1, 1));
+               } else {
+                       p.text += t[i];
+                       ++i;
                }
        }
 
+       /* 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)
 {