summaryrefslogtreecommitdiff
path: root/src/lib/font_config.cc
blob: 9d505e51ada3f0691db82b32a94532ecaef19e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
    Copyright (C) 2014-2023 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "dcpomatic_assert.h"
#include "dcpomatic_log.h"
#include "font.h"
#include "font_config.h"
#include "util.h"
#include <dcp/filesystem.h>
#include <fontconfig/fontconfig.h>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>


using std::shared_ptr;
using std::string;
using boost::optional;


FontConfig* FontConfig::_instance;


FontConfig::FontConfig()
{
	_config = FcInitLoadConfigAndFonts();
	FcConfigSetCurrent(_config);
}


FontConfig::~FontConfig()
{
	for (auto file: _temp_files) {
		boost::system::error_code ec;
		dcp::filesystem::remove(file, ec);
	}
}


string
FontConfig::make_font_available(shared_ptr<dcpomatic::Font> font)
{
	DCPOMATIC_ASSERT(font);

	auto existing = _available_fonts.find(font->content());
	if (existing != _available_fonts.end()) {
		return existing->second;
	}

	boost::filesystem::path font_file = default_font_file();
	if (font->file()) {
		font_file = *font->file();
	} else if (font->data()) {
		/* This font only exists in memory (so far) but FontConfig doesn't have an API to add a font
		 * from a memory buffer (AFAICS).
		 * https://gitlab.freedesktop.org/fontconfig/fontconfig/-/issues/12
		 * As a workaround, write the font data to a temporary file and use that.
		 */
		font_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
		_temp_files.push_back(font_file);
		font->data()->write(font_file);
	}


	/* Make this font available to DCP-o-matic */
	optional<string> font_name;
	FcConfigAppFontAddFile (_config, reinterpret_cast<FcChar8 const *>(font_file.string().c_str()));
	auto pattern = FcPatternBuild (
		0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *>(0)
		);
	auto object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
	auto font_set = FcFontList (_config, pattern, object_set);
	if (font_set) {
		for (int i = 0; i < font_set->nfont; ++i) {
			FcPattern* font = font_set->fonts[i];
			FcChar8* file;
			FcChar8* family;
			FcChar8* style;
			if (
				FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
				FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
				FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
				) {
				font_name = reinterpret_cast<char const *> (family);
			}
		}

		FcFontSetDestroy (font_set);
	}

	FcObjectSetDestroy (object_set);
	FcPatternDestroy (pattern);

	DCPOMATIC_ASSERT(font_name);

	/* We need to use the font object as the key, as we may be passed the same shared_ptr to a modified
	 * Font object in the future and in that case we need to load the new font.
	 */
	_available_fonts[font->content()] = *font_name;

	FcConfigBuildFonts(_config);
	return *font_name;
}


optional<boost::filesystem::path>
FontConfig::system_font_with_name(string name)
{
	optional<boost::filesystem::path> path;

	LOG_GENERAL("Searching system for font {}", name);
	auto pattern = FcNameParse(reinterpret_cast<FcChar8 const*>(name.c_str()));
	auto object_set = FcObjectSetBuild(FC_FILE, nullptr);
	auto font_set = FcFontList(_config, pattern, object_set);
	if (font_set) {
		LOG_GENERAL("{} candidate fonts found", font_set->nfont);
		for (int i = 0; i < font_set->nfont; ++i) {
			auto font = font_set->fonts[i];
			FcChar8* file;
			if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
				path = boost::filesystem::path(reinterpret_cast<char*>(file));
				LOG_GENERAL("Found {}", path->string());
				break;
			}
		}
		FcFontSetDestroy(font_set);
	} else {
		LOG_GENERAL("No candidate fonts found");
	}

	FcObjectSetDestroy(object_set);
	FcPatternDestroy(pattern);

	if (path) {
		LOG_GENERAL("Searched system for font {}, found {}", name, path->string());
	} else {
		LOG_GENERAL("Searched system for font {}; nothing found", name);
	}

	return path;
}


FontConfig *
FontConfig::instance()
{
	if (!_instance) {
		_instance = new FontConfig();
	}

	return _instance;
}


void
FontConfig::drop()
{
	delete _instance;
	_instance = nullptr;
}