diff options
| author | Carl Hetherington <cth@carlh.net> | 2014-01-28 22:41:09 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2014-01-28 22:41:09 +0000 |
| commit | 75aa6e480d998b29205c0eab328697a5d007986b (patch) | |
| tree | ac53060d67ac2f6c64994e405220977c5ae5d817 | |
| parent | 7f20aa518356f188946eb508239caf7c113da819 (diff) | |
Basic writer.
| -rw-r--r-- | src/stl_reader.cc | 12 | ||||
| -rw-r--r-- | src/stl_writer.cc | 84 | ||||
| -rw-r--r-- | src/stl_writer.h | 31 | ||||
| -rw-r--r-- | src/sub_time.cc | 8 | ||||
| -rw-r--r-- | src/sub_time.h | 2 | ||||
| -rw-r--r-- | src/subtitle.h | 24 | ||||
| -rw-r--r-- | src/writer.h | 38 | ||||
| -rw-r--r-- | src/wscript | 3 | ||||
| -rw-r--r-- | test/ref/test.stl | 5 | ||||
| -rw-r--r-- | test/stl_reader_test.cc | 75 | ||||
| -rw-r--r-- | test/stl_writer_test.cc | 51 | ||||
| -rw-r--r-- | test/test.cc | 24 | ||||
| -rw-r--r-- | test/test.h | 22 | ||||
| -rw-r--r-- | test/wscript | 1 |
14 files changed, 350 insertions, 30 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) diff --git a/test/ref/test.stl b/test/ref/test.stl new file mode 100644 index 0000000..f65dd80 --- /dev/null +++ b/test/ref/test.stl @@ -0,0 +1,5 @@ + +$FontName = Arial +$FontSize = 42 +0:0:41:9,0:0:42:21, This is a subtitle | and that's a line break +0:1:1:1,0:1:2:10, This is some ^Bbold^B and some ^B^Ibold italic^B^I and some ^Uunderlined^U. diff --git a/test/stl_reader_test.cc b/test/stl_reader_test.cc index 14a6ada..92b0d58 100644 --- a/test/stl_reader_test.cc +++ b/test/stl_reader_test.cc @@ -35,83 +35,108 @@ BOOST_AUTO_TEST_CASE (stl_reader_test) list<sub::Subtitle>::iterator i = subs.begin (); BOOST_CHECK (i != subs.end ()); - BOOST_CHECK_EQUAL (i->from, sub::Time (0, 0, 41, 9)); - BOOST_CHECK_EQUAL (i->to, sub::Time (0, 0, 42, 21)); + BOOST_CHECK_EQUAL (i->text, " This is a subtitle "); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->bold, false); BOOST_CHECK_EQUAL (i->italic, false); BOOST_CHECK_EQUAL (i->underline, false); - BOOST_CHECK_EQUAL (i->text, " This is a subtitle "); + BOOST_CHECK_EQUAL (i->line, 0); + BOOST_CHECK_EQUAL (i->from, sub::Time (0, 0, 41, 9)); + BOOST_CHECK_EQUAL (i->to, sub::Time (0, 0, 42, 21)); ++i; BOOST_CHECK (i != subs.end ()); - BOOST_CHECK_EQUAL (i->from, sub::Time (0, 0, 41, 9)); - BOOST_CHECK_EQUAL (i->to, sub::Time (0, 0, 42, 21)); + BOOST_CHECK_EQUAL (i->text, " and that's a line break"); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->bold, false); BOOST_CHECK_EQUAL (i->italic, false); BOOST_CHECK_EQUAL (i->underline, false); - BOOST_CHECK_EQUAL (i->text, " and that's a line break"); + BOOST_CHECK_EQUAL (i->line, 1); + BOOST_CHECK_EQUAL (i->from, sub::Time (0, 0, 41, 9)); + BOOST_CHECK_EQUAL (i->to, sub::Time (0, 0, 42, 21)); ++i; BOOST_CHECK (i != subs.end ()); - BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); - BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); + BOOST_CHECK_EQUAL (i->text, " This is some "); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->bold, false); BOOST_CHECK_EQUAL (i->italic, false); BOOST_CHECK_EQUAL (i->underline, false); - BOOST_CHECK_EQUAL (i->text, " This is some "); + BOOST_CHECK_EQUAL (i->line, 0); + BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); + BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); ++i; BOOST_CHECK (i != subs.end ()); - BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); - BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); + BOOST_CHECK_EQUAL (i->text, "bold"); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->bold, true); BOOST_CHECK_EQUAL (i->italic, false); BOOST_CHECK_EQUAL (i->underline, false); - BOOST_CHECK_EQUAL (i->text, "bold"); + BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); + BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); ++i; BOOST_CHECK (i != subs.end ()); + BOOST_CHECK_EQUAL (i->text, " and some "); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); BOOST_CHECK_EQUAL (i->bold, false); BOOST_CHECK_EQUAL (i->italic, false); BOOST_CHECK_EQUAL (i->underline, false); - BOOST_CHECK_EQUAL (i->text, " and some "); + BOOST_CHECK_EQUAL (i->line, 0); ++i; BOOST_CHECK (i != subs.end ()); - BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); - BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); + BOOST_CHECK_EQUAL (i->text, "bold italic"); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->bold, true); BOOST_CHECK_EQUAL (i->italic, true); BOOST_CHECK_EQUAL (i->underline, false); - BOOST_CHECK_EQUAL (i->text, "bold italic"); + BOOST_CHECK_EQUAL (i->line, 0); + BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); + BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); ++i; BOOST_CHECK (i != subs.end ()); - BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); - BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); + BOOST_CHECK_EQUAL (i->text, " and some "); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->bold, false); BOOST_CHECK_EQUAL (i->italic, false); BOOST_CHECK_EQUAL (i->underline, false); - BOOST_CHECK_EQUAL (i->text, " and some "); + BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); + BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); ++i; BOOST_CHECK (i != subs.end ()); - BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); - BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); + BOOST_CHECK_EQUAL (i->text, "underlined"); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->bold, false); BOOST_CHECK_EQUAL (i->italic, false); BOOST_CHECK_EQUAL (i->underline, true); - BOOST_CHECK_EQUAL (i->text, "underlined"); - ++i; - + BOOST_CHECK_EQUAL (i->line, 0); BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); + ++i; + + BOOST_CHECK_EQUAL (i->text, "."); + BOOST_CHECK_EQUAL (i->font, "Arial"); + BOOST_CHECK_EQUAL (i->font_size, 42); BOOST_CHECK_EQUAL (i->bold, false); BOOST_CHECK_EQUAL (i->italic, false); BOOST_CHECK_EQUAL (i->underline, false); - BOOST_CHECK_EQUAL (i->text, "."); + BOOST_CHECK_EQUAL (i->line, 0); + BOOST_CHECK_EQUAL (i->from, sub::Time (0, 1, 1, 1)); + BOOST_CHECK_EQUAL (i->to, sub::Time (0, 1, 2, 10)); ++i; BOOST_CHECK (i == subs.end ()); diff --git a/test/stl_writer_test.cc b/test/stl_writer_test.cc new file mode 100644 index 0000000..91b03b4 --- /dev/null +++ b/test/stl_writer_test.cc @@ -0,0 +1,51 @@ +/* + 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 <boost/test/unit_test.hpp> +#include <fstream> +#include "stl_writer.h" +#include "subtitle.h" +#include "test.h" + +using std::list; +using std::ifstream; +using std::ofstream; + +/* Test writing of an STL file */ +BOOST_AUTO_TEST_CASE (stl_writer_test) +{ + list<sub::Subtitle> subs; + subs.push_back (sub::Subtitle (" This is a subtitle ", "Arial", 42, false, false, false, 0, sub::Time (0, 0, 41, 9), sub::Time (0, 0, 42, 21))); + subs.push_back (sub::Subtitle (" and that's a line break", "Arial", 42, false, false, false, 1, sub::Time (0, 0, 41, 9), sub::Time (0, 0, 42, 21))); + subs.push_back (sub::Subtitle (" This is some ", "Arial", 42, false, false, false, 0, sub::Time (0, 1, 1, 1), sub::Time (0, 1, 2, 10))); + subs.push_back (sub::Subtitle ("bold", "Arial", 42, true, false, false, 0, sub::Time (0, 1, 1, 1), sub::Time (0, 1, 2, 10))); + subs.push_back (sub::Subtitle (" and some ", "Arial", 42, false, false, false, 0, sub::Time (0, 1, 1, 1), sub::Time (0, 1, 2, 10))); + subs.push_back (sub::Subtitle ("bold italic", "Arial", 42, true, true, false, 0, sub::Time (0, 1, 1, 1), sub::Time (0, 1, 2, 10))); + subs.push_back (sub::Subtitle (" and some ", "Arial", 42, false, false, false, 0, sub::Time (0, 1, 1, 1), sub::Time (0, 1, 2, 10))); + subs.push_back (sub::Subtitle ("underlined", "Arial", 42, false, false, true, 0, sub::Time (0, 1, 1, 1), sub::Time (0, 1, 2, 10))); + subs.push_back (sub::Subtitle (".", "Arial", 42, false, false, false, 0, sub::Time (0, 1, 1, 1), sub::Time (0, 1, 2, 10))); + + ofstream f ("build/test/test.stl"); + sub::STLWriter writer (subs, f); + f.close (); + + check_text ("test/ref/test.stl", "build/test/test.stl"); +} + + diff --git a/test/test.cc b/test/test.cc index eb6eecf..3556fa5 100644 --- a/test/test.cc +++ b/test/test.cc @@ -20,3 +20,27 @@ #define BOOST_TEST_DYN_LINK #define BOOST_TEST_MODULE libsub_test #include <boost/test/unit_test.hpp> +#include <fstream> +#include <string> + +using std::string; +using std::ifstream; +using std::getline; + +void +check_text (string a, string b) +{ + ifstream p (a.c_str ()); + ifstream q (b.c_str ()); + + string x; + string y; + while (p.good() && q.good()) { + getline (p, x); + getline (q, y); + BOOST_CHECK_EQUAL (x, y); + } + + BOOST_CHECK (p.good() == false); + BOOST_CHECK (q.good() == false); +} diff --git a/test/test.h b/test/test.h new file mode 100644 index 0000000..a898694 --- /dev/null +++ b/test/test.h @@ -0,0 +1,22 @@ +/* + 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 <string> + +void check_text (std::string a, std::string b); diff --git a/test/wscript b/test/wscript index 5d16abb..8de63f8 100644 --- a/test/wscript +++ b/test/wscript @@ -17,6 +17,7 @@ def build(bld): obj.use = 'libsub' obj.source = """ stl_reader_test.cc + stl_writer_test.cc test.cc """ obj.target = 'tests' |
