2918325bbc95bd4d546e940d6634860de7217486
[libdcp.git] / test / write_subtitle_test.cc
1 /*
2     Copyright (C) 2015-2021 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 "subtitle_string.h"
37 #include "subtitle_image.h"
38 #include "subtitle_asset_internal.h"
39 #include "reel_interop_subtitle_asset.h"
40 #include "reel.h"
41 #include "cpl.h"
42 #include "dcp.h"
43 #include "test.h"
44 #include "util.h"
45 #include <boost/test/unit_test.hpp>
46
47 using std::string;
48 using std::shared_ptr;
49 using std::vector;
50 using std::make_shared;
51 using boost::optional;
52
53 /** Test dcp::order::Font::take_intersection */
54 BOOST_AUTO_TEST_CASE (take_intersection_test)
55 {
56         dcp::order::Font A;
57         A._values["foo"] = "bar";
58         A._values["fred"] = "jim";
59
60         dcp::order::Font B;
61         B._values["foo"] = "bar";
62         B._values["sheila"] = "baz";
63
64         A.take_intersection (B);
65         BOOST_REQUIRE_EQUAL (A._values.size(), 1);
66         BOOST_CHECK_EQUAL (A._values["foo"], "bar");
67
68         A._values.clear ();
69         B._values.clear ();
70
71         A._values["foo"] = "bar";
72         A._values["fred"] = "jim";
73
74         B._values["foo"] = "hello";
75         B._values["sheila"] = "baz";
76
77         A.take_intersection (B);
78         BOOST_CHECK_EQUAL (A._values.size(), 0);
79 }
80
81 /** Test dcp::order::Font::take_difference */
82 BOOST_AUTO_TEST_CASE (take_difference_test)
83 {
84         dcp::order::Font A;
85         A._values["foo"] = "bar";
86         A._values["fred"] = "jim";
87
88         dcp::order::Font B;
89         B._values["foo"] = "bar";
90         B._values["sheila"] = "baz";
91
92         A.take_difference (B);
93         BOOST_REQUIRE_EQUAL (A._values.size(), 1);
94         BOOST_CHECK_EQUAL (A._values["fred"], "jim");
95 }
96
97 /** Test dcp::order::Subtitle::pull_fonts */
98 BOOST_AUTO_TEST_CASE (pull_fonts_test1)
99 {
100         auto root = make_shared<dcp::order::Part>(shared_ptr<dcp::order::Part>());
101         auto sub1 = make_shared<dcp::order::Subtitle>(root, dcp::Time(), dcp::Time(), dcp::Time(), dcp::Time());
102         root->children.push_back (sub1);
103         auto text1 = make_shared<dcp::order::Text>(sub1, dcp::HAlign::CENTER, 0, dcp::VAlign::TOP, 0, dcp::Direction::LTR);
104         sub1->children.push_back (text1);
105         text1->font._values["font"] = "Inconsolata";
106         text1->font._values["size"] = "42";
107
108         dcp::SubtitleAsset::pull_fonts (root);
109
110         BOOST_REQUIRE_EQUAL (sub1->font._values.size(), 2);
111         BOOST_CHECK_EQUAL (sub1->font._values["font"], "Inconsolata");
112         BOOST_CHECK_EQUAL (sub1->font._values["size"], "42");
113         BOOST_CHECK_EQUAL (text1->font._values.size(), 0);
114 }
115
116 /** Test dcp::order::Subtitle::pull_fonts */
117 BOOST_AUTO_TEST_CASE (pull_fonts_test2)
118 {
119         auto root = make_shared<dcp::order::Part>(shared_ptr<dcp::order::Part> ());
120         auto sub1 = make_shared<dcp::order::Subtitle>(root, dcp::Time(), dcp::Time(), dcp::Time(), dcp::Time());
121         root->children.push_back (sub1);
122         auto text1 = make_shared<dcp::order::Text>(sub1, dcp::HAlign::CENTER, 0, dcp::VAlign::TOP, 0, dcp::Direction::LTR);
123         sub1->children.push_back (text1);
124         text1->font._values["font"] = "Inconsolata";
125         text1->font._values["size"] = "42";
126         auto text2 = make_shared<dcp::order::Text>(sub1, dcp::HAlign::CENTER, 0, dcp::VAlign::TOP, 0, dcp::Direction::LTR);
127         sub1->children.push_back (text2);
128         text2->font._values["font"] = "Inconsolata";
129         text2->font._values["size"] = "48";
130
131         dcp::SubtitleAsset::pull_fonts (root);
132
133         BOOST_REQUIRE_EQUAL (sub1->font._values.size(), 1);
134         BOOST_CHECK_EQUAL (sub1->font._values["font"], "Inconsolata");
135         BOOST_REQUIRE_EQUAL (text1->font._values.size(), 1);
136         BOOST_CHECK_EQUAL (text1->font._values["size"], "42");
137         BOOST_REQUIRE_EQUAL (text2->font._values.size(), 1);
138         BOOST_CHECK_EQUAL (text2->font._values["size"], "48");
139 }
140
141 /** Test dcp::order::Subtitle::pull_fonts */
142 BOOST_AUTO_TEST_CASE (pull_fonts_test3)
143 {
144         auto root = make_shared<dcp::order::Part>(shared_ptr<dcp::order::Part> ());
145         auto sub1 = make_shared<dcp::order::Subtitle>(root, dcp::Time(), dcp::Time(), dcp::Time(), dcp::Time());
146         root->children.push_back (sub1);
147         auto text1 = make_shared<dcp::order::Text>(sub1, dcp::HAlign::CENTER, 0, dcp::VAlign::TOP, 0, dcp::Direction::LTR);
148         sub1->children.push_back (text1);
149         dcp::order::Font font;
150         font._values["font"] = "Inconsolata";
151         font._values["size"] = "42";
152         auto string1 = make_shared<dcp::order::String>(text1, font, "Hello world");
153         text1->children.push_back (string1);
154
155         dcp::SubtitleAsset::pull_fonts (root);
156
157         BOOST_REQUIRE_EQUAL (sub1->font._values.size(), 2);
158         BOOST_CHECK_EQUAL (sub1->font._values["font"], "Inconsolata");
159         BOOST_CHECK_EQUAL (sub1->font._values["size"], "42");
160 }
161