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