C++11 tidying.
[dcpomatic.git] / src / lib / dcp_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014-2021 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
22 #include "dcp_subtitle_decoder.h"
23 #include "dcp_subtitle_content.h"
24 #include <dcp/interop_subtitle_asset.h>
25 #include <dcp/load_font_node.h>
26 #include <iostream>
27
28
29 using std::cout;
30 using std::list;
31 using std::map;
32 using std::string;
33 using std::vector;
34 using std::shared_ptr;
35 using std::dynamic_pointer_cast;
36 using std::make_shared;
37 using boost::bind;
38 using namespace dcpomatic;
39
40
41 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const Film> film, shared_ptr<const DCPSubtitleContent> content)
42         : Decoder (film)
43 {
44         auto c = load (content->path(0));
45         c->fix_empty_font_ids ();
46         _subtitles = c->subtitles ();
47         _next = _subtitles.begin ();
48
49         ContentTime first;
50         if (_next != _subtitles.end()) {
51                 first = content_time_period(*_next).from;
52         }
53         text.push_back (make_shared<TextDecoder>(this, content->only_text(), first));
54
55         auto fm = c->font_data();
56         for (auto const& i: fm) {
57                 _fonts.push_back (FontData(i.first, i.second));
58         }
59
60         /* Add a default font for any LoadFont nodes in our file which we haven't yet found fonts for */
61         for (auto i: c->load_font_nodes()) {
62                 if (fm.find(i->id) == fm.end()) {
63                         _fonts.push_back (FontData(i->id, dcp::ArrayData(default_font_file())));
64                 }
65         }
66 }
67
68
69 void
70 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
71 {
72         Decoder::seek (time, accurate);
73
74         _next = _subtitles.begin ();
75         auto i = _subtitles.begin ();
76         while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
77                 ++i;
78         }
79 }
80
81
82 bool
83 DCPSubtitleDecoder::pass ()
84 {
85         if (_next == _subtitles.end ()) {
86                 return true;
87         }
88
89         /* Gather all subtitles with the same time period that are next
90            on the list.  We must emit all subtitles for the same time
91            period with the same emit*() call otherwise the
92            TextDecoder will assume there is nothing else at the
93            time of emitting the first.
94         */
95
96         list<dcp::SubtitleString> s;
97         list<dcp::SubtitleImage> i;
98         auto const p = content_time_period (*_next);
99
100         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
101                 auto ns = dynamic_pointer_cast<const dcp::SubtitleString>(*_next);
102                 if (ns) {
103                         s.push_back (*ns);
104                         ++_next;
105                 } else {
106                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
107                            this would need to be done both here and in DCPDecoder.
108                         */
109
110                         auto ni = dynamic_pointer_cast<const dcp::SubtitleImage>(*_next);
111                         if (ni) {
112                                 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
113                                 ++_next;
114                         }
115                 }
116         }
117
118         only_text()->emit_plain (p, s);
119         return false;
120 }
121
122
123 ContentTimePeriod
124 DCPSubtitleDecoder::content_time_period (shared_ptr<const dcp::Subtitle> s) const
125 {
126         return {
127                 ContentTime::from_seconds(s->in().as_seconds()),
128                 ContentTime::from_seconds(s->out().as_seconds())
129         };
130 }
131
132
133 vector<dcpomatic::FontData>
134 DCPSubtitleDecoder::fonts () const
135 {
136         return _fonts;
137 }
138