2 Copyright (C) 2014-2021 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/>.
22 #include "dcp_subtitle_content.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>
34 using std::dynamic_pointer_cast;
36 using std::make_shared;
37 using std::shared_ptr;
39 using dcp::raw_convert;
40 using namespace dcpomatic;
42 DCPSubtitleContent::DCPSubtitleContent (boost::filesystem::path path)
45 text.push_back (make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::OPEN_SUBTITLE));
48 DCPSubtitleContent::DCPSubtitleContent (cxml::ConstNodePtr node, int version)
50 , _length (node->number_child<ContentTime::Type> ("Length"))
53 text = TextContent::from_xml (this, node, version, notes);
57 DCPSubtitleContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
59 Content::examine (film, job);
61 auto subtitle_asset = load(path(0));
63 auto iop = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(subtitle_asset);
64 auto smpte = dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(subtitle_asset);
66 set_video_frame_rate(film, smpte->edit_rate().numerator);
69 boost::mutex::scoped_lock lm (_mutex);
71 /* Default to turning these subtitles on */
72 only_text()->set_use (true);
74 _length = ContentTime::from_seconds(subtitle_asset->latest_subtitle_out().as_seconds());
76 subtitle_asset->fix_empty_font_ids();
77 add_fonts(only_text(), subtitle_asset);
82 DCPSubtitleContent::add_fonts(shared_ptr<TextContent> content, shared_ptr<dcp::SubtitleAsset> subtitle_asset)
84 FontIDAllocator font_id_allocator;
86 for (auto node: subtitle_asset->load_font_nodes()) {
87 font_id_allocator.add_font(0, subtitle_asset->id(), node->id);
89 font_id_allocator.allocate();
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),
101 font = make_shared<Font>(
102 font_id_allocator.font_id(0, subtitle_asset->id(), node->id)
105 content->add_font(font);
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()));
115 DCPSubtitleContent::full_length (shared_ptr<const Film> film) const
117 FrameRateChange const frc (film, shared_from_this());
118 return DCPTime (_length, frc);
122 DCPSubtitleContent::approximate_length () const
124 return DCPTime (_length, FrameRateChange());
128 DCPSubtitleContent::summary () const
130 return path_summary() + " " + _("[subtitles]");
134 DCPSubtitleContent::technical_summary () const
136 return Content::technical_summary() + " - " + _("DCP XML subtitles");
140 DCPSubtitleContent::as_xml (xmlpp::Node* node, bool with_paths) const
142 node->add_child("Type")->add_child_text ("DCPSubtitle");
143 Content::as_xml (node, with_paths);
146 only_text()->as_xml (node);
149 node->add_child("Length")->add_child_text (raw_convert<string> (_length.get ()));