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