2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef LIBDCP_SUBTITLE_ASSET_H
21 #define LIBDCP_SUBTITLE_ASSET_H
25 #include "subtitle_string.h"
27 #include <libcxml/cxml.h>
28 #include <boost/shared_array.hpp>
34 struct interop_dcp_font_test;
35 struct smpte_dcp_font_test;
46 /** @class SubtitleAsset
47 * @brief A parent for classes representing a file containing subtitles.
49 * This class holds a list of SubtitleString objects which it can extract
50 * from the appropriate part of either an Interop or SMPTE XML file.
51 * Its subclasses InteropSubtitleAsset and SMPTESubtitleAsset handle the
52 * differences between the two types.
54 class SubtitleAsset : public Asset
58 SubtitleAsset (boost::filesystem::path file);
61 boost::shared_ptr<const Asset>,
66 std::list<SubtitleString> subtitles_during (Time from, Time to) const;
67 std::list<SubtitleString> const & subtitles () const {
71 void add (SubtitleString);
72 virtual void add_font (std::string id, boost::filesystem::path file) = 0;
73 std::map<std::string, Data> fonts () const;
75 virtual void write (boost::filesystem::path) const = 0;
76 virtual Glib::ustring xml_as_string () const = 0;
78 Time latest_subtitle_out () const;
80 virtual std::list<boost::shared_ptr<LoadFontNode> > load_font_nodes () const = 0;
83 friend struct ::interop_dcp_font_test;
84 friend struct ::smpte_dcp_font_test;
86 void parse_subtitles (boost::shared_ptr<cxml::Document> xml, std::list<boost::shared_ptr<FontNode> > font_nodes);
87 void subtitles_as_xml (xmlpp::Element* root, int time_code_rate, std::string xmlns) const;
88 void add_font_data (std::string id, boost::filesystem::path file);
90 /** All our subtitles, in no particular order */
91 std::list<SubtitleString> _subtitles;
93 class FileData : public Data {
97 FileData (boost::shared_array<uint8_t> data_, boost::uintmax_t size_)
101 FileData (boost::filesystem::path file_)
105 /** .ttf file that this data was last written to */
106 mutable boost::optional<boost::filesystem::path> file;
109 /** Font data, keyed by a subclass-dependent identifier.
110 * For Interop, the string is the font ID from the subtitle file.
111 * For SMPTE, the string is the font's URN from the subtitle file.
113 std::map<std::string, FileData> _fonts;
116 /** @struct ParseState
117 * @brief A struct to hold state when parsing a subtitle XML file.
120 std::list<boost::shared_ptr<FontNode> > font_nodes;
121 std::list<boost::shared_ptr<TextNode> > text_nodes;
122 std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes;
125 void maybe_add_subtitle (std::string text, ParseState const & parse_state);
127 void examine_font_nodes (
128 boost::shared_ptr<const cxml::Node> xml,
129 std::list<boost::shared_ptr<FontNode> > const & font_nodes,
130 ParseState& parse_state
133 void examine_text_nodes (
134 boost::shared_ptr<const cxml::Node> xml,
135 std::list<boost::shared_ptr<TextNode> > const & text_nodes,
136 ParseState& parse_state