Don't search for empty font names to avoid random fonts being chosen.
[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 void
64 StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
65 {
66         Content::examine (film, job);
67         StringTextFile file (shared_from_this());
68
69         /* Default to turning these subtitles on */
70         only_text()->set_use (true);
71
72         std::set<string> names;
73         for (auto const& subtitle: file.subtitles()) {
74                 for (auto const& line: subtitle.lines) {
75                         for (auto const& block: line.blocks) {
76                                 names.insert(block.font.get_value_or(""));
77                         }
78                 }
79         }
80
81         for (auto name: names) {
82                 optional<boost::filesystem::path> path;
83                 if (!name.empty()) {
84                         path = FontConfig::instance()->system_font_with_name(name);
85                 }
86                 if (path) {
87                         only_text()->add_font(make_shared<Font>(name, *path));
88                 } else {
89                         only_text()->add_font(make_shared<Font>(name));
90                 }
91         }
92
93         boost::mutex::scoped_lock lm (_mutex);
94         _length = file.length();
95 }
96
97
98 string
99 StringTextFileContent::summary () const
100 {
101         return path_summary() + " " + _("[subtitles]");
102 }
103
104
105 string
106 StringTextFileContent::technical_summary () const
107 {
108         return Content::technical_summary() + " - " + _("Text subtitles");
109
110 }
111
112
113 void
114 StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
115 {
116         node->add_child("Type")->add_child_text("TextSubtitle");
117         Content::as_xml (node, with_paths);
118
119         if (only_text()) {
120                 only_text()->as_xml(node);
121         }
122
123         node->add_child("Length")->add_child_text(raw_convert<string>(_length.get ()));
124 }
125
126
127 DCPTime
128 StringTextFileContent::full_length (shared_ptr<const Film> film) const
129 {
130         FrameRateChange const frc (film, shared_from_this());
131         return DCPTime (_length, frc);
132 }
133
134
135 DCPTime
136 StringTextFileContent::approximate_length () const
137 {
138         return DCPTime (_length, FrameRateChange());
139 }
140
141
142 string
143 StringTextFileContent::identifier () const
144 {
145         auto s = Content::identifier ();
146         s += "_" + only_text()->identifier();
147         return s;
148 }