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/filesystem.hpp>
26 #include <boost/optional.hpp>
30 using boost::optional;
33 FontConfig* FontConfig::_instance;
36 FontConfig::FontConfig()
38 _config = FcInitLoadConfigAndFonts();
39 FcConfigSetCurrent(_config);
44 FontConfig::make_font_available(boost::filesystem::path font_file)
46 auto existing = _available_fonts.find(font_file);
47 if (existing != _available_fonts.end()) {
48 return existing->second;
51 /* Make this font available to DCP-o-matic */
52 optional<string> font_name;
53 FcConfigAppFontAddFile (_config, reinterpret_cast<FcChar8 const *>(font_file.string().c_str()));
54 auto pattern = FcPatternBuild (
55 0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *>(0)
57 auto object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
58 auto font_set = FcFontList (_config, pattern, object_set);
60 for (int i = 0; i < font_set->nfont; ++i) {
61 FcPattern* font = font_set->fonts[i];
66 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
67 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
68 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
70 font_name = reinterpret_cast<char const *> (family);
74 FcFontSetDestroy (font_set);
77 FcObjectSetDestroy (object_set);
78 FcPatternDestroy (pattern);
80 DCPOMATIC_ASSERT(font_name);
82 _available_fonts[font_file] = *font_name;
84 FcConfigBuildFonts(_config);
89 optional<boost::filesystem::path>
90 FontConfig::system_font_with_name(string name)
92 optional<boost::filesystem::path> path;
94 auto pattern = FcNameParse(reinterpret_cast<FcChar8 const*>(name.c_str()));
95 auto object_set = FcObjectSetBuild(FC_FILE, nullptr);
96 auto font_set = FcFontList(_config, pattern, object_set);
98 for (int i = 0; i < font_set->nfont; ++i) {
99 auto font = font_set->fonts[i];
101 if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
102 path = boost::filesystem::path(reinterpret_cast<char*>(file));
106 FcFontSetDestroy(font_set);
109 FcObjectSetDestroy(object_set);
110 FcPatternDestroy(pattern);
117 FontConfig::instance()
120 _instance = new FontConfig();