Supporters update.
[dcpomatic.git] / src / lib / font_id_allocator.cc
1 /*
2     Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "compose.hpp"
23 #include "constants.h"
24 #include "dcpomatic_assert.h"
25 #include "font_id_allocator.h"
26 #include <dcp/reel.h>
27 #include <dcp/reel_closed_caption_asset.h>
28 #include <dcp/reel_subtitle_asset.h>
29 #include <dcp/subtitle_asset.h>
30 #include <set>
31 #include <string>
32 #include <vector>
33
34
35 using std::shared_ptr;
36 using std::set;
37 using std::string;
38 using std::vector;
39
40
41 void
42 FontIDAllocator::add_fonts_from_reels(vector<shared_ptr<dcp::Reel>> const& reels)
43 {
44         int reel_index = 0;
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());
49                         }
50                 }
51
52                 for (auto ccap: reel->closed_captions()) {
53                         if (ccap->asset_ref().resolved()) {
54                                 add_fonts_from_asset(reel_index, ccap->asset());
55                         }
56                 }
57
58                 ++reel_index;
59         }
60 }
61
62
63 void
64 FontIDAllocator::add_fonts_from_asset(int reel_index, shared_ptr<const dcp::SubtitleAsset> asset)
65 {
66         for (auto const& font: asset->font_data()) {
67                 add_font(reel_index, asset->id(), font.first);
68         }
69 }
70
71
72 void
73 FontIDAllocator::add_font(int reel_index, string asset_id, string font_id)
74 {
75         auto font = Font(reel_index, asset_id, font_id);
76         if (!_default_font) {
77                 _default_font = font;
78         }
79         _map[font] = 0;
80 }
81
82
83 void
84 FontIDAllocator::allocate()
85 {
86         /* Last reel index that we have; i.e. the last prefix number that would be used by "old"
87          * font IDs (i.e. ones before this FontIDAllocator was added and used)
88          */
89         auto const last_reel = std::max_element(
90                 _map.begin(),
91                 _map.end(),
92                 [] (std::pair<Font, int> const& a, std::pair<Font, int> const& b) {
93                         return a.first.reel_index < b.first.reel_index;
94                 })->first.reel_index;
95
96         /* Number of times each ID has been used */
97         std::map<string, int> used_count;
98
99         for (auto& font: _map) {
100                 auto const proposed = String::compose("%1_%2", font.first.reel_index, font.first.font_id);
101                 if (used_count.find(proposed) != used_count.end()) {
102                         /* This ID was already used; we need to disambiguate it.  Do so by using
103                          * an ID above last_reel
104                          */
105                         font.second = last_reel + used_count[proposed];
106                         ++used_count[proposed];
107                 } else {
108                         /* This ID was not yet used */
109                         used_count[proposed] = 1;
110                         font.second = font.first.reel_index;
111                 }
112         }
113 }
114
115
116 string
117 FontIDAllocator::font_id(int reel_index, string asset_id, string font_id) const
118 {
119         auto iter = _map.find(Font(reel_index, asset_id, font_id));
120         DCPOMATIC_ASSERT(iter != _map.end());
121         return String::compose("%1_%2", iter->second, font_id);
122 }
123
124
125 string
126 FontIDAllocator::default_font_id() const
127 {
128         if (_default_font) {
129                 return font_id(_default_font->reel_index, _default_font->asset_id, _default_font->font_id);
130         }
131
132         return "default";
133 }