2 Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "dcp_subtitle_decoder.h"
22 #include "dcp_subtitle_content.h"
23 #include <dcp/interop_subtitle_asset.h>
24 #include <dcp/load_font_node.h>
32 using std::shared_ptr;
33 using std::dynamic_pointer_cast;
35 using namespace dcpomatic;
37 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const Film> film, shared_ptr<const DCPSubtitleContent> content)
40 shared_ptr<dcp::SubtitleAsset> c (load (content->path (0)));
41 c->fix_empty_font_ids ();
42 _subtitles = c->subtitles ();
43 _next = _subtitles.begin ();
46 if (_next != _subtitles.end()) {
47 first = content_time_period(*_next).from;
49 text.push_back (shared_ptr<TextDecoder> (new TextDecoder (this, content->only_text(), first)));
51 map<string, dcp::ArrayData> fm = c->font_data();
52 for (map<string, dcp::ArrayData>::const_iterator j = fm.begin(); j != fm.end(); ++j) {
53 _fonts.push_back (FontData(j->first, j->second));
56 /* Add a default font for any LoadFont nodes in our file which we haven't yet found fonts for */
57 for (auto i: c->load_font_nodes()) {
58 if (fm.find(i->id) == fm.end()) {
59 _fonts.push_back (FontData(i->id, dcp::ArrayData(default_font_file())));
65 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
67 Decoder::seek (time, accurate);
69 _next = _subtitles.begin ();
70 auto i = _subtitles.begin ();
71 while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
77 DCPSubtitleDecoder::pass ()
79 if (_next == _subtitles.end ()) {
83 /* Gather all subtitles with the same time period that are next
84 on the list. We must emit all subtitles for the same time
85 period with the same emit*() call otherwise the
86 TextDecoder will assume there is nothing else at the
87 time of emitting the first.
90 list<dcp::SubtitleString> s;
91 list<dcp::SubtitleImage> i;
92 ContentTimePeriod const p = content_time_period (*_next);
94 while (_next != _subtitles.end () && content_time_period (*_next) == p) {
95 auto ns = dynamic_pointer_cast<const dcp::SubtitleString>(*_next);
100 /* XXX: perhaps these image subs should also be collected together like the string ones are;
101 this would need to be done both here and in DCPDecoder.
104 auto ni = dynamic_pointer_cast<const dcp::SubtitleImage>(*_next);
106 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
112 only_text()->emit_plain (p, s);
117 DCPSubtitleDecoder::content_time_period (shared_ptr<const dcp::Subtitle> s) const
120 ContentTime::from_seconds(s->in().as_seconds()),
121 ContentTime::from_seconds(s->out().as_seconds())
126 vector<dcpomatic::FontData>
127 DCPSubtitleDecoder::fonts () const