Fix warning.
[dcpomatic.git] / src / lib / active_text.cc
1 /*
2     Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "active_text.h"
22 #include "piece.h"
23 #include "text_content.h"
24 #include <boost/shared_ptr.hpp>
25 #include <boost/weak_ptr.hpp>
26
27 using std::list;
28 using std::pair;
29 using std::make_pair;
30 using boost::weak_ptr;
31 using boost::shared_ptr;
32 using boost::optional;
33
34 /** Get the subtitles that should be burnt into a given period.
35  *  @param period Period of interest.
36  *  @param always_burn_subtitles Always burn subtitles even if their content is not set to burn.
37  */
38 list<PlayerText>
39 ActiveText::get_burnt (DCPTimePeriod period, bool always_burn_subtitles) const
40 {
41         list<PlayerText> ps;
42
43         for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
44
45                 shared_ptr<Piece> piece = i->first.lock ();
46                 if (!piece) {
47                         continue;
48                 }
49
50                 if (!piece->content->subtitle->use() || (!always_burn_subtitles && !piece->content->subtitle->burn())) {
51                         /* Not burning this piece */
52                         continue;
53                 }
54
55                 BOOST_FOREACH (Period j, i->second) {
56                         DCPTimePeriod test (j.from, j.to.get_value_or(DCPTime::max()));
57                         optional<DCPTimePeriod> overlap = period.overlap (test);
58                         if (overlap && overlap->duration() > DCPTime(period.duration().get() / 2)) {
59                                 ps.push_back (j.subs);
60                         }
61                 }
62         }
63
64         return ps;
65 }
66
67 /** Remove subtitles that finish before a given time from our list.
68  *  @param time Time to remove before.
69  */
70 void
71 ActiveText::clear_before (DCPTime time)
72 {
73         Map updated;
74         for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
75                 list<Period> as;
76                 BOOST_FOREACH (Period j, i->second) {
77                         if (!j.to || j.to.get() >= time) {
78                                 as.push_back (j);
79                         }
80                 }
81                 if (!as.empty ()) {
82                         updated[i->first] = as;
83                 }
84         }
85         _data = updated;
86 }
87
88 /** Add a new subtitle with a from time.
89  *  @param piece Piece that the subtitle is from.
90  *  @param ps Subtitles.
91  *  @param from From time for these subtitles.
92  */
93 void
94 ActiveText::add_from (weak_ptr<Piece> piece, PlayerText ps, DCPTime from)
95 {
96         if (_data.find(piece) == _data.end()) {
97                 _data[piece] = list<Period>();
98         }
99         _data[piece].push_back (Period (ps, from));
100 }
101
102 /** Add the to time for the last subtitle added from a piece.
103  *  @param piece Piece that the subtitle is from.
104  *  @param to To time for the last subtitle submitted to add_from for this piece.
105  *  @return Return the corresponding subtitles and their from time.
106  */
107 pair<PlayerText, DCPTime>
108 ActiveText::add_to (weak_ptr<Piece> piece, DCPTime to)
109 {
110         DCPOMATIC_ASSERT (_data.find(piece) != _data.end());
111
112         _data[piece].back().to = to;
113
114         BOOST_FOREACH (PlainText& i, _data[piece].back().subs.text) {
115                 i.set_out (dcp::Time(to.seconds(), 1000));
116         }
117
118         return make_pair (_data[piece].back().subs, _data[piece].back().from);
119 }
120
121 /** @param piece A piece.
122  *  @return true if we have any active subtitles from this piece.
123  */
124 bool
125 ActiveText::have (weak_ptr<Piece> piece) const
126 {
127         Map::const_iterator i = _data.find(piece);
128         if (i == _data.end()) {
129                 return false;
130         }
131
132         return !i->second.empty();
133 }
134
135 void
136 ActiveText::clear ()
137 {
138         _data.clear ();
139 }