5e7c5b257001b666e5c415596df9f32a744a66bd
[libdcp.git] / src / subtitle_content.cc
1 /*
2     Copyright (C) 2012-2014 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 "raw_convert.h"
21 #include "subtitle_content.h"
22 #include "util.h"
23 #include "xml.h"
24 #include "font.h"
25 #include "text.h"
26 #include "subtitle_string.h"
27 #include "dcp_assert.h"
28 #include "AS_DCP.h"
29 #include "KM_util.h"
30 #include <libxml++/nodes/element.h>
31 #include <boost/algorithm/string.hpp>
32 #include <fstream>
33
34 using std::string;
35 using std::list;
36 using std::ostream;
37 using std::ofstream;
38 using std::stringstream;
39 using std::cout;
40 using boost::shared_ptr;
41 using boost::optional;
42 using boost::function;
43 using boost::dynamic_pointer_cast;
44 using namespace dcp;
45
46 SubtitleContent::SubtitleContent ()
47         : _reel_number ("1")
48 {
49
50 }
51
52 SubtitleContent::SubtitleContent (boost::filesystem::path file)
53         : Content (file)
54         , _reel_number ("1")
55 {
56
57 }
58
59 void
60 SubtitleContent::parse_common (shared_ptr<cxml::Document> xml, list<shared_ptr<dcp::Font> > font_nodes)
61 {
62         _reel_number = xml->string_child ("ReelNumber");
63         _language = xml->string_child ("Language");
64
65         /* Now make Subtitle objects to represent the raw XML nodes
66            in a sane way.
67         */
68
69         ParseState parse_state;
70         examine_font_nodes (xml, font_nodes, parse_state);
71 }
72
73 void
74 SubtitleContent::examine_font_nodes (
75         shared_ptr<const cxml::Node> xml,
76         list<shared_ptr<dcp::Font> > const & font_nodes,
77         ParseState& parse_state
78         )
79 {
80         for (list<shared_ptr<dcp::Font> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
81
82                 parse_state.font_nodes.push_back (*i);
83                 maybe_add_subtitle ((*i)->text, parse_state);
84
85                 for (list<shared_ptr<dcp::Subtitle> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
86                         parse_state.subtitle_nodes.push_back (*j);
87                         examine_text_nodes (xml, (*j)->text_nodes, parse_state);
88                         examine_font_nodes (xml, (*j)->font_nodes, parse_state);
89                         parse_state.subtitle_nodes.pop_back ();
90                 }
91         
92                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
93                 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
94                 
95                 parse_state.font_nodes.pop_back ();
96         }
97 }
98
99 void
100 SubtitleContent::examine_text_nodes (
101         shared_ptr<const cxml::Node> xml,
102         list<shared_ptr<dcp::Text> > const & text_nodes,
103         ParseState& parse_state
104         )
105 {
106         for (list<shared_ptr<dcp::Text> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
107                 parse_state.text_nodes.push_back (*i);
108                 maybe_add_subtitle ((*i)->text, parse_state);
109                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
110                 parse_state.text_nodes.pop_back ();
111         }
112 }
113
114 void
115 SubtitleContent::maybe_add_subtitle (string text, ParseState const & parse_state)
116 {
117         if (empty_or_white_space (text)) {
118                 return;
119         }
120         
121         if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
122                 return;
123         }
124
125         DCP_ASSERT (!parse_state.text_nodes.empty ());
126         DCP_ASSERT (!parse_state.subtitle_nodes.empty ());
127         
128         dcp::Font effective_font (parse_state.font_nodes);
129         dcp::Text effective_text (*parse_state.text_nodes.back ());
130         dcp::Subtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
131
132         _subtitles.push_back (
133                 SubtitleString (
134                         effective_font.id,
135                         effective_font.italic.get(),
136                         effective_font.colour.get(),
137                         effective_font.size,
138                         effective_subtitle.in,
139                         effective_subtitle.out,
140                         effective_text.v_position,
141                         effective_text.v_align,
142                         text,
143                         effective_font.effect ? effective_font.effect.get() : NONE,
144                         effective_font.effect_colour.get(),
145                         effective_subtitle.fade_up_time,
146                         effective_subtitle.fade_down_time
147                         )
148                 );
149 }
150
151 list<SubtitleString>
152 SubtitleContent::subtitles_during (Time from, Time to) const
153 {
154         list<SubtitleString> s;
155         for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
156                 if (i->out() >= from && i->in() <= to) {
157                         s.push_back (*i);
158                 }
159         }
160
161         return s;
162 }
163
164 void
165 SubtitleContent::add (SubtitleString s)
166 {
167         _subtitles.push_back (s);
168 }
169
170 void
171 SubtitleContent::write_xml (boost::filesystem::path p) const
172 {
173         FILE* f = fopen_boost (p, "w");
174         if (!f) {
175                 throw FileError ("Could not open file for writing", p, -1);
176         }
177         
178         Glib::ustring const s = xml_as_string ();
179         fwrite (s.c_str(), 1, s.bytes(), f);
180         fclose (f);
181
182         _file = p;
183 }
184
185 Time
186 SubtitleContent::latest_subtitle_out () const
187 {
188         Time t;
189         for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
190                 if (i->out() > t) {
191                         t = i->out ();
192                 }
193         }
194
195         return t;
196 }
197
198 bool
199 SubtitleContent::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, function<void (NoteType, std::string)> note) const
200 {
201         if (!Asset::equals (other_asset, options, note)) {
202                 return false;
203         }
204         
205         shared_ptr<const SubtitleContent> other = dynamic_pointer_cast<const SubtitleContent> (other_asset);
206         if (!other) {
207                 return false;
208         }
209
210         if (_reel_number != other->_reel_number) {
211                 note (DCP_ERROR, "subtitle reel numbers differ");
212                 return false;
213         }
214
215         if (_language != other->_language) {
216                 note (DCP_ERROR, "subtitle languages differ");
217                 return false;
218         }
219
220         if (_subtitles != other->_subtitles) {
221                 note (DCP_ERROR, "subtitles differ");
222                 return false;
223         }
224
225         return true;
226 }