Don't fill in vertical_position.lines with an arbitrary value.
[libsub.git] / src / ssa_reader.cc
index acaa46be4b0d5dc714474ffece2e9c09918f84b0..c76a78db34d006c4244e40f4d8e7423edfd5fc14 100644 (file)
@@ -156,6 +156,8 @@ SSAReader::parse_line (RawSubtitle base, string line)
        string style;
 
        current.vertical_position.line = 0;
+       /* XXX: arbitrary */
+       current.vertical_position.lines = 32;
        current.vertical_position.reference = TOP_OF_SUBTITLE;
 
        for (size_t i = 0; i < line.length(); ++i) {
@@ -166,7 +168,7 @@ SSAReader::parse_line (RawSubtitle base, string line)
                                state = STYLE;
                        } else if (c == '\\') {
                                state = BACKSLASH;
-                       } else {
+                       } else if (c != '\r' && c != '\n') {
                                current.text += c;
                        }
                        break;
@@ -188,7 +190,7 @@ SSAReader::parse_line (RawSubtitle base, string line)
                        }
                        break;
                case BACKSLASH:
-                       if (c == 'n' && !current.text.empty ()) {
+                       if ((c == 'n' || c == 'N') && !current.text.empty ()) {
                                subs.push_back (current);
                                current.text = "";
                                current.vertical_position.line = current.vertical_position.line.get() + 1;
@@ -225,6 +227,7 @@ SSAReader::read (function<optional<string> ()> get_line)
                }
 
                trim (*line);
+               remove_unicode_bom (line);
 
                if (starts_with (*line, ";") || line->empty ()) {
                        continue;
@@ -271,6 +274,15 @@ SSAReader::read (function<optional<string> ()> get_line)
                                vector<string> event;
                                split (event, body, is_any_of (","));
 
+                               /* There may be commas in the subtitle part; reassemble any extra parts
+                                  from when we just split it.
+                               */
+                               while (event.size() > event_format.size()) {
+                                       string const ex = event.back ();
+                                       event.pop_back ();
+                                       event.back() += "," + ex;
+                               }
+
                                SUB_ASSERT (!event.empty());
                                SUB_ASSERT (event_format.size() == event.size());
 
@@ -299,11 +311,11 @@ SSAReader::read (function<optional<string> ()> get_line)
                                                sub.vertical_position.line = 0;
 
                                        } else if (event_format[i] == "Text") {
-                                               sub.text = event[i];
+                                               BOOST_FOREACH (sub::RawSubtitle j, parse_line (sub, event[i])) {
+                                                       _subs.push_back (j);
+                                               }
                                        }
                                }
-
-                               _subs.push_back (sub);
                        }
                }