Supporters update.
[dcpomatic.git] / src / lib / font_config.cc
1 /*
2     Copyright (C) 2014-2023 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 "dcpomatic_log.h"
24 #include "font.h"
25 #include "font_config.h"
26 #include "util.h"
27 #include <dcp/filesystem.h>
28 #include <fontconfig/fontconfig.h>
29 #include <boost/filesystem.hpp>
30 #include <boost/optional.hpp>
31
32
33 using std::shared_ptr;
34 using std::string;
35 using boost::optional;
36
37
38 FontConfig* FontConfig::_instance;
39
40
41 FontConfig::FontConfig()
42 {
43         _config = FcInitLoadConfigAndFonts();
44         FcConfigSetCurrent(_config);
45 }
46
47
48 FontConfig::~FontConfig()
49 {
50         for (auto file: _temp_files) {
51                 boost::system::error_code ec;
52                 dcp::filesystem::remove(file, ec);
53         }
54 }
55
56
57 string
58 FontConfig::make_font_available(shared_ptr<dcpomatic::Font> font)
59 {
60         DCPOMATIC_ASSERT(font);
61
62         auto existing = _available_fonts.find(font->content());
63         if (existing != _available_fonts.end()) {
64                 return existing->second;
65         }
66
67         boost::filesystem::path font_file = default_font_file();
68         if (font->file()) {
69                 font_file = *font->file();
70         } else if (font->data()) {
71                 /* This font only exists in memory (so far) but FontConfig doesn't have an API to add a font
72                  * from a memory buffer (AFAICS).
73                  * https://gitlab.freedesktop.org/fontconfig/fontconfig/-/issues/12
74                  * As a workaround, write the font data to a temporary file and use that.
75                  */
76                 font_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
77                 _temp_files.push_back(font_file);
78                 font->data()->write(font_file);
79         }
80
81
82         /* Make this font available to DCP-o-matic */
83         optional<string> font_name;
84         FcConfigAppFontAddFile (_config, reinterpret_cast<FcChar8 const *>(font_file.string().c_str()));
85         auto pattern = FcPatternBuild (
86                 0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *>(0)
87                 );
88         auto object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
89         auto font_set = FcFontList (_config, pattern, object_set);
90         if (font_set) {
91                 for (int i = 0; i < font_set->nfont; ++i) {
92                         FcPattern* font = font_set->fonts[i];
93                         FcChar8* file;
94                         FcChar8* family;
95                         FcChar8* style;
96                         if (
97                                 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
98                                 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
99                                 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
100                                 ) {
101                                 font_name = reinterpret_cast<char const *> (family);
102                         }
103                 }
104
105                 FcFontSetDestroy (font_set);
106         }
107
108         FcObjectSetDestroy (object_set);
109         FcPatternDestroy (pattern);
110
111         DCPOMATIC_ASSERT(font_name);
112
113         /* We need to use the font object as the key, as we may be passed the same shared_ptr to a modified
114          * Font object in the future and in that case we need to load the new font.
115          */
116         _available_fonts[font->content()] = *font_name;
117
118         FcConfigBuildFonts(_config);
119         return *font_name;
120 }
121
122
123 optional<boost::filesystem::path>
124 FontConfig::system_font_with_name(string name)
125 {
126         optional<boost::filesystem::path> path;
127
128         LOG_GENERAL("Searching system for font %1", name);
129         auto pattern = FcNameParse(reinterpret_cast<FcChar8 const*>(name.c_str()));
130         auto object_set = FcObjectSetBuild(FC_FILE, nullptr);
131         auto font_set = FcFontList(_config, pattern, object_set);
132         if (font_set) {
133                 LOG_GENERAL("%1 candidate fonts found", font_set->nfont);
134                 for (int i = 0; i < font_set->nfont; ++i) {
135                         auto font = font_set->fonts[i];
136                         FcChar8* file;
137                         if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
138                                 path = boost::filesystem::path(reinterpret_cast<char*>(file));
139                                 LOG_GENERAL("Found %1", *path);
140                                 break;
141                         }
142                 }
143                 FcFontSetDestroy(font_set);
144         } else {
145                 LOG_GENERAL_NC("No candidate fonts found");
146         }
147
148         FcObjectSetDestroy(object_set);
149         FcPatternDestroy(pattern);
150
151         if (path) {
152                 LOG_GENERAL("Searched system for font %1, found %2", name, *path);
153         } else {
154                 LOG_GENERAL("Searched system for font %1; nothing found", name);
155         }
156
157         return path;
158 }
159
160
161 FontConfig *
162 FontConfig::instance()
163 {
164         if (!_instance) {
165                 _instance = new FontConfig();
166         }
167
168         return _instance;
169 }
170
171
172 void
173 FontConfig::drop()
174 {
175         delete _instance;
176         _instance = nullptr;
177 }
178