Forward-port some STL fixes from libsub.
[libsub.git] / test / ssa_reader_test.cc
index 46b5cb75600c010d003401afef027dedbdef633f..74faf49b7034df468b15796836c51b9a416d4c96 100644 (file)
 #include "subtitle.h"
 #include <boost/test/unit_test.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/foreach.hpp>
 #include <cstdio>
 #include <cmath>
+#include <iostream>
 
 using std::list;
 using std::fabs;
@@ -108,6 +110,16 @@ BOOST_AUTO_TEST_CASE (ssa_reader_line_test2)
        BOOST_CHECK_EQUAL (i->italic, true);
        ++i;
        BOOST_REQUIRE (i == r.end ());
+
+       r = sub::SSAReader::parse_line (base, "{\\i1}Italic{\\i0}\\Nand new line");
+       i = r.begin ();
+       BOOST_CHECK_EQUAL (i->text, "Italic");
+       BOOST_CHECK_EQUAL (i->italic, true);
+       BOOST_CHECK (fabs ((72.0 * 1.2 / 792) - i->vertical_position.proportional.get()) < 1e-5);
+       ++i;
+       BOOST_CHECK_EQUAL (i->text, "and new line");
+       BOOST_CHECK_EQUAL (i->italic, false);
+       BOOST_CHECK (i->vertical_position.proportional.get() < 1e-5);
 }
 
 static void
@@ -124,6 +136,7 @@ test (boost::filesystem::path p)
 BOOST_AUTO_TEST_CASE (ssa_reader_test2)
 {
        test ("DKH_UT_EN20160601def.ssa");
+       test ("dcpsubtest-en.ssa");
 }
 
 #define SUB_START(f, t) \
@@ -242,3 +255,181 @@ BOOST_AUTO_TEST_CASE (ssa_reader_test3)
 
        BOOST_REQUIRE (i == subs.end ());
 }
