c00c3f310a650197e3abf502c1d3327b14156d40
[libdcp.git] / src / subtitle_content.h
1 /*
2     Copyright (C) 2012-2014 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_CONTENT_H
21 #define LIBDCP_SUBTITLE_CONTENT_H
22
23 #include "content.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 Font;
33 class Text;
34 class Subtitle;
35 class LoadFont;
36
37 /** @class SubtitleContent
38  *  @brief A representation of an XML or MXF file containing subtitles.
39  *
40  *  XXX: perhaps this should inhert from MXF, or there should be different
41  *  classes for XML and MXF subs.
42  */
43 class SubtitleContent : public Content
44 {
45 public:
46         /** Construct a SubtitleContent.
47          *  @param file Filename.
48          *  @param mxf true if the file is an MXF file, false for XML.
49          */
50         SubtitleContent (boost::filesystem::path file, bool mxf);
51         SubtitleContent (Fraction edit_rate, std::string movie_title, std::string language);
52
53         bool equals (
54                 boost::shared_ptr<const Content>,
55                 EqualityOptions,
56                 boost::function<void (NoteType, std::string)> note
57                 ) const {
58                 /* XXX */
59                 note (DCP_ERROR, "subtitle content not compared yet");
60                 return true;
61         }
62
63         std::string language () const {
64                 return _language;
65         }
66
67         std::list<SubtitleString> subtitles_at (Time t) const;
68         std::list<SubtitleString> const & subtitles () const {
69                 return _subtitles;
70         }
71
72         void add (SubtitleString);
73
74         void write_xml (boost::filesystem::path) const;
75         Glib::ustring xml_as_string () const;
76
77 protected:
78         std::string pkl_type (Standard) const {
79                 return "text/xml";
80         }
81
82         std::string asdcp_kind () const {
83                 return "Subtitle";
84         }
85         
86 private:
87         std::string font_id_to_name (std::string id) const;
88
89         struct ParseState {
90                 std::list<boost::shared_ptr<Font> > font_nodes;
91                 std::list<boost::shared_ptr<Text> > text_nodes;
92                 std::list<boost::shared_ptr<Subtitle> > subtitle_nodes;
93         };
94
95         void maybe_add_subtitle (std::string text, ParseState const & parse_state);
96         
97         void examine_font_nodes (
98                 boost::shared_ptr<const cxml::Node> xml,
99                 std::list<boost::shared_ptr<Font> > const & font_nodes,
100                 ParseState& parse_state
101                 );
102         
103         void examine_text_nodes (
104                 boost::shared_ptr<const cxml::Node> xml,
105                 std::list<boost::shared_ptr<Text> > const & text_nodes,
106                 ParseState& parse_state
107                 );
108
109         boost::optional<std::string> _movie_title;
110         /* strangely, this is sometimes a string */
111         std::string _reel_number;
112         std::string _language;
113         std::list<boost::shared_ptr<LoadFont> > _load_font_nodes;
114
115         std::list<SubtitleString> _subtitles;
116         bool _need_sort;
117 };
118
119 }
120
121 #endif