Try to give basic progress indication on dcpdiff.
[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 #include "asset.h"
21 #include "xml.h"
22 #include "dcp_time.h"
23
24 namespace libdcp
25 {
26
27 class FontNode;
28
29 class TextNode : public XMLNode
30 {
31 public:
32         TextNode () {}
33         TextNode (xmlpp::Node const * node);
34
35         float v_position;
36         VAlign v_align;
37         std::string text;
38         std::list<boost::shared_ptr<FontNode> > font_nodes;
39 };
40
41 class SubtitleNode : public XMLNode
42 {
43 public:
44         SubtitleNode () {}
45         SubtitleNode (xmlpp::Node const * node);
46
47         Time in;
48         Time out;
49         Time fade_up_time;
50         Time fade_down_time;
51         std::list<boost::shared_ptr<FontNode> > font_nodes;
52         std::list<boost::shared_ptr<TextNode> > text_nodes;
53
54 private:
55         Time fade_time (std::string name);
56 };
57
58 class FontNode : public XMLNode
59 {
60 public:
61         FontNode () {}
62         FontNode (xmlpp::Node const * node);
63         FontNode (std::list<boost::shared_ptr<FontNode> > const & font_nodes);
64
65         std::string text;
66         std::string id;
67         int size;
68         boost::optional<bool> italic;
69         boost::optional<Color> color;
70         boost::optional<Effect> effect;
71         boost::optional<Color> effect_color;
72         
73         std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes;
74         std::list<boost::shared_ptr<FontNode> > font_nodes;
75         std::list<boost::shared_ptr<TextNode> > text_nodes;
76 };
77
78 class LoadFontNode : public XMLNode
79 {
80 public:
81         LoadFontNode () {}
82         LoadFontNode (xmlpp::Node const * node);
83
84         std::string id;
85         std::string uri;
86 };
87
88 class Subtitle
89 {
90 public:
91         Subtitle (
92                 std::string font,
93                 bool italic,
94                 Color color,
95                 int size,
96                 Time in,
97                 Time out,
98                 float v_position,
99                 VAlign v_align,
100                 std::string text,
101                 Effect effect,
102                 Color effect_color,
103                 Time fade_up_time,
104                 Time fade_down_time
105                 );
106
107         std::string font () const {
108                 return _font;
109         }
110
111         bool italic () const {
112                 return _italic;
113         }
114
115         Color color () const {
116                 return _color;
117         }
118
119         Time in () const {
120                 return _in;
121         }
122
123         Time out () const {
124                 return _out;
125         }
126
127         std::string text () const {
128                 return _text;
129         }
130
131         float v_position () const {
132                 return _v_position;
133         }
134
135         VAlign v_align () const {
136                 return _v_align;
137         }
138
139         Effect effect () const {
140                 return _effect;
141         }
142
143         Color effect_color () const {
144                 return _effect_color;
145         }
146
147         Time fade_up_time () const {
148                 return _fade_up_time;
149         }
150
151         Time fade_down_time () const {
152                 return _fade_down_time;
153         }
154
155         int size () const {
156                 return _size;
157         }
158         
159         int size_in_pixels (int screen_height) const;
160
161 private:
162         std::string _font;
163         bool _italic;
164         Color _color;
165         int _size;
166         Time _in;
167         Time _out;
168         float _v_position;
169         VAlign _v_align;
170         std::string _text;
171         Effect _effect;
172         Color _effect_color;
173         Time _fade_up_time;
174         Time _fade_down_time;
175 };
176
177 bool operator== (Subtitle const & a, Subtitle const & b);
178 std::ostream& operator<< (std::ostream& s, Subtitle const & sub);
179
180 class SubtitleAsset : public Asset
181 {
182 public:
183         SubtitleAsset (std::string directory, std::string xml_file);
184         SubtitleAsset (std::string directory, std::string movie_title, std::string language);
185
186         void write_to_cpl (std::ostream&) const;
187         virtual bool equals (boost::shared_ptr<const Asset>, EqualityOptions, boost::function<void (NoteType, std::string)> note) const {
188                 /* XXX */
189                 note (ERROR, "subtitle assets not compared yet");
190                 return true;
191         }
192
193         std::string language () const {
194                 return _language;
195         }
196
197         std::list<boost::shared_ptr<Subtitle> > subtitles_at (Time t) const;
198         std::list<boost::shared_ptr<Subtitle> > const & subtitles () const {
199                 return _subtitles;
200         }
201
202         void add (boost::shared_ptr<Subtitle>);
203
204         void read_xml (std::string);
205         void write_xml () const;
206         void write_xml (std::ostream& s) const;
207
208 private:
209         std::string font_id_to_name (std::string id) const;
210         std::string escape (std::string) const;
211
212         struct ParseState {
213                 std::list<boost::shared_ptr<FontNode> > font_nodes;
214                 std::list<boost::shared_ptr<TextNode> > text_nodes;
215                 std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes;
216         };
217
218         void maybe_add_subtitle (std::string text, ParseState const & parse_state);
219         
220         void examine_font_nodes (
221                 boost::shared_ptr<XMLFile> xml,
222                 std::list<boost::shared_ptr<FontNode> > const & font_nodes,
223                 ParseState& parse_state
224                 );
225         
226         void examine_text_nodes (
227                 boost::shared_ptr<XMLFile> xml,
228                 std::list<boost::shared_ptr<TextNode> > const & text_nodes,
229                 ParseState& parse_state
230                 );
231
232         std::string _movie_title;
233         /* strangely, this is sometimes a string */
234         std::string _reel_number;
235         std::string _language;
236         std::list<boost::shared_ptr<LoadFontNode> > _load_font_nodes;
237
238         std::list<boost::shared_ptr<Subtitle> > _subtitles;
239         bool _need_sort;
240 };
241
242 }