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 if (sub->asset_ref().resolved()) {
48 add_fonts_from_asset(reel_index, sub->asset());
52 for (auto ccap: reel->closed_captions()) {
53 if (ccap->asset_ref().resolved()) {
54 add_fonts_from_asset(reel_index, ccap->asset());
64 FontIDAllocator::add_fonts_from_asset(int reel_index, shared_ptr<const dcp::SubtitleAsset> asset)
66 for (auto const& font: asset->font_data()) {
67 add_font(reel_index, asset->id(), font.first);
73 FontIDAllocator::add_font(int reel_index, string asset_id, string font_id)
75 auto font = Font(reel_index, asset_id, font_id);
84 FontIDAllocator::allocate()
86 std::set<string> used_ids;
88 for (auto& font: _map) {
89 auto proposed = font.first.font_id;
91 while (used_ids.find(proposed) != used_ids.end()) {
92 proposed = String::compose("%1_%2", prefix++, font.first.font_id);
93 DCPOMATIC_ASSERT(prefix < 128);
95 font.second = proposed;
96 used_ids.insert(proposed);
102 FontIDAllocator::font_id(int reel_index, string asset_id, string font_id) const
104 auto iter = _map.find(Font(reel_index, asset_id, font_id));
105 if (iter == _map.end()) {
106 return default_font_id();
113 FontIDAllocator::default_font_id() const
116 return font_id(_default_font->reel_index, _default_font->asset_id, _default_font->font_id);