Ask fontconfig to scan the system for fonts requested by text subtitle files (#2264).
[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/filesystem.hpp>
26 #include <boost/optional.hpp>
27
28
29 using std::string;
30 using boost::optional;
31
32
33 FontConfig* FontConfig::_instance;
34
35
36 FontConfig::FontConfig()
37 {
38         _config = FcInitLoadConfigAndFonts();
39         FcConfigSetCurrent(_config);
40 }
41
42
43 string
44 FontConfig::make_font_available(boost::filesystem::path font_file)
45 {
46         auto existing = _available_fonts.find(font_file);
47         if (existing != _available_fonts.end()) {
48                 return existing->second;
49         }
50
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)
56                 );
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);
59         if (font_set) {
60                 for (int i = 0; i < font_set->nfont; ++i) {
61                         FcPattern* font = font_set->fonts[i];
62                         FcChar8* file;
63                         FcChar8* family;
64                         FcChar8* style;
65                         if (
66                                 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
67                                 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
68                                 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
69                                 ) {
70                                 font_name = reinterpret_cast<char const *> (family);
71                         }
72                 }
73
74                 FcFontSetDestroy (font_set);
75         }
76
77         FcObjectSetDestroy (object_set);
78         FcPatternDestroy (pattern);
79
80         DCPOMATIC_ASSERT(font_name);
81
82         _available_fonts[font_file] = *font_name;
83
84         FcConfigBuildFonts(_config);
85         return *font_name;
86 }
87
88
89 optional<boost::filesystem::path>
90 FontConfig::system_font_with_name(string name)
91 {
92         optional<boost::filesystem::path> path;
93
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);
97         if (font_set) {
98                 for (int i = 0; i < font_set->nfont; ++i) {
99                         auto font = font_set->fonts[i];
100                         FcChar8* file;
101                         if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
102                                 path = boost::filesystem::path(reinterpret_cast<char*>(file));
103                                 break;
104                         }
105                 }
106                 FcFontSetDestroy(font_set);
107         }
108
109         FcObjectSetDestroy(object_set);
110         FcPatternDestroy(pattern);
111
112         return path;
113 }
114
115
116 FontConfig *
117 FontConfig::instance()
118 {
119         if (!_instance) {
120                 _instance = new FontConfig();
121         }
122
123         return _instance;
124 }
125