Fix check for return value of EssenceType.
[libdcp.git] / test / dcp_font_test.cc
1 /*
2     Copyright (C) 2015-2019 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 "cpl.h"
36 #include "dcp.h"
37 #include "file.h"
38 #include "interop_subtitle_asset.h"
39 #include "reel.h"
40 #include "reel_interop_subtitle_asset.h"
41 #include "reel_smpte_subtitle_asset.h"
42 #include "smpte_subtitle_asset.h"
43 #include "test.h"
44 #include "util.h"
45 #include <boost/test/unit_test.hpp>
46 #include <cstdio>
47
48
49 using std::list;
50 using std::string;
51 using std::shared_ptr;
52 using std::dynamic_pointer_cast;
53 using std::make_shared;
54 using boost::shared_array;
55
56 /** Create a DCP with interop subtitles and check that the font is written and read back correctly */
57 BOOST_AUTO_TEST_CASE (interop_dcp_font_test)
58 {
59         boost::filesystem::path directory = "build/test/interop_dcp_font_test";
60         boost::filesystem::remove_all(directory);
61         dcp::DCP dcp (directory);
62
63         auto subs = make_shared<dcp::InteropSubtitleAsset>();
64         subs->add_font ("theFontId", dcp::ArrayData("test/data/dummy.ttf"));
65         subs->write (directory / "frobozz.xml");
66         check_file ("test/data/dummy.ttf", "build/test/interop_dcp_font_test/font_0.ttf");
67
68         auto reel = make_shared<dcp::Reel>();
69         reel->add (make_shared<dcp::ReelInteropSubtitleAsset>(subs, dcp::Fraction (24, 1), 24, 0));
70
71         auto cpl = make_shared<dcp::CPL>("", dcp::ContentKind::TRAILER, dcp::Standard::INTEROP);
72         cpl->add (reel);
73
74         dcp.add (cpl);
75         dcp.write_xml ();
76
77         dcp::DCP dcp2 (directory);
78         dcp2.read ();
79         auto subs2 = dynamic_pointer_cast<dcp::SubtitleAsset> (
80                 dcp2.cpls()[0]->reels()[0]->main_subtitle()->asset_ref().asset()
81                 );
82         BOOST_REQUIRE (subs2);
83         BOOST_REQUIRE_EQUAL (subs2->_fonts.size(), 1U);
84
85         auto const size = boost::filesystem::file_size ("test/data/dummy.ttf");
86         dcp::File f("test/data/dummy.ttf", "rb");
87         BOOST_REQUIRE (f);
88         shared_array<uint8_t> ref (new uint8_t[size]);
89         f.read(ref.get(), 1, size);
90         f.close();
91
92         BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data(), ref.get(), size), 0);
93 }
94
95 /** Create a DCP with SMPTE subtitles and check that the font is written and read back correctly */
96 BOOST_AUTO_TEST_CASE (smpte_dcp_font_test)
97 {
98         boost::filesystem::path directory = "build/test/smpte_dcp_font_test";
99         dcp::DCP dcp (directory);
100
101         auto subs = make_shared<dcp::SMPTESubtitleAsset>();
102         subs->add_font ("theFontId", dcp::ArrayData("test/data/dummy.ttf"));
103         subs->write (directory / "frobozz.mxf");
104
105         auto reel = make_shared<dcp::Reel>();
106         reel->add (make_shared<dcp::ReelSMPTESubtitleAsset>(subs, dcp::Fraction (24, 1), 24, 0));
107
108         auto cpl = make_shared<dcp::CPL>("", dcp::ContentKind::TRAILER, dcp::Standard::SMPTE);
109         cpl->add (reel);
110
111         dcp.add (cpl);
112         dcp.write_xml ();
113
114         dcp::DCP dcp2 (directory);
115         dcp2.read ();
116         auto subs2 = dynamic_pointer_cast<dcp::SubtitleAsset> (
117                 dcp2.cpls().front()->reels().front()->main_subtitle()->asset_ref().asset()
118                 );
119         BOOST_REQUIRE (subs2);
120         BOOST_REQUIRE_EQUAL (subs2->_fonts.size(), 1U);
121
122         auto const size = boost::filesystem::file_size ("test/data/dummy.ttf");
123         dcp::File f("test/data/dummy.ttf", "rb");
124         BOOST_REQUIRE (f);
125         shared_array<uint8_t> ref (new uint8_t[size]);
126         f.read(ref.get(), 1, size);
127         f.close();
128
129         BOOST_REQUIRE (subs2->_fonts.front().data.data());
130         BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data(), ref.get(), size), 0);
131 }