Rearrange subtitle font management.
[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 "string_text_file_content.h"
23 #include "util.h"
24 #include "string_text_file.h"
25 #include "film.h"
26 #include "font.h"
27 #include "text_content.h"
28 #include <dcp/raw_convert.h>
29 #include <libxml++/libxml++.h>
30 #include <iostream>
31
32
33 #include "i18n.h"
34
35
36 using std::cout;
37 using std::list;
38 using std::make_shared;
39 using std::shared_ptr;
40 using std::string;
41 using dcp::raw_convert;
42 using namespace dcpomatic;
43
44
45 StringTextFileContent::StringTextFileContent (boost::filesystem::path path)
46         : Content (path)
47 {
48         text.push_back (make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::UNKNOWN));
49 }
50
51
52 StringTextFileContent::StringTextFileContent (cxml::ConstNodePtr node, int version, list<string>& notes)
53         : Content (node)
54         , _length (node->number_child<ContentTime::Type>("Length"))
55 {
56         text = TextContent::from_xml (this, node, version, notes);
57 }
58
59
60 void
61 StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
62 {
63         Content::examine (film, job);
64         StringTextFile file (shared_from_this());
65
66         /* Default to turning these subtitles on */
67         only_text()->set_use (true);
68
69         std::set<string> names;
70         for (auto const& subtitle: file.subtitles()) {
71                 for (auto const& line: subtitle.lines) {
72                         for (auto const& block: line.blocks) {
73                                 names.insert(block.font.get_value_or(""));
74                         }
75                 }
76         }
77
78         for (auto name: names) {
79                 /* Make a font for each family name that somebody might later
80                  * ask about.
81                  */
82                 only_text()->add_font(make_shared<Font>(name));
83         }
84
85         boost::mutex::scoped_lock lm (_mutex);
86         _length = file.length();
87 }
88
89
90 string
91 StringTextFileContent::summary () const
92 {
93         return path_summary() + " " + _("[subtitles]");
94 }
95
96
97 string
98 StringTextFileContent::technical_summary () const
99 {
100         return Content::technical_summary() + " - " + _("Text subtitles");
101
102 }
103
104
105 void
106 StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
107 {
108         node->add_child("Type")->add_child_text("TextSubtitle");
109         Content::as_xml (node, with_paths);
110
111         if (only_text()) {
112                 only_text()->as_xml(node);
113         }
114
115         node->add_child("Length")->add_child_text(raw_convert<string>(_length.get ()));
116 }
117
118
119 DCPTime
120 StringTextFileContent::full_length (shared_ptr<const Film> film) const
121 {
122         FrameRateChange const frc (film, shared_from_this());
123         return DCPTime (_length, frc);
124 }
125
126
127 DCPTime
128 StringTextFileContent::approximate_length () const
129 {
130         return DCPTime (_length, FrameRateChange());
131 }
132
133
134 string
135 StringTextFileContent::identifier () const
136 {
137         auto s = Content::identifier ();
138         s += "_" + only_text()->identifier();
139         return s;
140 }