New libcxml API.
[dcpomatic.git] / test / dcp_subtitle_test.cc
1 /*
2     Copyright (C) 2014-2019 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 /** @file  test/dcp_subtitle_test.cc
22  *  @brief Test DCP subtitle content in various ways.
23  *  @ingroup feature
24  */
25
26 #include <boost/test/unit_test.hpp>
27 #include "lib/film.h"
28 #include "lib/dcp_subtitle_content.h"
29 #include "lib/dcp_content.h"
30 #include "lib/ratio.h"
31 #include "lib/dcp_decoder.h"
32 #include "lib/dcp_content_type.h"
33 #include "lib/dcp_subtitle_decoder.h"
34 #include "lib/text_content.h"
35 #include "lib/content_text.h"
36 #include "lib/font.h"
37 #include "lib/text_decoder.h"
38 #include "test.h"
39 #include <iostream>
40
41 using std::cout;
42 using std::list;
43 using std::shared_ptr;
44 using std::vector;
45 using boost::optional;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
48 #endif
49 using namespace dcpomatic;
50
51 optional<ContentStringText> stored;
52
53 static void
54 store (ContentStringText sub)
55 {
56         if (!stored) {
57                 stored = sub;
58         } else {
59                 for (auto i: sub.subs) {
60                         stored->subs.push_back (i);
61                 }
62         }
63 }
64
65 /** Test pass-through of a very simple DCP subtitle file */
66 BOOST_AUTO_TEST_CASE (dcp_subtitle_test)
67 {
68         shared_ptr<Film> film = new_test_film ("dcp_subtitle_test");
69         film->set_container (Ratio::from_id ("185"));
70         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
71         film->set_name ("frobozz");
72         film->set_interop (false);
73         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent ("test/data/dcp_sub.xml"));
74         film->examine_and_add_content (content);
75         BOOST_REQUIRE (!wait_for_jobs ());
76
77         BOOST_CHECK_EQUAL (content->full_length(film).get(), DCPTime::from_seconds(2).get());
78
79         content->only_text()->set_use (true);
80         content->only_text()->set_burn (false);
81         film->make_dcp ();
82         BOOST_REQUIRE (!wait_for_jobs ());
83
84         check_dcp ("test/data/dcp_subtitle_test", film->dir (film->dcp_name ()));
85 }
86
87 /** Test parsing of a subtitle within an existing DCP */
88 BOOST_AUTO_TEST_CASE (dcp_subtitle_within_dcp_test)
89 {
90         shared_ptr<Film> film = new_test_film ("dcp_subtitle_within_dcp_test");
91         film->set_container (Ratio::from_id ("185"));
92         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
93         film->set_name ("frobozz");
94         shared_ptr<DCPContent> content (new DCPContent(TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV"));
95         film->examine_and_add_content (content);
96         BOOST_REQUIRE (!wait_for_jobs ());
97
98         shared_ptr<DCPDecoder> decoder (new DCPDecoder (film, content, false, false, shared_ptr<DCPDecoder>()));
99         decoder->only_text()->PlainStart.connect (bind (store, _1));
100
101         stored = optional<ContentStringText> ();
102         while (!decoder->pass() && !stored) {}
103
104         BOOST_REQUIRE (stored);
105         BOOST_REQUIRE_EQUAL (stored->subs.size(), 2U);
106         BOOST_CHECK_EQUAL (stored->subs.front().text(), "Noch mal.");
107         BOOST_CHECK_EQUAL (stored->subs.back().text(), "Encore une fois.");
108 }
109
110 /** Test subtitles whose text includes things like &lt;b&gt; */
111 BOOST_AUTO_TEST_CASE (dcp_subtitle_test2)
112 {
113         shared_ptr<Film> film = new_test_film ("dcp_subtitle_test2");
114         film->set_container (Ratio::from_id ("185"));
115         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
116         film->set_name ("frobozz");
117         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent("test/data/dcp_sub2.xml"));
118         film->examine_and_add_content (content);
119         BOOST_REQUIRE (!wait_for_jobs ());
120
121         shared_ptr<DCPSubtitleDecoder> decoder (new DCPSubtitleDecoder(film, content));
122         decoder->only_text()->PlainStart.connect (bind (store, _1));
123
124         stored = optional<ContentStringText> ();
125         while (!decoder->pass()) {
126                 if (stored && stored->from() == ContentTime(0)) {
127                         BOOST_CHECK_EQUAL (stored->subs.front().text(), "&lt;b&gt;Hello world!&lt;/b&gt;");
128                 }
129         }
130 }
131
132 /** Test a failure case */
133 BOOST_AUTO_TEST_CASE (dcp_subtitle_test3)
134 {
135         shared_ptr<Film> film = new_test_film ("dcp_subtitle_test3");
136         film->set_container (Ratio::from_id ("185"));
137         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
138         film->set_name ("frobozz");
139         film->set_interop (true);
140         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent ("test/data/dcp_sub3.xml"));
141         film->examine_and_add_content (content);
142         BOOST_REQUIRE (!wait_for_jobs ());
143
144         film->make_dcp ();
145         BOOST_REQUIRE (!wait_for_jobs ());
146
147         shared_ptr<DCPSubtitleDecoder> decoder (new DCPSubtitleDecoder (film, content));
148         stored = optional<ContentStringText> ();
149         while (!decoder->pass ()) {
150                 decoder->only_text()->PlainStart.connect (bind (store, _1));
151                 if (stored && stored->from() == ContentTime::from_seconds(0.08)) {
152                         list<dcp::SubtitleString> s = stored->subs;
153                         list<dcp::SubtitleString>::const_iterator i = s.begin ();
154                         BOOST_CHECK_EQUAL (i->text(), "This");
155                         ++i;
156                         BOOST_REQUIRE (i != s.end ());
157                         BOOST_CHECK_EQUAL (i->text(), " is ");
158                         ++i;
159                         BOOST_REQUIRE (i != s.end ());
160                         BOOST_CHECK_EQUAL (i->text(), "wrong.");
161                         ++i;
162                         BOOST_REQUIRE (i == s.end ());
163                 }
164         }
165 }
166
167 /** Check that Interop DCPs aren't made with more than one <LoadFont> (#1273) */
168 BOOST_AUTO_TEST_CASE (dcp_subtitle_test4)
169 {
170         shared_ptr<Film> film = new_test_film2 ("dcp_subtitle_test4");
171         film->set_interop (true);
172
173         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent ("test/data/dcp_sub3.xml"));
174         film->examine_and_add_content (content);
175         shared_ptr<DCPSubtitleContent> content2 (new DCPSubtitleContent ("test/data/dcp_sub3.xml"));
176         film->examine_and_add_content (content2);
177         BOOST_REQUIRE (!wait_for_jobs ());
178
179         content->only_text()->add_font (shared_ptr<Font> (new Font ("font1")));
180         content2->only_text()->add_font (shared_ptr<Font> (new Font ("font2")));
181
182         film->make_dcp ();
183         BOOST_REQUIRE (!wait_for_jobs ());
184
185         cxml::Document doc ("DCSubtitle");
186         doc.read_file (subtitle_file (film));
187         BOOST_REQUIRE_EQUAL (doc.node_children("LoadFont").size(), 1U);
188 }
189
190 static
191 void
192 check_font_tags (vector<cxml::NodePtr> nodes)
193 {
194         for (auto i: nodes) {
195                 if (i->name() == "Font") {
196                         BOOST_CHECK (!i->optional_string_attribute("Id") || i->string_attribute("Id") != "");
197                 }
198                 check_font_tags (i->node_children());
199         }
200 }
201
202 /** Check that imported <LoadFont> tags with empty IDs (or corresponding Font tags with empty IDs)
203  *  are not passed through into the DCP.
204  */
205 BOOST_AUTO_TEST_CASE (dcp_subtitle_test5)
206 {
207         shared_ptr<Film> film = new_test_film2 ("dcp_subtitle_test5");
208         film->set_interop (true);
209
210         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent("test/data/dcp_sub6.xml"));
211         film->examine_and_add_content (content);
212         BOOST_REQUIRE (!wait_for_jobs());
213
214         film->make_dcp ();
215         BOOST_REQUIRE (!wait_for_jobs());
216         film->write_metadata ();
217
218         cxml::Document doc ("DCSubtitle");
219         doc.read_file (subtitle_file(film));
220         BOOST_REQUIRE_EQUAL (doc.node_children("LoadFont").size(), 1U);
221         BOOST_CHECK (doc.node_children("LoadFont").front()->string_attribute("Id") != "");
222
223         check_font_tags (doc.node_children());
224 }