Merge master.
[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 #include "parse/subtitle.h"
24
25 namespace libdcp
26 {
27
28 class Subtitle
29 {
30 public:
31         Subtitle (
32                 std::string font,
33                 bool italic,
34                 Color color,
35                 int size,
36                 Time in,
37                 Time out,
38                 float v_position,
39                 VAlign v_align,
40                 std::string text,
41                 Effect effect,
42                 Color effect_color,
43                 Time fade_up_time,
44                 Time fade_down_time
45                 );
46
47         std::string font () const {
48                 return _font;
49         }
50
51         bool italic () const {
52                 return _italic;
53         }
54
55         Color color () const {
56                 return _color;
57         }
58
59         Time in () const {
60                 return _in;
61         }
62
63         Time out () const {
64                 return _out;
65         }
66
67         std::string text () const {
68                 return _text;
69         }
70
71         float v_position () const {
72                 return _v_position;
73         }
74
75         VAlign v_align () const {
76                 return _v_align;
77         }
78
79         Effect effect () const {
80                 return _effect;
81         }
82
83         Color effect_color () const {
84                 return _effect_color;
85         }
86
87         Time fade_up_time () const {
88                 return _fade_up_time;
89         }
90
91         Time fade_down_time () const {
92                 return _fade_down_time;
93         }
94
95         int size () const {
96                 return _size;
97         }
98         
99         int size_in_pixels (int screen_height) const;
100
101 private:
102         std::string _font;
103         bool _italic;
104         Color _color;
105         int _size;
106         Time _in;
107         Time _out;
108         float _v_position;
109         VAlign _v_align;
110         std::string _text;
111         Effect _effect;
112         Color _effect_color;
113         Time _fade_up_time;
114         Time _fade_down_time;
115 };
116
117 bool operator== (Subtitle const & a, Subtitle const & b);
118 std::ostream& operator<< (std::ostream& s, Subtitle const & sub);
119
120 class SubtitleAsset : public Asset
121 {
122 public:
123         SubtitleAsset (std::string directory, std::string xml_file);
124         SubtitleAsset (std::string directory, std::string movie_title, std::string language);
125
126         void write_to_cpl (xmlpp::Node *) const;
127         virtual bool equals (boost::shared_ptr<const Asset>, EqualityOptions, boost::function<void (NoteType, std::string)> note) const {
128                 /* XXX */
129                 note (ERROR, "subtitle assets not compared yet");
130                 return true;
131         }
132
133         std::string language () const {
134                 return _language;
135         }
136
137         std::list<boost::shared_ptr<Subtitle> > subtitles_at (Time t) const;
138         std::list<boost::shared_ptr<Subtitle> > const & subtitles () const {
139                 return _subtitles;
140         }
141
142         void add (boost::shared_ptr<Subtitle>);
143
144         void read_xml (std::string);
145         void write_xml () const;
146         void write_xml (std::ostream &) const;
147
148 private:
149         std::string font_id_to_name (std::string id) const;
150
151         struct ParseState {
152                 std::list<boost::shared_ptr<parse::Font> > font_nodes;
153                 std::list<boost::shared_ptr<parse::Text> > text_nodes;
154                 std::list<boost::shared_ptr<parse::Subtitle> > subtitle_nodes;
155         };
156
157         void maybe_add_subtitle (std::string text, ParseState const & parse_state);
158         
159         void examine_font_nodes (
160                 boost::shared_ptr<const cxml::Node> xml,
161                 std::list<boost::shared_ptr<parse::Font> > const & font_nodes,
162                 ParseState& parse_state
163                 );
164         
165         void examine_text_nodes (
166                 boost::shared_ptr<const cxml::Node> xml,
167                 std::list<boost::shared_ptr<parse::Text> > const & text_nodes,
168                 ParseState& parse_state
169                 );
170
171         std::string _movie_title;
172         /* strangely, this is sometimes a string */
173         std::string _reel_number;
174         std::string _language;
175         std::list<boost::shared_ptr<parse::LoadFont> > _load_font_nodes;
176
177         std::list<boost::shared_ptr<Subtitle> > _subtitles;
178         bool _need_sort;
179 };
180
181 }