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