2 Copyright (C) 2014-2021 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"
23 #include "font_config.h"
24 #include <fontconfig/fontconfig.h>
25 #include <boost/optional.hpp>
29 using boost::optional;
32 FontConfig* FontConfig::_instance;
35 FontConfig::FontConfig()
37 _config = FcInitLoadConfigAndFonts();
38 FcConfigSetCurrent(_config);
43 FontConfig::make_font_available(boost::filesystem::path font_file)
45 auto existing = _available_fonts.find(font_file);
46 if (existing != _available_fonts.end()) {
47 return existing->second;
50 /* Make this font available to DCP-o-matic */
51 optional<string> font_name;
52 FcConfigAppFontAddFile (_config, reinterpret_cast<FcChar8 const *>(font_file.string().c_str()));
53 auto pattern = FcPatternBuild (
54 0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *>(0)
56 auto object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
57 auto font_set = FcFontList (_config, pattern, object_set);
59 for (int i = 0; i < font_set->nfont; ++i) {
60 FcPattern* font = font_set->fonts[i];
65 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
66 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
67 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
69 font_name = reinterpret_cast<char const *> (family);
73 FcFontSetDestroy (font_set);
76 FcObjectSetDestroy (object_set);
77 FcPatternDestroy (pattern);
79 DCPOMATIC_ASSERT(font_name);
81 _available_fonts[font_file] = *font_name;
83 FcConfigBuildFonts(_config);
89 FontConfig::instance()
92 _instance = new FontConfig();