2 Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 /** @file test/shared_subtitle_test.cc
36 * @brief Tests of the code that is shared between Interop and SMPTE subtitles.
40 #include "interop_subtitle_asset.h"
41 #include "smpte_subtitle_asset.h"
42 #include "subtitle_string.h"
43 #include "subtitle_image.h"
44 #include "subtitle_asset_internal.h"
45 #include "reel_interop_subtitle_asset.h"
51 #include <boost/test/unit_test.hpp>
54 using std::make_shared;
55 using std::shared_ptr;
58 using boost::optional;
61 /** Test dcp::order::Font::take_intersection */
62 BOOST_AUTO_TEST_CASE (take_intersection_test)
65 A._values["foo"] = "bar";
66 A._values["fred"] = "jim";
69 B._values["foo"] = "bar";
70 B._values["sheila"] = "baz";
72 A.take_intersection (B);
73 BOOST_REQUIRE_EQUAL (A._values.size(), 1U);
74 BOOST_CHECK_EQUAL (A._values["foo"], "bar");
79 A._values["foo"] = "bar";
80 A._values["fred"] = "jim";
82 B._values["foo"] = "hello";
83 B._values["sheila"] = "baz";
85 A.take_intersection (B);
86 BOOST_CHECK_EQUAL (A._values.size(), 0U);
89 /** Test dcp::order::Font::take_difference */
90 BOOST_AUTO_TEST_CASE (take_difference_test)
93 A._values["foo"] = "bar";
94 A._values["fred"] = "jim";
97 B._values["foo"] = "bar";
98 B._values["sheila"] = "baz";
100 A.take_difference (B);
101 BOOST_REQUIRE_EQUAL (A._values.size(), 1U);
102 BOOST_CHECK_EQUAL (A._values["fred"], "jim");
105 /** Test dcp::order::Subtitle::pull_fonts */
106 BOOST_AUTO_TEST_CASE (pull_fonts_test1)
108 auto root = make_shared<dcp::order::Part>(shared_ptr<dcp::order::Part>());
109 auto sub1 = make_shared<dcp::order::Subtitle>(root, dcp::Time(), dcp::Time(), dcp::Time(), dcp::Time());
110 root->children.push_back (sub1);
111 auto text1 = make_shared<dcp::order::Text>(sub1, dcp::HAlign::CENTER, 0, dcp::VAlign::TOP, 0, 0, dcp::Direction::LTR, std::vector<dcp::Ruby>());
112 sub1->children.push_back (text1);
113 text1->font._values["font"] = "Inconsolata";
114 text1->font._values["size"] = "42";
116 dcp::SubtitleAsset::pull_fonts (root);
118 BOOST_REQUIRE_EQUAL (sub1->font._values.size(), 2U);
119 BOOST_CHECK_EQUAL (sub1->font._values["font"], "Inconsolata");
120 BOOST_CHECK_EQUAL (sub1->font._values["size"], "42");
121 BOOST_CHECK_EQUAL (text1->font._values.size(), 0U);
124 /** Test dcp::order::Subtitle::pull_fonts */
125 BOOST_AUTO_TEST_CASE (pull_fonts_test2)
127 auto root = make_shared<dcp::order::Part>(shared_ptr<dcp::order::Part> ());
128 auto sub1 = make_shared<dcp::order::Subtitle>(root, dcp::Time(), dcp::Time(), dcp::Time(), dcp::Time());
129 root->children.push_back (sub1);
130 auto text1 = make_shared<dcp::order::Text>(sub1, dcp::HAlign::CENTER, 0, dcp::VAlign::TOP, 0, 0, dcp::Direction::LTR, std::vector<dcp::Ruby>());
131 sub1->children.push_back (text1);
132 text1->font._values["font"] = "Inconsolata";
133 text1->font._values["size"] = "42";
134 auto text2 = make_shared<dcp::order::Text>(sub1, dcp::HAlign::CENTER, 0, dcp::VAlign::TOP, 0, 0, dcp::Direction::LTR, std::vector<dcp::Ruby>());
135 sub1->children.push_back (text2);
136 text2->font._values["font"] = "Inconsolata";
137 text2->font._values["size"] = "48";
139 dcp::SubtitleAsset::pull_fonts (root);
141 BOOST_REQUIRE_EQUAL (sub1->font._values.size(), 1U);
142 BOOST_CHECK_EQUAL (sub1->font._values["font"], "Inconsolata");
143 BOOST_REQUIRE_EQUAL (text1->font._values.size(), 1U);
144 BOOST_CHECK_EQUAL (text1->font._values["size"], "42");
145 BOOST_REQUIRE_EQUAL (text2->font._values.size(), 1U);
146 BOOST_CHECK_EQUAL (text2->font._values["size"], "48");
149 /** Test dcp::order::Subtitle::pull_fonts */
150 BOOST_AUTO_TEST_CASE (pull_fonts_test3)
152 auto root = make_shared<dcp::order::Part>(shared_ptr<dcp::order::Part> ());
153 auto sub1 = make_shared<dcp::order::Subtitle>(root, dcp::Time(), dcp::Time(), dcp::Time(), dcp::Time());
154 root->children.push_back (sub1);
155 auto text1 = make_shared<dcp::order::Text>(sub1, dcp::HAlign::CENTER, 0, dcp::VAlign::TOP, 0, 0, dcp::Direction::LTR, std::vector<dcp::Ruby>());
156 sub1->children.push_back (text1);
157 dcp::order::Font font;
158 font._values["font"] = "Inconsolata";
159 font._values["size"] = "42";
160 auto string1 = make_shared<dcp::order::String>(text1, font, "Hello world", 0);
161 text1->children.push_back (string1);
163 dcp::SubtitleAsset::pull_fonts (root);
165 BOOST_REQUIRE_EQUAL (sub1->font._values.size(), 2U);
166 BOOST_CHECK_EQUAL (sub1->font._values["font"], "Inconsolata");
167 BOOST_CHECK_EQUAL (sub1->font._values["size"], "42");
171 /* Check that subtitle XML is prettily formatted without inserting any white space into
172 * <Text> node, which I think has the potential to alter appearance.
174 BOOST_AUTO_TEST_CASE (format_xml_test1)
177 auto root = doc.create_root_node("Foo");
178 root->add_child("Empty");
179 root->add_child("Text")->add_child_text("Hello world");
180 root->add_child("Font")->add_child("Text")->add_child_text("Say what");
181 auto fred = root->add_child("Text")->add_child("Font");
182 fred->set_attribute("bob", "job");
183 fred->add_child_text("Fred");
184 fred->add_child("Text")->add_child_text("Jim");
185 fred->add_child_text("Sheila");
186 BOOST_REQUIRE_EQUAL (dcp::SubtitleAsset::format_xml(doc, make_pair(string{}, string{"fred"})),
187 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
188 "<Foo xmlns=\"fred\">\n"
190 " <Text>Hello world</Text>\n"
192 " <Text>Say what</Text>\n"
194 " <Text><Font bob=\"job\">Fred<Text>Jim</Text>Sheila</Font></Text>\n"
199 BOOST_AUTO_TEST_CASE (format_xml_test2)
201 xmlpp::DomParser parser;
202 auto path = private_test / "DKH_UT_EN20160601def.xml";
203 parser.parse_file(path.string().c_str());
204 auto document = parser.get_document();
205 check_xml (dcp::file_to_string(private_test / "DKH_UT_EN20160601def.reformatted.xml"), dcp::SubtitleAsset::format_xml(*document, {}), {});
209 BOOST_AUTO_TEST_CASE (format_xml_entities_test)
212 auto root = doc.create_root_node("Foo");
213 root->add_child("Bar")->add_child_text("Don't panic & xml \"is\" 'great' & < > —");
214 BOOST_REQUIRE_EQUAL(dcp::SubtitleAsset::format_xml(doc, {}),
215 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
217 " <Bar>Don't panic &amp; xml \"is\" 'great' & < > —</Bar>\n"
222 BOOST_AUTO_TEST_CASE(ruby_round_trip_test)
224 dcp::InteropSubtitleAsset asset("test/data/ruby1.xml");
225 check_xml(dcp::file_to_string("test/data/ruby1.xml"), asset.xml_as_string(), {}, false);