Support multiple fonts in subtitles.
[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 namespace dcp;
31
32 InteropSubtitleContent::InteropSubtitleContent (boost::filesystem::path file)
33         : SubtitleContent (file)
34 {
35         shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
36         xml->read_file (file);
37         _id = xml->string_child ("SubtitleID");
38
39         _movie_title = xml->string_child ("MovieTitle");
40
41         _load_font_nodes = type_children<dcp::InteropLoadFont> (xml, "LoadFont");
42         list<shared_ptr<dcp::Font> > font_nodes = type_children<dcp::Font> (xml, "Font");
43
44         parse_common (xml, font_nodes);
45 }
46
47 InteropSubtitleContent::InteropSubtitleContent (string movie_title, string language)
48         : _movie_title (movie_title)
49 {
50         _language = language;
51 }
52
53 struct SubtitleSorter {
54         bool operator() (SubtitleString const & a, SubtitleString const & b) {
55                 if (a.in() != b.in()) {
56                         return a.in() < b.in();
57                 }
58                 return a.v_position() < b.v_position();
59         }
60 };
61
62 Glib::ustring
63 InteropSubtitleContent::xml_as_string () const
64 {
65         xmlpp::Document doc;
66         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
67         root->set_attribute ("Version", "1.0");
68
69         root->add_child("SubtitleID")->add_child_text (_id);
70         root->add_child("MovieTitle")->add_child_text (_movie_title);
71         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
72         root->add_child("Language")->add_child_text (_language);
73
74         for (list<shared_ptr<InteropLoadFont> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
75                 xmlpp::Element* load_font = root->add_child("LoadFont");
76                 load_font->set_attribute ("Id", (*i)->id);
77                 load_font->set_attribute ("URI", (*i)->uri);
78         }
79
80         list<SubtitleString> sorted = _subtitles;
81         sorted.sort (SubtitleSorter ());
82
83         /* XXX: script, underlined, weight not supported */
84
85         optional<string> font;
86         bool italic = false;
87         Color color;
88         int size = 0;
89         Effect effect = NONE;
90         Color effect_color;
91         int spot_number = 1;
92         Time last_in;
93         Time last_out;
94         Time last_fade_up_time;
95         Time last_fade_down_time;
96
97         xmlpp::Element* font_element = 0;
98         xmlpp::Element* subtitle_element = 0;
99
100         for (list<SubtitleString>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
101
102                 /* We will start a new <Font>...</Font> whenever some font property changes.
103                    I suppose we should really make an optimal hierarchy of <Font> tags, but
104                    that seems hard.
105                 */
106
107                 bool const font_changed =
108                         font         != i->font()         ||
109                         italic       != i->italic()       ||
110                         color        != i->color()        ||
111                         size         != i->size()         ||
112                         effect       != i->effect()       ||
113                         effect_color != i->effect_color();
114
115                 if (font_changed) {
116                         font = i->font ();
117                         italic = i->italic ();
118                         color = i->color ();
119                         size = i->size ();
120                         effect = i->effect ();
121                         effect_color = i->effect_color ();
122                 }
123
124                 if (!font_element || font_changed) {
125                         font_element = root->add_child ("Font");
126                         if (font) {
127                                 font_element->set_attribute ("Id", font.get ());
128                         }
129                         font_element->set_attribute ("Italic", italic ? "yes" : "no");
130                         font_element->set_attribute ("Color", color.to_argb_string());
131                         font_element->set_attribute ("Size", raw_convert<string> (size));
132                         font_element->set_attribute ("Effect", effect_to_string (effect));
133                         font_element->set_attribute ("EffectColor", effect_color.to_argb_string());
134                         font_element->set_attribute ("Script", "normal");
135                         font_element->set_attribute ("Underlined", "no");
136                         font_element->set_attribute ("Weight", "normal");
137                 }
138
139                 if (!subtitle_element || font_changed ||
140                     (last_in != i->in() ||
141                      last_out != i->out() ||
142                      last_fade_up_time != i->fade_up_time() ||
143                      last_fade_down_time != i->fade_down_time()
144                             )) {
145
146                         subtitle_element = font_element->add_child ("Subtitle");
147                         subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
148                         subtitle_element->set_attribute ("TimeIn", i->in().to_string());
149                         subtitle_element->set_attribute ("TimeOut", i->out().to_string());
150                         subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().to_ticks()));
151                         subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().to_ticks()));
152
153                         last_in = i->in ();
154                         last_out = i->out ();
155                         last_fade_up_time = i->fade_up_time ();
156                         last_fade_down_time = i->fade_down_time ();
157                 }
158
159                 xmlpp::Element* text = subtitle_element->add_child ("Text");
160                 text->set_attribute ("VAlign", valign_to_string (i->v_align()));                
161                 text->set_attribute ("VPosition", raw_convert<string> (i->v_position()));
162                 text->add_child_text (i->text());
163         }
164
165         return doc.write_to_string_formatted ("UTF-8");
166 }
167