335b9f37b0af22c3456b12b003348339d34e2d1a
[libdcp.git] / src / subtitle_asset.h
1 /*
2     Copyright (C) 2012 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 <libcxml/cxml.h>
24 #include "asset.h"
25 #include "dcp_time.h"
26
27 namespace libdcp
28 {
29
30 namespace parse
31 {
32         class Font;
33         class Text;
34         class Subtitle;
35         class LoadFont;
36 }
37
38 class Subtitle
39 {
40 public:
41         Subtitle (
42                 std::string font,
43                 bool italic,
44                 Color color,
45                 int size,
46                 Time in,
47                 Time out,
48                 float v_position,
49                 VAlign v_align,
50                 HAlign h_align,
51                 std::string text,
52                 Effect effect,
53                 Color effect_color,
54                 Time fade_up_time,
55                 Time fade_down_time
56                 );
57
58         std::string font () const {
59                 return _font;
60         }
61
62         bool italic () const {
63                 return _italic;
64         }
65
66         Color color () const {
67                 return _color;
68         }
69
70         Time in () const {
71                 return _in;
72         }
73
74         Time out () const {
75                 return _out;
76         }
77
78         std::string text () const {
79                 return _text;
80         }
81
82         void set_text (std::string t) {
83                 _text = t;
84         }
85
86         float v_position () const {
87                 return _v_position;
88         }
89
90         VAlign v_align () const {
91                 return _v_align;
92         }
93
94         HAlign h_align () const {
95                 return _h_align;
96         }
97
98         Effect effect () const {
99                 return _effect;
100         }
101
102         Color effect_color () const {
103                 return _effect_color;
104         }
105
106         Time fade_up_time () const {
107                 return _fade_up_time;
108         }
109
110         Time fade_down_time () const {
111                 return _fade_down_time;
112         }
113
114         int size () const {
115                 return _size;
116         }
117         
118         int size_in_pixels (int screen_height) const;
119
120 private:
121         std::string _font;
122         bool _italic;
123         Color _color;
124         /** Size in points as if the screen height is 11 inches, so a 72pt font
125          *  would be 1/11th of the screen height.
126          */ 
127         int _size;
128         Time _in;
129         Time _out;
130         /** Vertical position as a proportion of the screen height from the top
131          *  (between 0 and 100).
132          */
133         float _v_position;
134         VAlign _v_align;
135         HAlign _h_align;
136         std::string _text;
137         Effect _effect;
138         Color _effect_color;
139         Time _fade_up_time;
140         Time _fade_down_time;
141 };
142
143 bool operator== (Subtitle const & a, Subtitle const & b);
144 std::ostream& operator<< (std::ostream& s, Subtitle const & sub);
145
146 class SubtitleAsset : public Asset
147 {
148 public:
149         SubtitleAsset (std::string directory, std::string xml_file);
150         SubtitleAsset (std::string directory, std::string movie_title, std::string language);
151
152         void write_to_cpl (xmlpp::Element *) const;
153         virtual bool equals (boost::shared_ptr<const Asset>, EqualityOptions, boost::function<void (NoteType, std::string)> note) const {
154                 /* XXX */
155                 note (ERROR, "subtitle assets not compared yet");
156                 return true;
157         }
158
159         std::string language () const {
160                 return _language;
161         }
162
163         std::list<boost::shared_ptr<Subtitle> > subtitles_during (Time from, Time to) const;
164         std::list<boost::shared_ptr<Subtitle> > const & subtitles () const {
165                 return _subtitles;
166         }
167
168         void add (boost::shared_ptr<Subtitle>);
169
170         void read_xml (std::string);
171         void write_xml () const;
172         Glib::ustring xml_as_string () const;
173
174 protected:
175
176         std::string asdcp_kind () const {
177                 return "Subtitle";
178         }
179
180 private:
181         std::string font_id_to_name (std::string id) const;
182         void read_mxf (std::string);
183         void read_xml (boost::shared_ptr<cxml::Document>, bool smpte);
184
185         struct ParseState {
186                 std::list<boost::shared_ptr<parse::Font> > font_nodes;
187                 std::list<boost::shared_ptr<parse::Text> > text_nodes;
188                 std::list<boost::shared_ptr<parse::Subtitle> > subtitle_nodes;
189                 boost::shared_ptr<Subtitle> current;
190         };
191
192         void parse_node (xmlpp::Node* node, ParseState& parse_state, boost::optional<int> tcr);
193         void maybe_add_subtitle (std::string text, ParseState& parse_state);
194
195         boost::optional<std::string> _movie_title;
196         /* strangely, this is sometimes a string */
197         std::string _reel_number;
198         std::string _language;
199         std::list<boost::shared_ptr<parse::LoadFont> > _load_font_nodes;
200
201         std::list<boost::shared_ptr<Subtitle> > _subtitles;
202         bool _need_sort;
203 };
204
205 }
206
207 #endif