I want to extract a list of fonts by fontconfig matching a font specication.
I have experimented with the test code below. It can pick a single "best match", but when I instead try to list the alternatives it comes up blank. If I supply a blank pattern instead of a specification it throws up every font on the computer, so the listing code seems to work.
#include <stdio.h>
#include <stdlib.h>
#include <fontconfig/fontconfig.h>
int main(){
FcConfig* config=NULL;
FcPattern* pat=NULL;
FcPattern* font=NULL;
FcObjectSet* os=NULL;
FcFontSet* fs=NULL;
FcChar8* file=NULL;
char* fontFile=NULL;
FcResult result;
int i;
FcInit();
config = FcInitLoadConfigAndFonts();
pat = FcNameParse((const FcChar8*)"Mono");
//pat=FcPatternCreate();
FcConfigSubstitute(config, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
//os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, (char *) 0);
os = FcObjectSetBuild (FC_FAMILY, (char *) 0);
fs = FcFontList(config, pat, os);
printf("nfonts %d\n", fs->nfont);
for (i=0; i<fs->nfont; i++){
font=fs->fonts[i];
FcPatternGetString(font, FC_FILE, 0, &file);
printf("font# %d %s\n", i, (char*)file);
};
// font = FcFontMatch(config, pat, &result);
//
// if (font){
// file = NULL;
//
// if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch){
// fontFile = (char*)file;
// printf("%s\n",fontFile);
// };
//
};
FcFontSetDestroy(fs);
FcObjectSetDestroy(os);
//FcPatternDestroy(font);
FcPatternDestroy(pat);
FcConfigDestroy(config);
FcFini();
return 0;
};