Missed update to private test repo version.
[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         for (auto name: font_names(file)) {
93                 optional<boost::filesystem::path> path;
94                 if (!name.empty()) {
95                         path = FontConfig::instance()->system_font_with_name(name);
96                 }
97                 if (path) {
98                         only_text()->add_font(make_shared<Font>(name, *path));
99                 } else {
100                         only_text()->add_font(make_shared<Font>(name));
101                 }
102         }
103
104         boost::mutex::scoped_lock lm (_mutex);
105         _length = file.length();
106 }
107
108
109 string
110 StringTextFileContent::summary () const
111 {
112         return path_summary() + " " + _("[subtitles]");
113 }
114
115
116 string
117 StringTextFileContent::technical_summary () const
118 {
119         return Content::technical_summary() + " - " + _("Text subtitles");
120
121 }
122
123
124 void
125 StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
126 {
127         node->add_child("Type")->add_child_text("TextSubtitle");
128         Content::as_xml (node, with_paths);
129
130         if (only_text()) {
131                 only_text()->as_xml(node);
132         }
133
134         node->add_child("Length")->add_child_text(raw_convert<string>(_length.get ()));
135 }
136
137
138 DCPTime
139 StringTextFileContent::full_length (shared_ptr<const Film> film) const
140 {
141         FrameRateChange const frc (film, shared_from_this());
142         return DCPTime (_length, frc);
143 }
144
145
146 DCPTime
147 StringTextFileContent::approximate_length () const
148 {
149         return DCPTime (_length, FrameRateChange());
150 }
151
152
153 string
154 StringTextFileContent::identifier () const
155 {
156         auto s = Content::identifier ();
157         s += "_" + only_text()->identifier();
158         return s;
159 }
160
161
162 /** In 5a820bb8fae34591be5ac6d19a73461b9dab532a there were some changes to subtitle font management.
163  *
164  *  With StringTextFileContent we used to write a <Font> tag to the metadata with the id "font".  Users
165  *  could then set a font file that content should use, and (with some luck) it would end up in the DCP
166  *  that way.
167  *
168  *  After the changes we write a <Font> tag for every different font "id" (i.e. name) found in the source
169  *  file (including a <Font> with id "" in the .srt case where there are no font names).
170  *
171  *  However, this meant that making DCPs from old projects would fail, as the new code would see a font name
172  *  in the source, then lookup a Font object for it from the Content, and fail in doing so (since the content
173  *  only contains a font called "font").
174  *
175  *  To put it another way: after the changes, the code expects that any font ID (i.e. name) used in some content
176  *  will have a <Font> in the metadata and so a Font object in the TextContent.  Without that, making DCPs fails.
177  *
178  *  To work around this problem, this check_font_ids() is called for all subtitle content written by DoM versions
179  *  before 2.16.14.  We find all the font IDs in the content and map them all to the "legacy" font name (if there
180  *  is one).  This is more-or-less a re-examine()-ation, except that we try to preserve any settings that
181  *  the user had previously set up.
182  *
183  *  See #2271.
184  */
185 void
186 StringTextFileContent::check_font_ids()
187 {
188         StringTextFile file (shared_from_this());
189         auto names = font_names(file);
190
191         auto content = only_text();
192         optional<boost::filesystem::path> legacy_font_file;
193         if (auto legacy_font = content->get_font("font")) {
194                 legacy_font_file = legacy_font->file();
195         }
196
197         for (auto name: names) {
198                 if (!content->get_font(name)) {
199                         if (legacy_font_file) {
200                                 content->add_font(make_shared<Font>(name, *legacy_font_file));
201                         } else {
202                                 content->add_font(make_shared<Font>(name));
203                         }
204                 }
205         }
206 }
207