2 Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp 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.
11 libdcp 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.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
35 #include "smpte_load_font_node.h"
36 #include "smpte_subtitle_asset.h"
37 #include "stream_operators.h"
38 #include "subtitle_image.h"
41 #include <boost/optional/optional_io.hpp>
42 #include <boost/test/unit_test.hpp>
45 using std::make_shared;
47 using std::shared_ptr;
48 using std::dynamic_pointer_cast;
50 using boost::optional;
53 BOOST_AUTO_TEST_CASE (smpte_subtitle_id_test)
55 dcp::SMPTESubtitleAsset subs;
57 make_shared<dcp::SubtitleString>(
63 dcp::Time(0, 1, 2, 3, 24),
64 dcp::Time(0, 2, 2, 3, 24),
73 dcp::Time(0, 0, 0, 0, 24),
74 dcp::Time(0, 0, 0, 0, 24),
78 subs.write("build/test/smpte_subtitle_id_test.mxf");
80 dcp::SMPTESubtitleAsset check("build/test/smpte_subtitle_id_test.mxf");
81 BOOST_CHECK(check.id() != check.xml_id());
85 /** Check reading of a SMPTE subtitle file */
86 BOOST_AUTO_TEST_CASE (read_smpte_subtitle_test)
88 dcp::SMPTESubtitleAsset sc (
91 "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV" /
92 "8b48f6ae-c74b-4b80-b994-a8236bbbad74_sub.mxf"
95 BOOST_CHECK_EQUAL (sc.id(), "8b48f6ae-c74b-4b80-b994-a8236bbbad74");
96 BOOST_CHECK_EQUAL (sc.content_title_text(), "Journey to Jah");
97 BOOST_REQUIRE (sc.annotation_text());
98 BOOST_CHECK_EQUAL (sc.annotation_text().get(), "Journey to Jah");
99 BOOST_CHECK_EQUAL (sc.issue_date(), dcp::LocalTime ("2014-02-25T11:22:48.000-00:00"));
100 BOOST_REQUIRE (sc.reel_number());
101 BOOST_CHECK_EQUAL (sc.reel_number().get(), 1);
102 BOOST_REQUIRE (sc.language ());
103 BOOST_CHECK_EQUAL (sc.language().get (), "de");
104 BOOST_CHECK_EQUAL (sc.edit_rate(), dcp::Fraction (25, 1));
105 BOOST_CHECK_EQUAL (sc.time_code_rate(), 25);
106 BOOST_CHECK_EQUAL (sc.start_time(), dcp::Time (0, 0, 0, 0, 25));
107 auto lfn = sc.load_font_nodes ();
108 BOOST_REQUIRE_EQUAL (lfn.size(), 1U);
109 shared_ptr<dcp::SMPTELoadFontNode> smpte_lfn = dynamic_pointer_cast<dcp::SMPTELoadFontNode> (lfn.front ());
110 BOOST_REQUIRE (smpte_lfn);
111 BOOST_CHECK_EQUAL (smpte_lfn->id, "theFontId");
112 BOOST_CHECK_EQUAL (smpte_lfn->urn, "9118bbce-4105-4a05-b37c-a5a6f75e1fea");
113 BOOST_REQUIRE_EQUAL (sc.subtitles().size(), 63U);
114 BOOST_REQUIRE (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().front()));
115 BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().front())->text(), "Noch mal.");
116 BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().front())->space_before(), 0.0f);
117 BOOST_CHECK_EQUAL (sc.subtitles().front()->in(), dcp::Time (0, 0, 25, 12, 25));
118 BOOST_CHECK_EQUAL (sc.subtitles().front()->out(), dcp::Time (0, 0, 26, 4, 25));
119 BOOST_REQUIRE (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().back()));
120 BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().back())->text(), "Prochainement");
121 BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().back())->space_before(), 0.0f);
122 BOOST_CHECK_EQUAL (sc.subtitles().back()->in(), dcp::Time (0, 1, 57, 17, 25));
123 BOOST_CHECK_EQUAL (sc.subtitles().back()->out(), dcp::Time (0, 1, 58, 12, 25));
127 /** And another one featuring <Font> within <Text> and some <Space> */
128 BOOST_AUTO_TEST_CASE (read_smpte_subtitle_test2)
130 dcp::SMPTESubtitleAsset sc (private_test / "olsson.xml");
132 auto subs = sc.subtitles();
133 BOOST_REQUIRE_EQUAL (subs.size(), 6U);
135 auto is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
137 BOOST_CHECK_EQUAL (is->text(), "Testing is ");
138 BOOST_CHECK (!is->italic());
139 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
141 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
143 BOOST_CHECK_EQUAL (is->text(), "really");
144 BOOST_CHECK (is->italic());
145 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
147 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
149 BOOST_CHECK_EQUAL (is->text(), " fun!");
150 BOOST_CHECK (!is->italic());
151 BOOST_CHECK_CLOSE (is->space_before(), 5, 0.1);
153 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
155 BOOST_CHECK_EQUAL (is->text(), "This is the ");
156 BOOST_CHECK (!is->italic());
157 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
159 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
161 BOOST_CHECK_EQUAL (is->text(), "second");
162 BOOST_CHECK (is->italic());
163 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
165 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
167 BOOST_CHECK_EQUAL (is->text(), " line!");
168 BOOST_CHECK (!is->italic());
169 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
173 /* Write some subtitle content as SMPTE XML and check that it is right */
174 BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test)
176 dcp::SMPTESubtitleAsset c;
177 c.set_reel_number (1);
178 c.set_language (dcp::LanguageTag("en"));
179 c.set_content_title_text ("Test");
180 c.set_issue_date (dcp::LocalTime ("2016-04-01T03:52:00+00:00"));
183 make_shared<dcp::SubtitleString> (
188 dcp::Colour (255, 255, 255),
191 dcp::Time (0, 4, 9, 22, 24),
192 dcp::Time (0, 4, 11, 22, 24),
200 dcp::Colour (0, 0, 0),
201 dcp::Time (0, 0, 0, 0, 24),
202 dcp::Time (0, 0, 0, 0, 24),
208 make_shared<dcp::SubtitleString>(
209 boost::optional<string> (),
213 dcp::Colour (128, 0, 64),
216 dcp::Time (5, 41, 0, 21, 24),
217 dcp::Time (6, 12, 15, 21, 24),
225 dcp::Colour (1, 2, 3),
226 dcp::Time (1, 2, 3, 4, 24),
227 dcp::Time (5, 6, 7, 8, 24),
233 make_shared<dcp::SubtitleString>(
234 boost::optional<string> (),
238 dcp::Colour (128, 0, 64),
241 dcp::Time (5, 41, 0, 21, 24),
242 dcp::Time (6, 12, 15, 21, 24),
250 dcp::Colour (1, 2, 3),
251 dcp::Time (1, 2, 3, 4, 24),
252 dcp::Time (5, 6, 7, 8, 24),
257 c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
260 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
261 "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
262 " <Id>urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6</Id>\n"
263 " <ContentTitleText>Test</ContentTitleText>\n"
264 " <IssueDate>2016-04-01T03:52:00.000+00:00</IssueDate>\n"
265 " <ReelNumber>1</ReelNumber>\n"
266 " <Language>en</Language>\n"
267 " <EditRate>24 1</EditRate>\n"
268 " <TimeCodeRate>24</TimeCodeRate>\n"
270 " <Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"Frutiger\" Italic=\"no\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">\n"
271 " <Subtitle SpotNumber=\"1\" TimeIn=\"00:04:09:22\" TimeOut=\"00:04:11:22\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">\n"
272 " <Text Valign=\"top\" Vposition=\"80\">Hello world</Text>\n"
275 " <Font AspectAdjust=\"1.0\" Color=\"FF800040\" Effect=\"border\" EffectColor=\"FF010203\" Italic=\"yes\" Script=\"normal\" Size=\"91\" Underline=\"yes\" Weight=\"bold\">\n"
276 " <Subtitle SpotNumber=\"2\" TimeIn=\"05:41:00:21\" TimeOut=\"06:12:15:21\" FadeUpTime=\"01:02:03:04\" FadeDownTime=\"05:06:07:08\">\n"
277 " <Text Valign=\"bottom\" Vposition=\"40\" Direction=\"rtl\">What's going <Space Size=\"4.2\"/>on</Text>\n"
287 /* Write some subtitle content as SMPTE XML and check that it is right.
288 This includes in-line font changes.
290 BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test2)
292 dcp::SMPTESubtitleAsset c;
293 c.set_reel_number (1);
294 c.set_language (dcp::LanguageTag("en"));
295 c.set_content_title_text ("Test");
296 c.set_issue_date (dcp::LocalTime ("2016-04-01T03:52:00+00:00"));
299 make_shared<dcp::SubtitleString>(
304 dcp::Colour (255, 255, 255),
307 dcp::Time (0, 0, 1, 0, 24),
308 dcp::Time (0, 0, 9, 0, 24),
316 dcp::Colour (0, 0, 0),
317 dcp::Time (0, 0, 0, 0, 24),
318 dcp::Time (0, 0, 0, 0, 24),
324 make_shared<dcp::SubtitleString>(
329 dcp::Colour (255, 255, 255),
332 dcp::Time (0, 0, 1, 0, 24),
333 dcp::Time (0, 0, 9, 0, 24),
341 dcp::Colour (0, 0, 0),
342 dcp::Time (0, 0, 0, 0, 24),
343 dcp::Time (0, 0, 0, 0, 24),
349 make_shared<dcp::SubtitleString>(
354 dcp::Colour (255, 255, 255),
357 dcp::Time (0, 0, 1, 0, 24),
358 dcp::Time (0, 0, 9, 0, 24),
366 dcp::Colour (0, 0, 0),
367 dcp::Time (0, 0, 0, 0, 24),
368 dcp::Time (0, 0, 0, 0, 24),
374 make_shared<dcp::SubtitleString>(
379 dcp::Colour (255, 255, 255),
382 dcp::Time (0, 0, 1, 0, 24),
383 dcp::Time (0, 0, 9, 0, 24),
391 dcp::Colour (0, 0, 0),
392 dcp::Time (0, 0, 0, 0, 24),
393 dcp::Time (0, 0, 0, 0, 24),
399 make_shared<dcp::SubtitleString>(
404 dcp::Colour (255, 255, 255),
407 dcp::Time (0, 0, 1, 0, 24),
408 dcp::Time (0, 0, 9, 0, 24),
416 dcp::Colour (0, 0, 0),
417 dcp::Time (0, 0, 0, 0, 24),
418 dcp::Time (0, 0, 0, 0, 24),
424 make_shared<dcp::SubtitleString>(
429 dcp::Colour (255, 255, 255),
432 dcp::Time (0, 0, 1, 0, 24),
433 dcp::Time (0, 0, 9, 0, 24),
441 dcp::Colour (0, 0, 0),
442 dcp::Time (0, 0, 0, 0, 24),
443 dcp::Time (0, 0, 0, 0, 24),
448 c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
452 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
453 "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
454 " <Id>urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6</Id>\n"
455 " <ContentTitleText>Test</ContentTitleText>\n"
456 " <IssueDate>2016-04-01T03:52:00.000+00:00</IssueDate>\n"
457 " <ReelNumber>1</ReelNumber>\n"
458 " <Language>en</Language>\n"
459 " <EditRate>24 1</EditRate>\n"
460 " <TimeCodeRate>24</TimeCodeRate>\n"
462 " <Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"Arial\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">\n"
463 " <Subtitle SpotNumber=\"1\" TimeIn=\"00:00:01:00\" TimeOut=\"00:00:09:00\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">\n"
464 " <Text Valign=\"top\" Vposition=\"80\"><Font Italic=\"no\">Testing is </Font><Font Italic=\"yes\">really</Font><Font Italic=\"no\"> fun</Font></Text>\n"
465 " <Text Valign=\"top\" Vposition=\"90\"><Font Italic=\"no\">This is the </Font><Font Italic=\"yes\">second</Font><Font Italic=\"no\"> line</Font></Text>\n"
474 /* Write some subtitle content as SMPTE using bitmaps and check that it is right */
475 BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test3)
477 dcp::SMPTESubtitleAsset c;
478 c.set_reel_number (1);
479 c.set_language (dcp::LanguageTag("en"));
480 c.set_content_title_text ("Test");
481 c.set_start_time (dcp::Time());
483 boost::filesystem::path const sub_image = "test/data/sub.png";
486 make_shared<dcp::SubtitleImage>(
487 dcp::ArrayData(sub_image),
488 dcp::Time (0, 4, 9, 22, 24),
489 dcp::Time (0, 4, 11, 22, 24),
494 dcp::Time (0, 0, 0, 0, 24),
495 dcp::Time (0, 0, 0, 0, 24)
499 c._id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
501 boost::filesystem::path path = "build/test/write_smpte_subtitle_test3";
502 boost::filesystem::create_directories (path);
503 c.write (path / "subs.mxf");
505 dcp::SMPTESubtitleAsset read_back (path / "subs.mxf");
506 auto subs = read_back.subtitles ();
507 BOOST_REQUIRE_EQUAL (subs.size(), 1U);
508 auto image = dynamic_pointer_cast<const dcp::SubtitleImage>(subs[0]);
509 BOOST_REQUIRE (image);
511 BOOST_CHECK (image->png_image() == dcp::ArrayData(sub_image));
512 BOOST_CHECK (image->in() == dcp::Time(0, 4, 9, 22, 24));
513 BOOST_CHECK (image->out() == dcp::Time(0, 4, 11, 22, 24));
514 BOOST_CHECK_CLOSE (image->h_position(), 0.0, 1);
515 BOOST_CHECK (image->h_align() == dcp::HAlign::CENTER);
516 BOOST_CHECK_CLOSE (image->v_position(), 0.8, 1);
517 BOOST_CHECK (image->v_align() == dcp::VAlign::TOP);
518 BOOST_CHECK (image->fade_up_time() == dcp::Time(0, 0, 0, 0, 24));
519 BOOST_CHECK (image->fade_down_time() == dcp::Time(0, 0, 0, 0, 24));
523 /* Some closed caption systems require the <Text> elements to be written in order of their
524 * vertical position (see DoM bug #2106).
526 BOOST_AUTO_TEST_CASE (write_subtitles_in_vertical_order_with_top_alignment)
528 dcp::SMPTESubtitleAsset c;
529 c.set_reel_number (1);
530 c.set_language (dcp::LanguageTag("en"));
531 c.set_content_title_text ("Test");
532 c.set_issue_date (dcp::LocalTime ("2016-04-01T03:52:00+00:00"));
535 make_shared<dcp::SubtitleString>(
540 dcp::Colour (255, 255, 255),
543 dcp::Time (0, 0, 1, 0, 24),
544 dcp::Time (0, 0, 9, 0, 24),
552 dcp::Colour (0, 0, 0),
553 dcp::Time (0, 0, 0, 0, 24),
554 dcp::Time (0, 0, 0, 0, 24),
560 make_shared<dcp::SubtitleString>(
565 dcp::Colour (255, 255, 255),
568 dcp::Time (0, 0, 1, 0, 24),
569 dcp::Time (0, 0, 9, 0, 24),
577 dcp::Colour (0, 0, 0),
578 dcp::Time (0, 0, 0, 0, 24),
579 dcp::Time (0, 0, 0, 0, 24),
584 c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
588 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
589 "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
590 " <Id>urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6</Id>\n"
591 " <ContentTitleText>Test</ContentTitleText>\n"
592 " <IssueDate>2016-04-01T03:52:00.000+00:00</IssueDate>\n"
593 " <ReelNumber>1</ReelNumber>\n"
594 " <Language>en</Language>\n"
595 " <EditRate>24 1</EditRate>\n"
596 " <TimeCodeRate>24</TimeCodeRate>\n"
598 " <Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"Arial\" Italic=\"no\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">\n"
599 " <Subtitle SpotNumber=\"1\" TimeIn=\"00:00:01:00\" TimeOut=\"00:00:09:00\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">\n"
600 " <Text Valign=\"top\" Vposition=\"80\">Top line</Text>\n"
601 " <Text Valign=\"top\" Vposition=\"90\">Bottom line</Text>\n"
611 /* See the test above */
612 BOOST_AUTO_TEST_CASE (write_subtitles_in_vertical_order_with_bottom_alignment)
614 dcp::SMPTESubtitleAsset c;
615 c.set_reel_number (1);
616 c.set_language (dcp::LanguageTag("en"));
617 c.set_content_title_text ("Test");
618 c.set_issue_date (dcp::LocalTime ("2016-04-01T03:52:00+00:00"));
621 make_shared<dcp::SubtitleString>(
626 dcp::Colour (255, 255, 255),
629 dcp::Time (0, 0, 1, 0, 24),
630 dcp::Time (0, 0, 9, 0, 24),
638 dcp::Colour (0, 0, 0),
639 dcp::Time (0, 0, 0, 0, 24),
640 dcp::Time (0, 0, 0, 0, 24),
646 make_shared<dcp::SubtitleString>(
651 dcp::Colour (255, 255, 255),
654 dcp::Time (0, 0, 1, 0, 24),
655 dcp::Time (0, 0, 9, 0, 24),
663 dcp::Colour (0, 0, 0),
664 dcp::Time (0, 0, 0, 0, 24),
665 dcp::Time (0, 0, 0, 0, 24),
670 c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
674 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
675 "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
676 " <Id>urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6</Id>\n"
677 " <ContentTitleText>Test</ContentTitleText>\n"
678 " <IssueDate>2016-04-01T03:52:00.000+00:00</IssueDate>\n"
679 " <ReelNumber>1</ReelNumber>\n"
680 " <Language>en</Language>\n"
681 " <EditRate>24 1</EditRate>\n"
682 " <TimeCodeRate>24</TimeCodeRate>\n"
684 " <Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"Arial\" Italic=\"no\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">\n"
685 " <Subtitle SpotNumber=\"1\" TimeIn=\"00:00:01:00\" TimeOut=\"00:00:09:00\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">\n"
686 " <Text Valign=\"bottom\" Vposition=\"80\">Top line</Text>\n"
687 " <Text Valign=\"bottom\" Vposition=\"70\">Bottom line</Text>\n"