c7361bc04823e7547a74b5117c5dfdbd732bd252
[dcpomatic.git] / src / lib / font_config.cc
1 /*
2     Copyright (C) 2014-2021 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 #include "font_config.h"
24 #include <fontconfig/fontconfig.h>
25 #include <boost/optional.hpp>
26
27
28 using std::string;
29 using boost::optional;
30
31
32 FontConfig* FontConfig::_instance;
33
34
35 FontConfig::FontConfig()
36 {
37         _config = FcInitLoadConfigAndFonts();
38         FcConfigSetCurrent(_config);
39 }
40
41
42 string
43 FontConfig::make_font_available(boost::filesystem::path font_file)
44 {
45         auto existing = _available_fonts.find(font_file);
46         if (existing != _available_fonts.end()) {
47                 return existing->second;
48         }
49
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)
55                 );
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);
58         if (font_set) {
59                 for (int i = 0; i < font_set->nfont; ++i) {
60                         FcPattern* font = font_set->fonts[i];
61                         FcChar8* file;
62                         FcChar8* family;
63                         FcChar8* style;
64                         if (
65                                 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
66                                 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
67                                 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
68                                 ) {
69                                 font_name = reinterpret_cast<char const *> (family);
70                         }
71                 }
72
73                 FcFontSetDestroy (font_set);
74         }
75
76         FcObjectSetDestroy (object_set);
77         FcPatternDestroy (pattern);
78
79         DCPOMATIC_ASSERT(font_name);
80
81         _available_fonts[font_file] = *font_name;
82
83         FcConfigBuildFonts(_config);
84         return *font_name;
85 }
86
87
88 FontConfig *
89 FontConfig::instance()
90 {
91         if (!_instance) {
92                 _instance = new FontConfig();
93         }
94
95         return _instance;
96 }
97