From f5cc57f11946e1e269df25db434c5f8efe953a68 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sat, 22 Dec 2012 23:29:50 +0000 Subject: More various fixes to subtitle XML writing. --- src/subtitle_asset.cc | 44 ++++++++++++++++++++++++++++++++++++-------- src/subtitle_asset.h | 6 ++++-- 2 files changed, 40 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/subtitle_asset.cc b/src/subtitle_asset.cc index 6141b2c5..9ba85a43 100644 --- a/src/subtitle_asset.cc +++ b/src/subtitle_asset.cc @@ -19,6 +19,7 @@ #include #include +#include #include "subtitle_asset.h" #include "util.h" @@ -33,6 +34,7 @@ using namespace libdcp; SubtitleAsset::SubtitleAsset (string directory, string xml_file) : Asset (directory, xml_file) + , _need_sort (false) { read_xml (path().string()); } @@ -42,6 +44,7 @@ SubtitleAsset::SubtitleAsset (string directory, string movie_title, string langu , _movie_title (movie_title) , _reel_number ("1") , _language (language) + , _need_sort (false) { } @@ -368,6 +371,7 @@ void SubtitleAsset::add (shared_ptr s) { _subtitles.push_back (s); + _need_sort = true; } void @@ -392,24 +396,34 @@ struct SubtitleSorter { }; void -SubtitleAsset::write_xml () +SubtitleAsset::write_xml () const { ofstream f (path().string().c_str()); write_xml (f); } void -SubtitleAsset::write_xml (ostream& s) +SubtitleAsset::write_xml (ostream& s) const { s << "\n" << "\n" << " " << _uuid << "\n" << " " << _movie_title << "\n" << " " << _reel_number << "\n" - << " " << _language << "\n" - << " \n"; + << " " << _language << "\n"; - _subtitles.sort (SubtitleSorter ()); + if (_load_font_nodes.size() > 1) { + throw MiscError ("multiple LoadFont nodes not supported"); + } + + if (!_load_font_nodes.empty ()) { + s << " id << "\" URI=\"" << _load_font_nodes.front()->uri << "\"/>\n"; + } + + list > sorted = _subtitles; + if (_need_sort) { + sorted.sort (SubtitleSorter ()); + } /* XXX: multiple fonts not supported */ /* XXX: script, underlined, weight not supported */ @@ -426,7 +440,7 @@ SubtitleAsset::write_xml (ostream& s) Time last_fade_up_time; Time last_fade_down_time; - for (list >::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) { + for (list >::iterator i = sorted.begin(); i != sorted.end(); ++i) { /* We will start a new ... whenever some font property changes. I suppose should really make an optimal hierarchy of tags, but @@ -470,7 +484,13 @@ SubtitleAsset::write_xml (ostream& s) if (!first) { s << " \n"; } - s << " \n"; + + string id = "theFontId"; + if (!_load_font_nodes.empty()) { + id = _load_font_nodes.front()->id; + } + + s << " \n"; } s << " v_align()) << "\" " << "VPosition=\"" << (*i)->v_position() << "\"" - << ">" << (*i)->text() << "\n"; + << ">" << escape ((*i)->text()) << "\n"; first = false; } @@ -499,3 +519,11 @@ SubtitleAsset::write_xml (ostream& s) s << " \n"; s << "\n"; } + +/** XXX: Another reason why we should be writing with libxml++ */ +string +SubtitleAsset::escape (string s) const +{ + boost::replace_all (s, "&", "&"); + return s; +} diff --git a/src/subtitle_asset.h b/src/subtitle_asset.h index 5d996c7a..71ae42fc 100644 --- a/src/subtitle_asset.h +++ b/src/subtitle_asset.h @@ -202,11 +202,12 @@ public: void add (boost::shared_ptr); void read_xml (std::string); - void write_xml (); - void write_xml (std::ostream& s); + void write_xml () const; + void write_xml (std::ostream& s) const; private: std::string font_id_to_name (std::string id) const; + std::string escape (std::string) const; struct ParseState { std::list > font_nodes; @@ -235,6 +236,7 @@ private: std::list > _load_font_nodes; std::list > _subtitles; + bool _need_sort; }; } -- cgit v1.2.3