01e0fe60ff490450b3e30a950f640020f4ce22dd
[dcpomatic.git] / src / lib / string_text_file_content.cc
1 /*
2     Copyright (C) 2014-2018 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 #include "string_text_file_content.h"
22 #include "util.h"
23 #include "string_text_file.h"
24 #include "film.h"
25 #include "font.h"
26 #include "text_content.h"
27 #include <dcp/raw_convert.h>
28 #include <libxml++/libxml++.h>
29 #include <iostream>
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::cout;
35 using std::shared_ptr;
36 using std::make_shared;
37 using boost::optional;
38 using dcp::raw_convert;
39 using namespace dcpomatic;
40
41 StringTextFileContent::StringTextFileContent (boost::filesystem::path path)
42         : Content (path)
43 {
44         text.push_back (shared_ptr<TextContent> (new TextContent (this, TextType::OPEN_SUBTITLE, TextType::UNKNOWN)));
45 }
46
47 StringTextFileContent::StringTextFileContent (cxml::ConstNodePtr node, int version)
48         : Content (node)
49         , _length (node->number_child<ContentTime::Type> ("Length"))
50 {
51         text = TextContent::from_xml (this, node, version);
52 }
53
54 void
55 StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
56 {
57         Content::examine (film, job);
58         StringTextFile s (shared_from_this ());
59
60         /* Default to turning these subtitles on */
61         only_text()->set_use (true);
62
63         boost::mutex::scoped_lock lm (_mutex);
64         _length = s.length ();
65         only_text()->add_font (make_shared<Font>(TEXT_FONT_ID));
66 }
67
68 string
69 StringTextFileContent::summary () const
70 {
71         return path_summary() + " " + _("[subtitles]");
72 }
73
74 string
75 StringTextFileContent::technical_summary () const
76 {
77         return Content::technical_summary() + " - " + _("Text subtitles");
78 }
79
80 void
81 StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
82 {
83         node->add_child("Type")->add_child_text ("TextSubtitle");
84         Content::as_xml (node, with_paths);
85
86         if (only_text()) {
87                 only_text()->as_xml (node);
88         }
89
90         node->add_child("Length")->add_child_text (raw_convert<string> (_length.get ()));
91 }
92
93 DCPTime
94 StringTextFileContent::full_length (shared_ptr<const Film> film) const
95 {
96         FrameRateChange const frc (film, shared_from_this());
97         return DCPTime (_length, frc);
98 }
99
100 DCPTime
101 StringTextFileContent::approximate_length () const
102 {
103         return DCPTime (_length, FrameRateChange());
104 }
105
106 string
107 StringTextFileContent::identifier () const
108 {
109         string s = Content::identifier ();
110         s += "_" + only_text()->identifier();
111         return s;
112 }