(FontRenderer/FreeType) Use fontconfig to select fonts if available

This commit is contained in:
liushuyu 2021-01-30 18:49:47 -07:00
parent 5a028d79f5
commit 8132303d4f
No known key found for this signature in database
GPG Key ID: 23D1CE4534419437
3 changed files with 50 additions and 1 deletions

View File

@ -1079,6 +1079,10 @@ ifeq ($(HAVE_FREETYPE), 1)
OBJ += gfx/drivers_font_renderer/freetype.o
LIBS += $(FREETYPE_LIBS)
DEF_FLAGS += $(FREETYPE_CFLAGS)
ifeq ($(HAVE_FREETYPE), 1)
LIBS += $(FONTCONFIG_LIBS)
DEF_FLAGS += $(FONTCONFIG_CFLAGS)
endif
endif
ifeq ($(HAVE_THREADS), 1)

View File

@ -27,6 +27,11 @@
#include <retro_miscellaneous.h>
#include <string/stdstring.h>
#ifdef HAVE_FONTCONFIG
#include <fontconfig/fontconfig.h>
#include "../../msg_hash.h"
#endif
#ifdef WIIU
#include <wiiu/os.h>
#endif
@ -240,6 +245,43 @@ static void *font_renderer_ft_init(const char *font_path, float font_size)
err = FT_New_Memory_Face(handle->lib, font_data, font_size, 0, &handle->face);
}
else
#elif HAVE_FONTCONFIG
// if fallback font is requested, instead of loading it, we find the full font in the system
if (!*font_path || strstr(font_path, "fallback"))
{
FcConfig* config = FcInitLoadConfigAndFonts();
FcResult result = FcResultNoMatch;
FcChar8* font_path = NULL;
int face_index = 0;
// select Sans fonts
FcPattern* pattern = FcNameParse((const FcChar8*)"Sans");
// since fontconfig uses LL-TT style, we need to normalize locale names
FcChar8* locale = FcLangNormalize((const FcChar8*)get_user_language_iso639_1(false));
// box the locale data in a FcValue container
FcValue locale_boxed = {.type = FcTypeString, {locale}};
// configure fontconfig substitute policies, this will increase the search scope
FcConfigSubstitute(config, pattern, FcMatchPattern);
// pull in system-wide defaults, so the font selection respects system (or user) configurations
FcDefaultSubstitute(pattern);
// override locale settins, since we are not using the system locale
FcPatternAdd(pattern, FC_LANG, locale_boxed, false);
// let's find the best matching font given our search criteria
FcPattern* found = FcFontMatch(config, pattern, &result);
// uh-oh, for some reason, we can't find any font
if (result != FcResultMatch)
goto error;
if (FcPatternGetString(found, FC_FILE, 0, &font_path) != FcResultMatch)
goto error;
if (FcPatternGetInteger(found, FC_INDEX, 0, &face_index) != FcResultMatch)
goto error;
// initialize font renderer
err = FT_New_Face(handle->lib, (const char*)font_path, face_index, &handle->face);
// free up fontconfig internal structures
FcPatternDestroy(pattern);
FcPatternDestroy(found);
FcStrFree(locale);
FcConfigDestroy(config);
} else
#endif
{
if (!path_is_valid(font_path))
@ -304,7 +346,9 @@ static const char *font_paths[] = {
/* Highly OS/platform dependent. */
static const char *font_renderer_ft_get_default_font(void)
{
#ifdef WIIU
// Since fontconfig will return parameters more than a simple path
// we will process these in the init function
#if defined(WIIU) || defined(HAVE_FONTCONFIG)
return "";
#else
size_t i;

View File

@ -498,6 +498,7 @@ check_pkgconf DBUS dbus-1
check_val '' UDEV "-ludev" '' libudev '' '' false
check_val '' V4L2 -lv4l2 '' libv4l2 '' '' false
check_val '' FREETYPE -lfreetype freetype2 freetype2 '' '' false
check_val '' FONTCONFIG -lfontconfig fontconfig fontconfig '' '' false
check_val '' X11 -lX11 '' x11 '' '' false
if [ "$HAVE_X11" != 'no' ]; then