Cleanup: handle Filter objects by value rather than by reference.
[dcpomatic.git] / src / lib / font_id_allocator.h
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 #ifndef DCPOMATIC_FONT_ID_ALLOCATOR_H
23 #define DCPOMATIC_FONT_ID_ALLOCATOR_H
24
25
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <vector>
30
31
32 namespace dcp {
33         class Reel;
34         class SubtitleAsset;
35 }
36
37
38 /** A class which, given some pairs of (asset-id, font-id) can return a font ID
39  *  which is unique in a piece of content.
40  *
41  *  When we examine a 2-reel DCP we may have a pair of subtitle assets that
42  *  each have a font with ID "foo".  We want to store these in
43  *  TextContent::_fonts in such a way that they are distinguishable.
44  *
45  *  If TextContent is to carry on storing a list of dcpomatic::Font, we can
46  *  only do this by making each dcpomatic::Font have a different ID (i.e. not
47  *  both "foo").
48  *
49  *  Hence when we add the fonts to the TextContent we re-write them to have
50  *  unique IDs.
51  *
52  *  When we do this, we must do it in a repeatable way so that when the
53  *  DCPDecoder receives the "foo" font IDs it can obtain the same "new" ID given
54  *  "foo" and the asset ID that it came from.
55  *
56  *  FontIDAllocator can help with that: call add_fonts_from_reels() or add_font(),
57  *  then allocate(), then it will return repeatable unique "new" font IDs from
58  *  an asset map and "old" ID.
59  */
60 class FontIDAllocator
61 {
62 public:
63         void add_fonts_from_reels(std::vector<std::shared_ptr<dcp::Reel>> const& reels);
64         void add_font(int reel_index, std::string asset_id, std::string font_id);
65
66         void allocate();
67
68         std::string font_id(int reel_index, std::string asset_id, std::string font_id) const;
69
70 private:
71         void add_fonts_from_asset(int reel_index, std::shared_ptr<const dcp::SubtitleAsset> asset);
72
73         struct Font
74         {
75                 Font(int reel_index_, std::string asset_id_, std::string font_id_)
76                         : reel_index(reel_index_)
77                         , asset_id(asset_id_)
78                         , font_id(font_id_)
79                 {}
80
81                 bool operator<(Font const& other) const {
82                         if (reel_index != other.reel_index) {
83                                 return reel_index < other.reel_index;
84                         }
85
86                         if (asset_id != other.asset_id) {
87                                 return asset_id < other.asset_id;
88                         }
89
90                         return font_id < other.font_id;
91                 }
92
93                 int reel_index;
94                 std::string asset_id;
95                 std::string font_id;
96         };
97
98         std::map<Font, int> _map;
99 };
100
101
102 #endif