Support binary STL subtitle files.
[dcpomatic.git] / src / lib / string_text_file.cc
1 /*
2     Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "string_text_file.h"
22 #include "cross.h"
23 #include "exceptions.h"
24 #include "string_text_file_content.h"
25 #include <sub/subrip_reader.h>
26 #include <sub/ssa_reader.h>
27 #include <sub/stl_binary_reader.h>
28 #include <sub/collect.h>
29 #include <unicode/ucsdet.h>
30 #include <unicode/ucnv.h>
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::vector;
36 using std::cout;
37 using std::string;
38 using boost::shared_ptr;
39 using boost::scoped_array;
40 using boost::optional;
41 using dcp::Data;
42
43 StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
44 {
45         string ext = content->path(0).extension().string();
46         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
47
48         sub::Reader* reader = 0;
49
50         if (ext == ".stl") {
51                 FILE* f = fopen_boost (content->path(0), "rb");
52                 if (!f) {
53                         throw OpenFileError (content->path(0), errno, OpenFileError::READ);
54                 }
55                 try {
56                         reader = new sub::STLBinaryReader (f);
57                 } catch (...) {
58                         fclose (f);
59                         throw;
60                 }
61                 fclose (f);
62
63         } else {
64                 /* Text-based file; sort out its character encoding before we try to parse it */
65
66                 Data in (content->path (0));
67
68                 UErrorCode status = U_ZERO_ERROR;
69                 UCharsetDetector* detector = ucsdet_open (&status);
70                 ucsdet_setText (detector, reinterpret_cast<const char *> (in.data().get()), in.size(), &status);
71
72                 UCharsetMatch const * match = ucsdet_detect (detector, &status);
73                 char const * in_charset = ucsdet_getName (match, &status);
74
75                 UConverter* to_utf16 = ucnv_open (in_charset, &status);
76                 /* This is a guess; I think we should be able to encode any input in 4 times its input size */
77                 scoped_array<uint16_t> utf16 (new uint16_t[in.size() * 2]);
78                 int const utf16_len = ucnv_toUChars (
79                                 to_utf16, reinterpret_cast<UChar*>(utf16.get()), in.size() * 2,
80                                 reinterpret_cast<const char *> (in.data().get()), in.size(),
81                                 &status
82                                 );
83
84                 UConverter* to_utf8 = ucnv_open ("UTF-8", &status);
85                 /* Another guess */
86                 scoped_array<char> utf8 (new char[utf16_len * 2]);
87                 ucnv_fromUChars (to_utf8, utf8.get(), utf16_len * 2, reinterpret_cast<UChar*>(utf16.get()), utf16_len, &status);
88
89                 /* Fix OS X line endings */
90                 size_t utf8_len = strlen (utf8.get ());
91                 for (size_t i = 0; i < utf8_len; ++i) {
92                         if (utf8[i] == '\r' && ((i == utf8_len - 1) || utf8[i + 1] != '\n')) {
93                                 utf8[i] = '\n';
94                         }
95                 }
96
97                 ucsdet_close (detector);
98                 ucnv_close (to_utf16);
99                 ucnv_close (to_utf8);
100
101                 if (ext == ".srt") {
102                         reader = new sub::SubripReader (utf8.get());
103                 } else if (ext == ".ssa" || ext == ".ass") {
104                         reader = new sub::SSAReader (utf8.get());
105                 }
106         }
107
108         if (reader) {
109                 _subtitles = sub::collect<vector<sub::Subtitle> > (reader->subtitles ());
110         }
111
112         delete reader;
113 }
114
115 /** @return time of first subtitle, if there is one */
116 optional<ContentTime>
117 StringTextFile::first () const
118 {
119         if (_subtitles.empty()) {
120                 return optional<ContentTime>();
121         }
122
123         return ContentTime::from_seconds(_subtitles[0].from.all_as_seconds());
124 }
125
126 ContentTime
127 StringTextFile::length () const
128 {
129         if (_subtitles.empty ()) {
130                 return ContentTime ();
131         }
132
133         return ContentTime::from_seconds (_subtitles.back().to.all_as_seconds ());
134 }