Allow <Font> nodes inside <Subtitle> nodes.
[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         Time fade_up_time;
39         Time fade_down_time;
40
41 private:
42         Time fade_time (std::string name);
43         
44 };
45
46 class SubtitleNode : public XMLNode
47 {
48 public:
49         SubtitleNode () {}
50         SubtitleNode (xmlpp::Node const * node);
51
52         Time in;
53         Time out;
54         std::list<boost::shared_ptr<FontNode> > font_nodes;
55         std::list<boost::shared_ptr<TextNode> > text_nodes;
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 id;
66         int size;
67         boost::optional<bool> italic;
68         boost::optional<Color> color;
69         boost::optional<Effect> effect;
70         boost::optional<Color> effect_color;
71         
72         std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes;
73         std::list<boost::shared_ptr<FontNode> > font_nodes;
74         std::list<boost::shared_ptr<TextNode> > text_nodes;
75 };
76
77 class LoadFontNode : public XMLNode
78 {
79 public:
80         LoadFontNode () {}
81         LoadFontNode (xmlpp::Node const * node);
82
83         std::string id;
84         std::string uri;
85 };
86
87 class Subtitle
88 {
89 public:
90         Subtitle (
91                 std::string font,
92                 bool italic,
93                 Color color,
94                 int size,
95                 Time in,
96                 Time out,
97                 float v_position,
98                 VAlign v_align,
99                 std::string text,
100                 Effect effect,
101                 Color effect_color,
102                 Time fade_up_time,
103                 Time fade_down_time
104                 );
105
106         std::string font () const {
107                 return _font;
108         }
109
110         bool italic () const {
111                 return _italic;
112         }
113
114         Color color () const {
115                 return _color;
116         }
117
118         Time in () const {
119                 return _in;
120         }
121
122         Time out () const {
123                 return _out;
124         }
125
126         std::string text () const {
127                 return _text;
128         }
129
130         float v_position () const {
131                 return _v_position;
132         }
133
134         VAlign v_align () const {
135                 return _v_align;
136         }
137
138         Effect effect () const {
139                 return _effect;
140         }
141
142         Color effect_color () const {
143                 return _effect_color;
144         }
145
146         Time fade_up_time () const {
147                 return _fade_up_time;
148         }
149
150         Time fade_down_time () const {
151                 return _fade_down_time;
152         }
153
154         int size_in_pixels (int screen_height) const;
155
156 private:
157         std::string _font;
158         bool _italic;
159         Color _color;
160         int _size;
161         Time _in;
162         Time _out;
163         float _v_position;
164         VAlign _v_align;
165         std::string _text;
166         Effect _effect;
167         Color _effect_color;
168         Time _fade_up_time;
169         Time _fade_down_time;
170 };
171
172 class SubtitleAsset : public Asset, public XMLFile
173 {
174 public:
175         SubtitleAsset (std::string directory, std::string xml);
176
177         void write_to_cpl (std::ostream&) const {}
178         virtual std::list<std::string> equals (boost::shared_ptr<const Asset>, EqualityOptions) const {
179                 /* XXX */
180                 return std::list<std::string> ();
181         }
182
183         std::string language () const {
184                 return _language;
185         }
186
187         std::list<boost::shared_ptr<Subtitle> > subtitles_at (Time t) const;
188
189 private:
190         std::string font_id_to_name (std::string id) const;
191
192         void examine_font_nodes (
193                 std::list<boost::shared_ptr<FontNode> > const & font_nodes,
194                 std::list<boost::shared_ptr<FontNode> >& current_font_nodes,
195                 std::list<boost::shared_ptr<SubtitleNode> >& current_subtitle_nodes
196                 );
197         
198         void examine_text_nodes (
199                 boost::shared_ptr<SubtitleNode> subtitle_node,
200                 std::list<boost::shared_ptr<TextNode> > const & text_nodes,
201                 std::list<boost::shared_ptr<FontNode> >& current_font_nodes
202                 );
203         
204         std::string _subtitle_id;
205         std::string _movie_title;
206         int64_t _reel_number;
207         std::string _language;
208         std::list<boost::shared_ptr<LoadFontNode> > _load_font_nodes;
209
210         std::list<boost::shared_ptr<Subtitle> > _subtitles;
211 };
212
213 }