Subtitles at method.
authorCarl Hetherington <cth@carlh.net>
Mon, 13 Aug 2012 01:14:00 +0000 (02:14 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 13 Aug 2012 01:14:00 +0000 (02:14 +0100)
src/dcp_time.cc
src/dcp_time.h
src/subtitle_asset.cc
src/subtitle_asset.h
test/tests.cc

index 2e79e74293fe6e61f8c84724a57e6bc64d912fd0..5e8bf801a02909445ee4174668263fb2c58a7a92 100644 (file)
@@ -28,6 +28,28 @@ libdcp::operator== (Time const & a, Time const & b)
        return (a.h == b.h && a.m == b.m && a.s == b.s && a.ms == b.ms);
 }
 
+bool
+libdcp::operator<= (Time const & a, Time const & b)
+{
+       if (a.h != b.h) {
+               return a.h <= b.h;
+       }
+
+       if (a.m != b.m) {
+               return a.m <= b.m;
+       }
+
+       if (a.s != b.s) {
+               return a.s <= b.s;
+       }
+
+       if (a.ms != b.ms) {
+               return a.ms <= b.ms;
+       }
+
+       return true;
+}
+
 ostream &
 libdcp::operator<< (ostream& s, Time const & t)
 {
index 4e91dc182a8b2d200a97c48d53dda7c0ad081a67..48560596dece8500e38654f6dafa29af0eb8d919 100644 (file)
@@ -40,6 +40,7 @@ public:
 };
 
 extern bool operator== (Time const & a, Time const & b);
+extern bool operator<= (Time const & a, Time const & b);
 extern std::ostream & operator<< (std::ostream & s, Time const & t);
 
 }
index ca27d6dab08a770f6a67a71fdd80b66f0725792b..220982138aed30906031f0ede3309673f8bb036d 100644 (file)
 
 #include "subtitle_asset.h"
 
+using namespace std;
+using namespace boost;
 using namespace libdcp;
 
-SubtitleAsset::SubtitleAsset (std::string directory, std::string xml)
+SubtitleAsset::SubtitleAsset (string directory, string xml)
        : Asset (directory, xml)
        , XMLFile (path().string(), "DCSubtitle")
 {
@@ -55,3 +57,28 @@ Text::Text (xmlpp::Node const * node)
        _text = content ();
        _v_position = float_attribute ("VPosition");
 }
+
+list<shared_ptr<Text> >
+SubtitleAsset::subtitles_at (Time t) const
+{
+       for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
+               list<shared_ptr<Text> > s = (*i)->subtitles_at (t);
+               if (!s.empty ()) {
+                       return s;
+               }
+       }
+
+       return list<shared_ptr<Text> > ();
+}
+
+list<shared_ptr<Text> >
+Font::subtitles_at (Time t) const
+{
+       for (list<shared_ptr<Subtitle> >::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+               if ((*i)->in() <= t && t <= (*i)->out()) {
+                       return (*i)->texts ();
+               }
+       }
+
+       return list<shared_ptr<Text> > ();
+}
index 02db6815b27a76bc9275d10b03656e98ed345640..6d584a0a775cfcd343b875aa68b26a15e13a44c0 100644 (file)
@@ -77,6 +77,8 @@ public:
                return _subtitles;
        }
 
+       std::list<boost::shared_ptr<Text> > subtitles_at (Time t) const;
+       
 private:
        std::list<boost::shared_ptr<Subtitle> > _subtitles;
 };
@@ -95,6 +97,8 @@ public:
                return _language;
        }
 
+       std::list<boost::shared_ptr<Text> > subtitles_at (Time t) const;
+
        std::list<boost::shared_ptr<Font> > const & fonts () const {
                return _fonts;
        }
index b8d9ef976d40a6597965119492e796c562318246..cb180ec0f5c60c2d7b2f7ac16a600d3cb7c5bdbf 100644 (file)
@@ -124,6 +124,9 @@ BOOST_AUTO_TEST_CASE (subtitles)
        BOOST_CHECK_EQUAL ((*i)->texts().size(), 1);
        BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 15);
        BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "And these are Roy Hattersley's jeans");
+
+       BOOST_CHECK_EQUAL (subs.subtitles_at (libdcp::Time (0, 0, 14, 042)).size(), 1);
+       BOOST_CHECK_EQUAL (subs.subtitles_at (libdcp::Time (0, 0, 14, 042)).front()->text(), "And these are Roy Hattersley's jeans");
 }