Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / subrip_decoder.cc
index cdc8ccbfe4613f9a745966121101be82fb640a1c..2de27248111eb17eaddd8f93f2afcf095522224d 100644 (file)
 
 #include <dcp/subtitle_string.h>
 #include "subrip_decoder.h"
+#include "subrip_content.h"
+#include <iostream>
 
 using std::list;
+using std::vector;
+using std::string;
+using std::cout;
 using boost::shared_ptr;
+using boost::optional;
 
 SubRipDecoder::SubRipDecoder (shared_ptr<const SubRipContent> content)
-       : SubRip (content)
+       : SubtitleDecoder (content)
+       , SubRip (content)
        , _next (0)
 {
 
@@ -34,13 +41,11 @@ void
 SubRipDecoder::seek (ContentTime time, bool accurate)
 {
        SubtitleDecoder::seek (time, accurate);
-       
+
        _next = 0;
-       list<SubRipSubtitlePiece>::const_iterator i = _subtitles[_next].pieces.begin();
-       while (i != _subtitles[_next].pieces.end() && _subtitles[_next].from < time) {
-               ++i;
+       while (_next < _subtitles.size() && ContentTime::from_seconds (_subtitles[_next].from.all_as_seconds ()) < time) {
+               ++_next;
        }
-       
 }
 
 bool
@@ -49,29 +54,69 @@ SubRipDecoder::pass ()
        if (_next >= _subtitles.size ()) {
                return true;
        }
-       
+
+       /* XXX: we are ignoring positioning specified in the file */
+
        list<dcp::SubtitleString> out;
-       for (list<SubRipSubtitlePiece>::const_iterator i = _subtitles[_next].pieces.begin(); i != _subtitles[_next].pieces.end(); ++i) {
-               out.push_back (
-                       dcp::SubtitleString (
-                               "Arial",
-                               i->italic,
-                               dcp::Color (255, 255, 255),
-                               72,
-                               dcp::Time (rint (_subtitles[_next].from.seconds() * 250)),
-                               dcp::Time (rint (_subtitles[_next].to.seconds() * 250)),
-                               0.9,
-                               dcp::BOTTOM,
-                               i->text,
-                               dcp::NONE,
-                               dcp::Color (255, 255, 255),
-                               0,
-                               0
-                               )
-                       );
+       for (list<sub::Line>::const_iterator i = _subtitles[_next].lines.begin(); i != _subtitles[_next].lines.end(); ++i) {
+               for (list<sub::Block>::const_iterator j = i->blocks.begin(); j != i->blocks.end(); ++j) {
+                       out.push_back (
+                               dcp::SubtitleString (
+                                       SubRipContent::font_id,
+                                       j->italic,
+                                       dcp::Colour (j->colour.r * 255, j->colour.g * 255, j->colour.b * 255),
+                                       j->font_size.points (72 * 11),
+                                       1.0,
+                                       dcp::Time (_subtitles[_next].from.all_as_seconds()),
+                                       dcp::Time (_subtitles[_next].to.all_as_seconds()),
+                                       0,
+                                       dcp::HALIGN_CENTER,
+                                       i->vertical_position.line.get() * (1.5 / 22) + 0.8,
+                                       dcp::VALIGN_TOP,
+                                       j->text,
+                                       dcp::NONE,
+                                       dcp::Colour (255, 255, 255),
+                                       0,
+                                       0
+                                       )
+                               );
+               }
        }
 
-       text_subtitle (out);
-       _next++;
+       text_subtitle (content_time_period (_subtitles[_next]), out);
+
+       ++_next;
        return false;
 }
+
+list<ContentTimePeriod>
+SubRipDecoder::image_subtitles_during (ContentTimePeriod, bool) const
+{
+       return list<ContentTimePeriod> ();
+}
+
+list<ContentTimePeriod>
+SubRipDecoder::text_subtitles_during (ContentTimePeriod p, bool starting) const
+{
+       /* XXX: inefficient */
+
+       list<ContentTimePeriod> d;
+
+       for (vector<sub::Subtitle>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+               ContentTimePeriod t = content_time_period (*i);
+               if ((starting && p.contains (t.from)) || (!starting && p.overlaps (t))) {
+                       d.push_back (t);
+               }
+       }
+
+       return d;
+}
+
+ContentTimePeriod
+SubRipDecoder::content_time_period (sub::Subtitle s) const
+{
+       return ContentTimePeriod (
+               ContentTime::from_seconds (s.from.all_as_seconds()),
+               ContentTime::from_seconds (s.to.all_as_seconds())
+               );
+}