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