Store subtitle language(s) in Film, and allow setup of those
[dcpomatic.git] / src / lib / subtitle_encoder.cc
index cc31c792b96a93f2ba69ab901bff3b4f02e5471f..5e76f5e738d294449c7a33e5bb0e970be7c50cbd 100644 (file)
@@ -18,6 +18,7 @@
 
 */
 
+#include "font.h"
 #include "subtitle_encoder.h"
 #include "player.h"
 #include "compose.hpp"
@@ -39,9 +40,14 @@ using boost::shared_ptr;
 using boost::optional;
 using dcp::raw_convert;
 
-SubtitleEncoder::SubtitleEncoder (shared_ptr<const Film> film, shared_ptr<Job> job, boost::filesystem::path output, bool split_reels)
+/** @param output Directory, if there will be multiple output files, or a filename.
+ *  @param initial_name Hint that may be used to create filenames, if @ref output is a directory.
+ *  @param include_font true to refer to and export any font file (for Interop; ignored for SMPTE).
+ */
+SubtitleEncoder::SubtitleEncoder (shared_ptr<const Film> film, shared_ptr<Job> job, boost::filesystem::path output, string initial_name, bool split_reels, bool include_font)
        : Encoder (film, job)
        , _split_reels (split_reels)
+       , _include_font (include_font)
        , _reel_index (0)
        , _length (film->length())
 {
@@ -50,20 +56,23 @@ SubtitleEncoder::SubtitleEncoder (shared_ptr<const Film> film, shared_ptr<Job> j
        _player->set_ignore_audio ();
        _player->Text.connect (boost::bind(&SubtitleEncoder::text, this, _1, _2, _3, _4));
 
+       string const extension = film->interop() ? ".xml" : ".mxf";
+
        int const files = split_reels ? film->reels().size() : 1;
        for (int i = 0; i < files; ++i) {
 
                boost::filesystem::path filename = output;
-               string extension = boost::filesystem::extension (filename);
-               filename = boost::filesystem::change_extension (filename, "");
-
-               if (files > 1) {
-                       /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
-                       /// which reel it is.  Preserve the %1; it will be replaced with the reel number.
-                       filename = filename.string() + String::compose(_("_reel%1"), i + 1);
+               if (boost::filesystem::is_directory(filename)) {
+                       if (files > 1) {
+                               /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
+                               /// which reel it is.  Preserve the %1; it will be replaced with the reel number.
+                               filename /= String::compose("%1_reel%2", initial_name, i + 1);
+                       } else {
+                               filename /= initial_name;
+                       }
                }
 
-               _assets.push_back (make_pair(shared_ptr<dcp::SubtitleAsset>(), filename));
+               _assets.push_back (make_pair(shared_ptr<dcp::SubtitleAsset>(), boost::filesystem::change_extension(filename, extension)));
        }
 
        BOOST_FOREACH (dcpomatic::DCPTimePeriod i, film->reels()) {
@@ -101,6 +110,12 @@ SubtitleEncoder::go ()
                        }
                }
 
+               if (!_film->interop() || _include_font) {
+                       BOOST_FOREACH (shared_ptr<dcpomatic::Font> j, _player->get_subtitle_fonts()) {
+                               i->first->add_font (j->id(), default_font_file());
+                       }
+               }
+
                i->first->write (i->second);
                ++reel;
        }
@@ -115,18 +130,18 @@ SubtitleEncoder::text (PlayerText subs, TextType type, optional<DCPTextTrack> tr
 
        if (!_assets[_reel_index].first) {
                shared_ptr<dcp::SubtitleAsset> asset;
-               string lang = _film->subtitle_language ();
+               vector<dcp::LanguageTag> lang = _film->subtitle_languages ();
                if (_film->interop ()) {
                        shared_ptr<dcp::InteropSubtitleAsset> s (new dcp::InteropSubtitleAsset());
                        s->set_movie_title (_film->name());
-                       s->set_language (lang.empty() ? "Unknown" : lang);
+                       s->set_language (lang.empty() ? "Unknown" : lang.front().to_string());
                        s->set_reel_number (raw_convert<string>(_reel_index + 1));
                        _assets[_reel_index].first = s;
                } else {
                        shared_ptr<dcp::SMPTESubtitleAsset> s (new dcp::SMPTESubtitleAsset());
                        s->set_content_title_text (_film->name());
                        if (!lang.empty()) {
-                               s->set_language (lang);
+                               s->set_language (lang.front().to_string());
                        } else {
                                s->set_language (track->language);
                        }
@@ -145,6 +160,9 @@ SubtitleEncoder::text (PlayerText subs, TextType type, optional<DCPTextTrack> tr
                /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */
                i.set_in  (i.in());
                i.set_out (i.out());
+               if (_film->interop() && !_include_font) {
+                       i.unset_font ();
+               }
                _assets[_reel_index].first->add (shared_ptr<dcp::Subtitle>(new dcp::SubtitleString(i)));
        }