Basically-working interop subtitle font handling.
[libdcp.git] / test / dcp_font_test.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "interop_subtitle_asset.h"
21 #include "dcp.h"
22 #include "cpl.h"
23 #include "test.h"
24 #include "reel.h"
25 #include "util.h"
26 #include "reel_subtitle_asset.h"
27 #include <boost/test/unit_test.hpp>
28 #include <cstdio>
29
30 using std::list;
31 using std::string;
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
34 using boost::shared_array;
35
36 /** Create a DCP with interop subtitles and check that the font is written and read back correctly */
37 BOOST_AUTO_TEST_CASE (interop_dcp_font_test)
38 {
39         boost::filesystem::path directory = "build/test/interop_dcp_font_test";
40         dcp::DCP dcp (directory);
41
42         shared_ptr<dcp::InteropSubtitleAsset> subs (new dcp::InteropSubtitleAsset ());
43         subs->add_font ("theFontId", "test/data/dummy.ttf");
44         subs->write (directory / "frobozz.xml");
45         check_file ("test/data/dummy.ttf", "build/test/interop_dcp_font_test/dummy.ttf");
46
47         shared_ptr<dcp::Reel> reel (new dcp::Reel ());
48         reel->add (shared_ptr<dcp::ReelAsset> (new dcp::ReelSubtitleAsset (subs, dcp::Fraction (24, 1), 24, 0)));
49
50         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("", dcp::TRAILER));
51         cpl->add (reel);
52
53         dcp.add (cpl);
54         dcp.write_xml (dcp::INTEROP);
55
56         dcp::DCP dcp2 (directory);
57         dcp2.read ();
58         shared_ptr<dcp::SubtitleAsset> subs2 = dynamic_pointer_cast<dcp::SubtitleAsset> (
59                 dcp2.cpls().front()->reels().front()->main_subtitle()->asset_ref().object()
60                 );
61         BOOST_REQUIRE (subs2);
62         BOOST_REQUIRE_EQUAL (subs2->_fonts.size(), 1);
63
64         boost::uintmax_t const size = boost::filesystem::file_size ("test/data/dummy.ttf");
65         FILE* f = dcp::fopen_boost ("test/data/dummy.ttf", "r");
66         BOOST_REQUIRE (f);
67         shared_array<uint8_t> ref (new uint8_t[size]);
68         fread (ref.get(), 1, size, f);
69         fclose (f);
70
71         BOOST_CHECK_EQUAL (memcmp (subs2->_fonts["theFontId"].data.get(), ref.get(), size), 0);
72 }