Compare commits

..

1 Commits

Author SHA1 Message Date
Daijiro Fukuda
36eb860859
Merge 4a2883b4e2 into b35641f4a3 2024-04-16 04:46:54 +08:00
2 changed files with 15 additions and 72 deletions

View File

@ -50,7 +50,11 @@ if (RT_LIBRARY)
endif()
if (GLFW_BUILD_X11 OR GLFW_BUILD_WAYLAND)
target_compile_definitions(input_text PRIVATE FONTCONFIG_ENABLED)
find_package(Fontconfig)
if (FONTCONFIG_FOUND)
target_compile_definitions(input_text PRIVATE FONTCONFIG_ENABLED)
target_link_libraries(input_text fontconfig)
endif()
endif()
set(GUI_ONLY_BINARIES empty gamma icon input_text inputlag joysticks tearing

View File

@ -85,6 +85,10 @@
#include "getopt.h"
#if defined(FONTCONFIG_ENABLED)
#include <fontconfig/fontconfig.h>
#endif
#define MAX_BUFFER_LEN 1024
// https://github.com/Immediate-Mode-UI/Nuklear/wiki/Complete-font-guide
@ -227,89 +231,24 @@ static int load_custom_font()
#endif
#if defined(FONTCONFIG_ENABLED)
#include <dlfcn.h>
typedef struct _FcConfig FcConfig;
typedef struct _FcFontSet
{
int nfont;
int sfont;
void **fonts;
} FcFontSet;
typedef struct _FcValue
{
int type;
union
{
const unsigned char* s;
double d;
} u;
} FcValue;
struct FcLib
{
void* handle;
void* (*InitLoadConfigAndFonts)(void);
void* (*ConfigGetFonts)(void*, int);
void (*ConfigDestroy)(void*);
int (*PatternGet)(const void*, const char*, int, void*);
} fcLib;
static int init_fontconfig(void)
{
if (fcLib.handle)
return GLFW_TRUE;
fcLib.handle = dlopen("libfontconfig.so.1", RTLD_LAZY | RTLD_LOCAL);
if (!fcLib.handle)
return GLFW_FALSE;
#define GET_FC_SYMBOL(name) \
fcLib.name = dlsym(fcLib.handle, "Fc"#name); \
if (!fcLib.name) \
{ \
dlclose(fcLib.handle); \
fcLib.handle = NULL; \
return GLFW_FALSE; \
}
GET_FC_SYMBOL(InitLoadConfigAndFonts);
GET_FC_SYMBOL(ConfigGetFonts);
GET_FC_SYMBOL(ConfigDestroy);
GET_FC_SYMBOL(PatternGet);
#undef GET_FC_SYMBOL
return GLFW_TRUE;
}
static void load_font_list_by_fontconfig()
{
FcConfig* config;
FcFontSet* fontset;
if (!init_fontconfig())
return;
config = fcLib.InitLoadConfigAndFonts();
fontset = fcLib.ConfigGetFonts(config, 0 /* FcSetSystem */);
FcConfig* config = FcInitLoadConfigAndFonts();
FcFontSet* fontset = FcConfigGetFonts(config, FcSetSystem);
if (!fontset)
{
printf("load_font_list_by_fontconfig failed.\n");
fcLib.ConfigDestroy(config);
FcConfigDestroy(config);
return;
}
for (int i = 0; i < fontset->nfont; i++)
{
FcValue fvalue, dvalue;
if (fcLib.PatternGet(fontset->fonts[i], "family", 0, &fvalue) == 0)
if (FcResultMatch == FcPatternGet(fontset->fonts[i], FC_FAMILY, 0, &fvalue))
{
if (fcLib.PatternGet(fontset->fonts[i], "file", 0, &dvalue) == 0)
if (FcResultMatch == FcPatternGet(fontset->fonts[i], FC_FILE, 0, &dvalue))
{
const char* familyName = (const char*) fvalue.u.s;
const char* filePath = (const char*) dvalue.u.s;
@ -349,7 +288,7 @@ static void load_font_list_by_fontconfig()
}
}
fcLib.ConfigDestroy(config);
FcConfigDestroy(config);
}
#endif