libdcp
text_asset.h
Go to the documentation of this file.
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 
40 #ifndef LIBDCP_TEXT_ASSET_H
41 #define LIBDCP_TEXT_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 "text_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 FontNode;
74 class LoadFontNode;
75 class ReelAsset;
76 class SubtitleNode;
77 class TextImage;
78 class TextNode;
79 class TextString;
80 
81 
82 namespace order {
83  class Part;
84  struct Context;
85 }
86 
87 
96 class TextAsset : public Asset
97 {
98 public:
99  TextAsset();
100  explicit TextAsset(boost::filesystem::path file);
101 
102  bool equals (
103  std::shared_ptr<const Asset>,
104  EqualityOptions const&,
105  NoteHandler note
106  ) const override;
107 
108  std::vector<std::shared_ptr<const Text>> texts_during(Time from, Time to, bool starting) const;
109  std::vector<std::shared_ptr<const Text>> texts() const;
110 
111  virtual void add(std::shared_ptr<Text>);
112  virtual void add_font (std::string id, dcp::ArrayData data) = 0;
113  void ensure_font(std::string id, dcp::ArrayData data);
114  std::map<std::string, ArrayData> font_data () const;
115  std::map<std::string, boost::filesystem::path> font_filenames () const;
116 
117  virtual void write (boost::filesystem::path) const = 0;
118  virtual std::string xml_as_string () const = 0;
119 
120  Time latest_text_out() const;
121 
122  void fix_empty_font_ids ();
123 
124  virtual std::vector<std::shared_ptr<LoadFontNode>> load_font_nodes () const = 0;
125 
126  virtual int time_code_rate () const = 0;
127 
132  virtual boost::optional<std::string> raw_xml () const {
133  return _raw_xml;
134  }
135 
136  virtual SubtitleStandard subtitle_standard() const = 0;
137 
138  static std::string format_xml(xmlpp::Document const& document, boost::optional<std::pair<std::string, std::string>> xml_namespace);
139 
140 protected:
141  friend struct ::interop_dcp_font_test;
142  friend struct ::smpte_dcp_font_test;
143 
144  struct ParseState {
145  boost::optional<std::string> font_id;
146  boost::optional<int64_t> size;
147  boost::optional<float> aspect_adjust;
148  boost::optional<bool> italic;
149  boost::optional<bool> bold;
150  boost::optional<bool> underline;
151  boost::optional<Colour> colour;
152  boost::optional<Effect> effect;
153  boost::optional<Colour> effect_colour;
154  boost::optional<float> h_position;
155  boost::optional<HAlign> h_align;
156  boost::optional<float> v_position;
157  boost::optional<VAlign> v_align;
158  boost::optional<float> z_position;
159  boost::optional<Direction> direction;
160  boost::optional<Time> in;
161  boost::optional<Time> out;
162  boost::optional<Time> fade_up_time;
163  boost::optional<Time> fade_down_time;
164  enum class Type {
165  TEXT,
166  IMAGE
167  };
168  boost::optional<Type> type;
169  float space_before = 0;
170  };
171 
172  void parse_texts(xmlpp::Element const * node, std::vector<ParseState>& state, boost::optional<int> tcr, Standard standard);
173  ParseState font_node_state (xmlpp::Element const * node, Standard standard) const;
174  ParseState text_node_state (xmlpp::Element const * node) const;
175  ParseState image_node_state (xmlpp::Element const * node) const;
176  ParseState subtitle_node_state (xmlpp::Element const * node, boost::optional<int> tcr) const;
177  Time fade_time (xmlpp::Element const * node, std::string name, boost::optional<int> tcr) const;
178  void position_align (ParseState& ps, xmlpp::Element const * node) const;
179 
180  void texts_as_xml(xmlpp::Element* root, int time_code_rate, Standard standard) const;
181 
183  std::vector<std::shared_ptr<Text>> _texts;
184 
185  class Font
186  {
187  public:
188  Font (std::string load_id_, std::string uuid_, boost::filesystem::path file_)
189  : load_id (load_id_)
190  , uuid (uuid_)
191  , data (file_)
192  , file (file_)
193  {}
194 
195  Font (std::string load_id_, std::string uuid_, ArrayData data_)
196  : load_id (load_id_)
197  , uuid (uuid_)
198  , data (data_)
199  {}
200 
201  std::string load_id;
202  std::string uuid;
203  ArrayData data;
205  mutable boost::optional<boost::filesystem::path> file;
206  };
207 
209  std::vector<Font> _fonts;
210 
212  mutable boost::optional<std::string> _raw_xml;
213 
214 private:
215  friend struct ::pull_fonts_test1;
216  friend struct ::pull_fonts_test2;
217  friend struct ::pull_fonts_test3;
218 
219  void maybe_add_text(
220  std::string text,
221  std::vector<ParseState> const & parse_state,
222  float space_before,
223  Standard standard,
224  std::vector<Ruby> const& rubies
225  );
226 
227  static void pull_fonts (std::shared_ptr<order::Part> part);
228 };
229 
230 
231 }
232 
233 
234 #endif
ArrayData class.
Asset class.
Class to hold an arbitrary block of data.
Definition: array_data.h:55
Parent class for DCP assets, i.e. picture, sound, subtitles, closed captions, CPLs,...
Definition: asset.h:73
boost::optional< boost::filesystem::path > file() const
Definition: asset.h:100
A class to describe what "equality" means for a particular test.
boost::optional< boost::filesystem::path > file
Definition: text_asset.h:205
A parent for classes representing a file containing subtitles or captions.
Definition: text_asset.h:97
boost::optional< std::string > _raw_xml
Definition: text_asset.h:212
void fix_empty_font_ids()
Definition: text_asset.cc:854
void texts_as_xml(xmlpp::Element *root, int time_code_rate, Standard standard) const
Definition: text_asset.cc:727
std::vector< std::shared_ptr< Text > > _texts
Definition: text_asset.h:183
std::vector< Font > _fonts
Definition: text_asset.h:209
static std::string format_xml(xmlpp::Document const &document, boost::optional< std::pair< std::string, std::string >> xml_namespace)
Definition: text_asset.cc:967
virtual boost::optional< std::string > raw_xml() const
Definition: text_asset.h:132
A representation of time within a DCP.
Definition: dcp_time.h:73
Time class.
Namespace for everything in libdcp.
Definition: array_data.h:50
TextString class.