Simple pass-through of <Ruby> tags in subtitles.
[libdcp.git] / test / shared_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
35 /** @file  test/shared_subtitle_test.cc
36  *  @brief Tests of the code that is shared between Interop and SMPTE subtitles.
37  */
38
39
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"
46 #include "reel.h"
47 #include "cpl.h"
48 #include "dcp.h"
49 #include "test.h"
50 #include "util.h"
51 #include <boost/test/unit_test.hpp>
52
53
54 using std::make_shared;
55 using std::shared_ptr;
56 using std::string;
57 using std::vector;
58 using boost::optional;
59
60
61 /** Test dcp::order::Font::take_intersection */
62 BOOST_AUTO_TEST_CASE (take_intersection_test)
63 {
64         dcp::order::Font A;
65         A._values["foo"] = "bar";
66         A._values["fred"] = "jim";
67
68         dcp::order::Font B;
69         B._values["foo"] = "bar";
70         B._values["sheila"] = "baz";
71
72         A.take_intersection (B);
73         BOOST_REQUIRE_EQUAL (A._values.size(), 1U);
74         BOOST_CHECK_EQUAL (A._values["foo"], "bar");
75
76         A._values.clear ();
77         B._values.clear ();
78
79         A._values["foo"] = "bar";
80         A._values["fred"] = "jim";
81
82         B._values["foo"] = "hello";
83         B._values["sheila"] = "baz";
84
85         A.take_intersection (B);
86         BOOST_CHECK_EQUAL (A._values.size(), 0U);
87 }
88
89 /** Test dcp::order::Font::take_difference */
90 BOOST_AUTO_TEST_CASE (take_difference_test)
91 {
92         dcp::order::Font A;
93         A._values["foo"] = "bar";
94         A._values["fred"] = "jim";
95
96         dcp::order::Font B;
97         B._values["foo"] = "bar";
98         B._values["sheila"] = "baz";
99
100         A.take_difference (B);
101         BOOST_REQUIRE_EQUAL (A._values.size(), 1U);
102         BOOST_CHECK_EQUAL (A._values["fred"], "jim");
103 }
104
105 /** Test dcp::order::Subtitle::pull_fonts */
106 BOOST_AUTO_TEST_CASE (pull_fonts_test1)
107 {
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";
115
116         dcp::SubtitleAsset::pull_fonts (root);
117
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);
122 }
123
124 /** Test dcp::order::Subtitle::pull_fonts */
125 BOOST_AUTO_TEST_CASE (pull_fonts_test2)
126 {
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";
138
139         dcp::SubtitleAsset::pull_fonts (root);
140
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");
147 }
148
149 /** Test dcp::order::Subtitle::pull_fonts */
150 BOOST_AUTO_TEST_CASE (pull_fonts_test3)
151 {
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);
162
163         dcp::SubtitleAsset::pull_fonts (root);
164
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");
168 }
169
170
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.
173  */
174 BOOST_AUTO_TEST_CASE (format_xml_test1)
175 {
176         xmlpp::Document doc;
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"
189 "  <Empty/>\n"
190 "  <Text>Hello world</Text>\n"
191 "  <Font>\n"
192 "    <Text>Say what</Text>\n"
193 "  </Font>\n"
194 "  <Text><Font bob=\"job\">Fred<Text>Jim</Text>Sheila</Font></Text>\n"
195 "</Foo>\n");
196 }
197
198
199 BOOST_AUTO_TEST_CASE (format_xml_test2)
200 {
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, {}), {});
206 }
207
208
209 BOOST_AUTO_TEST_CASE (format_xml_entities_test)
210 {
211         xmlpp::Document doc;
212         auto root = doc.create_root_node("Foo");
213         root->add_child("Bar")->add_child_text("Don't panic &amp; xml \"is\" 'great' & < > —");
214         BOOST_REQUIRE_EQUAL(dcp::SubtitleAsset::format_xml(doc, {}),
215 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
216 "<Foo>\n"
217 "  <Bar>Don't panic &amp;amp; xml \"is\" 'great' &amp; &lt; &gt; —</Bar>\n"
218 "</Foo>\n");
219 }
220
221
222 BOOST_AUTO_TEST_CASE(ruby_round_trip_test)
223 {
224         dcp::InteropSubtitleAsset asset("test/data/ruby1.xml");
225         check_xml(dcp::file_to_string("test/data/ruby1.xml"), asset.xml_as_string(), {}, false);
226 }
227
228