+template <class T>
+shared_ptr<T>
+maybe_add_text (
+ shared_ptr<dcp::SubtitleAsset> asset,
+ int64_t picture_duration,
+ shared_ptr<dcp::Reel> reel,
+ list<ReferencedReelAsset> const & refs,
+ list<shared_ptr<Font> > const & fonts,
+ shared_ptr<const Film> film,
+ DCPTimePeriod period
+ )
+{
+ Frame const period_duration = period.duration().frames_round(film->video_frame_rate());
+
+ shared_ptr<T> reel_asset;
+
+ if (asset) {
+ boost::filesystem::path liberation_normal;
+ try {
+ liberation_normal = shared_path() / "LiberationSans-Regular.ttf";
+ if (!boost::filesystem::exists (liberation_normal)) {
+ /* Hack for unit tests */
+ liberation_normal = shared_path() / "fonts" / "LiberationSans-Regular.ttf";
+ }
+ } catch (boost::filesystem::filesystem_error& e) {
+
+ }
+
+ if (!boost::filesystem::exists(liberation_normal)) {
+ liberation_normal = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf";
+ }
+
+ /* Add all the fonts to the subtitle content */
+ BOOST_FOREACH (shared_ptr<Font> j, fonts) {
+ asset->add_font (j->id(), j->file(FontFiles::NORMAL).get_value_or(liberation_normal));
+ }
+
+ if (dynamic_pointer_cast<dcp::InteropSubtitleAsset> (asset)) {
+ boost::filesystem::path directory = film->dir (film->dcp_name ()) / asset->id ();
+ boost::filesystem::create_directories (directory);
+ asset->write (directory / ("sub_" + asset->id() + ".xml"));
+ } else {
+ /* All our assets should be the same length; use the picture asset length here
+ as a reference to set the subtitle one. We'll use the duration rather than
+ the intrinsic duration; we don't care if the picture asset has been trimmed, we're
+ just interested in its presentation length.
+ */
+ dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(asset)->set_intrinsic_duration (picture_duration);
+
+ asset->write (
+ film->dir(film->dcp_name()) / ("sub_" + asset->id() + ".mxf")
+ );
+ }
+
+ reel_asset.reset (
+ new T (
+ asset,
+ dcp::Fraction (film->video_frame_rate(), 1),
+ picture_duration,
+ 0
+ )
+ );
+ } else {
+ /* We don't have a subtitle asset of our own; hopefully we have one to reference */
+ BOOST_FOREACH (ReferencedReelAsset j, refs) {
+ shared_ptr<T> k = dynamic_pointer_cast<T> (j.asset);
+ if (k && j.period == period) {
+ reel_asset = k;
+ /* If we have a hash for this asset in the CPL, assume that it is correct */
+ if (k->hash()) {
+ k->asset_ref()->set_hash (k->hash().get());
+ }
+ }
+ }
+ }
+
+ if (reel_asset) {
+ if (reel_asset->duration() != period_duration) {
+ throw ProgrammingError (
+ __FILE__, __LINE__,
+ String::compose ("%1 vs %2", reel_asset->duration(), period_duration)
+ );
+ }
+ reel->add (reel_asset);
+ }
+
+ return reel_asset;
+}
+