+
+/** Test reading of a file within the libsub-test-private tree which exercises the parser */
+BOOST_AUTO_TEST_CASE (ssa_reader_test4)
+{
+       boost::filesystem::path p = private_test / "dcpsubtest2-en.ssa";
+       FILE* f = fopen (p.string().c_str(), "r");
+       sub::SSAReader reader (f);
+       fclose (f);
+       list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
+
+       list<sub::Subtitle>::iterator i = subs.begin ();
+       list<sub::Line>::iterator j;
+       list<sub::Block>::iterator k;
+
+       BOOST_REQUIRE (i != subs.end ());
+
+       SUB_START (sub::Time::from_hms (0, 0, 1, 0), sub::Time::from_hms (0, 0, 3, 0));
+       /* The first line should be one line (50 points, 1.2 times
+          spaced, as a proportion of the total screen height 729
+          points) up.
+       */
+       LINE ((50.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("1st line: This is normal", "Verdana", 50, false, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("2d line: this is bold", "Verdana", 50, true, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 3, 100), sub::Time::from_hms (0, 0, 5, 100));
+       LINE ((50.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("1st line: this is bold", "Verdana", 50, true, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("2nd line: This is normal", "Verdana", 50, false, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 5, 200), sub::Time::from_hms (0, 0, 7, 200));
+       LINE ((50.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("1st line: this is bold", "Verdana", 50, true, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("2nd line: this is italics", "Verdana", 50, false, true, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 7, 300), sub::Time::from_hms (0, 0, 9, 300));
+       LINE ((50.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("1st line: this is italics", "Verdana", 50, false, true, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("2nd line: this is bold", "Verdana", 50, true, false, false);
+       SUB_END ();
+}
+
+/** Test reading of a .ass file */
+BOOST_AUTO_TEST_CASE (ssa_reader_test5)
+{
+       boost::filesystem::path p = private_test / "dcpsubtest-en.ass";
+       FILE* f = fopen (p.string().c_str(), "r");
+       sub::SSAReader reader (f);
+       fclose (f);
+       list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
+
+       list<sub::Subtitle>::iterator i = subs.begin ();
+       list<sub::Line>::iterator j;
+       list<sub::Block>::iterator k;
+
+       BOOST_REQUIRE (i != subs.end ());
+
+       SUB_START (sub::Time::from_hms (0, 0, 1, 0), sub::Time::from_hms (0, 0, 3, 0));
+       /* The first line should be one line (26 points, 1.2 times
+          spaced, as a proportion of the total screen height 729
+          points) up.
+       */
+       LINE ((26.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("1st subtitle, 1st line", "arial", 26, true, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("2nd subtitle, 2nd line", "arial", 26, true, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 3, 100), sub::Time::from_hms (0, 0, 5, 100));
+       LINE ((26.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("2nd subtitle, 1st line", "arial", 26, true, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("2nd subtitle, 2nd line", "arial", 26, true, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 5, 200), sub::Time::from_hms (0, 0, 7, 200));
+       LINE ((26.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("3rd subtitle, 1st line", "arial", 26, true, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("3rd subtitle, 2nd line", "arial", 26, true, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 7, 300), sub::Time::from_hms (0, 0, 9, 300));
+       LINE ((26.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("4th subtitle, 1st line", "arial", 26, true, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("4th subtitle, 2nd line", "arial", 26, true, false, false);
+       SUB_END ();
+}
+
+/** Test reading of another .ass file */
+BOOST_AUTO_TEST_CASE (ssa_reader_test6)
+{
+       boost::filesystem::path p = private_test / "DCP-o-matic_test_subs_1.ass";
+       FILE* f = fopen (p.string().c_str(), "r");
+       BOOST_REQUIRE (f);
+       sub::SSAReader reader (f);
+       fclose (f);
+       list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
+
+       list<sub::Subtitle>::iterator i = subs.begin ();
+       list<sub::Line>::iterator j;
+       list<sub::Block>::iterator k;
+
+       BOOST_REQUIRE (i != subs.end ());
+
+       SUB_START (sub::Time::from_hms (0, 0, 0, 70), sub::Time::from_hms (0, 0, 1, 110));
+       /* The first line should be one line (30 points, 1.2 times
+          spaced, as a proportion of the total screen height 729
+          points) up.
+       */
+       LINE ((30.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is normal", "Arial", 30, false, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is bold", "Arial", 30, true, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 1, 200), sub::Time::from_hms (0, 0, 2, 240));
+       LINE ((30.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is bold", "Arial", 30, true, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is normal", "Arial", 30, false, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 2, 300), sub::Time::from_hms (0, 0, 3, 380));
+       LINE ((30.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is bold", "Arial", 30, true, false, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is italic", "Arial", 30, false, true, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 3, 400), sub::Time::from_hms (0, 0, 4, 480));
+       LINE ((30.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is italic", "Arial", 30, false, true, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is bold", "Arial", 30, true, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 4, 510), sub::Time::from_hms (0, 0, 5, 600));
+       LINE ((30.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("Last three words are ", "Arial", 30, false, false, false);
+       BLOCK ("bold AND italic", "Arial", 30, true, true, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("Last three words are ", "Arial", 30, false, false, false);
+       BLOCK ("italic AND bold", "Arial", 30, true, true, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 5, 620), sub::Time::from_hms (0, 0, 6, 710));
+       LINE ((30.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("Last three words are ", "Arial", 30, false, false, false);
+       BLOCK ("bold AND italic", "Arial", 30, true, true, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("First three words", "Arial", 30, true, true, false);
+       BLOCK (" are italic AND bold", "Arial", 30, false, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 6, 730), sub::Time::from_hms (0, 0, 8, 30));
+       LINE ((30.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("Last three words are ", "Arial", 30, false, false, false);
+       BLOCK ("bold AND italic", "Arial", 30, true, true, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("This line is normal", "Arial", 30, false, false, false);
+       SUB_END ();
+
+       SUB_START (sub::Time::from_hms (0, 0, 8, 90), sub::Time::from_hms (0, 0, 9, 210));
+       LINE ((30.0 * 1.2 / 792), sub::BOTTOM_OF_SCREEN);
+       BLOCK ("Both lines are bold AND italic", "Arial", 30, true, true, false);
+       LINE (0, sub::BOTTOM_OF_SCREEN);
+       BLOCK ("Both lines are bold AND italic", "Arial", 30, true, true, false);
+       SUB_END ();
+}