3c958f262b9bf6a1bc48dc4198cecb5dad882760
[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 }
128
129 /** Test reading of a file within the libsub tree which exercises the parser */
130 BOOST_AUTO_TEST_CASE (ssa_reader_test3)
131 {
132         boost::filesystem::path p = "test/data/test.ssa";
133         FILE* f = fopen (p.string().c_str(), "r");
134         sub::SSAReader reader (f);
135         fclose (f);
136         list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
137
138         list<sub::Subtitle>::iterator i = subs.begin ();
139
140         BOOST_REQUIRE (i != subs.end ());
141         BOOST_CHECK_EQUAL (i->from, sub::Time::from_hms (0, 0, 1, 230));
142         BOOST_CHECK_EQUAL (i->to, sub::Time::from_hms (0, 0, 4, 550));
143         list<sub::Line>::iterator j = i->lines.begin();
144         BOOST_REQUIRE (j != i->lines.end ());
145         BOOST_CHECK (j->vertical_position.proportional);
146         BOOST_CHECK_EQUAL (j->vertical_position.proportional.get(), 0);
147         BOOST_CHECK (j->vertical_position.reference);
148         BOOST_CHECK_EQUAL (j->vertical_position.reference.get(), sub::BOTTOM_OF_SCREEN);
149         BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
150         sub::Block b = j->blocks.front ();
151         BOOST_CHECK_EQUAL (b.text, "Hello world");
152         BOOST_CHECK_EQUAL (b.font.get(), "Arial");
153         BOOST_CHECK_EQUAL (b.font_size.points().get(), 20);
154         BOOST_CHECK_EQUAL (b.bold, false);
155         BOOST_CHECK_EQUAL (b.italic, false);
156         BOOST_CHECK_EQUAL (b.underline, false);
157         ++i;
158
159         BOOST_REQUIRE (i != subs.end ());
160         BOOST_CHECK_EQUAL (i->from, sub::Time::from_hms (0, 0, 5, 740));
161         BOOST_CHECK_EQUAL (i->to, sub::Time::from_hms (0, 0, 11, 0));
162         j = i->lines.begin();
163         BOOST_REQUIRE (j != i->lines.end ());
164         BOOST_CHECK (j->vertical_position.proportional);
165         /* The first line should be 900 pixels and one line (20
166            points, 1.2 times spaced, as a proportion of the total
167            screen height 729 points) up.
168         */
169         BOOST_CHECK (fabs (j->vertical_position.proportional.get() - (900.0 / 1080) - (20.0 * 1.2 / 792)) < 1e-5);
170         BOOST_CHECK (j->vertical_position.reference);
171         BOOST_CHECK_EQUAL (j->vertical_position.reference.get(), sub::BOTTOM_OF_SCREEN);
172         BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
173         b = j->blocks.front ();
174         BOOST_CHECK_EQUAL (b.text, "This is vertically moved");
175         BOOST_CHECK_EQUAL (b.font.get(), "Arial");
176         BOOST_CHECK_EQUAL (b.font_size.points().get(), 20);
177         BOOST_CHECK_EQUAL (b.bold, false);
178         BOOST_CHECK_EQUAL (b.italic, false);
179         BOOST_CHECK_EQUAL (b.underline, false);
180         ++j;
181         BOOST_CHECK (fabs (j->vertical_position.proportional.get() - (900.0 / 1080)) < 1e-5);
182         BOOST_CHECK (j->vertical_position.reference);
183         BOOST_CHECK_EQUAL (j->vertical_position.reference.get(), sub::BOTTOM_OF_SCREEN);
184         BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
185         b = j->blocks.front ();
186         BOOST_CHECK_EQUAL (b.text, "and has two lines.");
187         BOOST_CHECK_EQUAL (b.font.get(), "Arial");
188         BOOST_CHECK_EQUAL (b.font_size.points().get(), 20);
189         BOOST_CHECK_EQUAL (b.bold, false);
190         BOOST_CHECK_EQUAL (b.italic, false);
191         BOOST_CHECK_EQUAL (b.underline, false);
192         ++j;
193         BOOST_REQUIRE (j == i->lines.end());
194         ++i;
195
196         BOOST_REQUIRE (i == subs.end ());
197 }