summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2016-06-20 22:22:01 +0100
committerCarl Hetherington <cth@carlh.net>2016-06-20 22:22:01 +0100
commit6f1af34ae95586281f1632ceb87cc95784ccff28 (patch)
tree3c9b0881e8173786542ea5ddb9330a9ba0fb3691
parent5a77646bf70c5fdfc39210035a7c3c3de43e89ed (diff)
Fix bold and newlines (in some cases) in SSA.
-rw-r--r--src/ssa_reader.cc12
-rw-r--r--src/util.cc2
-rw-r--r--test/ssa_reader_test.cc60
3 files changed, 70 insertions, 4 deletions
diff --git a/src/ssa_reader.cc b/src/ssa_reader.cc
index 74d497a..eb85dea 100644
--- a/src/ssa_reader.cc
+++ b/src/ssa_reader.cc
@@ -239,6 +239,10 @@ SSAReader::parse_line (RawSubtitle base, string line)
current.italic = true;
} else if (style == "\\i0" || style == "\\i") {
current.italic = false;
+ } else if (style == "\\b1") {
+ current.bold = true;
+ } else if (style == "\\b0") {
+ current.bold = false;
} else if (style == "\\an1" || style == "\\an2" || style == "\\an3") {
current.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
} else if (style == "\\an4" || style == "\\an5" || style == "\\an6") {
@@ -253,9 +257,11 @@ SSAReader::parse_line (RawSubtitle base, string line)
}
break;
case BACKSLASH:
- if ((c == 'n' || c == 'N') && !current.text.empty ()) {
- subs.push_back (current);
- current.text = "";
+ if (c == 'n' || c == 'N') {
+ if (!current.text.empty ()) {
+ subs.push_back (current);
+ current.text = "";
+ }
/* Move down one line (1.2 times the font size) */
if (current.vertical_position.reference.get() == BOTTOM_OF_SCREEN) {
current.vertical_position.proportional = current.vertical_position.proportional.get() - line_size;
diff --git a/src/util.cc b/src/util.cc
index a736a91..a165126 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -59,7 +59,7 @@ sub::get_line_file (FILE* f)
{
char buffer[256];
char* r = fgets (buffer, sizeof (buffer), f);
- if (r == 0 || feof (f)) {
+ if (r == 0) {
return optional<string> ();
}
diff --git a/test/ssa_reader_test.cc b/test/ssa_reader_test.cc
index 30f8538..0fa326b 100644
--- a/test/ssa_reader_test.cc
+++ b/test/ssa_reader_test.cc
@@ -23,8 +23,10 @@
#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
@@ -243,3 +255,51 @@ 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 ();
+}