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