ee9bc4b8b2fbf033c8fc1fa4514197ecd4cdca30
[dcpomatic.git] / src / lib / plain_text.cc
1 /*
2     Copyright (C) 2014-2018 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 "plain_text.h"
22 #include "cross.h"
23 #include "exceptions.h"
24 #include "plain_text_content.h"
25 #include <sub/subrip_reader.h>
26 #include <sub/ssa_reader.h>
27 #include <sub/collect.h>
28 #include <unicode/ucsdet.h>
29 #include <unicode/ucnv.h>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::vector;
35 using std::cout;
36 using std::string;
37 using boost::shared_ptr;
38 using boost::scoped_array;
39 using boost::optional;
40 using dcp::Data;
41
42 TextSubtitle::TextSubtitle (shared_ptr<const PlainText> content)
43 {
44         Data in (content->path (0));
45
46         UErrorCode status = U_ZERO_ERROR;
47         UCharsetDetector* detector = ucsdet_open (&status);
48         ucsdet_setText (detector, reinterpret_cast<const char *> (in.data().get()), in.size(), &status);
49
50         UCharsetMatch const * match = ucsdet_detect (detector, &status);
51         char const * in_charset = ucsdet_getName (match, &status);
52
53         UConverter* to_utf16 = ucnv_open (in_charset, &status);
54         /* This is a guess; I think we should be able to encode any input in 4 times its input size */
55         scoped_array<uint16_t> utf16 (new uint16_t[in.size() * 2]);
56         int const utf16_len = ucnv_toUChars (
57                 to_utf16, reinterpret_cast<UChar*>(utf16.get()), in.size() * 2,
58                 reinterpret_cast<const char *> (in.data().get()), in.size(),
59                 &status
60                 );
61
62         UConverter* to_utf8 = ucnv_open ("UTF-8", &status);
63         /* Another guess */
64         scoped_array<char> utf8 (new char[utf16_len * 2]);
65         ucnv_fromUChars (to_utf8, utf8.get(), utf16_len * 2, reinterpret_cast<UChar*>(utf16.get()), utf16_len, &status);
66
67         /* Fix OS X line endings */
68         size_t utf8_len = strlen (utf8.get ());
69         for (size_t i = 0; i < utf8_len; ++i) {
70                 if (utf8[i] == '\r' && ((i == utf8_len - 1) || utf8[i + 1] != '\n')) {
71                         utf8[i] = '\n';
72                 }
73         }
74
75         ucsdet_close (detector);
76         ucnv_close (to_utf16);
77         ucnv_close (to_utf8);
78
79         sub::Reader* reader = 0;
80
81         string ext = content->path(0).extension().string();
82         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
83
84         if (ext == ".srt") {
85                 reader = new sub::SubripReader (utf8.get());
86         } else if (ext == ".ssa" || ext == ".ass") {
87                 reader = new sub::SSAReader (utf8.get());
88         }
89
90         if (reader) {
91                 _subtitles = sub::collect<vector<sub::Subtitle> > (reader->subtitles ());
92         }
93
94         delete reader;
95 }
96
97 /** @return time of first subtitle, if there is one */
98 optional<ContentTime>
99 TextSubtitle::first () const
100 {
101         if (_subtitles.empty()) {
102                 return optional<ContentTime>();
103         }
104
105         return ContentTime::from_seconds(_subtitles[0].from.all_as_seconds());
106 }
107
108 ContentTime
109 TextSubtitle::length () const
110 {
111         if (_subtitles.empty ()) {
112                 return ContentTime ();
113         }
114
115         return ContentTime::from_seconds (_subtitles.back().to.all_as_seconds ());
116 }