diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/subtitle_asset.cc | 44 | ||||
| -rw-r--r-- | src/subtitle_asset.h | 6 |
2 files changed, 40 insertions, 10 deletions
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 <fstream> #include <boost/lexical_cast.hpp> +#include <boost/algorithm/string.hpp> #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<Subtitle> 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 << "<?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" - << " <LoadFont Id=\"theFontId\" URI=\"arial.ttf\"/>\n"; + << " <Language>" << _language << "</Language>\n"; - _subtitles.sort (SubtitleSorter ()); + if (_load_font_nodes.size() > 1) { + throw 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"; + } + + list<shared_ptr<Subtitle> > 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<shared_ptr<Subtitle> >::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) { + 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 @@ -470,7 +484,13 @@ SubtitleAsset::write_xml (ostream& s) if (!first) { s << " </Font>\n"; } - s << " <Font Id=\"theFontId\" " << a.str() << ">\n"; + + string id = "theFontId"; + if (!_load_font_nodes.empty()) { + id = _load_font_nodes.front()->id; + } + + s << " <Font Id=\"" << id << "\" " << a.str() << ">\n"; } s << " <Subtitle " @@ -490,7 +510,7 @@ SubtitleAsset::write_xml (ostream& s) s << " <Text " << "VAlign=\"" << valign_to_string ((*i)->v_align()) << "\" " << "VPosition=\"" << (*i)->v_position() << "\"" - << ">" << (*i)->text() << "</Text>\n"; + << ">" << escape ((*i)->text()) << "</Text>\n"; first = false; } @@ -499,3 +519,11 @@ SubtitleAsset::write_xml (ostream& s) s << " </Font>\n"; s << "</DCSubtitle>\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<Subtitle>); 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<boost::shared_ptr<FontNode> > font_nodes; @@ -235,6 +236,7 @@ private: std::list<boost::shared_ptr<LoadFontNode> > _load_font_nodes; std::list<boost::shared_ptr<Subtitle> > _subtitles; + bool _need_sort; }; } |
