2 Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "compose.hpp"
23 #include "constants.h"
24 #include "dcpomatic_assert.h"
25 #include "font_id_allocator.h"
27 #include <dcp/reel_closed_caption_asset.h>
28 #include <dcp/reel_subtitle_asset.h>
29 #include <dcp/subtitle_asset.h>
35 using std::shared_ptr;
42 FontIDAllocator::add_fonts_from_reels(vector<shared_ptr<dcp::Reel>> const& reels)
45 for (auto reel: reels) {
46 if (auto sub = reel->main_subtitle()) {
47 add_fonts_from_asset(reel_index, sub->asset());
50 for (auto ccap: reel->closed_captions()) {
51 add_fonts_from_asset(reel_index, ccap->asset());
60 FontIDAllocator::add_fonts_from_asset(int reel_index, shared_ptr<const dcp::SubtitleAsset> asset)
62 for (auto const& font: asset->font_data()) {
63 _map[Font(reel_index, asset->id(), font.first)] = 0;
66 _map[Font(reel_index, asset->id(), "")] = 0;
71 FontIDAllocator::add_font(int reel_index, string asset_id, string font_id)
73 _map[Font(reel_index, asset_id, font_id)] = 0;
78 FontIDAllocator::allocate()
80 /* Last reel index that we have; i.e. the last prefix number that would be used by "old"
81 * font IDs (i.e. ones before this FontIDAllocator was added and used)
83 auto const last_reel = std::max_element(
86 [] (std::pair<Font, int> const& a, std::pair<Font, int> const& b) {
87 return a.first.reel_index < b.first.reel_index;
90 /* Number of times each ID has been used */
91 std::map<string, int> used_count;
93 for (auto& font: _map) {
94 auto const proposed = String::compose("%1_%2", font.first.reel_index, font.first.font_id);
95 if (used_count.find(proposed) != used_count.end()) {
96 /* This ID was already used; we need to disambiguate it. Do so by using
97 * an ID above last_reel
99 font.second = last_reel + used_count[proposed];
100 ++used_count[proposed];
102 /* This ID was not yet used */
103 used_count[proposed] = 1;
104 font.second = font.first.reel_index;
111 FontIDAllocator::font_id(int reel_index, string asset_id, string font_id) const
113 auto iter = _map.find(Font(reel_index, asset_id, font_id));
114 DCPOMATIC_ASSERT(iter != _map.end());
115 return String::compose("%1_%2", iter->second, font_id);