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