Rename Subtitle -> Text
[dcpomatic.git] / src / lib / text_subtitle.cc
index 8e3f92026881b9f32996acd549622a14c509dddd..35acb90228bc42d0f9cc4b7827b7d883d6420b31 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -21,7 +21,7 @@
 #include "text_subtitle.h"
 #include "cross.h"
 #include "exceptions.h"
-#include "text_subtitle_content.h"
+#include "text_text_content.h"
 #include <sub/subrip_reader.h>
 #include <sub/ssa_reader.h>
 #include <sub/collect.h>
@@ -36,9 +36,10 @@ using std::cout;
 using std::string;
 using boost::shared_ptr;
 using boost::scoped_array;
+using boost::optional;
 using dcp::Data;
 
-TextSubtitle::TextSubtitle (shared_ptr<const TextSubtitleContent> content)
+TextSubtitle::TextSubtitle (shared_ptr<const TextTextContent> content)
 {
        Data in (content->path (0));
 
@@ -63,15 +64,26 @@ TextSubtitle::TextSubtitle (shared_ptr<const TextSubtitleContent> content)
        scoped_array<char> utf8 (new char[utf16_len * 2]);
        ucnv_fromUChars (to_utf8, utf8.get(), utf16_len * 2, reinterpret_cast<UChar*>(utf16.get()), utf16_len, &status);
 
+       /* Fix OS X line endings */
+       size_t utf8_len = strlen (utf8.get ());
+       for (size_t i = 0; i < utf8_len; ++i) {
+               if (utf8[i] == '\r' && ((i == utf8_len - 1) || utf8[i + 1] != '\n')) {
+                       utf8[i] = '\n';
+               }
+       }
+
        ucsdet_close (detector);
        ucnv_close (to_utf16);
        ucnv_close (to_utf8);
 
        sub::Reader* reader = 0;
 
-       if (content->path(0).extension() == ".srt" || content->path(0).extension() == ".SRT") {
+       string ext = content->path(0).extension().string();
+       transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
+
+       if (ext == ".srt") {
                reader = new sub::SubripReader (utf8.get());
-       } else if (content->path(0).extension() == ".ssa" || content->path(0).extension() == ".SSA") {
+       } else if (ext == ".ssa" || ext == ".ass") {
                reader = new sub::SSAReader (utf8.get());
        }
 
@@ -82,6 +94,17 @@ TextSubtitle::TextSubtitle (shared_ptr<const TextSubtitleContent> content)
        delete reader;
 }
 
+/** @return time of first subtitle, if there is one */
+optional<ContentTime>
+TextSubtitle::first () const
+{
+       if (_subtitles.empty()) {
+               return optional<ContentTime>();
+       }
+
+       return ContentTime::from_seconds(_subtitles[0].from.all_as_seconds());
+}
+
 ContentTime
 TextSubtitle::length () const
 {