2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp 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 libdcp 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 libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
35 /** @file src/interop_subtitle_asset.cc
36 * @brief InteropSubtitleAsset class
40 #include "compose.hpp"
41 #include "dcp_assert.h"
42 #include "equality_options.h"
43 #include "font_asset.h"
45 #include "interop_load_font_node.h"
46 #include "interop_subtitle_asset.h"
47 #include "raw_convert.h"
48 #include "subtitle_asset_internal.h"
49 #include "subtitle_image.h"
53 LIBDCP_DISABLE_WARNINGS
54 #include <libxml++/libxml++.h>
55 LIBDCP_ENABLE_WARNINGS
56 #include <boost/weak_ptr.hpp>
63 using std::dynamic_pointer_cast;
64 using std::make_shared;
65 using std::shared_ptr;
68 using boost::optional;
72 InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
73 : SubtitleAsset (file)
75 _raw_xml = dcp::file_to_string (file);
77 auto xml = make_shared<cxml::Document>("DCSubtitle");
78 xml->read_file (file);
79 _id = xml->string_child ("SubtitleID");
80 _reel_number = xml->string_child ("ReelNumber");
81 _language = xml->string_child ("Language");
82 _movie_title = xml->string_child ("MovieTitle");
83 _load_font_nodes = type_children<InteropLoadFontNode> (xml, "LoadFont");
85 /* Now we need to drop down to xmlpp */
87 vector<ParseState> ps;
88 for (auto i: xml->node()->get_children()) {
89 auto e = dynamic_cast<xmlpp::Element const *>(i);
90 if (e && (e->get_name() == "Font" || e->get_name() == "Subtitle")) {
91 parse_subtitles (e, ps, optional<int>(), Standard::INTEROP);
95 for (auto i: _subtitles) {
96 auto si = dynamic_pointer_cast<SubtitleImage>(i);
98 si->read_png_file (file.parent_path() / String::compose("%1.png", si->id()));
104 InteropSubtitleAsset::InteropSubtitleAsset ()
111 InteropSubtitleAsset::xml_as_string () const
114 auto root = doc.create_root_node ("DCSubtitle");
115 root->set_attribute ("Version", "1.0");
117 root->add_child("SubtitleID")->add_child_text (_id);
118 root->add_child("MovieTitle")->add_child_text (_movie_title);
119 root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
120 root->add_child("Language")->add_child_text (_language);
122 for (auto i: _load_font_nodes) {
123 auto load_font = root->add_child("LoadFont");
124 load_font->set_attribute ("Id", i->id);
125 load_font->set_attribute ("URI", i->uri);
128 subtitles_as_xml (root, 250, Standard::INTEROP);
130 return format_xml(doc, {});
135 InteropSubtitleAsset::add_font (string load_id, dcp::ArrayData data)
137 _fonts.push_back (Font(load_id, make_uuid(), data));
138 auto const uri = String::compose("font_%1.ttf", _load_font_nodes.size());
139 _load_font_nodes.push_back (make_shared<InteropLoadFontNode>(load_id, uri));
144 InteropSubtitleAsset::equals(shared_ptr<const Asset> other_asset, EqualityOptions const& options, NoteHandler note) const
146 if (!SubtitleAsset::equals (other_asset, options, note)) {
150 auto other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
155 if (!options.load_font_nodes_can_differ) {
156 auto i = _load_font_nodes.begin();
157 auto j = other->_load_font_nodes.begin();
159 while (i != _load_font_nodes.end ()) {
160 if (j == other->_load_font_nodes.end ()) {
161 note (NoteType::ERROR, "<LoadFont> nodes differ");
166 note (NoteType::ERROR, "<LoadFont> nodes differ");
175 if (_movie_title != other->_movie_title) {
176 note (NoteType::ERROR, "Subtitle movie titles differ");
184 vector<shared_ptr<LoadFontNode>>
185 InteropSubtitleAsset::load_font_nodes () const
187 vector<shared_ptr<LoadFontNode>> lf;
188 copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
194 InteropSubtitleAsset::write (boost::filesystem::path p) const
198 throw FileError ("Could not open file for writing", p, -1);
201 _raw_xml = xml_as_string ();
202 /* length() here gives bytes not characters */
203 f.write(_raw_xml->c_str(), 1, _raw_xml->length());
207 /* Image subtitles */
208 for (auto i: _subtitles) {
209 auto im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
211 im->write_png_file(p.parent_path() / String::compose("%1.png", im->id()));
216 for (auto i: _load_font_nodes) {
217 auto file = p.parent_path() / i->uri;
218 auto font_with_id = std::find_if(_fonts.begin(), _fonts.end(), [i](Font const& font) { return font.load_id == i->id; });
219 if (font_with_id != _fonts.end()) {
220 font_with_id->data.write(file);
221 font_with_id->file = file;
227 /** Look at a supplied list of assets and find the fonts. Then match these
228 * fonts up with anything requested by a <LoadFont> so that _fonts contains
229 * a list of font ID, load ID and data.
232 InteropSubtitleAsset::resolve_fonts (vector<shared_ptr<Asset>> assets)
234 for (auto asset: assets) {
235 auto font = dynamic_pointer_cast<FontAsset>(asset);
242 for (auto load_font_node: _load_font_nodes) {
243 auto const path_in_load_font_node = _file->parent_path() / load_font_node->uri;
244 if (font->file() && path_in_load_font_node == *font->file()) {
245 auto existing = std::find_if(_fonts.begin(), _fonts.end(), [load_font_node](Font const& font) { return font.load_id == load_font_node->id; });
246 if (existing != _fonts.end()) {
247 *existing = Font(load_font_node->id, asset->id(), font->file().get());
249 _fonts.push_back(Font(load_font_node->id, asset->id(), font->file().get()));
257 vector<shared_ptr<Asset>>
258 InteropSubtitleAsset::font_assets()
260 vector<shared_ptr<Asset>> assets;
261 for (auto const& i: _fonts) {
263 assets.push_back(make_shared<FontAsset>(i.uuid, i.file.get()));
269 vector<shared_ptr<const Asset>>
270 InteropSubtitleAsset::font_assets() const
272 vector<shared_ptr<const Asset>> assets;
273 for (auto const& i: _fonts) {
275 assets.push_back(make_shared<const FontAsset>(i.uuid, i.file.get()));
282 InteropSubtitleAsset::add_to_assetmap (AssetMap& asset_map, boost::filesystem::path root) const
284 Asset::add_to_assetmap(asset_map, root);
286 for (auto i: _subtitles) {
287 auto im = dynamic_pointer_cast<dcp::SubtitleImage>(i);
289 DCP_ASSERT(im->file());
290 add_file_to_assetmap(asset_map, root, im->file().get(), im->id());
297 InteropSubtitleAsset::add_to_pkl (shared_ptr<PKL> pkl, boost::filesystem::path root) const
299 Asset::add_to_pkl (pkl, root);
301 for (auto i: _subtitles) {
302 auto im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
304 auto png_image = im->png_image ();
305 pkl->add_asset(im->id(), optional<string>(), make_digest(png_image), png_image.size(), "image/png", root.filename().string());
312 InteropSubtitleAsset::set_font_file (string load_id, boost::filesystem::path file)
314 for (auto& i: _fonts) {
315 if (i.load_id == load_id) {
320 for (auto i: _load_font_nodes) {
321 if (i->id == load_id) {
322 i->uri = file.filename().string();
329 InteropSubtitleAsset::unresolved_fonts() const
331 vector<string> unresolved;
332 for (auto load_font_node: _load_font_nodes) {
333 if (std::find_if(_fonts.begin(), _fonts.end(), [load_font_node](Font const& font) { return font.load_id == load_font_node->id; }) == _fonts.end()) {
334 unresolved.push_back(load_font_node->id);