BOOST_FOREACH.
[dcpomatic.git] / src / lib / reel_writer.cc
index 8ced98a114e81c4feede52fe24e89ef35b240ad0..b81c225c276517ee0e6e7b17c2cc255684f4d381 100644 (file)
@@ -25,7 +25,7 @@
 #include "log.h"
 #include "dcpomatic_log.h"
 #include "digester.h"
-#include "font.h"
+#include "font_data.h"
 #include "compose.hpp"
 #include "config.h"
 #include "audio_buffers.h"
@@ -51,7 +51,6 @@
 #include <dcp/smpte_subtitle_asset.h>
 #include <dcp/raw_convert.h>
 #include <dcp/subtitle_image.h>
-#include <boost/foreach.hpp>
 
 #include "i18n.h"
 
@@ -60,14 +59,15 @@ using std::string;
 using std::cout;
 using std::exception;
 using std::map;
+using std::set;
 using std::vector;
-using boost::shared_ptr;
+using std::shared_ptr;
 using boost::optional;
-using boost::dynamic_pointer_cast;
+using std::dynamic_pointer_cast;
 #if BOOST_VERSION >= 106100
 using namespace boost::placeholders;
 #endif
-using boost::weak_ptr;
+using std::weak_ptr;
 using dcp::ArrayData;
 using dcp::Data;
 using dcp::raw_convert;
