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