deb72ece8059a738ff121240981b047401c05bbe
[libdcp.git] / src / subtitle_asset.h
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef LIBDCP_SUBTITLE_ASSET_H
21 #define LIBDCP_SUBTITLE_ASSET_H
22
23 #include "asset.h"
24 #include "dcp_time.h"
25 #include "subtitle_string.h"
26 #include <libcxml/cxml.h>
27
28 namespace dcp
29 {
30
31 class SubtitleString;   
32 class FontNode;
33 class TextNode;
34 class SubtitleNode;
35 class LoadFontNode;
36
37 /** @class SubtitleAsset
38  *  @brief A parent for classes representing a file containing subtitles.
39  */
40 class SubtitleAsset : public Asset
41 {
42 public:
43         SubtitleAsset ();
44         SubtitleAsset (boost::filesystem::path file);
45
46         bool equals (
47                 boost::shared_ptr<const Asset>,
48                 EqualityOptions,
49                 NoteHandler note
50                 ) const;
51
52         std::string language () const {
53                 return _language;
54         }
55
56         std::list<SubtitleString> subtitles_during (Time from, Time to) const;
57         std::list<SubtitleString> const & subtitles () const {
58                 return _subtitles;
59         }
60
61         void add (SubtitleString);
62
63         void write_xml (boost::filesystem::path) const;
64         virtual Glib::ustring xml_as_string () const {
65                 /* XXX: this should be pure virtual when SMPTE writing is implemented */
66                 return "";
67         }
68
69         Time latest_subtitle_out () const;
70
71         virtual std::list<boost::shared_ptr<LoadFontNode> > load_font_nodes () const = 0;
72
73 protected:
74         void parse_common (boost::shared_ptr<cxml::Document> xml, std::list<boost::shared_ptr<FontNode> > font_nodes);
75         
76         std::string pkl_type (Standard) const {
77                 return "text/xml";
78         }
79
80         std::string asdcp_kind () const {
81                 return "Subtitle";
82         }
83         
84         /* strangely, this is sometimes a string */
85         std::string _reel_number;
86         std::string _language;
87
88         std::list<SubtitleString> _subtitles;
89
90 private:
91         struct ParseState {
92                 std::list<boost::shared_ptr<FontNode> > font_nodes;
93                 std::list<boost::shared_ptr<TextNode> > text_nodes;
94                 std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes;
95         };
96
97         void maybe_add_subtitle (std::string text, ParseState const & parse_state);
98         
99         void examine_font_nodes (
100                 boost::shared_ptr<const cxml::Node> xml,
101                 std::list<boost::shared_ptr<FontNode> > const & font_nodes,
102                 ParseState& parse_state
103                 );
104         
105         void examine_text_nodes (
106                 boost::shared_ptr<const cxml::Node> xml,
107                 std::list<boost::shared_ptr<TextNode> > const & text_nodes,
108                 ParseState& parse_state
109                 );
110 };
111
112 }
113
114 #endif