Pass optimisation setting through to subtitle XML writers.
[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_standard.h"
48 #include "subtitle_string.h"
49 #include <libcxml/cxml.h>
50 #include <boost/shared_array.hpp>
51 #include <map>
52 #include <string>
53 #include <utility>
54 #include <vector>
55
56
57 namespace xmlpp {
58         class Document;
59         class Element;
60 }
61
62
63 struct interop_dcp_font_test;
64 struct smpte_dcp_font_test;
65 struct pull_fonts_test1;
66 struct pull_fonts_test2;
67 struct pull_fonts_test3;
68
69
70 namespace dcp {
71
72
73 class SubtitleString;
74 class SubtitleImage;
75 class FontNode;
76 class TextNode;
77 class SubtitleNode;
78 class LoadFontNode;
79 class ReelAsset;
80
81
82 namespace order {
83         class Part;
84         struct Context;
85 }
86
87
88 enum class SubtitleOptimisation {
89         FOR_SUBTITLE,
90         FOR_CCAP,
91 };
92
93
94 /** @class SubtitleAsset
95  *  @brief A parent for classes representing a file containing subtitles
96  *
97  *  This class holds a list of Subtitle objects which it can extract
98  *  from the appropriate part of either an Interop or SMPTE XML file.
99  *  Its subclasses InteropSubtitleAsset and SMPTESubtitleAsset handle the
100  *  differences between the two types.
101  */
102 class SubtitleAsset : public Asset
103 {
104 public:
105         SubtitleAsset ();
106         explicit SubtitleAsset (boost::filesystem::path file);
107
108         bool equals (
109                 std::shared_ptr<const Asset>,
110                 EqualityOptions const&,
111                 NoteHandler note
112                 ) const override;
113
114         std::vector<std::shared_ptr<const Subtitle>> subtitles_during (Time from, Time to, bool starting) const;
115         std::vector<std::shared_ptr<const Subtitle>> subtitles () const;
116
117         virtual void add (std::shared_ptr<Subtitle>);
118         virtual void add_font (std::string id, dcp::ArrayData data) = 0;
119         void ensure_font(std::string id, dcp::ArrayData data);
120         std::map<std::string, ArrayData> font_data () const;
121         std::map<std::string, boost::filesystem::path> font_filenames () const;
122
123         virtual void write(boost::filesystem::path, SubtitleOptimisation optimisation = SubtitleOptimisation::FOR_SUBTITLE) const = 0;
124         virtual std::string xml_as_string(SubtitleOptimisation optimisation = SubtitleOptimisation::FOR_SUBTITLE) const = 0;
125
126         Time latest_subtitle_out () const;
127
128         void fix_empty_font_ids ();
129
130         virtual std::vector<std::shared_ptr<LoadFontNode>> load_font_nodes () const = 0;
131
132         virtual int time_code_rate () const = 0;
133
134         /** @return Raw XML loaded from, or written to, an on-disk asset, or boost::none if
135          *  - this object was not created from an existing on-disk asset and has not been written to one, or
136          *  - this asset is encrypted and no key is available.
137          */
138         virtual boost::optional<std::string> raw_xml () const {
139                 return _raw_xml;
140         }
141
142         virtual SubtitleStandard subtitle_standard() const = 0;
143
144         static std::string format_xml(xmlpp::Document const& document, boost::optional<std::pair<std::string, std::string>> xml_namespace);
145
146 protected:
147         friend struct ::interop_dcp_font_test;
148         friend struct ::smpte_dcp_font_test;
149
150         struct ParseState {
151                 boost::optional<std::string> font_id;
152                 boost::optional<int64_t> size;
153                 boost::optional<float> aspect_adjust;
154                 boost::optional<bool> italic;
155                 boost::optional<bool> bold;
156                 boost::optional<bool> underline;
157                 boost::optional<Colour> colour;
158                 boost::optional<Effect> effect;
159                 boost::optional<Colour> effect_colour;
160                 boost::optional<float> h_position;
161                 boost::optional<HAlign> h_align;
162                 boost::optional<float> v_position;
163                 boost::optional<VAlign> v_align;
164                 boost::optional<float> z_position;
165                 boost::optional<Direction> direction;
166                 boost::optional<Time> in;
167                 boost::optional<Time> out;
168                 boost::optional<Time> fade_up_time;
169                 boost::optional<Time> fade_down_time;
170                 enum class Type {
171                         TEXT,
172                         IMAGE
173                 };
174                 boost::optional<Type> type;
175                 float space_before = 0;
176         };
177
178         void parse_subtitles (xmlpp::Element const * node, std::vector<ParseState>& state, boost::optional<int> tcr, Standard standard);
179         ParseState font_node_state (xmlpp::Element const * node, Standard standard) const;
180         ParseState text_node_state (xmlpp::Element const * node) const;
181         ParseState image_node_state (xmlpp::Element const * node) const;
182         ParseState subtitle_node_state (xmlpp::Element const * node, boost::optional<int> tcr) const;
183         Time fade_time (xmlpp::Element const * node, std::string name, boost::optional<int> tcr) const;
184         void position_align (ParseState& ps, xmlpp::Element const * node) const;
185
186         void subtitles_as_xml(xmlpp::Element* root, int time_code_rate, Standard standard, SubtitleOptimisation optimisation = SubtitleOptimisation::FOR_SUBTITLE) const;
187
188         /** All our subtitles, in no particular order */
189         std::vector<std::shared_ptr<Subtitle>> _subtitles;
190
191         class Font
192         {
193         public:
194                 Font (std::string load_id_, std::string uuid_, boost::filesystem::path file_)
195                         : load_id (load_id_)
196                         , uuid (uuid_)
197                         , data (file_)
198                         , file (file_)
199                 {}
200
201                 Font (std::string load_id_, std::string uuid_, ArrayData data_)
202                         : load_id (load_id_)
203                         , uuid (uuid_)
204                         , data (data_)
205                 {}
206
207                 std::string load_id;
208                 std::string uuid;
209                 ArrayData data;
210                 /** .ttf file that this data was last written to, if applicable */
211                 mutable boost::optional<boost::filesystem::path> file;
212         };
213
214         /** TTF font data that we need */
215         std::vector<Font> _fonts;
216
217         /** The raw XML data that we read from or wrote to our asset; useful for validation */
218         mutable boost::optional<std::string> _raw_xml;
219
220 private:
221         friend struct ::pull_fonts_test1;
222         friend struct ::pull_fonts_test2;
223         friend struct ::pull_fonts_test3;
224
225         void maybe_add_subtitle(
226                 std::string text,
227                 std::vector<ParseState> const & parse_state,
228                 float space_before,
229                 Standard standard,
230                 std::vector<Ruby> const& rubies
231                 );
232
233         static void pull_fonts (std::shared_ptr<order::Part> part);
234 };
235
236
237 }
238
239
240 #endif