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