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