Fix problems when loading old projects with the new subtitle font code (#2271).
[dcpomatic.git] / src / lib / string_text_file_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
22 #include "film.h"
23 #include "font.h"
24 #include "font_config.h"
25 #include "string_text_file.h"
26 #include "string_text_file_content.h"
27 #include "text_content.h"
28 #include "util.h"
29 #include <dcp/raw_convert.h>
30 #include <fontconfig/fontconfig.h>
31 #include <libxml++/libxml++.h>
32 #include <iostream>
33
34
35 #include "i18n.h"
36
37
38 using std::cout;
39 using std::list;
40 using std::make_shared;
41 using std::shared_ptr;
42 using std::string;
43 using boost::optional;
44 using dcp::raw_convert;
45 using namespace dcpomatic;
46
47
48 StringTextFileContent::StringTextFileContent (boost::filesystem::path path)
49         : Content (path)
50 {
51         text.push_back (make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::UNKNOWN));
52 }
53
54
55 StringTextFileContent::StringTextFileContent (cxml::ConstNodePtr node, int version, list<string>& notes)
56         : Content (node)
57         , _length (node->number_child<ContentTime::Type>("Length"))
58 {
59         text = TextContent::from_xml (this, node, version, notes);
60 }
61
62
63 static
64 std::set<string>
65 font_names(StringTextFile const& string_text_file)
66 {
67         std::set<string> names;
68
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(""));
73                         }
74                 }
75         }
76
77         return names;
78 }
79
80
81 void
82 StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
83 {
84         Content::examine (film, job);
85         StringTextFile file (shared_from_this());
86
87         only_text()->clear_fonts();
88
89         /* Default to turning these subtitles on */
90         only_text()->set_use (true);
91
92         std::set<string> names = font_names(file);
93
94         for (auto name: names) {
95                 optional<boost::filesystem::path> path;
96                 if (!name.empty()) {
97                         path = FontConfig::instance()->system_font_with_name(name);
98                 }
99                 if (path) {
100                         only_text()->add_font(make_shared<Font>(name, *path));
101                 } else {
102                         only_text()->add_font(make_shared<Font>(name));
103                 }
104         }
105
106         boost::mutex::scoped_lock lm (_mutex);
107         _length = file.length();
108 }
109
110
111 string
112 StringTextFileContent::summary () const
113 {
114         return path_summary() + " " + _("[subtitles]");
115 }
116
117
118 string
119 StringTextFileContent::technical_summary () const
120 {
121         return Content::technical_summary() + " - " + _("Text subtitles");
122
123 }
124
125
126 void
127 StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
128 {
129         node->add_child("Type")->add_child_text("TextSubtitle");
130         Content::as_xml (node, with_paths);
131
132         if (only_text()) {
133                 only_text()->as_xml(node);
134         }
135
136         node->add_child("Length")->add_child_text(raw_convert<string>(_length.get ()));
137 }
138
139
140 DCPTime
141 StringTextFileContent::full_length (shared_ptr<const Film> film) const
142 {
143         FrameRateChange const frc (film, shared_from_this());
144         return DCPTime (_length, frc);
145 }
146
147
148 DCPTime
149 StringTextFileContent::approximate_length () const
150 {
151         return DCPTime (_length, FrameRateChange());
152 }
153
154
155 string
156 StringTextFileContent::identifier () const
157 {
158         auto s = Content::identifier ();
159         s += "_" + only_text()->identifier();
160         return s;
161 }
162
163
164 /** In 5a820bb8fae34591be5ac6d19a73461b9dab532a there were some changes to subtitle font management.
165  *
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
168  *  that way.
169  *
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).
172  *
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").
176  *
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.
179  *
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.
184  *
185  *  See #2271.
186  */
187 void
188 StringTextFileContent::check_font_ids()
189 {
190         StringTextFile file (shared_from_this());
191         auto names = font_names(file);
192
193         auto content = only_text();
194         auto legacy_font_file = content->get_font("font")->file();
195
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));
200                         } else {
201                                 content->add_font(make_shared<Font>(name));
202                         }
203                 }
204         }
205 }
206