Fix overlapping burnt-in subtitles in some cases (#959).
[dcpomatic.git] / src / lib / dcpomatic_time.h
index e6ca493b2bb921e58fbe0fb5c7e7f3c23c9aaf4c..da903cf09316e0e34865e62a5443a7ff1ba5b64e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 #include "frame_rate_change.h"
 #include "dcpomatic_assert.h"
-#include <locked_sstream.h>
+#include <boost/optional.hpp>
 #include <stdint.h>
 #include <cmath>
 #include <ostream>
 #include <iomanip>
+#include <cstdio>
 
 class dcpomatic_round_up_test;
 
@@ -181,14 +182,9 @@ public:
                int f;
                split (r, h, m, s, f);
 
-               locked_stringstream o;
-               o.width (2);
-               o.fill ('0');
-               o << std::setw(2) << std::setfill('0') << h << ":"
-                 << std::setw(2) << std::setfill('0') << m << ":"
-                 << std::setw(2) << std::setfill('0') << s << ":"
-                 << std::setw(2) << std::setfill('0') << f;
-               return o.str ();
+               char buffer[128];
+               snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d:%02d", h, m, s, f);
+               return buffer;
        }
 
 
@@ -261,8 +257,15 @@ public:
                return TimePeriod<T> (from + o, to + o);
        }
 
-       bool overlaps (TimePeriod<T> const & other) const {
-               return (from < other.to && to > other.from);
+       boost::optional<TimePeriod<T> > overlap (TimePeriod<T> const & other) {
+               T const max_from = std::max (from, other.from);
+               T const min_to = std::min (to, other.to);
+
+               if (max_from >= min_to) {
+                       return boost::optional<TimePeriod<T> > ();
+               }
+
+               return TimePeriod<T> (max_from, min_to);
        }
 
        bool contains (T const & other) const {
@@ -272,6 +275,10 @@ public:
        bool operator== (TimePeriod<T> const & other) const {
                return from == other.from && to == other.to;
        }
+
+       bool operator!= (TimePeriod<T> const & other) const {
+               return !(*this == other);
+       }
 };
 
 typedef TimePeriod<ContentTime> ContentTimePeriod;
@@ -281,8 +288,8 @@ DCPTime min (DCPTime a, DCPTime b);
 DCPTime max (DCPTime a, DCPTime b);
 ContentTime min (ContentTime a, ContentTime b);
 ContentTime max (ContentTime a, ContentTime b);
-std::ostream& operator<< (std::ostream& s, ContentTime t);
-std::ostream& operator<< (std::ostream& s, DCPTime t);
-std::ostream& operator<< (std::ostream& s, DCPTimePeriod p);
+std::string to_string (ContentTime t);
+std::string to_string (DCPTime t);
+std::string to_string (DCPTimePeriod p);
 
 #endif