Codechange: Use AutoRelease to simplify cleanup after FontConfig calls. (#14354)

This commit is contained in:
Peter Nelson 2025-06-11 21:24:31 +01:00 committed by GitHub
parent 9c16603da6
commit b2d69ae606
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,7 @@
#include "../../stdafx.h" #include "../../stdafx.h"
#include "../../misc/autorelease.hpp"
#include "../../debug.h" #include "../../debug.h"
#include "../../fontdetection.h" #include "../../fontdetection.h"
#include "../../string_func.h" #include "../../string_func.h"
@ -69,35 +70,37 @@ FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face)
return err; return err;
} }
auto fc_instance = FcConfigReference(nullptr); auto fc_instance = AutoRelease<FcConfig, FcConfigDestroy>(FcConfigReference(nullptr));
assert(fc_instance != nullptr); assert(fc_instance != nullptr);
/* Split & strip the font's style */ /* Split & strip the font's style */
auto [font_family, font_style] = SplitFontFamilyAndStyle(font_name); auto [font_family, font_style] = SplitFontFamilyAndStyle(font_name);
/* Resolve the name and populate the information structure */ /* Resolve the name and populate the information structure */
FcPattern *pat = FcPatternCreate(); auto pat = AutoRelease<FcPattern, FcPatternDestroy>(FcPatternCreate());
if (!font_family.empty()) FcPatternAddString(pat, FC_FAMILY, ToFcString(font_family)); if (!font_family.empty()) FcPatternAddString(pat.get(), FC_FAMILY, ToFcString(font_family));
if (!font_style.empty()) FcPatternAddString(pat, FC_STYLE, ToFcString(font_style)); if (!font_style.empty()) FcPatternAddString(pat.get(), FC_STYLE, ToFcString(font_style));
FcConfigSubstitute(nullptr, pat, FcMatchPattern); FcConfigSubstitute(nullptr, pat.get(), FcMatchPattern);
FcDefaultSubstitute(pat); FcDefaultSubstitute(pat.get());
FcFontSet *fs = FcFontSetCreate(); auto fs = AutoRelease<FcFontSet, FcFontSetDestroy>(FcFontSetCreate());
FcResult result; FcResult result;
FcPattern *match = FcFontMatch(nullptr, pat, &result); FcPattern *match = FcFontMatch(nullptr, pat.get(), &result);
if (fs != nullptr && match != nullptr) { if (fs == nullptr || match == nullptr) return err;
FcFontSetAdd(fs.get(), match);
for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
FcChar8 *family; FcChar8 *family;
FcChar8 *style; FcChar8 *style;
FcChar8 *file; FcChar8 *file;
int32_t index; int32_t index;
FcFontSetAdd(fs, match);
for (int i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
/* Try the new filename */ /* Try the new filename */
if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch && if (FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch) continue;
FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch && if (FcPatternGetString(font, FC_FAMILY, 0, &family) != FcResultMatch) continue;
FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch && if (FcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch) continue;
FcPatternGetInteger(fs->fonts[i], FC_INDEX, 0, &index) == FcResultMatch) { if (FcPatternGetInteger(font, FC_INDEX, 0, &index) != FcResultMatch) continue;
/* The correct style? */ /* The correct style? */
if (!font_style.empty() && !StrEqualsIgnoreCase(font_style, FromFcString(style))) continue; if (!font_style.empty() && !StrEqualsIgnoreCase(font_style, FromFcString(style))) continue;
@ -107,14 +110,9 @@ FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face)
* same as the supplied name */ * same as the supplied name */
if (StrEqualsIgnoreCase(font_family, FromFcString(family))) { if (StrEqualsIgnoreCase(font_family, FromFcString(family))) {
err = FT_New_Face(_ft_library, FromFcString(file), index, face); err = FT_New_Face(_ft_library, FromFcString(file), index, face);
if (err == FT_Err_Ok) return err;
} }
} }
}
}
FcPatternDestroy(pat);
FcFontSetDestroy(fs);
FcConfigDestroy(fc_instance);
return err; return err;
} }
@ -125,7 +123,7 @@ bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_is
if (!FcInit()) return ret; if (!FcInit()) return ret;
auto fc_instance = FcConfigReference(nullptr); auto fc_instance = AutoRelease<FcConfig, FcConfigDestroy>(FcConfigReference(nullptr));
assert(fc_instance != nullptr); assert(fc_instance != nullptr);
/* Fontconfig doesn't handle full language isocodes, only the part /* Fontconfig doesn't handle full language isocodes, only the part
@ -134,29 +132,22 @@ bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_is
std::string lang = fmt::format(":lang={}", language_isocode.substr(0, language_isocode.find('_'))); std::string lang = fmt::format(":lang={}", language_isocode.substr(0, language_isocode.find('_')));
/* First create a pattern to match the wanted language. */ /* First create a pattern to match the wanted language. */
FcPattern *pat = FcNameParse(ToFcString(lang)); auto pat = AutoRelease<FcPattern, FcPatternDestroy>(FcNameParse(ToFcString(lang)));
/* We only want to know these attributes. */ /* We only want to know these attributes. */
FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_INDEX, FC_SPACING, FC_SLANT, FC_WEIGHT, nullptr); auto os = AutoRelease<FcObjectSet, FcObjectSetDestroy>(FcObjectSetBuild(FC_FILE, FC_INDEX, FC_SPACING, FC_SLANT, FC_WEIGHT, nullptr));
/* Get the list of filenames matching the wanted language. */ /* Get the list of filenames matching the wanted language. */
FcFontSet *fs = FcFontList(nullptr, pat, os); auto fs = AutoRelease<FcFontSet, FcFontSetDestroy>(FcFontList(nullptr, pat.get(), os.get()));
/* We don't need these anymore. */ if (fs == nullptr) return ret;
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
if (fs != nullptr) {
int best_weight = -1; int best_weight = -1;
const char *best_font = nullptr; const char *best_font = nullptr;
int best_index = 0; int best_index = 0;
for (int i = 0; i < fs->nfont; i++) { for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
FcPattern *font = fs->fonts[i];
FcChar8 *file = nullptr; FcChar8 *file = nullptr;
FcResult res = FcPatternGetString(font, FC_FILE, 0, &file); FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
if (res != FcResultMatch || file == nullptr) { if (res != FcResultMatch || file == nullptr) continue;
continue;
}
/* Get a font with the right spacing .*/ /* Get a font with the right spacing .*/
int value = 0; int value = 0;
@ -188,16 +179,9 @@ bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_is
} }
} }
if (best_font != nullptr) { if (best_font == nullptr) return false;
ret = true;
callback->SetFontNames(settings, best_font, &best_index); callback->SetFontNames(settings, best_font, &best_index);
InitFontCache(callback->Monospace()); InitFontCache(callback->Monospace());
} return true;
/* Clean up the list of filenames. */
FcFontSetDestroy(fs);
}
FcConfigDestroy(fc_instance);
return ret;
} }