Neater and more correct XML subtitle parser.
[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         struct ParseState {
102                 boost::optional<std::string> font_id;
103                 boost::optional<int64_t> size;
104                 boost::optional<float> aspect_adjust;
105                 boost::optional<bool> italic;
106                 boost::optional<bool> bold;
107                 boost::optional<bool> underline;
108                 boost::optional<Colour> colour;
109                 boost::optional<Effect> effect;
110                 boost::optional<Colour> effect_colour;
111                 boost::optional<float> h_position;
112                 boost::optional<HAlign> h_align;
113                 boost::optional<float> v_position;
114                 boost::optional<VAlign> v_align;
115                 boost::optional<Direction> direction;
116                 boost::optional<Time> in;
117                 boost::optional<Time> out;
118                 boost::optional<Time> fade_up_time;
119                 boost::optional<Time> fade_down_time;
120         };
121
122         void parse_subtitles (xmlpp::Element const * node, std::list<ParseState>& state, boost::optional<int> tcr, Standard standard);
123         ParseState font_node_state (xmlpp::Element const * node, Standard standard) const;
124         ParseState text_node_state (xmlpp::Element const * node) const;
125         ParseState subtitle_node_state (xmlpp::Element const * node, boost::optional<int> tcr) const;
126         Time fade_time (xmlpp::Element const * node, std::string name, boost::optional<int> tcr) const;
127
128         void subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const;
129
130         /** All our subtitles, in no particular order */
131         std::list<SubtitleString> _subtitles;
132
133         class Font
134         {
135         public:
136                 Font (std::string load_id_, std::string uuid_, boost::filesystem::path file_)
137                         : load_id (load_id_)
138                         , uuid (uuid_)
139                         , data (file_)
140                         , file (file_)
141                 {}
142
143                 Font (std::string load_id_, std::string uuid_, Data data_)
144                         : load_id (load_id_)
145                         , uuid (uuid_)
146                         , data (data_)
147                 {}
148
149                 std::string load_id;
150                 std::string uuid;
151                 Data data;
152                 /** .ttf file that this data was last written to, if applicable */
153                 mutable boost::optional<boost::filesystem::path> file;
154         };
155
156         std::list<Font> _fonts;
157
158 private:
159         void maybe_add_subtitle (std::string text, std::list<ParseState> const & parse_state);
160 };
161
162 }
163
164 #endif