mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-06-18 19:19:29 +01:00
Codechange: Use AutoRelease to simplify cleanup after FontConfig calls. (#14354)
This commit is contained in:
parent
9c16603da6
commit
b2d69ae606
@ -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,53 +70,50 @@ 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(font, FC_FILE, 0, &file) != FcResultMatch) continue;
|
||||||
if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
|
if (FcPatternGetString(font, FC_FAMILY, 0, &family) != FcResultMatch) continue;
|
||||||
FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
|
if (FcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch) continue;
|
||||||
FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch &&
|
if (FcPatternGetInteger(font, FC_INDEX, 0, &index) != FcResultMatch) continue;
|
||||||
FcPatternGetInteger(fs->fonts[i], FC_INDEX, 0, &index) == FcResultMatch) {
|
|
||||||
|
|
||||||
/* 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;
|
||||||
|
|
||||||
/* Font config takes the best shot, which, if the family name is spelled
|
/* Font config takes the best shot, which, if the family name is spelled
|
||||||
* wrongly a 'random' font, so check whether the family name is the
|
* wrongly a 'random' font, so check whether the family name is the
|
||||||
* 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,70 +132,56 @@ 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;
|
||||||
|
FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
|
||||||
|
if (res != FcResultMatch || file == nullptr) continue;
|
||||||
|
|
||||||
FcChar8 *file = nullptr;
|
/* Get a font with the right spacing .*/
|
||||||
FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
|
int value = 0;
|
||||||
if (res != FcResultMatch || file == nullptr) {
|
FcPatternGetInteger(font, FC_SPACING, 0, &value);
|
||||||
continue;
|
if (callback->Monospace() != (value == FC_MONO) && value != FC_DUAL) continue;
|
||||||
}
|
|
||||||
|
|
||||||
/* Get a font with the right spacing .*/
|
/* Do not use those that explicitly say they're slanted. */
|
||||||
int value = 0;
|
FcPatternGetInteger(font, FC_SLANT, 0, &value);
|
||||||
FcPatternGetInteger(font, FC_SPACING, 0, &value);
|
if (value != 0) continue;
|
||||||
if (callback->Monospace() != (value == FC_MONO) && value != FC_DUAL) continue;
|
|
||||||
|
|
||||||
/* Do not use those that explicitly say they're slanted. */
|
/* We want the fatter font as they look better at small sizes. */
|
||||||
FcPatternGetInteger(font, FC_SLANT, 0, &value);
|
FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
|
||||||
if (value != 0) continue;
|
if (value <= best_weight) continue;
|
||||||
|
|
||||||
/* We want the fatter font as they look better at small sizes. */
|
/* Possible match based on attributes, get index. */
|
||||||
FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
|
int32_t index;
|
||||||
if (value <= best_weight) continue;
|
res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
|
||||||
|
if (res != FcResultMatch) continue;
|
||||||
|
|
||||||
/* Possible match based on attributes, get index. */
|
callback->SetFontNames(settings, FromFcString(file), &index);
|
||||||
int32_t index;
|
|
||||||
res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
|
|
||||||
if (res != FcResultMatch) continue;
|
|
||||||
|
|
||||||
callback->SetFontNames(settings, FromFcString(file), &index);
|
bool missing = callback->FindMissingGlyphs();
|
||||||
|
Debug(fontcache, 1, "Font \"{}\" misses{} glyphs", FromFcString(file), missing ? "" : " no");
|
||||||
|
|
||||||
bool missing = callback->FindMissingGlyphs();
|
if (!missing) {
|
||||||
Debug(fontcache, 1, "Font \"{}\" misses{} glyphs", FromFcString(file), missing ? "" : " no");
|
best_weight = value;
|
||||||
|
best_font = FromFcString(file);
|
||||||
if (!missing) {
|
best_index = index;
|
||||||
best_weight = value;
|
|
||||||
best_font = FromFcString(file);
|
|
||||||
best_index = index;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (best_font != nullptr) {
|
|
||||||
ret = true;
|
|
||||||
callback->SetFontNames(settings, best_font, &best_index);
|
|
||||||
InitFontCache(callback->Monospace());
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Clean up the list of filenames. */
|
|
||||||
FcFontSetDestroy(fs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FcConfigDestroy(fc_instance);
|
if (best_font == nullptr) return false;
|
||||||
return ret;
|
|
||||||
|
callback->SetFontNames(settings, best_font, &best_index);
|
||||||
|
InitFontCache(callback->Monospace());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user