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