*/
#include <fstream>
-#include <boost/lexical_cast.hpp>
+#include <cerrno>
#include <boost/algorithm/string.hpp>
+#include <libxml++/nodes/element.h>
+#include "AS_DCP.h"
+#include "KM_util.h"
#include "subtitle_asset.h"
+#include "parse/subtitle.h"
#include "util.h"
#include "xml.h"
+#include "raw_convert.h"
using std::string;
using std::list;
using std::ostream;
using std::ofstream;
using std::stringstream;
+using std::cout;
using boost::shared_ptr;
-using boost::lexical_cast;
using boost::optional;
using namespace libdcp;
-SubtitleAsset::SubtitleAsset (string directory, string xml_file)
- : Asset (directory, xml_file)
+SubtitleAsset::SubtitleAsset (string directory, string file)
+ : Asset (directory, file)
, _need_sort (false)
{
- read_xml (path().string());
+ /* Grotesque hack: we should look in the PKL to see what type this file is;
+ instead we'll look at the first character to decide what to do.
+ I think this is easily fixable (properly) in 1.0.
+ */
+
+ FILE* f = fopen_boost (path(), "r");
+ if (!f) {
+ throw FileError ("Could not open file for reading", file, errno);
+ }
+ unsigned char test[1];
+ fread (test, 1, 1, f);
+ fclose (f);
+
+ if (test[0] == '<' || test[0] == 0xef) {
+ read_xml (path().string());
+ } else {
+ read_mxf (path().string());
+ }
}
SubtitleAsset::SubtitleAsset (string directory, string movie_title, string language)
}
+void
+SubtitleAsset::read_mxf (string mxf_file)
+{
+ ASDCP::TimedText::MXFReader reader;
+ Kumu::Result_t r = reader.OpenRead (mxf_file.c_str ());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", mxf_file, r));
+ }
+
+ string s;
+ reader.ReadTimedTextResource (s, 0, 0);
+ shared_ptr<cxml::Document> xml (new cxml::Document ("SubtitleReel"));
+ stringstream t;
+ t << s;
+ xml->read_stream (t);
+ read_xml (xml);
+}
+
void
SubtitleAsset::read_xml (string xml_file)
{
- shared_ptr<cxml::File> xml (new cxml::File (xml_file, "DCSubtitle"));
+ shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
+ xml->read_file (xml_file);
+ read_xml (xml);
+}
+
+void
+SubtitleAsset::read_xml (shared_ptr<cxml::Document> xml)
+{
+ /* XXX: hacks aplenty in here; need separate parsers for DCSubtitle and SubtitleReel */
- _uuid = xml->string_child ("SubtitleID");
- _movie_title = xml->string_child ("MovieTitle");
+ /* DCSubtitle */
+ optional<string> x = xml->optional_string_child ("SubtitleID");
+ if (!x) {
+ /* SubtitleReel */
+ x = xml->optional_string_child ("Id");
+ }
+ _uuid = x.get_value_or ("");
+
+ _movie_title = xml->optional_string_child ("MovieTitle");
_reel_number = xml->string_child ("ReelNumber");
_language = xml->string_child ("Language");
in a sane way.
*/
+ shared_ptr<cxml::Node> subtitle_list = xml->optional_node_child ("SubtitleList");
+ if (subtitle_list) {
+ list<shared_ptr<libdcp::parse::Font> > font = type_children<libdcp::parse::Font> (subtitle_list, "Font");
+ copy (font.begin(), font.end(), back_inserter (font_nodes));
+ }
+
ParseState parse_state;
examine_font_nodes (xml, font_nodes, parse_state);
}
return "";
}
- if ((*i)->uri == "arial.ttf") {
+ if ((*i)->uri && (*i)->uri.get() == "arial.ttf") {
return "Arial";
}
}
void
-SubtitleAsset::write_to_cpl (ostream& s) const
+SubtitleAsset::write_to_cpl (xmlpp::Element* node) const
{
/* XXX: should EditRate, Duration and IntrinsicDuration be in here? */
-
- s << " <MainSubtitle>\n"
- << " <Id>urn:uuid:" << _uuid << "</Id>\n"
- << " <AnnotationText>" << _file_name << "</AnnotationText>\n"
- << " <EntryPoint>0</EntryPoint>\n"
- << " </MainSubtitle>\n";
+
+ xmlpp::Node* ms = node->add_child ("MainSubtitle");
+ ms->add_child("Id")->add_child_text("urn:uuid:" + _uuid);
+ ms->add_child("AnnotationText")->add_child_text (_file_name.string ());
+ /* XXX */
+ ms->add_child("EntryPoint")->add_child_text ("0");
}
struct SubtitleSorter {
void
SubtitleAsset::write_xml () const
{
- ofstream f (path().string().c_str());
- write_xml (f);
+ FILE* f = fopen_boost (path (), "r");
+ Glib::ustring const s = xml_as_string ();
+ fwrite (s.c_str(), 1, s.length(), f);
+ fclose (f);
}
-void
-SubtitleAsset::write_xml (ostream& s) const
+Glib::ustring
+SubtitleAsset::xml_as_string () const
{
- s << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
- << "<DCSubtitle Version=\"1.0\">\n"
- << " <SubtitleID>" << _uuid << "</SubtitleID>\n"
- << " <MovieTitle>" << _movie_title << "</MovieTitle>\n"
- << " <ReelNumber>" << _reel_number << "</ReelNumber>\n"
- << " <Language>" << _language << "</Language>\n";
+ xmlpp::Document doc;
+ xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
+ root->set_attribute ("Version", "1.0");
+
+ root->add_child("SubtitleID")->add_child_text (_uuid);
+ if (_movie_title) {
+ root->add_child("MovieTitle")->add_child_text (_movie_title.get ());
+ }
+ root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
+ root->add_child("Language")->add_child_text (_language);
if (_load_font_nodes.size() > 1) {
boost::throw_exception (MiscError ("multiple LoadFont nodes not supported"));
}
if (!_load_font_nodes.empty ()) {
- s << " <LoadFont Id=\"" << _load_font_nodes.front()->id << "\" URI=\"" << _load_font_nodes.front()->uri << "\"/>\n";
+ xmlpp::Element* load_font = root->add_child("LoadFont");
+ load_font->set_attribute("Id", _load_font_nodes.front()->id);
+ if (_load_font_nodes.front()->uri) {
+ load_font->set_attribute("URI", _load_font_nodes.front()->uri.get ());
+ }
}
list<shared_ptr<Subtitle> > sorted = _subtitles;
/* XXX: multiple fonts not supported */
/* XXX: script, underlined, weight not supported */
- bool first = true;
bool italic = false;
Color color;
int size = 0;
Time last_fade_up_time;
Time last_fade_down_time;
+ xmlpp::Element* font = 0;
+ xmlpp::Element* subtitle = 0;
+
for (list<shared_ptr<Subtitle> >::iterator i = sorted.begin(); i != sorted.end(); ++i) {
/* We will start a new <Font>...</Font> whenever some font property changes.
- I suppose should really make an optimal hierarchy of <Font> tags, but
+ I suppose we should really make an optimal hierarchy of <Font> tags, but
that seems hard.
*/
- bool const font_changed = first ||
+ bool const font_changed =
italic != (*i)->italic() ||
color != (*i)->color() ||
size != (*i)->size() ||
effect != (*i)->effect() ||
effect_color != (*i)->effect_color();
- stringstream a;
if (font_changed) {
italic = (*i)->italic ();
- a << "Italic=\"" << (italic ? "yes" : "no") << "\" ";
color = (*i)->color ();
- a << "Color=\"" << color.to_argb_string() << "\" ";
size = (*i)->size ();
- a << "Size=\"" << size << "\" ";
effect = (*i)->effect ();
- a << "Effect=\"" << effect_to_string(effect) << "\" ";
effect_color = (*i)->effect_color ();
- a << "EffectColor=\"" << effect_color.to_argb_string() << "\" ";
- a << "Script=\"normal\" Underlined=\"no\" Weight=\"normal\"";
}
- if (first || font_changed ||
+ if (!font || font_changed) {
+ font = root->add_child ("Font");
+ string id = "theFontId";
+ if (!_load_font_nodes.empty()) {
+ id = _load_font_nodes.front()->id;
+ }
+ font->set_attribute ("Id", id);
+ font->set_attribute ("Italic", italic ? "yes" : "no");
+ font->set_attribute ("Color", color.to_argb_string());
+ font->set_attribute ("Size", raw_convert<string> (size));
+ font->set_attribute ("Effect", effect_to_string (effect));
+ font->set_attribute ("EffectColor", effect_color.to_argb_string());
+ font->set_attribute ("Script", "normal");
+ font->set_attribute ("Underlined", "no");
+ font->set_attribute ("Weight", "normal");
+ }
+
+ if (!subtitle || font_changed ||
(last_in != (*i)->in() ||
last_out != (*i)->out() ||
last_fade_up_time != (*i)->fade_up_time() ||
last_fade_down_time != (*i)->fade_down_time()
)) {
- if (!first) {
- s << " </Subtitle>\n";
- }
-
- if (font_changed) {
- if (!first) {
- s << " </Font>\n";
- }
-
- string id = "theFontId";
- if (!_load_font_nodes.empty()) {
- id = _load_font_nodes.front()->id;
- }
-
- s << " <Font Id=\"" << id << "\" " << a.str() << ">\n";
- }
-
- s << " <Subtitle "
- << "SpotNumber=\"" << spot_number++ << "\" "
- << "TimeIn=\"" << (*i)->in().to_string() << "\" "
- << "TimeOut=\"" << (*i)->out().to_string() << "\" "
- << "FadeUpTime=\"" << (*i)->fade_up_time().to_ticks() << "\" "
- << "FadeDownTime=\"" << (*i)->fade_down_time().to_ticks() << "\""
- << ">\n";
+ subtitle = font->add_child ("Subtitle");
+ subtitle->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
+ subtitle->set_attribute ("TimeIn", (*i)->in().to_string());
+ subtitle->set_attribute ("TimeOut", (*i)->out().to_string());
+ subtitle->set_attribute ("FadeUpTime", raw_convert<string> ((*i)->fade_up_time().to_ticks()));
+ subtitle->set_attribute ("FadeDownTime", raw_convert<string> ((*i)->fade_down_time().to_ticks()));
last_in = (*i)->in ();
last_out = (*i)->out ();
last_fade_down_time = (*i)->fade_down_time ();
}
- s << " <Text "
- << "VAlign=\"" << valign_to_string ((*i)->v_align()) << "\" "
- << "VPosition=\"" << (*i)->v_position() << "\""
- << ">" << escape ((*i)->text()) << "</Text>\n";
-
- first = false;
+ xmlpp::Element* text = subtitle->add_child ("Text");
+ text->set_attribute ("VAlign", valign_to_string ((*i)->v_align()));
+ text->set_attribute ("VPosition", raw_convert<string> ((*i)->v_position()));
+ text->add_child_text ((*i)->text());
}
- s << " </Subtitle>\n";
- s << " </Font>\n";
- s << "</DCSubtitle>\n";
+ return doc.write_to_string_formatted ("UTF-8");
}
-/** XXX: Another reason why we should be writing with libxml++ */
-string
-SubtitleAsset::escape (string s) const
-{
- boost::replace_all (s, "&", "&");
- return s;
-}