Handle vertical alignment of subs correctly wrt the difference between Interop and...
[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 <dcp/subtitle_string.h>
33 #include <boost/test/unit_test.hpp>
34
35
36 using std::shared_ptr;
37
38
39 static void
40 add (std::list<StringText>& s, std::string text, bool italic, bool bold, bool underline)
41 {
42         s.push_back (
43                 StringText (
44                         dcp::SubtitleString (
45                                 boost::optional<std::string> (),
46                                 italic,
47                                 bold,
48                                 underline,
49                                 dcp::Colour (255, 255, 255),
50                                 42,
51                                 1,
52                                 dcp::Time (),
53                                 dcp::Time (),
54                                 1,
55                                 dcp::HAlign::LEFT,
56                                 1,
57                                 dcp::VAlign::TOP,
58                                 dcp::Direction::LTR,
59                                 text,
60                                 dcp::Effect::NONE,
61                                 dcp::Colour (0, 0, 0),
62                                 dcp::Time (),
63                                 dcp::Time (),
64                                 0
65                                 ),
66                         2,
67                         std::shared_ptr<dcpomatic::Font>(),
68                         dcp::Standard::SMPTE
69                         )
70                 );
71 }
72
73
74 BOOST_AUTO_TEST_CASE (marked_up_test1)
75 {
76         std::list<StringText> s;
77         add (s, "Hello", false, false, false);
78         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
79 }
80
81
82 BOOST_AUTO_TEST_CASE (marked_up_test2)
83 {
84         std::list<StringText> s;
85         add (s, "Hello", false, true, false);
86         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span weight=\"bold\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
87 }
88
89
90 BOOST_AUTO_TEST_CASE (marked_up_test3)
91 {
92         std::list<StringText> s;
93         add (s, "Hello", true, true, false);
94         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span style=\"italic\" weight=\"bold\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
95 }
96
97 BOOST_AUTO_TEST_CASE (marked_up_test4)
98 {
99         std::list<StringText> s;
100         add (s, "Hello", true, true, true);
101         BOOST_CHECK_EQUAL(marked_up(s, 1024, 1, ""), "<span style=\"italic\" weight=\"bold\" underline=\"single\" size=\"41705\" alpha=\"65535\" color=\"#FFFFFF\">Hello</span>");
102 }
103
104 BOOST_AUTO_TEST_CASE (marked_up_test5)
105 {
106         std::list<StringText> s;
107         add (s, "Hello", false, true, false);
108         add (s, " world.", false, false, false);
109         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>");
110 }
111
112 BOOST_AUTO_TEST_CASE (marked_up_test6)
113 {
114         std::list<StringText> s;
115         add (s, "Hello", true, false, false);
116         add (s, " world ", false, false, false);
117         add (s, "we are bold.", false, true, false);
118         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>");
119 }
120
121
122 #if 0
123
124 BOOST_AUTO_TEST_CASE (render_text_test)
125 {
126         auto dcp_string = dcp::SubtitleString(
127                 {}, false, false, false, dcp::Colour(255, 255, 255), 42, 1.0,
128                 dcp::Time(0, 0, 0, 0, 24), dcp::Time(0, 0, 1, 0, 24),
129                 0.5, dcp::HAlign::CENTER,
130                 0.5, dcp::VAlign::CENTER,
131                 dcp::Direction::LTR,
132                 "HÄllo jokers",
133                 dcp::Effect::NONE, dcp::Colour(0, 0, 0),
134                 {}, {},
135                 0
136                 );
137
138         auto string_text = StringText(dcp_string, 0, shared_ptr<dcpomatic::Font>());
139
140         auto images = render_text({ string_text }, dcp::Size(1998, 1080), {}, 24);
141
142         BOOST_CHECK_EQUAL(images.size(), 1U);
143         image_as_png(Image::ensure_alignment(images.front().image, Image::Alignment::PADDED)).write("build/test/render_text_test.png");
144 }
145
146 #endif