summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2014-01-28 22:41:09 +0000
committerCarl Hetherington <cth@carlh.net>2014-01-28 22:41:09 +0000
commit75aa6e480d998b29205c0eab328697a5d007986b (patch)
treeac53060d67ac2f6c64994e405220977c5ae5d817 /src
parent7f20aa518356f188946eb508239caf7c113da819 (diff)
Basic writer.
Diffstat (limited to 'src')
-rw-r--r--src/stl_reader.cc12
-rw-r--r--src/stl_writer.cc84
-rw-r--r--src/stl_writer.h31
-rw-r--r--src/sub_time.cc8
-rw-r--r--src/sub_time.h2
-rw-r--r--src/subtitle.h24
-rw-r--r--src/writer.h38
-rw-r--r--src/wscript3
8 files changed, 197 insertions, 5 deletions
diff --git a/src/stl_reader.cc b/src/stl_reader.cc
index c86d607..f40ba77 100644
--- a/src/stl_reader.cc
+++ b/src/stl_reader.cc
@@ -99,6 +99,7 @@ STLReader::STLReader (istream& in)
for (size_t i = 0; i < text.length(); ++i) {
if (text[i] == '|') {
maybe_push ();
+ _current.line++;
} else if (text[i] == '^') {
maybe_push ();
if ((i + 1) < text.length()) {
@@ -121,6 +122,7 @@ STLReader::STLReader (istream& in)
}
maybe_push ();
+ _current.line = 0;
}
}
}
@@ -141,15 +143,15 @@ STLReader::time (string t) const
void
STLReader::set (string name, string value)
{
- if (name == "FontName") {
+ if (name == "$FontName") {
_current.font = value;
- } else if (name == "Bold") {
+ } else if (name == "$Bold") {
_current.bold = value == "True";
- } else if (name == "Italic") {
+ } else if (name == "$Italic") {
_current.italic = value == "True";
- } else if (name == "Underlined") {
+ } else if (name == "$Underlined") {
_current.underline = value == "True";
- } else if (name == "FontSize") {
+ } else if (name == "$FontSize") {
_current.font_size = lexical_cast<int> (value);
}
}
diff --git a/src/stl_writer.cc b/src/stl_writer.cc
new file mode 100644
index 0000000..36c89e5
--- /dev/null
+++ b/src/stl_writer.cc
@@ -0,0 +1,84 @@
+/*
+ Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "stl_writer.h"
+#include <boost/optional.hpp>
+
+using std::list;
+using std::ostream;
+using std::string;
+using boost::optional;
+using namespace sub;
+
+STLWriter::STLWriter (list<Subtitle> subtitles, ostream& out)
+ : Writer (subtitles)
+{
+ optional<string> font;
+ optional<int> font_size;
+ bool bold = false;
+ bool italic = false;
+ bool underline = false;
+ int line = 0;
+ optional<Time> from;
+ optional<Time> to;
+
+ for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
+ bool started_new = false;
+ if (!font || font.get() != i->font) {
+ out << "\n$FontName = " << i->font;
+ font = i->font;
+ started_new = true;
+ }
+ if (!font_size || font_size.get() != i->font_size) {
+ out << "\n$FontSize = " << i->font_size;
+ font_size = i->font_size;
+ started_new = true;
+ }
+ string text;
+ if (bold != i->bold) {
+ text += "^B";
+ bold = i->bold;
+ }
+ if (italic != i->italic) {
+ text += "^I";
+ italic = i->italic;
+ }
+ if (underline != i->underline) {
+ text += "^U";
+ underline = i->underline;
+ }
+
+ text += i->text;
+
+ if (from && from.get() == i->from && to && to.get() == i->to && !started_new) {
+ for (int j = line; j < i->line; ++j) {
+ out << "|";
+ }
+ out << text;
+ line = i->line;
+ } else {
+ out << "\n" << i->from.timecode() << "," << i->to.timecode() << "," << text;
+ from = i->from;
+ to = i->to;
+ line = 0;
+ }
+ }
+
+ out << "\n";
+}
diff --git a/src/stl_writer.h b/src/stl_writer.h
new file mode 100644
index 0000000..712aaa5
--- /dev/null
+++ b/src/stl_writer.h
@@ -0,0 +1,31 @@
+/*
+ Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "writer.h"
+#include "subtitle.h"
+
+namespace sub {
+
+class STLWriter : public Writer
+{
+public:
+ STLWriter (std::list<Subtitle> subtitles, std::ostream &);
+};
+
+}
diff --git a/src/sub_time.cc b/src/sub_time.cc
index 6de2456..a8e3a31 100644
--- a/src/sub_time.cc
+++ b/src/sub_time.cc
@@ -18,9 +18,11 @@
*/
#include "sub_time.h"
+#include "compose.hpp"
#include <iostream>
using std::ostream;
+using std::string;
using namespace sub;
bool
@@ -35,3 +37,9 @@ sub::operator<< (ostream& s, Time const & t)
s << t._hours << ":" << t._minutes << ":" << t._seconds << ":" << t._frames;
return s;
}
+
+string
+Time::timecode () const
+{
+ return String::compose ("%1:%2:%3:%4", _hours, _minutes, _seconds, _frames);
+}
diff --git a/src/sub_time.h b/src/sub_time.h
index ee14745..5596970 100644
--- a/src/sub_time.h
+++ b/src/sub_time.h
@@ -41,6 +41,8 @@ public:
, _frames (f)
{}
+ std::string timecode () const;
+
private:
friend bool operator== (Time const & a, Time const & b);
friend std::ostream& operator<< (std::ostream& s, Time const & t);
diff --git a/src/subtitle.h b/src/subtitle.h
index 5cc1284..d12dfc1 100644
--- a/src/subtitle.h
+++ b/src/subtitle.h
@@ -33,6 +33,29 @@ public:
, bold (false)
, italic (false)
, underline (false)
+ , line (0)
+ {}
+
+ Subtitle (
+ std::string text,
+ std::string font,
+ int font_size,
+ bool bold,
+ bool italic,
+ bool underline,
+ int line,
+ Time from,
+ Time to
+ )
+ : text (text)
+ , font (font)
+ , font_size (font_size)
+ , bold (bold)
+ , italic (italic)
+ , underline (underline)
+ , line (line)
+ , from (from)
+ , to (to)
{}
std::string text;
@@ -41,6 +64,7 @@ public:
bool bold;
bool italic;
bool underline;
+ int line;
Time from;
Time to;
};
diff --git a/src/writer.h b/src/writer.h
new file mode 100644
index 0000000..274c455
--- /dev/null
+++ b/src/writer.h
@@ -0,0 +1,38 @@
+/*
+ Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <list>
+#include <iostream>
+
+namespace sub {
+
+class Subtitle;
+
+class Writer
+{
+public:
+ Writer (std::list<Subtitle> subtitles)
+ : _subs (subtitles)
+ {}
+
+protected:
+ std::list<Subtitle> _subs;
+};
+
+}
diff --git a/src/wscript b/src/wscript
index b7b53b0..7f00d20 100644
--- a/src/wscript
+++ b/src/wscript
@@ -12,13 +12,16 @@ def build(bld):
obj.source = """
reader.cc
stl_reader.cc
+ stl_writer.cc
sub_time.cc
"""
headers = """
reader.h
stl_reader.h
+ stl_writer.h
sub_time.h
+ writer.h
"""
bld.install_files('${PREFIX}/include/libsub', headers)