Add --no-encrypt with the same idea as the previous commit.
[dcpomatic.git] / test / render_subtitles_test.cc
1 /*
2     Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file  test/render_text_test.cc
23  *  @brief Check markup of subtitles for rendering.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/image.h"
29 #include "lib/image_png.h"
30 #include "lib/render_text.h"
31 #include "lib/string_text.h"
32 #include "test.h"
33 #include <dcp/subtitle_string.h>
34 #include <boost/test/unit_test.hpp>
35
36
37 using std::make_shared;
38 using std::shared_ptr;
39
40
41 static void
42 add(std::vector<StringText>& s, std::string text, bool italic, bool bold, bool underline)
43 {
44         s.push_back (
45                 StringText (
46                         dcp::SubtitleString (
47                                 boost::optional<std::string> (),
48                                 italic,
49                                 bold,
50                                 underline,
51                                 dcp::Colour (255, 255, 255),
52                                 42,
53                                 1,
54                                 dcp::Time (),
55                                 dcp::Time (),
56                                 1,
57                                 dcp::HAlign::LEFT,
58                                 1,
59                                 dcp::VAlign::TOP,
60                                 0,
61                                 dcp::Direction::LTR,
62                                 text,
63                                 dcp::Effect::NONE,
64                                 dcp::Colour (0, 0, 0),
65                                 dcp::Time (),
66                                 dcp::Time (),
67                                 0
68                                 ),
69                         2,
70                         std::shared_ptr<dcpomatic::Font>(),
71                         dcp::SubtitleStandard::SMPTE_2014
72                         )
73                 );
74 }
75
76
77 BOOST_AUTO_TEST_CASE (marked_up_test1)
78 {
79         std::vector<StringText> s;
80         add (s, "Hello", false, false, false);
81         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
82 }
83
84
85 BOOST_AUTO_TEST_CASE (marked_up_test2)
86 {
87         std::vector<StringText> s;
88         add (s, "Hello", false, true, false);
89         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span weight=\"bold\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
90 }
91
92
93 BOOST_AUTO_TEST_CASE (marked_up_test3)
94 {
95         std::vector<StringText> s;
96         add (s, "Hello", true, true, false);
97         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span style=\"italic\" weight=\"bold\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
98 }
99
100 BOOST_AUTO_TEST_CASE (marked_up_test4)
101 {
102         std::vector<StringText> s;
103         add (s, "Hello", true, true, true);
104         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span style=\"italic\" weight=\"bold\" underline=\"single\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
105 }
106
107 BOOST_AUTO_TEST_CASE (marked_up_test5)
108 {
109         std::vector<StringText> s;
110         add (s, "Hello", false, true, false);
111         add (s, " world.", false, false, false);
112         BOOST_CHECK_EQUAL (marked_up(s, 1024, 1, ""), "<span weight=\"bold\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span><span size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\"> world.</span>");
113 }
114
115 BOOST_AUTO_TEST_CASE (marked_up_test6)
116 {
117         std::vector<StringText> s;
118         add (s, "Hello", true, false, false);
119         add (s, " world ", false, false, false);
120         add (s, "we are bold.", false, true, false);
121         BOOST_CHECK_EQUAL (marked_up(s, 1024, 1, ""), "<span style=\"italic\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span><span size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\"> world </span><span weight=\"bold\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">we are bold.</span>");
122 }
123
124
125 BOOST_AUTO_TEST_CASE(render_text_with_newline_test)
126 {
127         std::list<dcp::SubtitleString> ss = {
128                 {
129                         {}, true, false, false, dcp::Colour(255, 255, 255), 42, 1.0,
130                         dcp::Time(0, 0, 0, 0, 24), dcp::Time(0, 0, 1, 0, 24),
131                         0.5, dcp::HAlign::CENTER,
132                         0.5, dcp::VAlign::CENTER,
133                         0.0,
134                         dcp::Direction::LTR,
135                         "Hello                     world",
136                         dcp::Effect::NONE, dcp::Colour(0, 0, 0),
137                         {}, {},
138                         0
139                 },
140                 {
141                         {}, true, false, false, dcp::Colour(255, 255, 255), 42, 1.0,
142                         dcp::Time(0, 0, 0, 0, 24), dcp::Time(0, 0, 1, 0, 24),
143                         0.5, dcp::HAlign::CENTER,
144                         0.5, dcp::VAlign::CENTER,
145                         0.0,
146                         dcp::Direction::LTR,
147                         "\n",
148                         dcp::Effect::NONE, dcp::Colour(0, 0, 0),
149                         {}, {},
150                         0
151                 }
152         };
153
154         std::vector<StringText> st;
155         for (auto i: ss) {
156                 st.push_back({i, 0, make_shared<dcpomatic::Font>("foo"), dcp::SubtitleStandard::SMPTE_2014});
157         }
158
159         auto images = render_text(st, dcp::Size(1998, 1080), {}, 24);
160
161         BOOST_CHECK_EQUAL(images.size(), 1U);
162         image_as_png(Image::ensure_alignment(images.front().image, Image::Alignment::PADDED)).write("build/test/render_text_with_newline_test.png");
163 #if defined(DCPOMATIC_OSX)
164         check_image("test/data/mac/render_text_with_newline_test.png", "build/test/render_text_with_newline_test.png");
165 #elif defined(DCPOMATIC_WINDOWS)
166         check_image("test/data/windows/render_text_with_newline_test.png", "build/test/render_text_with_newline_test.png");
167 #else
168         check_image("test/data/render_text_with_newline_test.png", "build/test/render_text_with_newline_test.png");
169 #endif
170 }
171
172
173 #if 0
174
175 BOOST_AUTO_TEST_CASE (render_text_test)
176 {
177         auto dcp_string = dcp::SubtitleString(
178                 {}, false, false, false, dcp::Colour(255, 255, 255), 42, 1.0,
179                 dcp::Time(0, 0, 0, 0, 24), dcp::Time(0, 0, 1, 0, 24),
180                 0.5, dcp::HAlign::CENTER,
181                 0.5, dcp::VAlign::CENTER,
182                 dcp::Direction::LTR,
183                 "HÄllo jokers",
184                 dcp::Effect::NONE, dcp::Colour(0, 0, 0),
185                 {}, {},
186                 0
187                 );
188
189         auto string_text = StringText(dcp_string, 0, shared_ptr<dcpomatic::Font>());
190
191         auto images = render_text({ string_text }, dcp::Size(1998, 1080), {}, 24);
192
193         BOOST_CHECK_EQUAL(images.size(), 1U);
194         image_as_png(Image::ensure_alignment(images.front().image, Image::Alignment::PADDED)).write("build/test/render_text_test.png");
195 }
196
197 #endif