summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2016-02-15 08:39:50 +0000
committerCarl Hetherington <cth@carlh.net>2016-02-15 23:18:23 +0000
commit02a2f97a593b0735af02532d206b34f9988c734d (patch)
tree75eab9f12e4da430c3d7c9121b329e73fb1eb26c /src
parentd79b29fd5130b3b9fdfb59689a3162b93f439cca (diff)
Basic support for parsing in-line markup in SSA.
Diffstat (limited to 'src')
-rw-r--r--src/ssa_reader.cc63
-rw-r--r--src/ssa_reader.h2
2 files changed, 65 insertions, 0 deletions
diff --git a/src/ssa_reader.cc b/src/ssa_reader.cc
index d4908a2..703ed26 100644
--- a/src/ssa_reader.cc
+++ b/src/ssa_reader.cc
@@ -34,6 +34,7 @@ using std::stringstream;
using std::vector;
using std::map;
using std::cout;
+using std::list;
using boost::optional;
using boost::function;
using namespace boost::algorithm;
@@ -137,6 +138,68 @@ SSAReader::parse_time (string t) const
);
}
+list<RawSubtitle>
+SSAReader::parse_line (RawSubtitle base, string line)
+{
+ enum {
+ TEXT,
+ STYLE,
+ BACKSLASH
+ } state = TEXT;
+
+ list<RawSubtitle> subs;
+ RawSubtitle current = base;
+ string style;
+
+ current.vertical_position.line = 0;
+
+ for (size_t i = 0; i < line.length(); ++i) {
+ char const c = line[i];
+ switch (state) {
+ case TEXT:
+ if (c == '{') {
+ state = STYLE;
+ } else if (c == '\\') {
+ state = BACKSLASH;
+ } else {
+ current.text += c;
+ }
+ break;
+ case STYLE:
+ if (c == '}') {
+ if (!current.text.empty ()) {
+ subs.push_back (current);
+ current.text = "";
+ }
+ if (style == "i1") {
+ current.italic = true;
+ } else if (style == "i0") {
+ current.italic = false;
+ }
+ style = "";
+ state = TEXT;
+ } else {
+ style += c;
+ }
+ break;
+ case BACKSLASH:
+ if (c == 'n' && !current.text.empty ()) {
+ subs.push_back (current);
+ current.text = "";
+ current.vertical_position.line = current.vertical_position.line.get() + 1;
+ }
+ state = TEXT;
+ break;
+ }
+ }
+
+ if (!current.text.empty ()) {
+ subs.push_back (current);
+ }
+
+ return subs;
+}
+
void
SSAReader::read (function<optional<string> ()> get_line)
{
diff --git a/src/ssa_reader.h b/src/ssa_reader.h
index 0f575ff..1fe64f3 100644
--- a/src/ssa_reader.h
+++ b/src/ssa_reader.h
@@ -35,6 +35,8 @@ public:
SSAReader (FILE* f);
SSAReader (std::string const & subs);
+ static std::list<RawSubtitle> parse_line (RawSubtitle base, std::string line);
+
private:
void read (boost::function<boost::optional<std::string> ()> get_line);
Time parse_time (std::string t) const;