diff options
| author | Carl Hetherington <cth@carlh.net> | 2015-01-09 09:18:23 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2015-01-09 09:18:23 +0000 |
| commit | 94399f35aea4e37c699918f06eddfa931e4aaefc (patch) | |
| tree | 3cfedd40361b563cbfeaeede1c362f7c91531cc7 | |
| parent | c73bf7010a07aa00dc79d45662b2da1494c8ffb0 (diff) | |
| parent | d65ae002711e5c82ea4e382b577b2314944e1dbd (diff) | |
Merge branch 'master' of git.carlh.net:git/libsub
| -rw-r--r-- | src/stl_binary_reader.cc | 1 | ||||
| -rw-r--r-- | src/stl_binary_writer.cc | 14 | ||||
| -rw-r--r-- | src/stl_text_reader.cc | 2 | ||||
| -rw-r--r-- | src/subrip_reader.cc | 2 | ||||
| -rw-r--r-- | src/vertical_position.cc | 35 | ||||
| -rw-r--r-- | src/vertical_position.h | 6 | ||||
| -rw-r--r-- | test/stl_binary_writer_test.cc | 3 | ||||
| -rw-r--r-- | test/vertical_position_test.cc | 95 | ||||
| -rw-r--r-- | test/wscript | 1 | ||||
| -rw-r--r-- | tools/dumpsubs.cc | 2 |
10 files changed, 150 insertions, 11 deletions
diff --git a/src/stl_binary_reader.cc b/src/stl_binary_reader.cc index ff3d9a0..2246a01 100644 --- a/src/stl_binary_reader.cc +++ b/src/stl_binary_reader.cc @@ -98,6 +98,7 @@ STLBinaryReader::STLBinaryReader (istream& in) sub.from.set_frame (get_timecode (5)); sub.to.set_frame (get_timecode (9)); sub.vertical_position.line = get_int (13, 1) + i; + sub.vertical_position.lines = maximum_rows; sub.vertical_position.reference = TOP_OF_SCREEN; string text; diff --git a/src/stl_binary_writer.cc b/src/stl_binary_writer.cc index aa9a032..b9ad86f 100644 --- a/src/stl_binary_writer.cc +++ b/src/stl_binary_writer.cc @@ -192,8 +192,8 @@ sub::write_stl_binary ( output.write (buffer, 1024); - int N = 0; for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) { + int N = 0; for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) { memset (buffer, 0, 1024); @@ -235,18 +235,16 @@ sub::write_stl_binary ( break; } } else if (j->vertical_position.line) { - /* XXX: how many lines are there...? We need to correct for any difference - between the number of lines and our ROWS. - */ + float const prop = float (j->vertical_position.line.get()) / j->vertical_position.lines.get (); switch (j->vertical_position.reference.get_value_or (TOP_OF_SCREEN)) { case TOP_OF_SCREEN: - vp = j->vertical_position.line.get(); + vp = prop * ROWS; break; case CENTRE_OF_SCREEN: - vp = j->vertical_position.line.get() + (ROWS / 2); + vp = (prop + 0.5) * ROWS; break; case BOTTOM_OF_SCREEN: - vp = ROWS - (j->vertical_position.line.get()); + vp = (1 - prop) * ROWS; break; default: break; @@ -295,6 +293,8 @@ sub::write_stl_binary ( put_string (buffer + 16, text); output.write (buffer, 128); + + ++N; } } diff --git a/src/stl_text_reader.cc b/src/stl_text_reader.cc index 3c60e41..b86a656 100644 --- a/src/stl_text_reader.cc +++ b/src/stl_text_reader.cc @@ -39,6 +39,8 @@ using namespace sub; STLTextReader::STLTextReader (istream& in) { _subtitle.vertical_position.line = 0; + /* XXX: no idea what this should be */ + _subtitle.vertical_position.lines = 32; _subtitle.vertical_position.reference = TOP_OF_SCREEN; while (in.good ()) { diff --git a/src/subrip_reader.cc b/src/subrip_reader.cc index 8bd2005..0aba120 100644 --- a/src/subrip_reader.cc +++ b/src/subrip_reader.cc @@ -132,6 +132,8 @@ SubripReader::convert_line (string t, int line_number, TimePair from, TimePair t p.from = from; p.to = to; p.vertical_position.line = line_number; + /* XXX: arbitrary */ + p.vertical_position.lines = 32; p.vertical_position.reference = TOP_OF_SUBTITLE; /* XXX: missing <font> support */ diff --git a/src/vertical_position.cc b/src/vertical_position.cc index 1fdb1e1..37930d0 100644 --- a/src/vertical_position.cc +++ b/src/vertical_position.cc @@ -21,14 +21,45 @@ using namespace sub; +float +VerticalPosition::fraction_from_screen_top () const +{ + if (!reference || (!proportional && !line)) { + return 0; + } + + float const prop = proportional ? proportional.get() : (float (line.get()) / lines.get ()); + + switch (reference.get ()) { + case TOP_OF_SCREEN: + return prop; + case CENTRE_OF_SCREEN: + return prop + 0.5; + case BOTTOM_OF_SCREEN: + return 1 - prop; + case TOP_OF_SUBTITLE: + return prop; + } + + return 0; +} + bool VerticalPosition::operator== (VerticalPosition const & other) const { if (proportional && reference && other.proportional && other.reference) { return proportional.get() == other.proportional.get() && reference.get() == other.reference.get(); - } else if (reference && line && other.reference && other.line) { - return line.get() == other.line.get() && reference.get() == other.reference.get(); + } else if (reference && line && lines && other.reference && other.line && other.lines) { + return line.get() == other.line.get() && lines.get() == other.lines.get() && reference.get() == other.reference.get(); } return false; } + +bool +VerticalPosition::operator< (VerticalPosition const & other) const +{ + return fraction_from_screen_top() < other.fraction_from_screen_top(); +} + + diff --git a/src/vertical_position.h b/src/vertical_position.h index 1d0b939..9f04056 100644 --- a/src/vertical_position.h +++ b/src/vertical_position.h @@ -35,11 +35,15 @@ public: boost::optional<float> proportional; /** line number offset from some reference point */ boost::optional<int> line; + /** number of lines on the whole screen (i.e. height of the screen in lines) */ + boost::optional<int> lines; /** reference point */ boost::optional<VerticalReference> reference; bool operator== (VerticalPosition const & other) const; - + bool operator< (VerticalPosition const & other) const; + + float fraction_from_screen_top () const; }; } diff --git a/test/stl_binary_writer_test.cc b/test/stl_binary_writer_test.cc index 90142a7..9bc3ec7 100644 --- a/test/stl_binary_writer_test.cc +++ b/test/stl_binary_writer_test.cc @@ -40,6 +40,7 @@ BOOST_AUTO_TEST_CASE (stl_binary_writer_test) b.font_size.set_points (42); sub::Line l; l.vertical_position.line = 0; + l.vertical_position.lines = 32; l.vertical_position.reference = sub::TOP_OF_SCREEN; l.blocks.push_back (b); s.lines.push_back (l); @@ -52,6 +53,7 @@ BOOST_AUTO_TEST_CASE (stl_binary_writer_test) b.font_size.set_points (42); sub::Line l; l.vertical_position.line = 1; + l.vertical_position.lines = 32; l.vertical_position.reference = sub::TOP_OF_SCREEN; l.blocks.push_back (b); s.lines.push_back (l); @@ -67,6 +69,7 @@ BOOST_AUTO_TEST_CASE (stl_binary_writer_test) sub::Line l; l.vertical_position.line = 0; + l.vertical_position.lines = 32; l.vertical_position.reference = sub::TOP_OF_SCREEN; sub::Block b; diff --git a/test/vertical_position_test.cc b/test/vertical_position_test.cc new file mode 100644 index 0000000..3204113 --- /dev/null +++ b/test/vertical_position_test.cc @@ -0,0 +1,95 @@ +/* + Copyright (C) 2015 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 "vertical_position.h" +#include <boost/test/unit_test.hpp> + +using boost::optional; + +/* Test operator< for VerticalPosition */ +BOOST_AUTO_TEST_CASE (vertical_position_test) +{ + sub::VerticalPosition a; + sub::VerticalPosition b; + + /* Simple */ + + a.proportional = 0.3; + a.reference = sub::TOP_OF_SCREEN; + b.proportional = 0.4; + b.reference = sub::TOP_OF_SCREEN; + BOOST_CHECK (a < b); + + a.proportional = 0.5; + a.reference = sub::TOP_OF_SCREEN; + b.proportional = 0.1; + b.reference = sub::TOP_OF_SCREEN; + BOOST_CHECK (b < a); + + a.proportional = optional<float> (); + b.proportional = optional<float> (); + + a.line = 4; + a.lines = 25; + a.reference = sub::TOP_OF_SCREEN; + b.line = 6; + b.lines = 25; + b.reference = sub::TOP_OF_SCREEN; + BOOST_CHECK (a < b); + + a.line = 17; + a.lines = 25; + a.reference = sub::TOP_OF_SCREEN; + b.line = 2; + b.lines = 25; + b.reference = sub::TOP_OF_SCREEN; + BOOST_CHECK (b < a); + + /* Different reference points with proportional */ + + a.line = optional<int> (); + a.lines = optional<int> (); + b.line = optional<int> (); + b.lines = optional<int> (); + + a.proportional = 0.7; + a.reference = sub::TOP_OF_SCREEN; + b.proportional = 0.3; + b.reference = sub::CENTRE_OF_SCREEN; + BOOST_CHECK (a < b); + + a.proportional = 0.9; + a.reference = sub::BOTTOM_OF_SCREEN; + b.proportional = 0.1; + b.reference = sub::TOP_OF_SCREEN; + BOOST_CHECK (b < a); + + /* Different line counts with lines */ + + a.proportional = optional<float> (); + b.proportional = optional<float> (); + + a.line = 12; + a.lines = 99; + a.reference = sub::TOP_OF_SCREEN; + b.line = 12; + b.lines = 25; + b.reference = sub::TOP_OF_SCREEN; + BOOST_CHECK (a < b); +} diff --git a/test/wscript b/test/wscript index 2c8c327..286f62f 100644 --- a/test/wscript +++ b/test/wscript @@ -29,6 +29,7 @@ def build(bld): subrip_reader_test.cc time_test.cc test.cc + vertical_position_test.cc """ obj.target = 'tests' obj.install_path = '' diff --git a/tools/dumpsubs.cc b/tools/dumpsubs.cc index d8fb0c2..fef3885 100644 --- a/tools/dumpsubs.cc +++ b/tools/dumpsubs.cc @@ -93,7 +93,7 @@ main (int argc, char* argv[]) if (j->vertical_position.proportional) { cout << j->vertical_position.proportional.get() << " of screen"; } else if (j->vertical_position.line) { - cout << j->vertical_position.line.get() << " lines"; + cout << j->vertical_position.line.get() << " lines of " << j->vertical_position.lines.get(); } if (j->vertical_position.reference) { cout << " from "; |
