Merge remote-tracking branch 'origin/main' into v2.17.x
[dcpomatic.git] / test / subtitle_font_id_test.cc
1 /*
2     Copyright (C) 2022 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 #include "lib/check_content_job.h"
23 #include "lib/content_factory.h"
24 #include "lib/dcp_content.h"
25 #include "lib/film.h"
26 #include "lib/font.h"
27 #include "lib/player.h"
28 #include "lib/text_content.h"
29 #include "lib/util.h"
30 #include <dcp/cpl.h>
31 #include <dcp/dcp.h>
32 #include <dcp/reel.h>
33 #include <dcp/reel_subtitle_asset.h>
34 #include <dcp/smpte_subtitle_asset.h>
35 #include "test.h"
36 #include <boost/test/unit_test.hpp>
37
38
39 using std::make_shared;
40
41
42 BOOST_AUTO_TEST_CASE(full_dcp_subtitle_font_id_test)
43 {
44         auto dcp = make_shared<DCPContent>(TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV");
45         auto film = new_test_film("full_dcp_subtitle_font_id_test", { dcp });
46
47         auto content = film->content();
48         BOOST_REQUIRE_EQUAL(content.size(), 1U);
49         auto text = content[0]->only_text();
50         BOOST_REQUIRE(text);
51
52         BOOST_REQUIRE_EQUAL(text->fonts().size(), 1U);
53         auto font = text->fonts().front();
54         BOOST_CHECK_EQUAL(font->id(), "theFontId");
55         BOOST_REQUIRE(font->data());
56         BOOST_CHECK_EQUAL(font->data()->size(), 367112);
57 }
58
59
60 BOOST_AUTO_TEST_CASE(dcp_subtitle_font_id_test)
61 {
62         auto subs = content_factory(TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV" / "8b48f6ae-c74b-4b80-b994-a8236bbbad74_sub.mxf");
63         auto film = new_test_film("dcp_subtitle_font_id_test", subs);
64
65         auto content = film->content();
66         BOOST_REQUIRE_EQUAL(content.size(), 1U);
67         auto text = content[0]->only_text();
68         BOOST_REQUIRE(text);
69
70         BOOST_REQUIRE_EQUAL(text->fonts().size(), 1U);
71         auto font = text->fonts().front();
72         BOOST_CHECK_EQUAL(font->id(), "theFontId");
73         BOOST_REQUIRE(font->data());
74         BOOST_CHECK_EQUAL(font->data()->size(), 367112);
75 }
76
77
78 BOOST_AUTO_TEST_CASE(make_dcp_with_subs_from_interop_dcp)
79 {
80         auto dcp = make_shared<DCPContent>("test/data/Iopsubs_FTR-1_F_XX-XX_MOS_2K_20220710_IOP_OV");
81         auto film = new_test_film("make_dcp_with_subs_from_interop_dcp", { dcp });
82         dcp->text.front()->set_use(true);
83         make_and_verify_dcp(
84                 film,
85                 {
86                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
87                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME
88                 }
89         );
90 }
91
92
93 BOOST_AUTO_TEST_CASE(make_dcp_with_subs_from_smpte_dcp)
94 {
95         Cleanup cl;
96
97         auto dcp = make_shared<DCPContent>(TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV");
98         auto film = new_test_film("make_dcp_with_subs_from_smpte_dcp", { dcp }, &cl);
99         dcp->text.front()->set_use(true);
100         make_and_verify_dcp(film);
101
102         cl.run();
103 }
104
105
106 BOOST_AUTO_TEST_CASE(make_dcp_with_subs_from_mkv)
107 {
108         auto subs = content_factory(TestPaths::private_data() / "clapperboard_with_subs.mkv");
109         auto film = new_test_film("make_dcp_with_subs_from_mkv", subs);
110         subs[0]->text.front()->set_use(true);
111         subs[0]->text.front()->set_language(dcp::LanguageTag("en"));
112         make_and_verify_dcp(film, { dcp::VerificationNote::Code::INVALID_PICTURE_FRAME_RATE_FOR_2K });
113 }
114
115
116 BOOST_AUTO_TEST_CASE(make_dcp_with_subs_without_font_tag)
117 {
118         auto subs = content_factory("test/data/no_font.xml");
119         auto film = new_test_film("make_dcp_with_subs_without_font_tag", { subs });
120         subs[0]->text.front()->set_use(true);
121         subs[0]->text.front()->set_language(dcp::LanguageTag("de"));
122         make_and_verify_dcp(
123                 film,
124                 {
125                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
126                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
127                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
128                 });
129
130         auto check_file = subtitle_file(film);
131         dcp::SMPTESubtitleAsset check_asset(check_file);
132         BOOST_CHECK_EQUAL(check_asset.load_font_nodes().size(), 1U);
133         auto check_font_data = check_asset.font_data();
134         BOOST_CHECK_EQUAL(check_font_data.size(), 1U);
135         BOOST_CHECK(check_font_data.begin()->second == dcp::ArrayData(default_font_file()));
136 }
137
138
139 BOOST_AUTO_TEST_CASE(make_dcp_with_subs_in_dcp_without_font_tag)
140 {
141         /* Make a DCP with some subs in */
142         auto source_subs = content_factory("test/data/short.srt");
143         auto source = new_test_film("make_dcp_with_subs_in_dcp_without_font_tag_source", { source_subs });
144         source->set_interop(true);
145         source_subs[0]->only_text()->set_language(dcp::LanguageTag("de"));
146         make_and_verify_dcp(
147                 source,
148                 {
149                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
150                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
151                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
152                         dcp::VerificationNote::Code::INVALID_STANDARD
153                 });
154
155         /* Find the ID of the subs */
156         dcp::DCP source_dcp(source->dir(source->dcp_name()));
157         source_dcp.read();
158         BOOST_REQUIRE(!source_dcp.cpls().empty());
159         BOOST_REQUIRE(!source_dcp.cpls()[0]->reels().empty());
160         BOOST_REQUIRE(source_dcp.cpls()[0]->reels()[0]->main_subtitle());
161         auto const id = source_dcp.cpls()[0]->reels()[0]->main_subtitle()->asset()->id();
162
163         /* Graft in some bad subs with no <Font> tag */
164         auto source_subtitle_file = subtitle_file(source);
165 #if BOOST_VERSION >= 107400
166         boost::filesystem::copy_file("test/data/no_font.xml", source_subtitle_file, boost::filesystem::copy_options::overwrite_existing);
167 #else
168         boost::filesystem::copy_file("test/data/no_font.xml", source_subtitle_file, boost::filesystem::copy_option::overwrite_if_exists);
169 #endif
170
171         /* Fix the <Id> tag */
172         {
173                 Editor editor(source_subtitle_file);
174                 editor.replace("4dd8ee05-5986-4c67-a6f8-bbeac62e21db", id);
175         }
176
177         /* Now make a project which imports that DCP and makes another DCP from it */
178         auto dcp_content = make_shared<DCPContent>(source->dir(source->dcp_name()));
179         auto film = new_test_film("make_dcp_with_subs_without_font_tag", { dcp_content });
180         BOOST_REQUIRE(!dcp_content->text.empty());
181         dcp_content->text.front()->set_use(true);
182         make_and_verify_dcp(
183                 film,
184                 {
185                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
186                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
187                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
188                 });
189
190         auto check_file = subtitle_file(film);
191         dcp::SMPTESubtitleAsset check_asset(check_file);
192         BOOST_CHECK_EQUAL(check_asset.load_font_nodes().size(), 1U);
193         auto check_font_data = check_asset.font_data();
194         BOOST_CHECK_EQUAL(check_font_data.size(), 1U);
195         BOOST_CHECK(check_font_data.begin()->second == dcp::ArrayData(default_font_file()));
196 }
197
198
199 BOOST_AUTO_TEST_CASE(filler_subtitle_reels_have_load_font_tags)
200 {
201         auto const name = boost::unit_test::framework::current_test_case().full_name();
202
203         auto subs = content_factory("test/data/short.srt")[0];
204         auto video1 = content_factory("test/data/flat_red.png")[0];
205         auto video2 = content_factory("test/data/flat_red.png")[0];
206
207         auto film = new_test_film(name, { video1, video2, subs });
208         film->set_reel_type(ReelType::BY_VIDEO_CONTENT);
209
210         make_and_verify_dcp(
211                 film,
212                 {
213                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
214                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
215                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
216                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
217                 });
218 }
219
220
221 BOOST_AUTO_TEST_CASE(subtitle_with_no_font_test)
222 {
223         auto const name_base = boost::unit_test::framework::current_test_case().full_name();
224
225         auto video1 = content_factory("test/data/flat_red.png")[0];
226         auto video2 = content_factory("test/data/flat_red.png")[0];
227         auto subs = content_factory("test/data/short.srt")[0];
228
229         auto bad_film = new_test_film(name_base + "_bad", { video1, video2, subs });
230         bad_film->set_reel_type(ReelType::BY_VIDEO_CONTENT);
231         video2->set_position(bad_film, video1->end(bad_film));
232         subs->set_position(bad_film, video1->end(bad_film));
233         subs->text[0]->add_font(make_shared<dcpomatic::Font>("foo", "test/data/LiberationSans-Regular.ttf"));
234
235         make_and_verify_dcp(
236                 bad_film,
237                 {
238                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
239                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME
240                 });
241
242         /* When this test was written, this DCP would have one reel whose subtitles had <LoadFont>s
243          * but the subtitles specified no particular font.  This triggers bug #2649, which this test
244          * is intended to trigger.  First, make sure that the DCP has the required characteristics,
245          * to guard against a case where for some reason the DCP here is different enough that it
246          * doesn't trigger the bug.
247          */
248         dcp::DCP check(bad_film->dir(bad_film->dcp_name()));
249         check.read();
250         BOOST_REQUIRE_EQUAL(check.cpls().size(), 1U);
251         auto cpl = check.cpls()[0];
252         BOOST_REQUIRE_EQUAL(cpl->reels().size(), 2U);
253         auto check_subs_reel = cpl->reels()[0]->main_subtitle();
254         BOOST_REQUIRE(check_subs_reel);
255         auto check_subs = check_subs_reel->asset();
256         BOOST_REQUIRE(check_subs);
257
258         BOOST_CHECK_EQUAL(check_subs->font_data().size(), 1U);
259         BOOST_REQUIRE_EQUAL(check_subs->subtitles().size(), 1U);
260         BOOST_CHECK(!std::dynamic_pointer_cast<const dcp::SubtitleString>(check_subs->subtitles()[0])->font().has_value());
261
262         auto check_film = new_test_film(name_base + "_check", { make_shared<DCPContent>(bad_film->dir(bad_film->dcp_name())) });
263         make_and_verify_dcp(check_film);
264 }
265
266
267 BOOST_AUTO_TEST_CASE(load_dcp_with_empty_font_id_test)
268 {
269         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "kr_vf");
270         auto film = new_test_film("load_dcp_with_empty_font_id_test", { dcp });
271 }
272
273
274 BOOST_AUTO_TEST_CASE(use_first_loadfont_as_default)
275 {
276         auto dcp = std::make_shared<DCPContent>("test/data/use_default_font");
277         auto film = new_test_film("use_first_loadfont_as_default", { dcp });
278         dcp->only_text()->set_use(true);
279         dcp->only_text()->set_language(dcp::LanguageTag("de"));
280         make_and_verify_dcp(
281                 film,
282                 { dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME }
283                 );
284
285         dcp::DCP test(film->dir(film->dcp_name()));
286         test.read();
287         BOOST_REQUIRE(!test.cpls().empty());
288         auto cpl = test.cpls()[0];
289         BOOST_REQUIRE(!cpl->reels().empty());
290         auto reel = cpl->reels()[0];
291         BOOST_REQUIRE(reel->main_subtitle()->asset());
292         auto subtitle = std::dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(reel->main_subtitle()->asset());
293         BOOST_REQUIRE_EQUAL(subtitle->font_data().size(), 1U);
294         BOOST_CHECK(subtitle->font_data().begin()->second == dcp::ArrayData("test/data/Inconsolata-VF.ttf"));
295 }
296
297
298 BOOST_AUTO_TEST_CASE(no_error_with_ccap_that_mentions_no_font)
299 {
300         auto dcp = make_shared<DCPContent>("test/data/ccap_only");
301         auto film = new_test_film("no_error_with_ccap_that_mentions_no_font", { dcp });
302         auto player = Player(film, film->playlist());
303         while (!player.pass()) {}
304 }
305
306
307 BOOST_AUTO_TEST_CASE(subtitle_font_ids_survive_project_save)
308 {
309         std::string const name = "subtitle_font_ids_survive_project_save";
310
311         auto subs = content_factory("test/data/short.srt")[0];
312         auto film = new_test_film(name + "_film", { subs });
313         film->set_interop(false);
314         make_and_verify_dcp(
315                 film,
316                 {
317                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
318                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
319                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
320                 });
321
322         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
323         auto film2 = new_test_film(name + "_film2", { dcp });
324         film2->write_metadata();
325
326         auto film3 = std::make_shared<Film>(film2->dir("."));
327         film3->read_metadata();
328         BOOST_REQUIRE(!film3->content().empty());
329         auto check_dcp = std::dynamic_pointer_cast<DCPContent>(film3->content()[0]);
330         BOOST_REQUIRE(check_dcp);
331
332         check_dcp->check_font_ids();
333 }
334
335
336 BOOST_AUTO_TEST_CASE(cope_with_unloaded_font_id)
337 {
338         /* This file has a <Font> with an ID that corresponds to no <LoadFont> */
339         auto subs = content_factory("test/data/unloaded_font.xml")[0];
340         auto film = new_test_film2("cope_with_unloaded_font_id", { subs });
341
342         make_and_verify_dcp(
343                 film,
344                 {
345                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
346                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
347                 });
348 }
349