Rename some methods.
[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 "subtitle_content.h"
23 #include <dcp/subtitle_string.h>
24 #include <boost/foreach.hpp>
25 #include <iostream>
26
27 using std::list;
28 using std::vector;
29 using std::string;
30 using std::cout;
31 using std::max;
32 using boost::shared_ptr;
33 using boost::optional;
34 using boost::dynamic_pointer_cast;
35
36 TextSubtitleDecoder::TextSubtitleDecoder (shared_ptr<const TextSubtitleContent> content)
37         : TextSubtitle (content)
38         , _next (0)
39 {
40         subtitle.reset (
41                 new SubtitleDecoder (
42                         this,
43                         content->subtitle,
44                         bind (&TextSubtitleDecoder::image_subtitles_during, this, _1, _2),
45                         bind (&TextSubtitleDecoder::text_subtitles_during, this, _1, _2)
46                         )
47                 );
48 }
49
50 void
51 TextSubtitleDecoder::seek (ContentTime time, bool accurate)
52 {
53         subtitle->seek (time, accurate);
54
55         _next = 0;
56         while (_next < _subtitles.size() && ContentTime::from_seconds (_subtitles[_next].from.all_as_seconds ()) < time) {
57                 ++_next;
58         }
59 }
60
61 bool
62 TextSubtitleDecoder::pass (PassReason, bool)
63 {
64         if (_next >= _subtitles.size ()) {
65                 return true;
66         }
67
68         /* XXX: we are ignoring positioning specified in the file */
69
70         list<dcp::SubtitleString> out;
71
72         /* Highest line index in this subtitle */
73         int highest = 0;
74         BOOST_FOREACH (sub::Line i, _subtitles[_next].lines) {
75                 DCPOMATIC_ASSERT (i.vertical_position.reference && i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE);
76                 DCPOMATIC_ASSERT (i.vertical_position.line);
77                 highest = max (highest, i.vertical_position.line.get());
78         }
79
80         BOOST_FOREACH (sub::Line i, _subtitles[_next].lines) {
81                 BOOST_FOREACH (sub::Block j, i.blocks) {
82                         out.push_back (
83                                 dcp::SubtitleString (
84                                         TextSubtitleContent::font_id,
85                                         j.italic,
86                                         j.bold,
87                                         /* force the colour to whatever is configured */
88                                         subtitle->content()->colour(),
89                                         j.font_size.points (72 * 11),
90                                         1.0,
91                                         dcp::Time (_subtitles[_next].from.all_as_seconds(), 1000),
92                                         dcp::Time (_subtitles[_next].to.all_as_seconds(), 1000),
93                                         0,
94                                         dcp::HALIGN_CENTER,
95                                         /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
96                                            of the screen a bit to a pleasing degree.
97                                         */
98                                         1.015 - ((1 + highest - i.vertical_position.line.get()) * 1.5 / 22),
99                                         dcp::VALIGN_TOP,
100                                         dcp::DIRECTION_LTR,
101                                         j.text,
102                                         subtitle->content()->outline() ? dcp::BORDER : dcp::NONE,
103                                         subtitle->content()->outline_colour(),
104                                         dcp::Time (0, 1000),
105                                         dcp::Time (0, 1000)
106                                         )
107                                 );
108                 }
109         }
110
111         subtitle->give_text (content_time_period (_subtitles[_next]), out);
112
113         ++_next;
114         return false;
115 }
116
117 list<ContentTimePeriod>
118 TextSubtitleDecoder::image_subtitles_during (ContentTimePeriod, bool) const
119 {
120         return list<ContentTimePeriod> ();
121 }
122
123 list<ContentTimePeriod>
124 TextSubtitleDecoder::text_subtitles_during (ContentTimePeriod p, bool starting) const
125 {
126         /* XXX: inefficient */
127
128         list<ContentTimePeriod> d;
129
130         for (vector<sub::Subtitle>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
131                 ContentTimePeriod t = content_time_period (*i);
132                 if ((starting && p.contains (t.from)) || (!starting && p.overlaps (t))) {
133                         d.push_back (t);
134                 }
135         }
136
137         return d;
138 }
139
140 ContentTimePeriod
141 TextSubtitleDecoder::content_time_period (sub::Subtitle s) const
142 {
143         return ContentTimePeriod (
144                 ContentTime::from_seconds (s.from.all_as_seconds()),
145                 ContentTime::from_seconds (s.to.all_as_seconds())
146                 );
147 }