2 Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "dcpomatic_assert.h"
25 /** Comparator to allow dcpomatic::Font::Content to be compared for use in a map */
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);
34 if (!a_has_file && !a_has_data && !b_has_file && !b_has_data) {
35 /* Neither font has any font data: a == b */
37 } else if (!a_has_file && !a_has_data) {
38 /* Fonts with no data are the "lowest": a < b */
40 } else if (!b_has_file && !b_has_data) {
41 /* ... so here b < a */
43 } else if (a_has_file && !b_has_file) {
44 /* Fonts with file are lower than fonts with data: a < b */
46 } else if (!a_has_file && b_has_file) {
47 /* ... so here b < a */
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;
59 return memcmp(a.data->data(), b.data->data(), a_size) < 0;
62 /* Should never get here */
63 DCPOMATIC_ASSERT(false);