Rename Data -> ArrayData.
[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
38 #include "array_data.h"
39 #include "asset.h"
40 #include "dcp_time.h"
41 #include "subtitle_string.h"
42 #include <libcxml/cxml.h>
43 #include <boost/shared_array.hpp>
44 #include <map>
45
46 namespace xmlpp {
47         class Element;
48 }
49
50 struct interop_dcp_font_test;
51 struct smpte_dcp_font_test;
52 struct pull_fonts_test1;
53 struct pull_fonts_test2;
54 struct pull_fonts_test3;
55
56 namespace dcp
57 {
58
59 class SubtitleString;
60 class SubtitleImage;
61 class FontNode;
62 class TextNode;
63 class SubtitleNode;
64 class LoadFontNode;
65
66 namespace order {
67         class Part;
68         struct Context;
69 }
70
71 /** @class SubtitleAsset
72  *  @brief A parent for classes representing a file containing subtitles.
73  *
74  *  This class holds a list of Subtitle objects which it can extract
75  *  from the appropriate part of either an Interop or SMPTE XML file.
76  *  Its subclasses InteropSubtitleAsset and SMPTESubtitleAsset handle the
77  *  differences between the two types.
78  */
79 class SubtitleAsset : public Asset
80 {
81 public:
82         SubtitleAsset ();
83         explicit SubtitleAsset (boost::filesystem::path file);
84
85         bool equals (
86                 boost::shared_ptr<const Asset>,
87                 EqualityOptions,
88                 NoteHandler note
89                 ) const;
90
91         std::list<boost::shared_ptr<Subtitle> > subtitles_during (Time from, Time to, bool starting) const;
92         std::list<boost::shared_ptr<Subtitle> > const & subtitles () const {
93                 return _subtitles;
94         }
95
96         virtual void add (boost::shared_ptr<Subtitle>);
97         virtual void add_font (std::string id, boost::filesystem::path file) = 0;
98         std::map<std::string, ArrayData> font_data () const;
99         std::map<std::string, boost::filesystem::path> font_filenames () const;
100
101         virtual void write (boost::filesystem::path) const = 0;
102         virtual std::string xml_as_string () const = 0;
103
104         Time latest_subtitle_out () const;
105
106         void fix_empty_font_ids ();
107
108         virtual std::list<boost::shared_ptr<LoadFontNode> > load_font_nodes () const = 0;
109
110         std::string raw_xml () const {
111                 return _raw_xml;
112         }
113
114 protected:
115         friend struct ::interop_dcp_font_test;
116         friend struct ::smpte_dcp_font_test;
117
118         struct ParseState {
119                 boost::optional<std::string> font_id;
120                 boost::optional<int64_t> size;
121                 boost::optional<float> aspect_adjust;
122                 boost::optional<bool> italic;
123                 boost::optional<bool> bold;
124                 boost::optional<bool> underline;
125                 boost::optional<Colour> colour;
126                 boost::optional<Effect> effect;
127                 boost::optional<Colour> effect_colour;
128                 boost::optional<float> h_position;
129                 boost::optional<HAlign> h_align;
130                 boost::optional<float> v_position;
131                 boost::optional<VAlign> v_align;
132                 boost::optional<Direction> direction;
133                 boost::optional<Time> in;
134                 boost::optional<Time> out;
135                 boost::optional<Time> fade_up_time;
136                 boost::optional<Time> fade_down_time;
137                 enum Type {
138                         TEXT,
139                         IMAGE
140                 };
141                 boost::optional<Type> type;
142         };
143
144         void parse_subtitles (xmlpp::Element const * node, std::list<ParseState>& state, boost::optional<int> tcr, Standard standard);
145         ParseState font_node_state (xmlpp::Element const * node, Standard standard) const;
146         ParseState text_node_state (xmlpp::Element const * node) const;
147         ParseState image_node_state (xmlpp::Element const * node) const;
148         ParseState subtitle_node_state (xmlpp::Element const * node, boost::optional<int> tcr) const;
149         Time fade_time (xmlpp::Element const * node, std::string name, boost::optional<int> tcr) const;
150         void position_align (ParseState& ps, xmlpp::Element const * node) const;
151
152         void subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const;
153
154         /** All our subtitles, in no particular order */
155         std::list<boost::shared_ptr<Subtitle> > _subtitles;
156
157         class Font
158         {
159         public:
160                 Font (std::string load_id_, std::string uuid_, boost::filesystem::path file_)
161                         : load_id (load_id_)
162                         , uuid (uuid_)
163                         , data (file_)
164                         , file (file_)
165                 {}
166
167                 Font (std::string load_id_, std::string uuid_, ArrayData data_)
168                         : load_id (load_id_)
169                         , uuid (uuid_)
170                         , data (data_)
171                 {}
172
173                 std::string load_id;
174                 std::string uuid;
175                 ArrayData data;
176                 /** .ttf file that this data was last written to, if applicable */
177                 mutable boost::optional<boost::filesystem::path> file;
178         };
179
180         /** TTF font data that we need */
181         std::list<Font> _fonts;
182
183         /** The raw XML data that we read from our asset; useful for validation */
184         std::string _raw_xml;
185
186 private:
187         friend struct ::pull_fonts_test1;
188         friend struct ::pull_fonts_test2;
189         friend struct ::pull_fonts_test3;
190
191         void maybe_add_subtitle (std::string text, std::list<ParseState> const & parse_state, Standard standard);
192
193         static void pull_fonts (boost::shared_ptr<order::Part> part);
194 };
195
196 }
197
198 #endif