2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "raw_convert.h"
21 #include "subtitle_content.h"
26 #include "load_font.h"
27 #include "subtitle_string.h"
30 #include <libxml++/nodes/element.h>
31 #include <boost/algorithm/string.hpp>
38 using std::stringstream;
40 using boost::shared_ptr;
41 using boost::optional;
44 SubtitleContent::SubtitleContent (boost::filesystem::path file, bool mxf)
48 shared_ptr<cxml::Document> xml;
51 ASDCP::TimedText::MXFReader reader;
52 Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
53 if (ASDCP_FAILURE (r)) {
54 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file, r));
58 reader.ReadTimedTextResource (s, 0, 0);
59 xml.reset (new cxml::Document ("SubtitleReel"));
64 ASDCP::WriterInfo info;
65 reader.FillWriterInfo (info);
68 Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
72 xml.reset (new cxml::Document ("DCSubtitle"));
73 xml->read_file (file);
74 _id = xml->string_child ("SubtitleID");
77 /* XXX: hacks aplenty in here; probably need separate parsers for DCSubtitle and SubtitleReel */
79 _movie_title = xml->optional_string_child ("MovieTitle");
80 _reel_number = xml->string_child ("ReelNumber");
81 _language = xml->string_child ("Language");
83 xml->ignore_child ("LoadFont");
85 list<shared_ptr<dcp::Font> > font_nodes = type_children<dcp::Font> (xml, "Font");
86 _load_font_nodes = type_children<dcp::LoadFont> (xml, "LoadFont");
88 /* Now make Subtitle objects to represent the raw XML nodes
92 shared_ptr<cxml::Node> subtitle_list = xml->optional_node_child ("SubtitleList");
94 list<shared_ptr<dcp::Font> > font = type_children<dcp::Font> (subtitle_list, "Font");
95 copy (font.begin(), font.end(), back_inserter (font_nodes));
98 ParseState parse_state;
99 examine_font_nodes (xml, font_nodes, parse_state);
102 SubtitleContent::SubtitleContent (Fraction edit_rate, string movie_title, string language)
103 : Content (edit_rate)
104 , _movie_title (movie_title)
106 , _language (language)
113 SubtitleContent::examine_font_nodes (
114 shared_ptr<const cxml::Node> xml,
115 list<shared_ptr<dcp::Font> > const & font_nodes,
116 ParseState& parse_state
119 for (list<shared_ptr<dcp::Font> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
121 parse_state.font_nodes.push_back (*i);
122 maybe_add_subtitle ((*i)->text, parse_state);
124 for (list<shared_ptr<dcp::Subtitle> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
125 parse_state.subtitle_nodes.push_back (*j);
126 examine_text_nodes (xml, (*j)->text_nodes, parse_state);
127 examine_font_nodes (xml, (*j)->font_nodes, parse_state);
128 parse_state.subtitle_nodes.pop_back ();
131 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
132 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
134 parse_state.font_nodes.pop_back ();
139 SubtitleContent::examine_text_nodes (
140 shared_ptr<const cxml::Node> xml,
141 list<shared_ptr<dcp::Text> > const & text_nodes,
142 ParseState& parse_state
145 for (list<shared_ptr<dcp::Text> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
146 parse_state.text_nodes.push_back (*i);
147 maybe_add_subtitle ((*i)->text, parse_state);
148 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
149 parse_state.text_nodes.pop_back ();
154 SubtitleContent::maybe_add_subtitle (string text, ParseState const & parse_state)
156 if (empty_or_white_space (text)) {
160 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
164 assert (!parse_state.text_nodes.empty ());
165 assert (!parse_state.subtitle_nodes.empty ());
167 dcp::Font effective_font (parse_state.font_nodes);
168 dcp::Text effective_text (*parse_state.text_nodes.back ());
169 dcp::Subtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
171 _subtitles.push_back (
173 font_id_to_name (effective_font.id),
174 effective_font.italic.get(),
175 effective_font.color.get(),
177 effective_subtitle.in,
178 effective_subtitle.out,
179 effective_text.v_position,
180 effective_text.v_align,
182 effective_font.effect ? effective_font.effect.get() : NONE,
183 effective_font.effect_color.get(),
184 effective_subtitle.fade_up_time,
185 effective_subtitle.fade_down_time
191 SubtitleContent::subtitles_at (Time t) const
193 list<SubtitleString> s;
194 for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
195 if (i->in() <= t && t <= i->out ()) {
204 SubtitleContent::font_id_to_name (string id) const
206 list<shared_ptr<dcp::LoadFont> >::const_iterator i = _load_font_nodes.begin();
207 while (i != _load_font_nodes.end() && (*i)->id != id) {
211 if (i == _load_font_nodes.end ()) {
215 if ((*i)->uri && (*i)->uri.get() == "arial.ttf") {
223 SubtitleContent::add (SubtitleString s)
225 _subtitles.push_back (s);
229 struct SubtitleSorter {
230 bool operator() (SubtitleString const & a, SubtitleString const & b) {
231 if (a.in() != b.in()) {
232 return a.in() < b.in();
234 return a.v_position() < b.v_position();
239 SubtitleContent::write_xml (boost::filesystem::path p) const
241 FILE* f = fopen_boost (p, "r");
242 Glib::ustring const s = xml_as_string ();
243 fwrite (s.c_str(), 1, s.length(), f);
248 SubtitleContent::xml_as_string () const
251 xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
252 root->set_attribute ("Version", "1.0");
254 root->add_child("SubtitleID")->add_child_text (_id);
256 root->add_child("MovieTitle")->add_child_text (_movie_title.get ());
258 root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
259 root->add_child("Language")->add_child_text (_language);
261 if (_load_font_nodes.size() > 1) {
262 boost::throw_exception (MiscError ("multiple LoadFont nodes not supported"));
265 if (!_load_font_nodes.empty ()) {
266 xmlpp::Element* load_font = root->add_child("LoadFont");
267 load_font->set_attribute ("Id", _load_font_nodes.front()->id);
268 if (_load_font_nodes.front()->uri) {
269 load_font->set_attribute ("URI", _load_font_nodes.front()->uri.get ());
273 list<SubtitleString> sorted = _subtitles;
275 sorted.sort (SubtitleSorter ());
278 /* XXX: multiple fonts not supported */
279 /* XXX: script, underlined, weight not supported */
284 Effect effect = NONE;
289 Time last_fade_up_time;
290 Time last_fade_down_time;
292 xmlpp::Element* font = 0;
293 xmlpp::Element* subtitle = 0;
295 for (list<SubtitleString>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
297 /* We will start a new <Font>...</Font> whenever some font property changes.
298 I suppose we should really make an optimal hierarchy of <Font> tags, but
302 bool const font_changed =
303 italic != i->italic() ||
304 color != i->color() ||
306 effect != i->effect() ||
307 effect_color != i->effect_color();
310 italic = i->italic ();
313 effect = i->effect ();
314 effect_color = i->effect_color ();
317 if (!font || font_changed) {
318 font = root->add_child ("Font");
319 string id = "theFontId";
320 if (!_load_font_nodes.empty()) {
321 id = _load_font_nodes.front()->id;
323 font->set_attribute ("Id", id);
324 font->set_attribute ("Italic", italic ? "yes" : "no");
325 font->set_attribute ("Color", color.to_argb_string());
326 font->set_attribute ("Size", raw_convert<string> (size));
327 font->set_attribute ("Effect", effect_to_string (effect));
328 font->set_attribute ("EffectColor", effect_color.to_argb_string());
329 font->set_attribute ("Script", "normal");
330 font->set_attribute ("Underlined", "no");
331 font->set_attribute ("Weight", "normal");
334 if (!subtitle || font_changed ||
335 (last_in != i->in() ||
336 last_out != i->out() ||
337 last_fade_up_time != i->fade_up_time() ||
338 last_fade_down_time != i->fade_down_time()
341 subtitle = font->add_child ("Subtitle");
342 subtitle->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
343 subtitle->set_attribute ("TimeIn", i->in().to_string());
344 subtitle->set_attribute ("TimeOut", i->out().to_string());
345 subtitle->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().to_ticks()));
346 subtitle->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().to_ticks()));
349 last_out = i->out ();
350 last_fade_up_time = i->fade_up_time ();
351 last_fade_down_time = i->fade_down_time ();
354 xmlpp::Element* text = subtitle->add_child ("Text");
355 text->set_attribute ("VAlign", valign_to_string (i->v_align()));
356 text->set_attribute ("VPosition", raw_convert<string> (i->v_position()));
357 text->add_child_text (i->text());
360 return doc.write_to_string_formatted ("UTF-8");