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