summaryrefslogtreecommitdiff
path: root/src/lib/string_text_file_content.cc
blob: d8c195be77af1fe53ada9e66830d776ad4810826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
    Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "film.h"
#include "font.h"
#include "font_config.h"
#include "string_text_file.h"
#include "string_text_file_content.h"
#include "text_content.h"
#include "util.h"
#include <dcp/raw_convert.h>
#include <fontconfig/fontconfig.h>
#include <libxml++/libxml++.h>
#include <iostream>


#include "i18n.h"


using std::cout;
using std::list;
using std::make_shared;
using std::shared_ptr;
using std::string;
using boost::optional;
using dcp::raw_convert;
using namespace dcpomatic;


StringTextFileContent::StringTextFileContent (boost::filesystem::path path)
	: Content (path)
{
	text.push_back (make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::UNKNOWN));
}


StringTextFileContent::StringTextFileContent (cxml::ConstNodePtr node, int version, list<string>& notes)
	: Content (node)
	, _length (node->number_child<ContentTime::Type>("Length"))
{
	text = TextContent::from_xml (this, node, version, notes);
}


static
std::set<string>
font_names(StringTextFile const& string_text_file)
{
	std::set<string> names;

	for (auto const& subtitle: string_text_file.subtitles()) {
		for (auto const& line: subtitle.lines) {
			for (auto const& block: line.blocks) {
				names.insert(block.font.get_value_or(""));
			}
		}
	}

	return names;
}


void
StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
{
	Content::examine (film, job);
	StringTextFile file (shared_from_this());

	only_text()->clear_fonts();

	/* Default to turning these subtitles on */
	only_text()->set_use (true);

	for (auto name: font_names(file)) {
		optional<boost::filesystem::path> path;
		if (!name.empty()) {
			path = FontConfig::instance()->system_font_with_name(name);
		}
		if (path) {
			only_text()->add_font(make_shared<Font>(name, *path));
		} else {
			only_text()->add_font(make_shared<Font>(name));
		}
	}

	boost::mutex::scoped_lock lm (_mutex);
	_length = file.length();
}


string
StringTextFileContent::summary () const
{
	return path_summary() + " " + _("[subtitles]");
}


string
StringTextFileContent::technical_summary () const
{
	return Content::technical_summary() + " - " + _("Text subtitles");

}


void
StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
{
	node->add_child("Type")->add_child_text("TextSubtitle");
	Content::as_xml (node, with_paths);

	if (only_text()) {
		only_text()->as_xml(node);
	}

	node->add_child("Length")->add_child_text(raw_convert<string>(_length.get ()));
}


DCPTime
StringTextFileContent::full_length (shared_ptr<const Film> film) const
{
	FrameRateChange const frc (film, shared_from_this());
	return DCPTime (_length, frc);
}


DCPTime
StringTextFileContent::approximate_length () const
{
	return DCPTime (_length, FrameRateChange());
}


string
StringTextFileContent::identifier () const
{
	auto s = Content::identifier ();
	s += "_" + only_text()->identifier();
	return s;
}


/** In 5a820bb8fae34591be5ac6d19a73461b9dab532a there were some changes to subtitle font management.
 *
 *  With StringTextFileContent we used to write a <Font> tag to the metadata with the id "font".  Users
 *  could then set a font file that content should use, and (with some luck) it would end up in the DCP
 *  that way.
 *
 *  After the changes we write a <Font> tag for every different font "id" (i.e. name) found in the source
 *  file (including a <Font> with id "" in the .srt case where there are no font names).
 *
 *  However, this meant that making DCPs from old projects would fail, as the new code would see a font name
 *  in the source, then lookup a Font object for it from the Content, and fail in doing so (since the content
 *  only contains a font called "font").
 *
 *  To put it another way: after the changes, the code expects that any font ID (i.e. name) used in some content
 *  will have a <Font> in the metadata and so a Font object in the TextContent.  Without that, making DCPs fails.
 *
 *  To work around this problem, this check_font_ids() is called for all subtitle content written by DoM versions
 *  before 2.16.14.  We find all the font IDs in the content and map them all to the "legacy" font name (if there
 *  is one).  This is more-or-less a re-examine()-ation, except that we try to preserve any settings that
 *  the user had previously set up.
 *
 *  See #2271.
 */
void
StringTextFileContent::check_font_ids()
{
	StringTextFile file (shared_from_this());
	auto names = font_names(file);

	auto content = only_text();
	optional<boost::filesystem::path> legacy_font_file;
	if (auto legacy_font = content->get_font("font")) {
		legacy_font_file = legacy_font->file();
	}

	for (auto name: names) {
		if (!content->get_font(name)) {
			if (legacy_font_file) {
				content->add_font(make_shared<Font>(name, *legacy_font_file));
			} else {
				content->add_font(make_shared<Font>(name));
			}
		}
	}
}