7faecb459b4f2b6f7f6f20a2007ea4d77da8fcd3
[dcpomatic.git] / src / lib / text_subtitle.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "text_subtitle.h"
21 #include "cross.h"
22 #include "exceptions.h"
23 #include "text_subtitle_content.h"
24 #include <sub/subrip_reader.h>
25 #include <sub/ssa_reader.h>
26 #include <sub/collect.h>
27 #include <unicode/ucsdet.h>
28 #include <unicode/ucnv.h>
29 #include <iostream>
30
31 #include "i18n.h"
32
33 using std::vector;
34 using std::cout;
35 using std::string;
36 using boost::shared_ptr;
37 using boost::scoped_array;
38 using dcp::Data;
39
40 TextSubtitle::TextSubtitle (shared_ptr<const TextSubtitleContent> content)
41 {
42         Data in (content->path (0));
43
44         UErrorCode status = U_ZERO_ERROR;
45         UCharsetDetector* detector = ucsdet_open (&status);
46         ucsdet_setText (detector, reinterpret_cast<const char *> (in.data().get()), in.size(), &status);
47
48         UCharsetMatch const * match = ucsdet_detect (detector, &status);
49         char const * in_charset = ucsdet_getName (match, &status);
50
51         UConverter* to_utf16 = ucnv_open (in_charset, &status);
52         /* This is a guess; I think we should be able to encode any input in 4 times its input size */
53         scoped_array<uint16_t> utf16 (new uint16_t[in.size() * 2]);
54         int const utf16_len = ucnv_toUChars (
55                 to_utf16, reinterpret_cast<UChar*>(utf16.get()), in.size() * 2,
56                 reinterpret_cast<const char *> (in.data().get()), in.size(),
57                 &status
58                 );
59
60         UConverter* to_utf8 = ucnv_open ("UTF-8", &status);
61         /* Another guess */
62         scoped_array<char> utf8 (new char[utf16_len * 2]);
63         ucnv_fromUChars (to_utf8, utf8.get(), utf16_len * 2, reinterpret_cast<UChar*>(utf16.get()), utf16_len, &status);
64
65         ucsdet_close (detector);
66         ucnv_close (to_utf16);
67         ucnv_close (to_utf8);
68
69         sub::Reader* reader = 0;
70
71         if (content->path(0).extension() == ".srt" || content->path(0).extension() == ".SRT") {
72                 reader = new sub::SubripReader (utf8.get());
73         } else if (content->path(0).extension() == ".ssa" || content->path(0).extension() == ".SSA") {
74                 reader = new sub::SSAReader (utf8.get());
75         }
76
77         if (reader) {
78                 _subtitles = sub::collect<vector<sub::Subtitle> > (reader->subtitles ());
79         }
80 }
81
82 ContentTime
83 TextSubtitle::length () const
84 {
85         if (_subtitles.empty ()) {
86                 return ContentTime ();
87         }
88
89         return ContentTime::from_seconds (_subtitles.back().to.all_as_seconds ());
90 }