Ask fontconfig to scan the system for fonts requested by text subtitle files (#2264).
[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 dcp::raw_convert;
44 using namespace dcpomatic;
45
46
47 StringTextFileContent::StringTextFileContent (boost::filesystem::path path)
48         : Content (path)
49 {
50         text.push_back (make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::UNKNOWN));
51 }
52
53
54 StringTextFileContent::StringTextFileContent (cxml::ConstNodePtr node, int version, list<string>& notes)
55         : Content (node)
56         , _length (node->number_child<ContentTime::Type>("Length"))
57 {
58         text = TextContent::from_xml (this, node, version, notes);
59 }
60
61
62 void
63 StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
64 {
65         Content::examine (film, job);
66         StringTextFile file (shared_from_this());
67
68         /* Default to turning these subtitles on */
69         only_text()->set_use (true);
70
71         std::set<string> names;
72         for (auto const& subtitle: file.subtitles()) {
73                 for (auto const& line: subtitle.lines) {
74                         for (auto const& block: line.blocks) {
75                                 names.insert(block.font.get_value_or(""));
76                         }
77                 }
78         }
79
80         for (auto name: names) {
81                 auto path = FontConfig::instance()->system_font_with_name(name);
82                 if (path) {
83                         only_text()->add_font(make_shared<Font>(name, *path));
84                 } else {
85                         only_text()->add_font(make_shared<Font>(name));
86                 }
87         }
88
89         boost::mutex::scoped_lock lm (_mutex);
90         _length = file.length();
91 }
92
93
94 string
95 StringTextFileContent::summary () const
96 {
97         return path_summary() + " " + _("[subtitles]");
98 }
99
100
101 string
102 StringTextFileContent::technical_summary () const
103 {
104         return Content::technical_summary() + " - " + _("Text subtitles");
105
106 }
107
108
109 void
110 StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
111 {
112         node->add_child("Type")->add_child_text("TextSubtitle");
113         Content::as_xml (node, with_paths);
114
115         if (only_text()) {
116                 only_text()->as_xml(node);
117         }
118
119         node->add_child("Length")->add_child_text(raw_convert<string>(_length.get ()));
120 }
121
122
123 DCPTime
124 StringTextFileContent::full_length (shared_ptr<const Film> film) const
125 {
126         FrameRateChange const frc (film, shared_from_this());
127         return DCPTime (_length, frc);
128 }
129
130
131 DCPTime
132 StringTextFileContent::approximate_length () const
133 {
134         return DCPTime (_length, FrameRateChange());
135 }
136
137
138 string
139 StringTextFileContent::identifier () const
140 {
141         auto s = Content::identifier ();
142         s += "_" + only_text()->identifier();
143         return s;
144 }