@@ -154,7 +154,7 @@ ReelWriter::ReelWriter (
 
                _picture_asset->set_file (asset);
                _picture_asset_writer = _picture_asset->start_write (asset, _first_nonexistant_frame > 0);
-       } else {
+       } else if (!text_only) {
                /* We already have a complete picture asset that we can just re-use */
                /* XXX: what about if the encryption key changes? */
                if (film()->three_d()) {
@@ -178,7 +178,7 @@ ReelWriter::ReelWriter (
                DCPOMATIC_ASSERT (film()->directory());
 
                vector<dcp::Channel> active;
-               BOOST_FOREACH (int i, film()->mapped_audio_channels()) {
+               for (auto i: film()->mapped_audio_channels()) {
                        active.push_back (static_cast<dcp::Channel>(i));
                }
 
@@ -191,6 +191,8 @@ ReelWriter::ReelWriter (
                        film()->contains_atmos_content()
                        );
        }
+
+       _default_font = dcp::ArrayData(default_font_file());
 }
 
 /** @param frame reel-relative frame */
@@ -443,7 +445,8 @@ maybe_add_text (
        int64_t picture_duration,
        shared_ptr<dcp::Reel> reel,
        list<ReferencedReelAsset> const & refs,
-       list<shared_ptr<Font> > const & fonts,
+       vector<FontData> const & fonts,
+       dcp::ArrayData default_font,
        shared_ptr<const Film> film,
        DCPTimePeriod period,
        boost::filesystem::path output_dcp,
@@ -456,8 +459,8 @@ maybe_add_text (
 
        if (asset) {
                /* Add the font to the subtitle content */
-               BOOST_FOREACH (shared_ptr<Font> j, fonts) {
-                       asset->add_font (j->id(), j->file().get_value_or(default_font_file()));
+               for (auto const& j: fonts) {
+                       asset->add_font (j.id, j.data.get_value_or(default_font));
                }
 
                if (dynamic_pointer_cast<dcp::InteropSubtitleAsset> (asset)) {
@@ -487,7 +490,7 @@ maybe_add_text (
                        );
        } else {
                /* We don't have a subtitle asset of our own; hopefully we have one to reference */
-               BOOST_FOREACH (ReferencedReelAsset j, refs) {
+               for (auto j: refs) {
                        shared_ptr<T> k = dynamic_pointer_cast<T> (j.asset);
                        if (k && j.period == period) {
                                reel_asset = k;
@@ -532,7 +535,7 @@ ReelWriter::create_reel_picture (shared_ptr<dcp::Reel> reel, list<ReferencedReel
        } else {
                LOG_GENERAL ("no picture asset of our own; look through %1", refs.size());
                /* We don't have a picture asset of our own; hopefully we have one to reference */
-               BOOST_FOREACH (ReferencedReelAsset j, refs) {
+               for (auto j: refs) {
                        shared_ptr<dcp::ReelPictureAsset> k = dynamic_pointer_cast<dcp::ReelPictureAsset> (j.asset);
                        if (k) {
                                LOG_GENERAL ("candidate picture asset period is %1-%2", j.period.from.get(), j.period.to.get());
@@ -574,7 +577,7 @@ ReelWriter::create_reel_sound (shared_ptr<dcp::Reel> reel, list<ReferencedReelAs
        } else {
                LOG_GENERAL ("no sound asset of our own; look through %1", refs.size());
                /* We don't have a sound asset of our own; hopefully we have one to reference */
-               BOOST_FOREACH (ReferencedReelAsset j, refs) {
+               for (auto j: refs) {
                        shared_ptr<dcp::ReelSoundAsset> k = dynamic_pointer_cast<dcp::ReelSoundAsset> (j.asset);
                        if (k) {
                                LOG_GENERAL ("candidate sound asset period is %1-%2", j.period.from.get(), j.period.to.get());
@@ -614,27 +617,60 @@ void
 ReelWriter::create_reel_text (
        shared_ptr<dcp::Reel> reel,
        list<ReferencedReelAsset> const & refs,
-       list<shared_ptr<Font> > const& fonts,
+       vector<FontData> const& fonts,
        int64_t duration,
-       boost::filesystem::path output_dcp
+       boost::filesystem::path output_dcp,
+       bool ensure_subtitles,
+       set<DCPTextTrack> ensure_closed_captions
        ) const
 {
        shared_ptr<dcp::ReelSubtitleAsset> subtitle = maybe_add_text<dcp::ReelSubtitleAsset> (
-               _subtitle_asset, duration, reel, refs, fonts, film(), _period, output_dcp, _text_only
+               _subtitle_asset, duration, reel, refs, fonts, _default_font, film(), _period, output_dcp, _text_only
                );
-       if (subtitle && !film()->subtitle_languages().empty()) {
-               subtitle->set_language (film()->subtitle_languages().front());
+
+       if (subtitle) {
+               /* We have a subtitle asset that we either made or are referencing */
+               if (!film()->subtitle_languages().empty()) {
+                       subtitle->set_language (film()->subtitle_languages().front());
+               }
+       } else if (ensure_subtitles) {
+               /* We had no subtitle asset, but we've been asked to make sure there is one */
+               subtitle = maybe_add_text<dcp::ReelSubtitleAsset>(
+                       empty_text_asset(TEXT_OPEN_SUBTITLE, optional<DCPTextTrack>()),
+                       duration,
+                       reel,
+                       refs,
+                       fonts,
+                       _default_font,
+                       film(),
+                       _period,
+                       output_dcp,
+                       _text_only
+                       );
        }
 
        for (map<DCPTextTrack, shared_ptr<dcp::SubtitleAsset> >::const_iterator i = _closed_caption_assets.begin(); i != _closed_caption_assets.end(); ++i) {
                shared_ptr<dcp::ReelClosedCaptionAsset> a = maybe_add_text<dcp::ReelClosedCaptionAsset> (
-                       i->second, duration, reel, refs, fonts, film(), _period, output_dcp, _text_only
+                       i->second, duration, reel, refs, fonts, _default_font, film(), _period, output_dcp, _text_only
                        );
-               if (a) {
-                       a->set_annotation_text (i->first.name);
-                       if (!i->first.language.empty()) {
-                               a->set_language (dcp::LanguageTag(i->first.language));
-                       }
+               DCPOMATIC_ASSERT (a);
+               a->set_annotation_text (i->first.name);
+               if (!i->first.language.empty()) {
+                       a->set_language (dcp::LanguageTag(i->first.language));
+               }
+
+               ensure_closed_captions.erase (i->first);
+       }
+
+       /* Make empty tracks for anything we've been asked to ensure but that we haven't added */
+       for (auto i: ensure_closed_captions) {
+               shared_ptr<dcp::ReelClosedCaptionAsset> a = maybe_add_text<dcp::ReelClosedCaptionAsset> (
+                       empty_text_asset(TEXT_CLOSED_CAPTION, i), duration, reel, refs, fonts, _default_font, film(), _period, output_dcp, _text_only
+                       );
+               DCPOMATIC_ASSERT (a);
+               a->set_annotation_text (i.name);
+               if (!i.language.empty()) {
+                       a->set_language (dcp::LanguageTag(i.language));
                }
        }
 }
@@ -666,8 +702,17 @@ ReelWriter::create_reel_markers (shared_ptr<dcp::Reel> reel) const
 }
 
 
+/** @param ensure_subtitles true to make sure the reel has a subtitle asset.
+ *  @param ensure_closed_captions make sure the reel has these closed caption tracks.
+ */
 shared_ptr<dcp::Reel>
-ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr<Font> > const & fonts, boost::filesystem::path output_dcp)
+ReelWriter::create_reel (
+       list<ReferencedReelAsset> const & refs,
+       vector<FontData> const & fonts,
+       boost::filesystem::path output_dcp,
+       bool ensure_subtitles,
+       set<DCPTextTrack> ensure_closed_captions
+       )
 {
        LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count);
 
@@ -685,7 +730,7 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                create_reel_markers (reel);
        }
 
-       create_reel_text (reel, refs, fonts, duration, output_dcp);
+       create_reel_text (reel, refs, fonts, duration, output_dcp, ensure_subtitles, ensure_closed_captions);
 
        if (_atmos_asset) {
                reel->add (shared_ptr<dcp::ReelAtmosAsset>(new dcp::ReelAtmosAsset(_atmos_asset, 0)));
@@ -801,14 +846,14 @@ ReelWriter::write (PlayerText subs, TextType type, optional<DCPTextTrack> track,
                DCPOMATIC_ASSERT (false);
        }
 
-       BOOST_FOREACH (StringText i, subs.string) {
+       for (auto i: subs.string) {
                /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */
                i.set_in  (i.in()  - dcp::Time (_period.from.seconds(), i.in().tcr));
                i.set_out (i.out() - dcp::Time (_period.from.seconds(), i.out().tcr));
                asset->add (shared_ptr<dcp::Subtitle>(new dcp::SubtitleString(i)));
        }
 
-       BOOST_FOREACH (BitmapText i, subs.bitmap) {
+       for (auto i: subs.bitmap) {
                asset->add (
                        shared_ptr<dcp::Subtitle>(
                                new dcp::SubtitleImage(