diff options
| author | Carl Hetherington <cth@carlh.net> | 2023-10-14 21:48:25 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2023-10-15 09:10:18 +0200 |
| commit | 3c802dd6d1451c2c8a7e188f8379738d72e907eb (patch) | |
| tree | 454396cf5451535b8708a0c4961c7d5c2b30ea1f /src/lib/font_id_allocator.cc | |
| parent | 1bfe44b1503fb0f5cffda135076709014337de52 (diff) | |
Fix DCP content font ID allocation to cope with DCPs that have multiple fonts
with the same name in the same reel (#2600).
Previously we had this id_for_font_in_reel() which would give an ID
of N_font-ID. This means we got duplicate font IDs.
Here we replace that method with FontAllocator, which gives an ID of
N_font-ID for the first font and M_font-ID, where M is a number higher than
the highest reel index. The idea is to support the required new IDs
without breaking exisiting projects.
There is some documentation of how it works in doc/design/fonts
Diffstat (limited to 'src/lib/font_id_allocator.cc')
| -rw-r--r-- | src/lib/font_id_allocator.cc | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/src/lib/font_id_allocator.cc b/src/lib/font_id_allocator.cc new file mode 100644 index 000000000..ef25dc642 --- /dev/null +++ b/src/lib/font_id_allocator.cc @@ -0,0 +1,119 @@ +/* + Copyright (C) 2023 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#include "compose.hpp" +#include "constants.h" +#include "dcpomatic_assert.h" +#include "font_id_allocator.h" +#include <dcp/reel.h> +#include <dcp/reel_closed_caption_asset.h> +#include <dcp/reel_subtitle_asset.h> +#include <dcp/subtitle_asset.h> +#include <set> +#include <string> +#include <vector> + + +using std::shared_ptr; +using std::set; +using std::string; +using std::vector; + + +void +FontIDAllocator::add_fonts_from_reels(vector<shared_ptr<dcp::Reel>> const& reels) +{ + int reel_index = 0; + for (auto reel: reels) { + if (auto sub = reel->main_subtitle()) { + add_fonts_from_asset(reel_index, sub->asset()); + } + + for (auto ccap: reel->closed_captions()) { + add_fonts_from_asset(reel_index, ccap->asset()); + } + + ++reel_index; + } +} + + +void +FontIDAllocator::add_fonts_from_asset(int reel_index, shared_ptr<const dcp::SubtitleAsset> asset) +{ + for (auto const& font: asset->font_data()) { + _map[Font(reel_index, asset->id(), font.first)] = 0; + } + + if (asset->font_data().empty()) { + _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; +} + + +void +FontIDAllocator::allocate() +{ + /* Last reel index that we have; i.e. the last prefix number that would be used by "old" + * font IDs (i.e. ones before this FontIDAllocator was added and used) + */ + auto const last_reel = std::max_element( + _map.begin(), + _map.end(), + [] (std::pair<Font, int> const& a, std::pair<Font, int> const& b) { + return a.first.reel_index < b.first.reel_index; + })->first.reel_index; + + /* Number of times each ID has been used */ + std::map<string, int> used_count; + + for (auto& font: _map) { + auto const proposed = String::compose("%1_%2", font.first.reel_index, font.first.font_id); + if (used_count.find(proposed) != used_count.end()) { + /* This ID was already used; we need to disambiguate it. Do so by using + * an ID above last_reel + */ + font.second = last_reel + used_count[proposed]; + ++used_count[proposed]; + } else { + /* This ID was not yet used */ + used_count[proposed] = 1; + font.second = font.first.reel_index; + } + } +} + + +string +FontIDAllocator::font_id(int reel_index, string asset_id, string font_id) const +{ + auto iter = _map.find(Font(reel_index, asset_id, font_id)); + DCPOMATIC_ASSERT(iter != _map.end()); + return String::compose("%1_%2", iter->second, font_id); +} + |
