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