From db22f81ccce9e1a5f205e6d8b3c0631fc039a173 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sat, 13 Jan 2024 23:34:35 +0100 Subject: Fix handling of empty font IDs and default DCP fonts (#2721) (part of #2722). Previously we used an empty font ID as the default for when a subtitle has no Font, but in #2721 we saw a DCP with an empty font ID which raised an assertion (because we'd already added our default font with the empty ID). Here we try to fix this (and also make the default font correctly be that from the first ). --- src/lib/dcp_decoder.cc | 6 +++++- src/lib/dcp_examiner.cc | 14 ++++++++++---- src/lib/dcp_examiner.h | 2 -- src/lib/dcp_subtitle_content.cc | 30 +++++++++++++++++++++++++++--- src/lib/dcp_subtitle_content.h | 2 ++ src/lib/dcp_subtitle_decoder.cc | 23 ++++++++++++++++++----- src/lib/dcp_subtitle_decoder.h | 6 +++++- src/lib/font_id_allocator.cc | 20 ++++++++++++++++---- src/lib/font_id_allocator.h | 7 +++++++ 9 files changed, 90 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/lib/dcp_decoder.cc b/src/lib/dcp_decoder.cc index 3a1871863..24ff507e6 100644 --- a/src/lib/dcp_decoder.cc +++ b/src/lib/dcp_decoder.cc @@ -314,7 +314,11 @@ DCPDecoder::pass_texts ( } dcp::SubtitleString is_copy = *is; - is_copy.set_font(_font_id_allocator.font_id(_reel - _reels.begin(), asset->id(), is_copy.font().get_value_or(""))); + if (is_copy.font()) { + is_copy.set_font(_font_id_allocator.font_id(_reel - _reels.begin(), asset->id(), is_copy.font().get())); + } else { + is_copy.set_font(_font_id_allocator.default_font_id()); + } strings.push_back(is_copy); } diff --git a/src/lib/dcp_examiner.cc b/src/lib/dcp_examiner.cc index 400c7d26b..d9c904720 100644 --- a/src/lib/dcp_examiner.cc +++ b/src/lib/dcp_examiner.cc @@ -25,6 +25,7 @@ #include "dcp_examiner.h" #include "dcpomatic_log.h" #include "exceptions.h" +#include "font_id_allocator.h" #include "image.h" #include "text_content.h" #include "util.h" @@ -210,7 +211,6 @@ DCPExaminer::DCPExaminer (shared_ptr content, bool tolerant) for (auto const& font: asset->font_data()) { _fonts.push_back({reel_index, asset->id(), make_shared(font.first, font.second)}); } - _fonts.push_back({reel_index, asset->id(), make_shared("")}); } } @@ -367,16 +367,22 @@ DCPExaminer::DCPExaminer (shared_ptr content, bool tolerant) void DCPExaminer::add_fonts(shared_ptr content) { + FontIDAllocator font_id_allocator; + for (auto const& font: _fonts) { - _font_id_allocator.add_font(font.reel_index, font.asset_id, font.font->id()); + font_id_allocator.add_font(font.reel_index, font.asset_id, font.font->id()); } - _font_id_allocator.allocate(); + font_id_allocator.allocate(); for (auto const& font: _fonts) { auto font_copy = make_shared(*font.font); - font_copy->set_id(_font_id_allocator.font_id(font.reel_index, font.asset_id, font.font->id())); + font_copy->set_id(font_id_allocator.font_id(font.reel_index, font.asset_id, font.font->id())); content->add_font(font_copy); } + + if (!font_id_allocator.has_default_font()) { + content->add_font(make_shared(font_id_allocator.default_font_id(), default_font_file())); + } } diff --git a/src/lib/dcp_examiner.h b/src/lib/dcp_examiner.h index 54e283548..444fb7567 100644 --- a/src/lib/dcp_examiner.h +++ b/src/lib/dcp_examiner.h @@ -27,7 +27,6 @@ #include "audio_examiner.h" #include "dcp_text_track.h" #include "dcpomatic_assert.h" -#include "font_id_allocator.h" #include "video_examiner.h" #include #include @@ -224,5 +223,4 @@ private: }; std::vector _fonts; - FontIDAllocator _font_id_allocator; }; diff --git a/src/lib/dcp_subtitle_content.cc b/src/lib/dcp_subtitle_content.cc index b3e24d5e2..8de5967ef 100644 --- a/src/lib/dcp_subtitle_content.cc +++ b/src/lib/dcp_subtitle_content.cc @@ -21,6 +21,7 @@ #include "font.h" #include "dcp_subtitle_content.h" #include "film.h" +#include "font_id_allocator.h" #include "text_content.h" #include #include @@ -73,20 +74,43 @@ DCPSubtitleContent::examine (shared_ptr film, shared_ptr job) _length = ContentTime::from_seconds(subtitle_asset->latest_subtitle_out().as_seconds()); subtitle_asset->fix_empty_font_ids(); + add_fonts(only_text(), subtitle_asset); +} + + +void +DCPSubtitleContent::add_fonts(shared_ptr content, shared_ptr subtitle_asset) +{ + FontIDAllocator font_id_allocator; + + for (auto node: subtitle_asset->load_font_nodes()) { + font_id_allocator.add_font(0, subtitle_asset->id(), node->id); + } + font_id_allocator.allocate(); auto font_data = subtitle_asset->font_data(); for (auto node: subtitle_asset->load_font_nodes()) { auto data = font_data.find(node->id); + shared_ptr font; if (data != font_data.end()) { - only_text()->add_font(make_shared(node->id, data->second)); + font = make_shared( + font_id_allocator.font_id(0, subtitle_asset->id(), node->id), + data->second + ); } else { - only_text()->add_font(make_shared(node->id)); + font = make_shared( + font_id_allocator.font_id(0, subtitle_asset->id(), node->id) + ); } + content->add_font(font); } - only_text()->add_font(make_shared("")); + if (!font_id_allocator.has_default_font()) { + content->add_font(make_shared(font_id_allocator.default_font_id(), default_font_file())); + } } + DCPTime DCPSubtitleContent::full_length (shared_ptr film) const { diff --git a/src/lib/dcp_subtitle_content.h b/src/lib/dcp_subtitle_content.h index 5949f8b0b..89a6f26a2 100644 --- a/src/lib/dcp_subtitle_content.h +++ b/src/lib/dcp_subtitle_content.h @@ -35,5 +35,7 @@ public: dcpomatic::DCPTime approximate_length () const override; private: + void add_fonts(std::shared_ptr content, std::shared_ptr subtitle_asset); + dcpomatic::ContentTime _length; }; diff --git a/src/lib/dcp_subtitle_decoder.cc b/src/lib/dcp_subtitle_decoder.cc index b3e6d7553..711dc77f2 100644 --- a/src/lib/dcp_subtitle_decoder.cc +++ b/src/lib/dcp_subtitle_decoder.cc @@ -43,15 +43,22 @@ DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr film, shared_ptr< : Decoder (film) { /* Load the XML or MXF file */ - auto const asset = load (content->path(0)); - asset->fix_empty_font_ids (); - _subtitles = asset->subtitles (); + _asset = load(content->path(0)); + _asset->fix_empty_font_ids(); + _subtitles = _asset->subtitles(); _next = _subtitles.begin (); - _subtitle_standard = asset->subtitle_standard(); + _subtitle_standard = _asset->subtitle_standard(); text.push_back (make_shared(this, content->only_text())); update_position(); + + FontIDAllocator font_id_allocator; + + for (auto node: _asset->load_font_nodes()) { + _font_id_allocator.add_font(0, _asset->id(), node->id); + } + _font_id_allocator.allocate(); } @@ -91,7 +98,13 @@ DCPSubtitleDecoder::pass () while (_next != _subtitles.end () && content_time_period (*_next) == p) { auto ns = dynamic_pointer_cast(*_next); if (ns) { - s.push_back (*ns); + dcp::SubtitleString ns_copy = *ns; + if (ns_copy.font()) { + ns_copy.set_font(_font_id_allocator.font_id(0, _asset->id(), ns_copy.font().get())); + } else { + ns_copy.set_font(_font_id_allocator.default_font_id()); + } + s.push_back(ns_copy); ++_next; } else { /* XXX: perhaps these image subs should also be collected together like the string ones are; diff --git a/src/lib/dcp_subtitle_decoder.h b/src/lib/dcp_subtitle_decoder.h index 45a4999dd..9d0851253 100644 --- a/src/lib/dcp_subtitle_decoder.h +++ b/src/lib/dcp_subtitle_decoder.h @@ -19,8 +19,9 @@ */ -#include "text_decoder.h" #include "dcp_subtitle.h" +#include "font_id_allocator.h" +#include "text_decoder.h" class DCPSubtitleContent; @@ -44,4 +45,7 @@ private: std::vector>::const_iterator _next; dcp::SubtitleStandard _subtitle_standard; + + std::shared_ptr _asset; + FontIDAllocator _font_id_allocator; }; diff --git a/src/lib/font_id_allocator.cc b/src/lib/font_id_allocator.cc index c566c3676..5263e7f90 100644 --- a/src/lib/font_id_allocator.cc +++ b/src/lib/font_id_allocator.cc @@ -64,17 +64,19 @@ void FontIDAllocator::add_fonts_from_asset(int reel_index, shared_ptr asset) { for (auto const& font: asset->font_data()) { - _map[Font(reel_index, asset->id(), font.first)] = 0; + add_font(reel_index, asset->id(), font.first); } - - _map[Font(reel_index, asset->id(), "")] = 0; } void FontIDAllocator::add_font(int reel_index, string asset_id, string font_id) { - _map[Font(reel_index, asset_id, font_id)] = 0; + auto font = Font(reel_index, asset_id, font_id); + if (!_default_font) { + _default_font = font; + } + _map[font] = 0; } @@ -119,3 +121,13 @@ FontIDAllocator::font_id(int reel_index, string asset_id, string font_id) const return String::compose("%1_%2", iter->second, font_id); } + +string +FontIDAllocator::default_font_id() const +{ + if (_default_font) { + return font_id(_default_font->reel_index, _default_font->asset_id, _default_font->font_id); + } + + return "default"; +} diff --git a/src/lib/font_id_allocator.h b/src/lib/font_id_allocator.h index bd99cad63..fe4b9ef07 100644 --- a/src/lib/font_id_allocator.h +++ b/src/lib/font_id_allocator.h @@ -27,6 +27,7 @@ #include #include #include +#include namespace dcp { @@ -66,6 +67,11 @@ public: void allocate(); std::string font_id(int reel_index, std::string asset_id, std::string font_id) const; + std::string default_font_id() const; + + bool has_default_font() const { + return static_cast(_default_font); + } private: void add_fonts_from_asset(int reel_index, std::shared_ptr asset); @@ -96,6 +102,7 @@ private: }; std::map _map; + boost::optional _default_font; }; -- cgit v1.2.3