e81a6313d59b4f5ea79f34d7c6e23805ab4d6972
[libdcp.git] / test / smpte_subtitle_test.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
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
23     including the two.
24
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.
32 */
33
34
35 #include "smpte_load_font_node.h"
36 #include "smpte_subtitle_asset.h"
37 #include "subtitle_image.h"
38 #include "test.h"
39 #include "types.h"
40 #include <boost/optional/optional_io.hpp>
41 #include <boost/test/unit_test.hpp>
42
43
44 using std::string;
45 using boost::dynamic_pointer_cast;
46 using boost::optional;
47 using boost::shared_ptr;
48
49
50 BOOST_AUTO_TEST_CASE (smpte_subtitle_id_test)
51 {
52         dcp::SMPTESubtitleAsset subs;
53         subs.add(
54                 shared_ptr<dcp::Subtitle> (
55                         new dcp::SubtitleString(
56                                 optional<string>(),
57                                 false, false, false,
58                                 dcp::Colour(),
59                                 64,
60                                 1,
61                                 dcp::Time(0, 1, 2, 3, 24),
62                                 dcp::Time(0, 2, 2, 3, 24),
63                                 0.5,
64                                 dcp::HALIGN_CENTER,
65                                 0.5,
66                                 dcp::VALIGN_CENTER,
67                                 dcp::DIRECTION_LTR,
68                                 "Hello",
69                                 dcp::NONE,
70                                 dcp::Colour(),
71                                 dcp::Time(0, 0, 0, 0, 24),
72                                 dcp::Time(0, 0, 0, 0, 24)
73                                 )
74                         )
75                 );
76         subs.write("build/test/smpte_subtitle_id_test.mxf");
77
78         dcp::SMPTESubtitleAsset check("build/test/smpte_subtitle_id_test.mxf");
79         BOOST_CHECK(check.id() != check.xml_id());
80 }
81
82
83 /** Check reading of a SMPTE subtitle file */
84 BOOST_AUTO_TEST_CASE (read_smpte_subtitle_test)
85 {
86         dcp::SMPTESubtitleAsset sc (
87                 private_test /
88                 "data" /
89                 "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV" /
90                 "8b48f6ae-c74b-4b80-b994-a8236bbbad74_sub.mxf"
91                 );
92
93         BOOST_CHECK_EQUAL (sc.id(), "8b48f6ae-c74b-4b80-b994-a8236bbbad74");
94         BOOST_CHECK_EQUAL (sc.content_title_text(), "Journey to Jah");
95         BOOST_REQUIRE (sc.annotation_text());
96         BOOST_CHECK_EQUAL (sc.annotation_text().get(), "Journey to Jah");
97         BOOST_CHECK_EQUAL (sc.issue_date(), dcp::LocalTime ("2014-02-25T11:22:48.000-00:00"));
98         BOOST_REQUIRE (sc.reel_number());
99         BOOST_CHECK_EQUAL (sc.reel_number().get(), 1);
100         BOOST_REQUIRE (sc.language ());
101         BOOST_CHECK_EQUAL (sc.language().get (), "de");
102         BOOST_CHECK_EQUAL (sc.edit_rate(), dcp::Fraction (25, 1));
103         BOOST_CHECK_EQUAL (sc.time_code_rate(), 25);
104         BOOST_CHECK_EQUAL (sc.start_time(), dcp::Time (0, 0, 0, 0, 25));
105         auto lfn = sc.load_font_nodes ();
106         BOOST_REQUIRE_EQUAL (lfn.size(), 1);
107         shared_ptr<dcp::SMPTELoadFontNode> smpte_lfn = dynamic_pointer_cast<dcp::SMPTELoadFontNode> (lfn.front ());
108         BOOST_REQUIRE (smpte_lfn);
109         BOOST_CHECK_EQUAL (smpte_lfn->id, "theFontId");
110         BOOST_CHECK_EQUAL (smpte_lfn->urn, "9118bbce-4105-4a05-b37c-a5a6f75e1fea");
111         BOOST_REQUIRE_EQUAL (sc.subtitles().size(), 63);
112         BOOST_REQUIRE (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().front()));
113         BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().front())->text(), "Noch mal.");
114         BOOST_CHECK_EQUAL (sc.subtitles().front()->in(), dcp::Time (0, 0, 25, 12, 25));
115         BOOST_CHECK_EQUAL (sc.subtitles().front()->out(), dcp::Time (0, 0, 26, 4, 25));
116         BOOST_REQUIRE (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().back()));
117         BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().back())->text(), "Prochainement");
118         BOOST_CHECK_EQUAL (sc.subtitles().back()->in(), dcp::Time (0, 1, 57, 17, 25));
119         BOOST_CHECK_EQUAL (sc.subtitles().back()->out(), dcp::Time (0, 1, 58, 12, 25));
120 }
121
122
123 /** And another one featuring <Font> within <Text> */
124 BOOST_AUTO_TEST_CASE (read_smpte_subtitle_test2)
125 {
126         dcp::SMPTESubtitleAsset sc (private_test / "olsson.xml");
127
128         auto subs = sc.subtitles();
129         BOOST_REQUIRE_EQUAL (subs.size(), 6);
130         auto i = subs.begin();
131         auto is = dynamic_pointer_cast<const dcp::SubtitleString>(*i);
132         BOOST_REQUIRE (is);
133         BOOST_CHECK_EQUAL (is->text(), "Testing is ");
134         BOOST_CHECK (!is->italic());
135         ++i;
136         is = dynamic_pointer_cast<const dcp::SubtitleString>(*i);
137         BOOST_REQUIRE (is);
138         BOOST_CHECK_EQUAL (is->text(), "really");
139         BOOST_CHECK (is->italic());
140         ++i;
141         is = dynamic_pointer_cast<const dcp::SubtitleString>(*i);
142         BOOST_REQUIRE (is);
143         BOOST_CHECK_EQUAL (is->text(), " fun!");
144         BOOST_CHECK (!is->italic());
145         ++i;
146         is = dynamic_pointer_cast<const dcp::SubtitleString>(*i);
147         BOOST_REQUIRE (is);
148         BOOST_CHECK_EQUAL (is->text(), "This is the ");
149         BOOST_CHECK (!is->italic());
150         ++i;
151         is = dynamic_pointer_cast<const dcp::SubtitleString>(*i);
152         BOOST_REQUIRE (is);
153         BOOST_CHECK_EQUAL (is->text(), "second");
154         BOOST_CHECK (is->italic());
155         ++i;
156         is = dynamic_pointer_cast<const dcp::SubtitleString>(*i);
157         BOOST_REQUIRE (is);
158         BOOST_CHECK_EQUAL (is->text(), " line!");
159         BOOST_CHECK (!is->italic());
160 }
161
162
163 /** And another one featuring image subtitles */
164 BOOST_AUTO_TEST_CASE (read_smpte_subtitle_test3)
165 {
166         dcp::SMPTESubtitleAsset subs ("test/data/subs.mxf");
167
168         BOOST_REQUIRE_EQUAL (subs.subtitles().size(), 1);
169         auto si = dynamic_pointer_cast<const dcp::SubtitleImage>(subs.subtitles().front());
170         BOOST_REQUIRE (si);
171         BOOST_CHECK (si->png_image() == dcp::Data("test/data/sub.png"));
172 }