Take Film pointer out of Content.
[dcpomatic.git] / test / dcp_subtitle_test.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 /** @file  test/dcp_subtitle_test.cc
22  *  @brief Test DCP subtitle content in various ways.
23  *  @ingroup specific
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 boost::shared_ptr;
44 using boost::optional;
45
46 optional<ContentStringText> stored;
47
48 static void
49 store (ContentStringText sub)
50 {
51         if (!stored) {
52                 stored = sub;
53         } else {
54                 BOOST_FOREACH (dcp::SubtitleString i, sub.subs) {
55                         stored->subs.push_back (i);
56                 }
57         }
58 }
59
60 /** Test pass-through of a very simple DCP subtitle file */
61 BOOST_AUTO_TEST_CASE (dcp_subtitle_test)
62 {
63         shared_ptr<Film> film = new_test_film ("dcp_subtitle_test");
64         film->set_container (Ratio::from_id ("185"));
65         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
66         film->set_name ("frobozz");
67         film->set_interop (false);
68         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent ("test/data/dcp_sub.xml"));
69         film->examine_and_add_content (content);
70         BOOST_REQUIRE (!wait_for_jobs ());
71
72         BOOST_CHECK_EQUAL (content->full_length(film).get(), DCPTime::from_seconds(2).get());
73
74         content->only_text()->set_use (true);
75         content->only_text()->set_burn (false);
76         film->make_dcp ();
77         BOOST_REQUIRE (!wait_for_jobs ());
78
79         check_dcp ("test/data/dcp_subtitle_test", film->dir (film->dcp_name ()));
80 }
81
82 /** Test parsing of a subtitle within an existing DCP */
83 BOOST_AUTO_TEST_CASE (dcp_subtitle_within_dcp_test)
84 {
85         shared_ptr<Film> film = new_test_film ("dcp_subtitle_within_dcp_test");
86         film->set_container (Ratio::from_id ("185"));
87         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
88         film->set_name ("frobozz");
89         shared_ptr<DCPContent> content (new DCPContent(private_data / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV"));
90         film->examine_and_add_content (content);
91         BOOST_REQUIRE (!wait_for_jobs ());
92
93         shared_ptr<DCPDecoder> decoder (new DCPDecoder (content, false));
94         decoder->only_text()->PlainStart.connect (bind (store, _1));
95
96         stored = optional<ContentStringText> ();
97         while (!decoder->pass(film) && !stored) {}
98
99         BOOST_REQUIRE (stored);
100         BOOST_REQUIRE_EQUAL (stored->subs.size(), 2);
101         BOOST_CHECK_EQUAL (stored->subs.front().text(), "Noch mal.");
102         BOOST_CHECK_EQUAL (stored->subs.back().text(), "Encore une fois.");
103 }
104
105 /** Test subtitles whose text includes things like &lt;b&gt; */
106 BOOST_AUTO_TEST_CASE (dcp_subtitle_test2)
107 {
108         shared_ptr<Film> film = new_test_film ("dcp_subtitle_test2");
109         film->set_container (Ratio::from_id ("185"));
110         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
111         film->set_name ("frobozz");
112         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent("test/data/dcp_sub2.xml"));
113         film->examine_and_add_content (content);
114         BOOST_REQUIRE (!wait_for_jobs ());
115
116         shared_ptr<DCPSubtitleDecoder> decoder (new DCPSubtitleDecoder(content));
117         decoder->only_text()->PlainStart.connect (bind (store, _1));
118
119         stored = optional<ContentStringText> ();
120         while (!decoder->pass(film)) {
121                 if (stored && stored->from() == ContentTime(0)) {
122                         BOOST_CHECK_EQUAL (stored->subs.front().text(), "&lt;b&gt;Hello world!&lt;/b&gt;");
123                 }
124         }
125 }
126
127 /** Test a failure case */
128 BOOST_AUTO_TEST_CASE (dcp_subtitle_test3)
129 {
130         shared_ptr<Film> film = new_test_film ("dcp_subtitle_test3");
131         film->set_container (Ratio::from_id ("185"));
132         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
133         film->set_name ("frobozz");
134         film->set_interop (true);
135         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent ("test/data/dcp_sub3.xml"));
136         film->examine_and_add_content (content);
137         BOOST_REQUIRE (!wait_for_jobs ());
138
139         film->make_dcp ();
140         BOOST_REQUIRE (!wait_for_jobs ());
141
142         shared_ptr<DCPSubtitleDecoder> decoder (new DCPSubtitleDecoder (content));
143         stored = optional<ContentStringText> ();
144         while (!decoder->pass (film)) {
145                 decoder->only_text()->PlainStart.connect (bind (store, _1));
146                 if (stored && stored->from() == ContentTime::from_seconds(0.08)) {
147                         list<dcp::SubtitleString> s = stored->subs;
148                         list<dcp::SubtitleString>::const_iterator i = s.begin ();
149                         BOOST_CHECK_EQUAL (i->text(), "This");
150                         ++i;
151                         BOOST_REQUIRE (i != s.end ());
152                         BOOST_CHECK_EQUAL (i->text(), " is ");
153                         ++i;
154                         BOOST_REQUIRE (i != s.end ());
155                         BOOST_CHECK_EQUAL (i->text(), "wrong.");
156                         ++i;
157                         BOOST_REQUIRE (i == s.end ());
158                 }
159         }
160 }
161
162 /** Check that Interop DCPs aren't made with more than one <LoadFont> (#1273) */
163 BOOST_AUTO_TEST_CASE (dcp_subtitle_test4)
164 {
165         shared_ptr<Film> film = new_test_film2 ("dcp_subtitle_test4");
166         film->set_interop (true);
167
168         shared_ptr<DCPSubtitleContent> content (new DCPSubtitleContent ("test/data/dcp_sub3.xml"));
169         film->examine_and_add_content (content);
170         shared_ptr<DCPSubtitleContent> content2 (new DCPSubtitleContent ("test/data/dcp_sub3.xml"));
171         film->examine_and_add_content (content2);
172         BOOST_REQUIRE (!wait_for_jobs ());
173
174         content->only_text()->add_font (shared_ptr<Font> (new Font ("font1")));
175         content2->only_text()->add_font (shared_ptr<Font> (new Font ("font2")));
176
177         film->make_dcp ();
178         BOOST_REQUIRE (!wait_for_jobs ());
179
180         cxml::Document doc ("DCSubtitle");
181         doc.read_file (subtitle_file (film));
182         BOOST_REQUIRE_EQUAL (doc.node_children("LoadFont").size(), 1);
183 }