2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 /** @file src/subtitle_asset.h
36 * @brief SubtitleAsset class
40 #ifndef LIBDCP_SUBTITLE_ASSET_H
41 #define LIBDCP_SUBTITLE_ASSET_H
44 #include "array_data.h"
47 #include "subtitle_standard.h"
48 #include "subtitle_string.h"
49 #include <libcxml/cxml.h>
50 #include <boost/shared_array.hpp>
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;
88 /** @class SubtitleAsset
89 * @brief A parent for classes representing a file containing subtitles
91 * This class holds a list of Subtitle objects which it can extract
92 * from the appropriate part of either an Interop or SMPTE XML file.
93 * Its subclasses InteropSubtitleAsset and SMPTESubtitleAsset handle the
94 * differences between the two types.
96 class SubtitleAsset : public Asset
100 explicit SubtitleAsset (boost::filesystem::path file);
103 std::shared_ptr<const Asset>,
108 std::vector<std::shared_ptr<const Subtitle>> subtitles_during (Time from, Time to, bool starting) const;
109 std::vector<std::shared_ptr<const Subtitle>> subtitles_in_reel(std::shared_ptr<const dcp::ReelAsset> asset) const;
110 std::vector<std::shared_ptr<const Subtitle>> subtitles () const;
112 virtual void add (std::shared_ptr<Subtitle>);
113 virtual void add_font (std::string id, dcp::ArrayData data) = 0;
114 void ensure_font(std::string id, dcp::ArrayData data);
115 std::map<std::string, ArrayData> font_data () const;
116 std::map<std::string, boost::filesystem::path> font_filenames () const;
118 virtual void write (boost::filesystem::path) const = 0;
119 virtual std::string xml_as_string () const = 0;
121 Time latest_subtitle_out () const;
123 void fix_empty_font_ids ();
125 virtual std::vector<std::shared_ptr<LoadFontNode>> load_font_nodes () const = 0;
127 virtual int time_code_rate () const = 0;
129 /** @return Raw XML loaded from, or written to, an on-disk asset, or boost::none if
130 * - this object was not created from an existing on-disk asset and has not been written to one, or
131 * - this asset is encrypted and no key is available.
133 virtual boost::optional<std::string> raw_xml () const {
137 virtual SubtitleStandard subtitle_standard() const = 0;
139 static std::string format_xml(xmlpp::Document const& document, boost::optional<std::pair<std::string, std::string>> xml_namespace);
142 friend struct ::interop_dcp_font_test;
143 friend struct ::smpte_dcp_font_test;
146 boost::optional<std::string> font_id;
147 boost::optional<int64_t> size;
148 boost::optional<float> aspect_adjust;
149 boost::optional<bool> italic;
150 boost::optional<bool> bold;
151 boost::optional<bool> underline;
152 boost::optional<Colour> colour;
153 boost::optional<Effect> effect;
154 boost::optional<Colour> effect_colour;
155 boost::optional<float> h_position;
156 boost::optional<HAlign> h_align;
157 boost::optional<float> v_position;
158 boost::optional<VAlign> v_align;
159 boost::optional<float> z_position;
160 boost::optional<Direction> direction;
161 boost::optional<Time> in;
162 boost::optional<Time> out;
163 boost::optional<Time> fade_up_time;
164 boost::optional<Time> fade_down_time;
169 boost::optional<Type> type;
170 float space_before = 0;
173 void parse_subtitles (xmlpp::Element const * node, std::vector<ParseState>& state, boost::optional<int> tcr, Standard standard);
174 ParseState font_node_state (xmlpp::Element const * node, Standard standard) const;
175 ParseState text_node_state (xmlpp::Element const * node) const;
176 ParseState image_node_state (xmlpp::Element const * node) const;
177 ParseState subtitle_node_state (xmlpp::Element const * node, boost::optional<int> tcr) const;
178 Time fade_time (xmlpp::Element const * node, std::string name, boost::optional<int> tcr) const;
179 void position_align (ParseState& ps, xmlpp::Element const * node) const;
181 void subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const;
183 /** All our subtitles, in no particular order */
184 std::vector<std::shared_ptr<Subtitle>> _subtitles;
189 Font (std::string load_id_, std::string uuid_, boost::filesystem::path file_)
196 Font (std::string load_id_, std::string uuid_, ArrayData data_)
205 /** .ttf file that this data was last written to, if applicable */
206 mutable boost::optional<boost::filesystem::path> file;
209 /** TTF font data that we need */
210 std::vector<Font> _fonts;
212 /** The raw XML data that we read from or wrote to our asset; useful for validation */
213 mutable boost::optional<std::string> _raw_xml;
216 friend struct ::pull_fonts_test1;
217 friend struct ::pull_fonts_test2;
218 friend struct ::pull_fonts_test3;
220 void maybe_add_subtitle (std::string text, std::vector<ParseState> const & parse_state, float space_before, Standard standard);
222 static void pull_fonts (std::shared_ptr<order::Part> part);