1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/*
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 "constants.h"
#include "dcpomatic_assert.h"
#include "font_id_allocator.h"
#include <dcp/reel.h>
#include <dcp/reel_text_asset.h>
#include <dcp/text_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()) {
if (sub->asset_ref().resolved()) {
add_fonts_from_asset(reel_index, sub->asset());
}
}
if (auto sub = reel->main_caption()) {
if (sub->asset_ref().resolved()) {
add_fonts_from_asset(reel_index, sub->asset());
}
}
for (auto ccap: reel->closed_subtitles()) {
if (ccap->asset_ref().resolved()) {
add_fonts_from_asset(reel_index, ccap->asset());
}
}
for (auto ccap: reel->closed_captions()) {
if (ccap->asset_ref().resolved()) {
add_fonts_from_asset(reel_index, ccap->asset());
}
}
++reel_index;
}
}
void
FontIDAllocator::add_fonts_from_asset(int reel_index, shared_ptr<const dcp::TextAsset> asset)
{
for (auto const& font: asset->font_data()) {
add_font(reel_index, asset->id(), font.first);
}
}
void
FontIDAllocator::add_font(int reel_index, string asset_id, string font_id)
{
auto font = Font(reel_index, asset_id, font_id);
if (!_default_font) {
_default_font = font;
}
_map[font] = {};
}
void
FontIDAllocator::allocate()
{
std::set<string> used_ids;
for (auto& font: _map) {
auto proposed = font.first.font_id;
int prefix = 0;
while (used_ids.find(proposed) != used_ids.end()) {
proposed = fmt::format("{}_{}", prefix++, font.first.font_id);
DCPOMATIC_ASSERT(prefix < 128);
}
font.second = proposed;
used_ids.insert(proposed);
}
}
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));
if (iter == _map.end()) {
return default_font_id();
}
return iter->second;
}
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";
}
|