Fix the build for older macOS.
[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 "font.h"
25 #include "text_content.h"
26 #include <dcp/interop_subtitle_asset.h>
27 #include <dcp/load_font_node.h>
28 #include <iostream>
29
30
31 using std::cout;
32 using std::list;
33 using std::string;
34 using std::vector;
35 using std::shared_ptr;
36 using std::dynamic_pointer_cast;
37 using std::make_shared;
38 using namespace dcpomatic;
39
40
41 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const Film> film, shared_ptr<const DCPSubtitleContent> content)
42         : Decoder (film)
43 {
44         /* Load the XML or MXF file */
45         shared_ptr<dcp::SubtitleAsset> const c = load (content->path(0));
46         c->fix_empty_font_ids ();
47         _subtitles = c->subtitles ();
48         _next = _subtitles.begin ();
49
50         ContentTime first;
51         if (_next != _subtitles.end()) {
52                 first = content_time_period(*_next).from;
53         }
54         text.push_back (make_shared<TextDecoder>(this, content->only_text(), first));
55
56         /* The fonts that are included in content's file; if it's interop there will be none
57          * (as the fonts are held in separate assets).
58          */
59         auto fonts_in_asset = c->font_data();
60
61         /* Fonts specified in the TextContent */
62         list<shared_ptr<dcpomatic::Font>> fonts_in_content;
63         for (auto i: content->text) {
64                 auto this_fonts = i->fonts();
65                 std::copy(this_fonts.begin(), this_fonts.end(), std::back_inserter(fonts_in_content));
66         }
67         fonts_in_content.sort();
68         fonts_in_content.unique();
69
70         /* Find a font for each <LoadFont> Node */
71         for (auto i: c->load_font_nodes()) {
72                 bool done = false;
73                 for (auto j: fonts_in_content) {
74                         if (j->id() == i->id && j->file()) {
75                                 // One was specified in the content
76                                 _fonts.push_back (FontData(i->id, dcp::ArrayData(*j->file())));
77                                 done = true;
78                         }
79                 }
80                 if (!done) {
81                         if (fonts_in_asset.find(i->id) != fonts_in_asset.end()) {
82                                 // One was included in the subtitle file
83                                 _fonts.push_back (FontData(i->id, fonts_in_asset[i->id]));
84                                 done = true;
85                         }
86                 }
87                 if (!done) {
88                         // Give up and add a default
89                         _fonts.push_back (FontData(i->id, dcp::ArrayData(default_font_file())));
90                 }
91         }
92 }
93
94
95 void
96 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
97 {
98         Decoder::seek (time, accurate);
99
100         _next = _subtitles.begin ();
101         auto i = _subtitles.begin ();
102         while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
103                 ++i;
104         }
105 }
106
107
108 bool
109 DCPSubtitleDecoder::pass ()
110 {
111         if (_next == _subtitles.end ()) {
112                 return true;
113         }
114
115         /* Gather all subtitles with the same time period that are next
116            on the list.  We must emit all subtitles for the same time
117            period with the same emit*() call otherwise the
118            TextDecoder will assume there is nothing else at the
119            time of emitting the first.
120         */
121
122         list<dcp::SubtitleString> s;
123         list<dcp::SubtitleImage> i;
124         auto const p = content_time_period (*_next);
125
126         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
127                 auto ns = dynamic_pointer_cast<const dcp::SubtitleString>(*_next);
128                 if (ns) {
129                         s.push_back (*ns);
130                         ++_next;
131                 } else {
132                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
133                            this would need to be done both here and in DCPDecoder.
134                         */
135
136                         auto ni = dynamic_pointer_cast<const dcp::SubtitleImage>(*_next);
137                         if (ni) {
138                                 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
139                                 ++_next;
140                         }
141                 }
142         }
143
144         only_text()->emit_plain (p, s);
145         return false;
146 }
147
148
149 ContentTimePeriod
150 DCPSubtitleDecoder::content_time_period (shared_ptr<const dcp::Subtitle> s) const
151 {
152         return {
153                 ContentTime::from_seconds(s->in().as_seconds()),
154                 ContentTime::from_seconds(s->out().as_seconds())
155         };
156 }
157
158
159 vector<dcpomatic::FontData>
160 DCPSubtitleDecoder::fonts () const
161 {
162         return _fonts;
163 }
164