Supporters update.
[dcpomatic.git] / src / lib / font_comparator.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 #include "dcpomatic_assert.h"
23
24
25 /** Comparator to allow dcpomatic::Font::Content to be compared for use in a map */
26 struct FontComparator
27 {
28         bool operator()(dcpomatic::Font::Content const& a, dcpomatic::Font::Content const& b) const {
29                 auto const a_has_file = static_cast<bool>(a.file);
30                 auto const b_has_file = static_cast<bool>(b.file);
31                 auto const a_has_data = static_cast<bool>(a.data);
32                 auto const b_has_data = static_cast<bool>(b.data);
33
34                 if (!a_has_file && !a_has_data && !b_has_file && !b_has_data) {
35                         /* Neither font has any font data: a == b */
36                         return false;
37                 } else if (!a_has_file && !a_has_data) {
38                         /* Fonts with no data are the "lowest": a < b */
39                         return true;
40                 } else if (!b_has_file && !b_has_data) {
41                         /* ... so here b < a */
42                         return false;
43                 } else if (a_has_file && !b_has_file) {
44                         /* Fonts with file are lower than fonts with data: a < b */
45                         return true;
46                 } else if (!a_has_file && b_has_file) {
47                         /* ... so here b < a */
48                         return false;
49                 } else if (a_has_file && b_has_file) {
50                         /* Compared as "equals" */
51                         return a.file->string() < b.file->string();
52                 } else if (a_has_data && b_has_data) {
53                         /* Compared as "equals" */
54                         auto const a_size = a.data->size();
55                         auto const b_size = b.data->size();
56                         if (a_size != b_size) {
57                                 return a_size < b_size;
58                         }
59                         return memcmp(a.data->data(), b.data->data(), a_size) < 0;
60                 }
61
62                 /* Should never get here */
63                 DCPOMATIC_ASSERT(false);
64                 return false;
65         };
66 };
67
68