Basics of subtitle split.
[dcpomatic.git] / src / lib / text_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "text_subtitle_decoder.h"
21 #include "text_subtitle_content.h"
22 #include <dcp/subtitle_string.h>
23 #include <boost/foreach.hpp>
24 #include <iostream>
25
26 using std::list;
27 using std::vector;
28 using std::string;
29 using std::cout;
30 using std::max;
31 using boost::shared_ptr;
32 using boost::optional;
33 using boost::dynamic_pointer_cast;
34
35 TextSubtitleDecoder::TextSubtitleDecoder (shared_ptr<const TextSubtitleContent> content)
36         : SubtitleDecoder (content->subtitle)
37         , TextSubtitle (content)
38         , _next (0)
39 {
40
41 }
42
43 void
44 TextSubtitleDecoder::seek (ContentTime time, bool accurate)
45 {
46         SubtitleDecoder::seek (time, accurate);
47
48         _next = 0;
49         while (_next < _subtitles.size() && ContentTime::from_seconds (_subtitles[_next].from.all_as_seconds ()) < time) {
50                 ++_next;
51         }
52 }
53
54 bool
55 TextSubtitleDecoder::pass (PassReason, bool)
56 {
57         if (_next >= _subtitles.size ()) {
58                 return true;
59         }
60
61         /* XXX: we are ignoring positioning specified in the file */
62
63         shared_ptr<const TextSubtitleContent> content = dynamic_pointer_cast<const TextSubtitleContent> (_subtitle_content);
64         DCPOMATIC_ASSERT (content);
65
66         list<dcp::SubtitleString> out;
67
68         /* Highest line index in this subtitle */
69         int highest = 0;
70         BOOST_FOREACH (sub::Line i, _subtitles[_next].lines) {
71                 DCPOMATIC_ASSERT (i.vertical_position.reference && i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE);
72                 DCPOMATIC_ASSERT (i.vertical_position.line);
73                 highest = max (highest, i.vertical_position.line.get());
74         }
75
76         BOOST_FOREACH (sub::Line i, _subtitles[_next].lines) {
77                 BOOST_FOREACH (sub::Block j, i.blocks) {
78                         out.push_back (
79                                 dcp::SubtitleString (
80                                         TextSubtitleContent::font_id,
81                                         j.italic,
82                                         j.bold,
83                                         /* force the colour to whatever is configured */
84                                         content->colour(),
85                                         j.font_size.points (72 * 11),
86                                         1.0,
87                                         dcp::Time (_subtitles[_next].from.all_as_seconds(), 1000),
88                                         dcp::Time (_subtitles[_next].to.all_as_seconds(), 1000),
89                                         0,
90                                         dcp::HALIGN_CENTER,
91                                         /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
92                                            of the screen a bit to a pleasing degree.
93                                         */
94                                         1.015 - ((1 + highest - i.vertical_position.line.get()) * 1.5 / 22),
95                                         dcp::VALIGN_TOP,
96                                         dcp::DIRECTION_LTR,
97                                         j.text,
98                                         content->outline() ? dcp::BORDER : dcp::NONE,
99                                         content->outline_colour(),
100                                         dcp::Time (0, 1000),
101                                         dcp::Time (0, 1000)
102                                         )
103                                 );
104                 }
105         }
106
107         text_subtitle (content_time_period (_subtitles[_next]), out);
108
109         ++_next;
110         return false;
111 }
112
113 list<ContentTimePeriod>
114 TextSubtitleDecoder::image_subtitles_during (ContentTimePeriod, bool) const
115 {
116         return list<ContentTimePeriod> ();
117 }
118
119 list<ContentTimePeriod>
120 TextSubtitleDecoder::text_subtitles_during (ContentTimePeriod p, bool starting) const
121 {
122         /* XXX: inefficient */
123
124         list<ContentTimePeriod> d;
125
126         for (vector<sub::Subtitle>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
127                 ContentTimePeriod t = content_time_period (*i);
128                 if ((starting && p.contains (t.from)) || (!starting && p.overlaps (t))) {
129                         d.push_back (t);
130                 }
131         }
132
133         return d;
134 }
135
136 ContentTimePeriod
137 TextSubtitleDecoder::content_time_period (sub::Subtitle s) const
138 {
139         return ContentTimePeriod (
140                 ContentTime::from_seconds (s.from.all_as_seconds()),
141                 ContentTime::from_seconds (s.to.all_as_seconds())
142                 );
143 }