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/>.
24 #include "font_config.h"
25 #include "string_text_file.h"
26 #include "string_text_file_content.h"
27 #include "text_content.h"
29 #include <dcp/raw_convert.h>
30 #include <fontconfig/fontconfig.h>
31 #include <libxml++/libxml++.h>
40 using std::make_shared;
41 using std::shared_ptr;
43 using boost::optional;
44 using dcp::raw_convert;
45 using namespace dcpomatic;
48 StringTextFileContent::StringTextFileContent (boost::filesystem::path path)
51 text.push_back (make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::UNKNOWN));
55 StringTextFileContent::StringTextFileContent (cxml::ConstNodePtr node, int version, list<string>& notes)
57 , _length (node->number_child<ContentTime::Type>("Length"))
59 text = TextContent::from_xml (this, node, version, notes);
65 font_names(StringTextFile const& string_text_file)
67 std::set<string> names;
69 for (auto const& subtitle: string_text_file.subtitles()) {
70 for (auto const& line: subtitle.lines) {
71 for (auto const& block: line.blocks) {
72 names.insert(block.font.get_value_or(""));
82 StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
84 Content::examine (film, job);
85 StringTextFile file (shared_from_this());
87 only_text()->clear_fonts();
89 /* Default to turning these subtitles on */
90 only_text()->set_use (true);
92 std::set<string> names = font_names(file);
94 for (auto name: names) {
95 optional<boost::filesystem::path> path;
97 path = FontConfig::instance()->system_font_with_name(name);
100 only_text()->add_font(make_shared<Font>(name, *path));
102 only_text()->add_font(make_shared<Font>(name));
106 boost::mutex::scoped_lock lm (_mutex);
107 _length = file.length();
112 StringTextFileContent::summary () const
114 return path_summary() + " " + _("[subtitles]");
119 StringTextFileContent::technical_summary () const
121 return Content::technical_summary() + " - " + _("Text subtitles");
127 StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
129 node->add_child("Type")->add_child_text("TextSubtitle");
130 Content::as_xml (node, with_paths);
133 only_text()->as_xml(node);
136 node->add_child("Length")->add_child_text(raw_convert<string>(_length.get ()));
141 StringTextFileContent::full_length (shared_ptr<const Film> film) const
143 FrameRateChange const frc (film, shared_from_this());
144 return DCPTime (_length, frc);
149 StringTextFileContent::approximate_length () const
151 return DCPTime (_length, FrameRateChange());
156 StringTextFileContent::identifier () const
158 auto s = Content::identifier ();
159 s += "_" + only_text()->identifier();
164 /** In 5a820bb8fae34591be5ac6d19a73461b9dab532a there were some changes to subtitle font management.
166 * With StringTextFileContent we used to write a <Font> tag to the metadata with the id "font". Users
167 * could then set a font file that content should use, and (with some luck) it would end up in the DCP
170 * After the changes we write a <Font> tag for every different font "id" (i.e. name) found in the source
171 * file (including a <Font> with id "" in the .srt case where there are no font names).
173 * However, this meant that making DCPs from old projects would fail, as the new code would see a font name
174 * in the source, then lookup a Font object for it from the Content, and fail in doing so (since the content
175 * only contains a font called "font").
177 * To put it another way: after the changes, the code expects that any font ID (i.e. name) used in some content
178 * will have a <Font> in the metadata and so a Font object in the TextContent. Without that, making DCPs fails.
180 * To work around this problem, this check_font_ids() is called for all subtitle content written by DoM versions
181 * before 2.16.14. We find all the font IDs in the content and map them all to the "legacy" font name (if there
182 * is one). This is more-or-less a re-examine()-ation, except that we try to preserve any settings that
183 * the user had previously set up.
188 StringTextFileContent::check_font_ids()
190 StringTextFile file (shared_from_this());
191 auto names = font_names(file);
193 auto content = only_text();
194 auto legacy_font_file = content->get_font("font")->file();
196 for (auto name: names) {
197 if (!content->get_font(name)) {
198 if (legacy_font_file) {
199 content->add_font(make_shared<Font>(name, *legacy_font_file));
201 content->add_font(make_shared<Font>(name));