Supporters update.
[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                                 std::vector<dcp::Ruby>()
69                                 ),
70                         2,
71                         std::shared_ptr<dcpomatic::Font>(),
72                         dcp::SubtitleStandard::SMPTE_2014
73                         )
74                 );
75 }
76
77
78 BOOST_AUTO_TEST_CASE (marked_up_test1)
79 {
80         std::vector<StringText> s;
81         add (s, "Hello", false, false, false);
82         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
83 }
84
85
86 BOOST_AUTO_TEST_CASE (marked_up_test2)
87 {
88         std::vector<StringText> s;
89         add (s, "Hello", false, true, false);
90         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span weight=\"bold\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
91 }
92
93
94 BOOST_AUTO_TEST_CASE (marked_up_test3)
95 {
96         std::vector<StringText> s;
97         add (s, "Hello", true, true, false);
98         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span style=\"italic\" weight=\"bold\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
99 }
100
101 BOOST_AUTO_TEST_CASE (marked_up_test4)
102 {
103         std::vector<StringText> s;
104         add (s, "Hello", true, true, true);
105         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span style=\"italic\" weight=\"bold\" underline=\"single\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
106 }
107
108 BOOST_AUTO_TEST_CASE (marked_up_test5)
109 {
110         std::vector<StringText> s;
111         add (s, "Hello", false, true, false);
112         add (s, " world.", false, false, false);
113         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>");
114 }
115
116 BOOST_AUTO_TEST_CASE (marked_up_test6)
117 {
118         std::vector<StringText> s;
119         add (s, "Hello", true, false, false);
120         add (s, " world ", false, false, false);
121         add (s, "we are bold.", false, true, false);
122         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>");
123 }
124
125
126 BOOST_AUTO_TEST_CASE(render_text_with_newline_test)
127 {
128         std::list<dcp::SubtitleString> ss = {
129                 {
130                         {}, true, false, false, dcp::Colour(255, 255, 255), 42, 1.0,
131                         dcp::Time(0, 0, 0, 0, 24), dcp::Time(0, 0, 1, 0, 24),
132                         0.5, dcp::HAlign::CENTER,
133                         0.5, dcp::VAlign::CENTER,
134                         0.0,
135                         dcp::Direction::LTR,
136                         "Hello                     world",
137                         dcp::Effect::NONE, dcp::Colour(0, 0, 0),
138                         {}, {},
139                         0,
140                         std::vector<dcp::Ruby>()
141                 },
142                 {
143                         {}, true, false, false, dcp::Colour(255, 255, 255), 42, 1.0,
144                         dcp::Time(0, 0, 0, 0, 24), dcp::Time(0, 0, 1, 0, 24),
145                         0.5, dcp::HAlign::CENTER,
146                         0.5, dcp::VAlign::CENTER,
147                         0.0,
148                         dcp::Direction::LTR,
149                         "\n",
150                         dcp::Effect::NONE, dcp::Colour(0, 0, 0),
151                         {}, {},
152                         0,
153                         std::vector<dcp::Ruby>()
154                 }
155         };
156
157         std::vector<StringText> st;
158         for (auto i: ss) {
159                 st.push_back({i, 0, make_shared<dcpomatic::Font>("foo"), dcp::SubtitleStandard::SMPTE_2014});
160         }
161
162         auto images = render_text(st, dcp::Size(1998, 1080), {}, 24);
163
164         BOOST_CHECK_EQUAL(images.size(), 1U);
165         image_as_png(Image::ensure_alignment(images.front().image, Image::Alignment::PADDED)).write("build/test/render_text_with_newline_test.png");
166 #if defined(DCPOMATIC_OSX)
167         check_image("test/data/mac/render_text_with_newline_test.png", "build/test/render_text_with_newline_test.png");
168 #elif defined(DCPOMATIC_WINDOWS)
169         check_image("test/data/windows/render_text_with_newline_test.png", "build/test/render_text_with_newline_test.png");
170 #else
171         check_image("test/data/render_text_with_newline_test.png", "build/test/render_text_with_newline_test.png");
172 #endif
173 }
174
175
176 #if 0
177
178 BOOST_AUTO_TEST_CASE (render_text_test)
179 {
180         auto dcp_string = dcp::SubtitleString(
181                 {}, false, false, false, dcp::Colour(255, 255, 255), 42, 1.0,
182                 dcp::Time(0, 0, 0, 0, 24), dcp::Time(0, 0, 1, 0, 24),
183                 0.5, dcp::HAlign::CENTER,
184                 0.5, dcp::VAlign::CENTER,
185                 dcp::Direction::LTR,
186                 "HÄllo jokers",
187                 dcp::Effect::NONE, dcp::Colour(0, 0, 0),
188                 {}, {},
189                 0
190                 );
191
192         auto string_text = StringText(dcp_string, 0, shared_ptr<dcpomatic::Font>());
193
194         auto images = render_text({ string_text }, dcp::Size(1998, 1080), {}, 24);
195
196         BOOST_CHECK_EQUAL(images.size(), 1U);
197         image_as_png(Image::ensure_alignment(images.front().image, Image::Alignment::PADDED)).write("build/test/render_text_test.png");
198 }
199
200 #endif