Fix missing version string when Popen communicate returns byte strings.
[libdcp.git] / src / subtitle_asset.h
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #ifndef LIBDCP_SUBTITLE_ASSET_H
35 #define LIBDCP_SUBTITLE_ASSET_H
36
37 #include "asset.h"
38 #include "dcp_time.h"
39 #include "subtitle_string.h"
40 #include "data.h"
41 #include <libcxml/cxml.h>
42 #include <boost/shared_array.hpp>
43 #include <map>
44
45 namespace xmlpp {
46         class Element;
47 }
48
49 struct interop_dcp_font_test;
50 struct smpte_dcp_font_test;
51 struct pull_fonts_test1;
52 struct pull_fonts_test2;
53 struct pull_fonts_test3;
54
55 namespace dcp
56 {
57
58 class SubtitleString;
59 class SubtitleImage;
60 class FontNode;
61 class TextNode;
62 class SubtitleNode;
63 class LoadFontNode;
64
65 namespace order {
66         class Part;
67         class Context;
68 }
69
70 /** @class SubtitleAsset
71  *  @brief A parent for classes representing a file containing subtitles.
72  *
73  *  This class holds a list of Subtitle objects which it can extract
74  *  from the appropriate part of either an Interop or SMPTE XML file.
75  *  Its subclasses InteropSubtitleAsset and SMPTESubtitleAsset handle the
76  *  differences between the two types.
77  */
78 class SubtitleAsset : public Asset
79 {
80 public:
81         SubtitleAsset ();
82         explicit SubtitleAsset (boost::filesystem::path file);
83
84         bool equals (
85                 boost::shared_ptr<const Asset>,
86                 EqualityOptions,
87                 NoteHandler note
88                 ) const;
89
90         std::list<boost::shared_ptr<Subtitle> > subtitles_during (Time from, Time to, bool starting) const;
91         std::list<boost::shared_ptr<Subtitle> > const & subtitles () const {
92                 return _subtitles;
93         }
94
95         virtual void add (boost::shared_ptr<Subtitle>);
96         virtual void add_font (std::string id, boost::filesystem::path file) = 0;
97         std::map<std::string, Data> fonts_with_load_ids () const;
98
99         virtual void write (boost::filesystem::path) const = 0;
100         virtual std::string xml_as_string () const = 0;
101
102         Time latest_subtitle_out () const;
103
104         virtual std::list<boost::shared_ptr<LoadFontNode> > load_font_nodes () const = 0;
105
106 protected:
107         friend struct ::interop_dcp_font_test;
108         friend struct ::smpte_dcp_font_test;
109
110         struct ParseState {
111                 boost::optional<std::string> font_id;
112                 boost::optional<int64_t> size;
113                 boost::optional<float> aspect_adjust;
114                 boost::optional<bool> italic;
115                 boost::optional<bool> bold;
116                 boost::optional<bool> underline;
117                 boost::optional<Colour> colour;
118                 boost::optional<Effect> effect;
119                 boost::optional<Colour> effect_colour;
120                 boost::optional<float> h_position;
121                 boost::optional<HAlign> h_align;
122                 boost::optional<float> v_position;
123                 boost::optional<VAlign> v_align;
124                 boost::optional<Direction> direction;
125                 boost::optional<Time> in;
126                 boost::optional<Time> out;
127                 boost::optional<Time> fade_up_time;
128                 boost::optional<Time> fade_down_time;
129                 enum Type {
130                         TEXT,
131                         IMAGE
132                 };
133                 boost::optional<Type> type;
134         };
135
136         void parse_subtitles (xmlpp::Element const * node, std::list<ParseState>& state, boost::optional<int> tcr, Standard standard);
137         ParseState font_node_state (xmlpp::Element const * node, Standard standard) const;
138         ParseState text_node_state (xmlpp::Element const * node) const;
139         ParseState image_node_state (xmlpp::Element const * node) const;
140         ParseState subtitle_node_state (xmlpp::Element const * node, boost::optional<int> tcr) const;
141         Time fade_time (xmlpp::Element const * node, std::string name, boost::optional<int> tcr) const;
142         void position_align (ParseState& ps, xmlpp::Element const * node) const;
143
144         void subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const;
145
146         /** All our subtitles, in no particular order */
147         std::list<boost::shared_ptr<Subtitle> > _subtitles;
148
149         class Font
150         {
151         public:
152                 Font (std::string load_id_, std::string uuid_, boost::filesystem::path file_)
153                         : load_id (load_id_)
154                         , uuid (uuid_)
155                         , data (file_)
156                         , file (file_)
157                 {}
158
159                 Font (std::string load_id_, std::string uuid_, Data data_)
160                         : load_id (load_id_)
161                         , uuid (uuid_)
162                         , data (data_)
163                 {}
164
165                 std::string load_id;
166                 std::string uuid;
167                 Data data;
168                 /** .ttf file that this data was last written to, if applicable */
169                 mutable boost::optional<boost::filesystem::path> file;
170         };
171
172         /** TTF font data that we need */
173         std::list<Font> _fonts;
174
175 private:
176         friend struct ::pull_fonts_test1;
177         friend struct ::pull_fonts_test2;
178         friend struct ::pull_fonts_test3;
179
180         void maybe_add_subtitle (std::string text, std::list<ParseState> const & parse_state, Standard standard);
181
182         static void pull_fonts (boost::shared_ptr<order::Part> part);
183 };
184
185 }
186
187 #endif