Fix some small errors in SSA/ASS parsing.
[libsub.git] / test / ssa_reader_test.cc
1 /*
2     Copyright (C) 2016 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 #include "test.h"
21 #include "ssa_reader.h"
22 #include "collect.h"
23 #include "subtitle.h"
24 #include <boost/test/unit_test.hpp>
25 #include <boost/filesystem.hpp>
26 #include <cstdio>
27 #include <cmath>
28
29 using std::list;
30 using std::fabs;
31
32 BOOST_AUTO_TEST_CASE (ssa_reader_test)
33 {
34         boost::filesystem::path p = private_test / "example.ssa";
35         FILE* f = fopen (p.string().c_str(), "r");
36         sub::SSAReader reader (f);
37         fclose (f);
38         list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
39
40         list<sub::Subtitle>::iterator i = subs.begin ();
41
42         BOOST_REQUIRE (i != subs.end ());
43         BOOST_CHECK_EQUAL (i->from, sub::Time::from_hms (0, 2, 40, 650));
44         BOOST_CHECK_EQUAL (i->to, sub::Time::from_hms (0, 2, 41, 790));
45         list<sub::Line>::iterator j = i->lines.begin();
46         BOOST_REQUIRE (j != i->lines.end ());
47         BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
48         sub::Block b = j->blocks.front ();
49         BOOST_CHECK_EQUAL (b.text, "Et les enregistrements de ses ondes delta ?");
50         BOOST_CHECK_EQUAL (b.font.get(), "Wolf_Rain");
51         BOOST_CHECK_EQUAL (b.font_size.points().get(), 56);
52         BOOST_CHECK_EQUAL (b.bold, false);
53         BOOST_CHECK_EQUAL (b.italic, false);
54         BOOST_CHECK_EQUAL (b.underline, false);
55         ++i;
56
57         BOOST_REQUIRE (i != subs.end ());
58         BOOST_CHECK_EQUAL (i->from, sub::Time::from_hms (0, 2, 42, 420));
59         BOOST_CHECK_EQUAL (i->to, sub::Time::from_hms (0, 2, 44, 150));
60         j = i->lines.begin();
61         BOOST_REQUIRE (j != i->lines.end ());
62         BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
63         b = j->blocks.front ();
64         BOOST_CHECK_EQUAL (b.text, "Toujours rien.");
65         BOOST_CHECK_EQUAL (b.font.get(), "Wolf_Rain");
66         BOOST_CHECK_EQUAL (b.font_size.points().get(), 56);
67         BOOST_CHECK_EQUAL (b.bold, false);
68         BOOST_CHECK_EQUAL (b.italic, false);
69         BOOST_CHECK_EQUAL (b.underline, false);
70         ++i;
71
72         BOOST_CHECK (i == subs.end());
73 }
74
75 BOOST_AUTO_TEST_CASE (ssa_reader_line_test1)
76 {
77         sub::RawSubtitle base;
78         list<sub::RawSubtitle> r = sub::SSAReader::parse_line (base, "This is a line with some {\\i1}italics{\\i0} and then\\nthere is a new line.");
79
80         list<sub::RawSubtitle>::const_iterator i = r.begin ();
81         BOOST_CHECK_EQUAL (i->text, "This is a line with some ");
82         BOOST_CHECK_EQUAL (i->italic, false);
83         ++i;
84         BOOST_REQUIRE (i != r.end ());
85
86         BOOST_CHECK_EQUAL (i->text, "italics");
87         BOOST_CHECK_EQUAL (i->italic, true);
88         ++i;
89         BOOST_REQUIRE (i != r.end ());
90
91         BOOST_CHECK_EQUAL (i->text, " and then");
92         BOOST_CHECK_EQUAL (i->italic, false);
93         ++i;
94         BOOST_REQUIRE (i != r.end ());
95
96         BOOST_CHECK_EQUAL (i->text, "there is a new line.");
97         ++i;
98         BOOST_REQUIRE (i == r.end ());
99 }
100
101 BOOST_AUTO_TEST_CASE (ssa_reader_line_test2)
102 {
103         sub::RawSubtitle base;
104         list<sub::RawSubtitle> r = sub::SSAReader::parse_line (base, "{\\i1}It's all just italics{\\i0}");
105
106         list<sub::RawSubtitle>::const_iterator i = r.begin ();
107         BOOST_CHECK_EQUAL (i->text, "It's all just italics");
108         BOOST_CHECK_EQUAL (i->italic, true);
109         ++i;
110         BOOST_REQUIRE (i == r.end ());
111 }
112
113 static void
114 test (boost::filesystem::path p)
115 {
116         p = private_test / p;
117         FILE* f = fopen (p.string().c_str(), "r");
118         BOOST_REQUIRE (f);
119         sub::SSAReader r (f);
120         fclose (f);
121 }
122
123 /** Test of reading some typical .ssa files */
124 BOOST_AUTO_TEST_CASE (ssa_reader_test2)
125 {
126         test ("DKH_UT_EN20160601def.ssa");
127         test ("dcpsubtest-en.ssa");
128 }
129
130 #define SUB_START(f, t) \
131         BOOST_REQUIRE (i != subs.end ()); \
132         BOOST_CHECK_EQUAL (i->from, f); \
133         BOOST_CHECK_EQUAL (i->to, t); \
134         j = i->lines.begin ();
135
136 #define LINE(p, r)                                                      \
137         BOOST_REQUIRE (j != i->lines.end ()); \
138         BOOST_CHECK (j->vertical_position.proportional); \
139         BOOST_CHECK (fabs (j->vertical_position.proportional.get() - p) < 1e-5); \
140         BOOST_CHECK (j->vertical_position.reference); \
141         BOOST_CHECK_EQUAL (j->vertical_position.reference.get(), r); \
142         k = j->blocks.begin (); \
143         ++j;
144
145 #define BLOCK(t, f, s, b, i, u) \
146         BOOST_REQUIRE (k != j->blocks.end ()); \
147         BOOST_CHECK_EQUAL (k->text, t); \
148         BOOST_CHECK_EQUAL (k->font.get(), f); \
149         BOOST_CHECK_EQUAL (k->font_size.points().get(), s); \
150         BOOST_CHECK_EQUAL (k->bold, b); \
151         BOOST_CHECK_EQUAL (k->italic, i); \
152         BOOST_CHECK_EQUAL (k->underline, u); \
153         ++k;
154
155 #define SUB_END() \
156         ++i;
157
158 /** Test reading of a file within the libsub tree which exercises the parser */
159 BOOST_AUTO_TEST_CASE (ssa_reader_test3)
160 {
161         boost::filesystem::path p = "test/data/test.ssa";
162         FILE* f = fopen (p.string().c_str(), "r");
163         sub::SSAReader reader (f);
164         fclose (f);
165         list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
166
167         list<sub::Subtitle>::iterator i = subs.begin ();
168         list<sub::Line>::iterator j;
169         list<sub::Block>::iterator k;
170
171         /* Hello world */
172         SUB_START (sub::Time::from_hms (0, 0, 1, 230), sub::Time::from_hms (0, 0, 4, 550));
173         LINE (0, sub::BOTTOM_OF_SCREEN);
174         BLOCK ("Hello world", "Arial", 20, false, false, false);
175         SUB_END();
176
177         /* This is vertically moved\nand has two lines. */
178         SUB_START (sub::Time::from_hms (0, 0, 5, 740), sub::Time::from_hms (0, 0, 11, 0));
179         /* The first line should be 900 pixels and one line (20
180            points, 1.2 times spaced, as a proportion of the total
181            screen height 729 points) up.
182         */
183         LINE((900.0 / 1080) - (20.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
184         BLOCK("This is vertically moved", "Arial", 20, false, false, false);
185         LINE((900.0 / 1080), sub::BOTTOM_OF_SCREEN);
186         BLOCK("and has two lines.", "Arial", 20, false, false, false);
187         SUB_END();
188
189         /* Some {\i1}italics{\i} are here. */
190         SUB_START (sub::Time::from_hms (0, 0, 7, 740), sub::Time::from_hms (0, 0, 9, 0));
191         LINE(0, sub::BOTTOM_OF_SCREEN);
192         BLOCK("Some ", "Arial", 20, false, false, false);
193         BLOCK("italics", "Arial", 20, false, true, false);
194         BLOCK(" are here.", "Arial", 20, false, false, false);
195         SUB_END();
196
197         /* Alignments */
198
199         SUB_START (sub::Time::from_hms (0, 0, 9, 230), sub::Time::from_hms (0, 0, 11, 560));
200         LINE (0, sub::BOTTOM_OF_SCREEN);
201         BLOCK("bottom left", "Arial", 20, false, false, false);
202         SUB_END ();
203
204         SUB_START (sub::Time::from_hms (0, 0, 9, 240), sub::Time::from_hms (0, 0, 11, 560));
205         LINE (0, sub::BOTTOM_OF_SCREEN);
206         BLOCK("bottom centre", "Arial", 20, false, false, false);
207         SUB_END ();
208
209         SUB_START (sub::Time::from_hms (0, 0, 9, 250), sub::Time::from_hms (0, 0, 11, 560));
210         LINE (0, sub::BOTTOM_OF_SCREEN);
211         BLOCK("bottom right", "Arial", 20, false, false, false);
212         SUB_END ();
213
214         SUB_START (sub::Time::from_hms (0, 0, 9, 260), sub::Time::from_hms (0, 0, 11, 560));
215         LINE (0, sub::CENTRE_OF_SCREEN);
216         BLOCK("middle left", "Arial", 20, false, false, false);
217         SUB_END ();
218
219         SUB_START (sub::Time::from_hms (0, 0, 9, 270), sub::Time::from_hms (0, 0, 11, 560));
220         LINE (0, sub::CENTRE_OF_SCREEN);
221         BLOCK("middle centre", "Arial", 20, false, false, false);
222         SUB_END ();
223
224         SUB_START (sub::Time::from_hms (0, 0, 9, 280), sub::Time::from_hms (0, 0, 11, 560));
225         LINE (0, sub::CENTRE_OF_SCREEN);
226         BLOCK("middle right", "Arial", 20, false, false, false);
227         SUB_END ();
228
229         SUB_START (sub::Time::from_hms (0, 0, 9, 290), sub::Time::from_hms (0, 0, 11, 560));
230         LINE (0, sub::TOP_OF_SCREEN);
231         BLOCK("top left", "Arial", 20, false, false, false);
232         SUB_END ();
233
234         SUB_START (sub::Time::from_hms (0, 0, 9, 300), sub::Time::from_hms (0, 0, 11, 560));
235         LINE (0, sub::TOP_OF_SCREEN);
236         BLOCK("top centre", "Arial", 20, false, false, false);
237         SUB_END ();
238
239         SUB_START (sub::Time::from_hms (0, 0, 9, 310), sub::Time::from_hms (0, 0, 11, 560));
240         LINE (0, sub::TOP_OF_SCREEN);
241         BLOCK("top right", "Arial", 20, false, false, false);
242         SUB_END ();
243
244         BOOST_REQUIRE (i == subs.end ());
245 }