Supporters update.
[dcpomatic.git] / src / lib / dcp_subtitle_content.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 #include "font.h"
22 #include "dcp_subtitle_content.h"
23 #include "film.h"
24 #include "font_id_allocator.h"
25 #include "text_content.h"
26 #include <dcp/raw_convert.h>
27 #include <dcp/interop_subtitle_asset.h>
28 #include <dcp/smpte_subtitle_asset.h>
29 #include <dcp/interop_load_font_node.h>
30 #include <libxml++/libxml++.h>
31
32 #include "i18n.h"
33
34 using std::dynamic_pointer_cast;
35 using std::list;
36 using std::make_shared;
37 using std::shared_ptr;
38 using std::string;
39 using dcp::raw_convert;
40 using namespace dcpomatic;
41
42 DCPSubtitleContent::DCPSubtitleContent (boost::filesystem::path path)
43         : Content (path)
44 {
45         text.push_back (make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::OPEN_SUBTITLE));
46 }
47
48 DCPSubtitleContent::DCPSubtitleContent (cxml::ConstNodePtr node, int version)
49         : Content (node)
50         , _length (node->number_child<ContentTime::Type> ("Length"))
51 {
52         list<string> notes;
53         text = TextContent::from_xml (this, node, version, notes);
54 }
55
56 void
57 DCPSubtitleContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
58 {
59         Content::examine (film, job);
60
61         auto subtitle_asset = load(path(0));
62
63         auto iop = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(subtitle_asset);
64         auto smpte = dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(subtitle_asset);
65         if (smpte) {
66                 set_video_frame_rate(film, smpte->edit_rate().numerator);
67         }
68
69         boost::mutex::scoped_lock lm (_mutex);
70
71         /* Default to turning these subtitles on */
72         only_text()->set_use (true);
73
74         _length = ContentTime::from_seconds(subtitle_asset->latest_subtitle_out().as_seconds());
75
76         subtitle_asset->fix_empty_font_ids();
77         add_fonts(only_text(), subtitle_asset);
78 }
79
80
81 void
82 DCPSubtitleContent::add_fonts(shared_ptr<TextContent> content, shared_ptr<dcp::SubtitleAsset> subtitle_asset)
83 {
84         FontIDAllocator font_id_allocator;
85
86         for (auto node: subtitle_asset->load_font_nodes()) {
87                 font_id_allocator.add_font(0, subtitle_asset->id(), node->id);
88         }
89         font_id_allocator.allocate();
90
91         auto font_data = subtitle_asset->font_data();
92         for (auto node: subtitle_asset->load_font_nodes()) {
93                 auto data = font_data.find(node->id);
94                 shared_ptr<dcpomatic::Font> font;
95                 if (data != font_data.end()) {
96                         font = make_shared<Font>(
97                                 font_id_allocator.font_id(0, subtitle_asset->id(), node->id),
98                                 data->second
99                                 );
100                 } else {
101                         font = make_shared<Font>(
102                                 font_id_allocator.font_id(0, subtitle_asset->id(), node->id)
103                                 );
104                 }
105                 content->add_font(font);
106         }
107
108         if (!font_id_allocator.has_default_font()) {
109                 content->add_font(make_shared<dcpomatic::Font>(font_id_allocator.default_font_id(), default_font_file()));
110         }
111 }
112
113
114 DCPTime
115 DCPSubtitleContent::full_length (shared_ptr<const Film> film) const
116 {
117         FrameRateChange const frc (film, shared_from_this());
118         return DCPTime (_length, frc);
119 }
120
121 DCPTime
122 DCPSubtitleContent::approximate_length () const
123 {
124         return DCPTime (_length, FrameRateChange());
125 }
126
127 string
128 DCPSubtitleContent::summary () const
129 {
130         return path_summary() + " " + _("[subtitles]");
131 }
132
133 string
134 DCPSubtitleContent::technical_summary () const
135 {
136         return Content::technical_summary() + " - " + _("DCP XML subtitles");
137 }
138
139 void
140 DCPSubtitleContent::as_xml (xmlpp::Node* node, bool with_paths) const
141 {
142         node->add_child("Type")->add_child_text ("DCPSubtitle");
143         Content::as_xml (node, with_paths);
144
145         if (only_text()) {
146                 only_text()->as_xml (node);
147         }
148
149         node->add_child("Length")->add_child_text (raw_convert<string> (_length.get ()));
150 }