diff options
| author | Carl Hetherington <cth@carlh.net> | 2022-06-02 12:52:59 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2022-06-07 17:01:06 +0200 |
| commit | 9a7b67aee32a40539f29bc2d7017edd4a4f65f11 (patch) | |
| tree | 1aaf01ecd62cce4d89e2fa5c8c9db1e79d5e6f53 /src/lib/font_config.cc | |
| parent | ab03306fdc02e49b0a3d5b45f103d317ff6611e5 (diff) | |
Move fontconfig-related code out to a class.
Diffstat (limited to 'src/lib/font_config.cc')
| -rw-r--r-- | src/lib/font_config.cc | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/lib/font_config.cc b/src/lib/font_config.cc new file mode 100644 index 000000000..c7361bc04 --- /dev/null +++ b/src/lib/font_config.cc @@ -0,0 +1,97 @@ +/* + Copyright (C) 2014-2021 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 "font_config.h" +#include <fontconfig/fontconfig.h> +#include <boost/optional.hpp> + + +using std::string; +using boost::optional; + + +FontConfig* FontConfig::_instance; + + +FontConfig::FontConfig() +{ + _config = FcInitLoadConfigAndFonts(); + FcConfigSetCurrent(_config); +} + + +string +FontConfig::make_font_available(boost::filesystem::path font_file) +{ + auto existing = _available_fonts.find(font_file); + if (existing != _available_fonts.end()) { + return existing->second; + } + + /* 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); + + _available_fonts[font_file] = *font_name; + + FcConfigBuildFonts(_config); + return *font_name; +} + + +FontConfig * +FontConfig::instance() +{ + if (!_instance) { + _instance = new FontConfig(); + } + + return _instance; +} + |
