Merge branch '1.0' of git.carlh.net:git/libdcp into 1.0
[libdcp.git] / src / interop_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 "interop_subtitle_content.h"
21 #include "interop_load_font.h"
22 #include "xml.h"
23 #include "raw_convert.h"
24 #include "font.h"
25
26 using std::list;
27 using std::string;
28 using boost::shared_ptr;
29 using boost::optional;
30 using boost::dynamic_pointer_cast;
31 using namespace dcp;
32
33 InteropSubtitleContent::InteropSubtitleContent (boost::filesystem::path file)
34         : SubtitleContent (file)
35 {
36         shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
37         xml->read_file (file);
38         _id = xml->string_child ("SubtitleID");
39
40         _movie_title = xml->string_child ("MovieTitle");
41
42         _load_font_nodes = type_children<dcp::InteropLoadFont> (xml, "LoadFont");
43         list<shared_ptr<dcp::Font> > font_nodes = type_children<dcp::Font> (xml, "Font");
44
45         parse_common (xml, font_nodes);
46 }
47
48 InteropSubtitleContent::InteropSubtitleContent (string movie_title, string language)
49         : _movie_title (movie_title)
50 {
51         _language = language;
52 }
53
54 struct SubtitleSorter {
55         bool operator() (SubtitleString const & a, SubtitleString const & b) {
56                 if (a.in() != b.in()) {
57                         return a.in() < b.in();
58                 }
59                 return a.v_position() < b.v_position();
60         }
61 };
62
63 Glib::ustring
64 InteropSubtitleContent::xml_as_string () const
65 {
66         xmlpp::Document doc;
67         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
68         root->set_attribute ("Version", "1.0");
69
70         root->add_child("SubtitleID")->add_child_text (_id);
71         root->add_child("MovieTitle")->add_child_text (_movie_title);
72         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
73         root->add_child("Language")->add_child_text (_language);
74
75         for (list<shared_ptr<InteropLoadFont> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
76                 xmlpp::Element* load_font = root->add_child("LoadFont");
77                 load_font->set_attribute ("Id", (*i)->id);
78                 load_font->set_attribute ("URI", (*i)->uri);
79         }
80
81         list<SubtitleString> sorted = _subtitles;
82         sorted.sort (SubtitleSorter ());
83
84         /* XXX: script, underlined, weight not supported */
85
86         optional<string> font;
87         bool italic = false;
88         Colour colour;
89         int size = 0;
90         Effect effect = NONE;
91         Colour effect_colour;
92         int spot_number = 1;
93         Time last_in;
94         Time last_out;
95         Time last_fade_up_time;
96         Time last_fade_down_time;
97
98         xmlpp::Element* font_element = 0;
99         xmlpp::Element* subtitle_element = 0;
100
101         for (list<SubtitleString>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
102
103                 /* We will start a new <Font>...</Font> whenever some font property changes.
104                    I suppose we should really make an optimal hierarchy of <Font> tags, but
105                    that seems hard.
106                 */
107
108                 bool const font_changed =
109                         font         != i->font()         ||
110                         italic       != i->italic()       ||
111                         colour        != i->colour()        ||
112                         size         != i->size()         ||
113                         effect       != i->effect()       ||
114                         effect_colour != i->effect_colour();
115
116                 if (font_changed) {
117                         font = i->font ();
118                         italic = i->italic ();
119                         colour = i->colour ();
120                         size = i->size ();
121                         effect = i->effect ();
122                         effect_colour = i->effect_colour ();
123                 }
124
125                 if (!font_element || font_changed) {
126                         font_element = root->add_child ("Font");
127                         if (font) {
128                                 font_element->set_attribute ("Id", font.get ());
129                         }
130                         font_element->set_attribute ("Italic", italic ? "yes" : "no");
131                         font_element->set_attribute ("Color", colour.to_argb_string());
132                         font_element->set_attribute ("Size", raw_convert<string> (size));
133                         font_element->set_attribute ("Effect", effect_to_string (effect));
134                         font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
135                         font_element->set_attribute ("Script", "normal");
136                         font_element->set_attribute ("Underlined", "no");
137                         font_element->set_attribute ("Weight", "normal");
138                 }
139
140                 if (!subtitle_element || font_changed ||
141                     (last_in != i->in() ||
142                      last_out != i->out() ||
143                      last_fade_up_time != i->fade_up_time() ||
144                      last_fade_down_time != i->fade_down_time()
145                             )) {
146
147                         subtitle_element = font_element->add_child ("Subtitle");
148                         subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
149                         subtitle_element->set_attribute ("TimeIn", i->in().to_string());
150                         subtitle_element->set_attribute ("TimeOut", i->out().to_string());
151                         subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().to_ticks()));
152                         subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().to_ticks()));
153
154                         last_in = i->in ();
155                         last_out = i->out ();
156                         last_fade_up_time = i->fade_up_time ();
157                         last_fade_down_time = i->fade_down_time ();
158                 }
159
160                 xmlpp::Element* text = subtitle_element->add_child ("Text");
161                 text->set_attribute ("VAlign", valign_to_string (i->v_align()));                
162                 text->set_attribute ("VPosition", raw_convert<string> (i->v_position()));
163                 text->add_child_text (i->text());
164         }
165
166         return doc.write_to_string_formatted ("UTF-8");
167 }
168
169 void
170 InteropSubtitleContent::add_font (string id, string uri)
171 {
172         _load_font_nodes.push_back (shared_ptr<InteropLoadFont> (new InteropLoadFont (id, uri)));
173 }
174
175 bool
176 InteropSubtitleContent::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
177 {
178         if (!SubtitleContent::equals (other_asset, options, note)) {
179                 return false;
180         }
181         
182         shared_ptr<const InteropSubtitleContent> other = dynamic_pointer_cast<const InteropSubtitleContent> (other_asset);
183         if (!other) {
184                 return false;
185         }
186
187         list<shared_ptr<InteropLoadFont> >::const_iterator i = _load_font_nodes.begin ();
188         list<shared_ptr<InteropLoadFont> >::const_iterator j = other->_load_font_nodes.begin ();
189
190         while (i != _load_font_nodes.end ()) {
191                 if (j == _load_font_nodes.end ()) {
192                         note (DCP_ERROR, "<LoadFont> nodes differ");
193                         return false;
194                 }
195
196                 if (**i != **j) {
197                         note (DCP_ERROR, "<LoadFont> nodes differ");
198                         return false;
199                 }
200
201                 ++i;
202                 ++j;
203         }
204
205         if (_movie_title != other->_movie_title) {
206                 note (DCP_ERROR, "Subtitle movie titles differ");
207                 return false;
208         }
209
210         return true;
211 }
212
213 list<shared_ptr<LoadFont> >
214 InteropSubtitleContent::load_font_nodes () const
215 {
216         list<shared_ptr<LoadFont> > lf;
217         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
218         return lf;
219 }