/*
- 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.
using std::string;
using boost::shared_ptr;
using boost::scoped_array;
+using boost::optional;
using dcp::Data;
TextSubtitle::TextSubtitle (shared_ptr<const TextSubtitleContent> content)
{
Data in (content->path (0));
+ /* Fix OS X line endings */
+ for (int i = 0; i < in.size(); ++i) {
+ if (in.data()[i] == '\r' && ((i == in.size() - 1) || in.data()[i + 1] != '\n')) {
+ in.data()[i] = '\n';
+ }
+ }
+
UErrorCode status = U_ZERO_ERROR;
UCharsetDetector* detector = ucsdet_open (&status);
ucsdet_setText (detector, reinterpret_cast<const char *> (in.data().get()), in.size(), &status);
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());
}
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
{