Add OpenSSL licence exception.
[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
52 namespace dcp
53 {
54
55 class SubtitleString;
56 class FontNode;
57 class TextNode;
58 class SubtitleNode;
59 class LoadFontNode;
60
61 /** @class SubtitleAsset
62  *  @brief A parent for classes representing a file containing subtitles.
63  *
64  *  This class holds a list of SubtitleString objects which it can extract
65  *  from the appropriate part of either an Interop or SMPTE XML file.
66  *  Its subclasses InteropSubtitleAsset and SMPTESubtitleAsset handle the
67  *  differences between the two types.
68  */
69 class SubtitleAsset : public Asset
70 {
71 public:
72         SubtitleAsset ();
73         explicit SubtitleAsset (boost::filesystem::path file);
74
75         bool equals (
76                 boost::shared_ptr<const Asset>,
77                 EqualityOptions,
78                 NoteHandler note
79                 ) const;
80
81         std::list<SubtitleString> subtitles_during (Time from, Time to, bool starting) const;
82         std::list<SubtitleString> const & subtitles () const {
83                 return _subtitles;
84         }
85
86         virtual void add (SubtitleString);
87         virtual void add_font (std::string id, boost::filesystem::path file) = 0;
88         std::map<std::string, Data> fonts_with_load_ids () const;
89
90         virtual void write (boost::filesystem::path) const = 0;
91         virtual std::string xml_as_string () const = 0;
92
93         Time latest_subtitle_out () const;
94
95         virtual std::list<boost::shared_ptr<LoadFontNode> > load_font_nodes () const = 0;
96
97 protected:
98         friend struct ::interop_dcp_font_test;
99         friend struct ::smpte_dcp_font_test;
100
101         void parse_subtitles (
102                 boost::shared_ptr<cxml::Document> xml,
103                 std::list<boost::shared_ptr<FontNode> > font_nodes,
104                 std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes
105                 );
106
107         void subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const;
108
109         /** All our subtitles, in no particular order */
110         std::list<SubtitleString> _subtitles;
111
112         class Font
113         {
114         public:
115                 Font (std::string load_id_, std::string uuid_, boost::filesystem::path file_)
116                         : load_id (load_id_)
117                         , uuid (uuid_)
118                         , data (file_)
119                         , file (file_)
120                 {}
121
122                 Font (std::string load_id_, std::string uuid_, Data data_)
123                         : load_id (load_id_)
124                         , uuid (uuid_)
125                         , data (data_)
126                 {}
127
128                 std::string load_id;
129                 std::string uuid;
130                 Data data;
131                 /** .ttf file that this data was last written to, if applicable */
132                 mutable boost::optional<boost::filesystem::path> file;
133         };
134
135         std::list<Font> _fonts;
136
137 private:
138         /** @struct ParseState
139          *  @brief  A struct to hold state when parsing a subtitle XML file.
140          */
141         struct ParseState {
142                 std::list<boost::shared_ptr<FontNode> > font_nodes;
143                 std::list<boost::shared_ptr<TextNode> > text_nodes;
144                 std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes;
145         };
146
147         void maybe_add_subtitle (std::string text, ParseState const & parse_state);
148
149         void examine_nodes (
150                 boost::shared_ptr<const cxml::Node> xml,
151                 std::list<boost::shared_ptr<FontNode> > const & font_nodes,
152                 ParseState& parse_state
153                 );
154
155         void examine_nodes (
156                 boost::shared_ptr<const cxml::Node> xml,
157                 std::list<boost::shared_ptr<TextNode> > const & text_nodes,
158                 ParseState& parse_state
159                 );
160
161         void examine_nodes (
162                 boost::shared_ptr<const cxml::Node> xml,
163                 std::list<boost::shared_ptr<SubtitleNode> > const & subtitle_nodes,
164                 ParseState& parse_state
165                 );
166 };
167
168 }
169
170 #endif