White space.
[dcpomatic.git] / test / srt_subtitle_test.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  test/subtitle_write_test.cc
21  *  @brief Test writing DCPs with XML subtitles.
22  */
23
24 #include "lib/film.h"
25 #include "lib/text_subtitle_content.h"
26 #include "lib/dcp_content_type.h"
27 #include "lib/font.h"
28 #include "lib/ratio.h"
29 #include "test.h"
30 #include <boost/test/unit_test.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <list>
33
34 using std::string;
35 using std::list;
36 using boost::shared_ptr;
37
38 /** Make a very short DCP with a single subtitle from .srt with no specified fonts */
39 BOOST_AUTO_TEST_CASE (srt_subtitle_test)
40 {
41         shared_ptr<Film> film = new_test_film ("srt_subtitle_test");
42         film->set_container (Ratio::from_id ("185"));
43         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
44         film->set_name ("frobozz");
45         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
46         film->examine_and_add_content (content);
47         wait_for_jobs ();
48
49         content->set_use_subtitles (true);
50         content->set_burn_subtitles (false);
51         film->make_dcp ();
52         wait_for_jobs ();
53
54         /* Should be blank video with a subtitle MXF */
55         check_dcp ("test/data/srt_subtitle_test", film->dir (film->dcp_name ()));
56 }
57
58 /** Same again but with a `font' specified */
59 BOOST_AUTO_TEST_CASE (srt_subtitle_test2)
60 {
61         shared_ptr<Film> film = new_test_film ("srt_subtitle_test2");
62         film->set_container (Ratio::from_id ("185"));
63         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
64         film->set_name ("frobozz");
65         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
66         film->examine_and_add_content (content);
67         wait_for_jobs ();
68
69         content->set_use_subtitles (true);
70         content->set_burn_subtitles (false);
71         /* Use test/data/subrip2.srt as if it were a font file  */
72         content->fonts().front()->set_file (FontFiles::NORMAL, "test/data/subrip2.srt");
73
74         film->make_dcp ();
75         wait_for_jobs ();
76
77         /* Should be blank video with a subtitle MXF */
78         check_dcp ("test/data/srt_subtitle_test2", film->dir (film->dcp_name ()));
79 }
80
81 /** Make another DCP with a longer .srt file */
82 BOOST_AUTO_TEST_CASE (srt_subtitle_test3)
83 {
84         shared_ptr<Film> film = new_test_film ("srt_subtitle_test3");
85
86         film->set_container (Ratio::from_id ("185"));
87         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
88         film->set_name ("frobozz");
89         film->set_interop (true);
90         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, private_data / "Ankoemmling.srt"));
91         film->examine_and_add_content (content);
92         wait_for_jobs ();
93
94         content->set_use_subtitles (true);
95         content->set_burn_subtitles (false);
96
97         film->make_dcp ();
98         wait_for_jobs ();
99
100         /* Find the subtitle file and check it */
101         for (
102                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->directory() / film->dcp_name (false));
103                 i != boost::filesystem::directory_iterator ();
104                 ++i) {
105
106                 if (boost::filesystem::is_directory (i->path ())) {
107                         for (
108                                 boost::filesystem::directory_iterator j = boost::filesystem::directory_iterator (i->path ());
109                                 j != boost::filesystem::directory_iterator ();
110                                 ++j) {
111
112                                 if (boost::algorithm::starts_with (j->path().leaf().string(), "sub_")) {
113                                         list<string> ignore;
114                                         ignore.push_back ("SubtitleID");
115                                         check_xml (*j, private_data / "Ankoemmling.xml", ignore);
116                                 }
117                         }
118                 }
119         }
120 }
121
122 #if 0
123 /* XXX: this is disabled; there is some difference in font rendering
124    between the test machine and others.
125 */
126
127 /** Test rendering of a SubRip subtitle */
128 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
129 {
130         shared_ptr<Film> film = new_test_film ("subrip_render_test");
131         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip.srt"));
132         content->examine (shared_ptr<Job> (), true);
133         BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
134
135         shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
136         list<ContentTextSubtitle> cts = decoder->get_text_subtitles (
137                 ContentTimePeriod (
138                         ContentTime::from_seconds (109), ContentTime::from_seconds (110)
139                         ), false
140                 );
141         BOOST_CHECK_EQUAL (cts.size(), 1);
142
143         PositionImage image = render_subtitles (cts.front().subs, dcp::Size (1998, 1080));
144         write_image (image.image, "build/test/subrip_render_test.png");
145         check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");
146 }
147 #endif