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