I had a similar question and I found this post (fontconfig documentation is a bit complicated to go through). MindaugasJ's answer was useful, but make sure that extra lines call things like FcPatternPrint() or print the results of FcNameUnparse() . In addition, you need to add the FC_FILE argument to the list of arguments passed to FcObjectSetBuild . Something like that:
FcConfig* config = FcInitLoadConfigAndFonts(); FcPattern* pat = FcPatternCreate(); FcObjectSet* os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, (char *) 0); FcFontSet* fs = FcFontList(config, pat, os); printf("Total matching fonts: %d\n", fs->nfont); for (int i=0; fs && i < fs->nfont; ++i) { FcPattern* font = fs->fonts[i]; FcChar8 *file, *style, *family; if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch && FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch && FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch) { printf("Filename: %s (family %s, style %s)\n", file, family, style); } } if (fs) FcFontSetDestroy(fs);
I had a slightly different problem to solve that I needed to find a font file to go to the freetype FC_New_Face() function, given some font "name". This code is able to use fontconfig to find the best file according to the name:
FcConfig* config = FcInitLoadConfigAndFonts(); // configure the search pattern, // assume "name" is a std::string with the desired font name in it FcPattern* pat = FcNameParse((const FcChar8*)(name.c_str())); FcConfigSubstitute(config, pat, FcMatchPattern); FcDefaultSubstitute(pat); // find the font FcPattern* font = FcFontMatch(config, pat, NULL); if (font) { FcChar8* file = NULL; if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) { // save the file to another std::string fontFile = (char*)file; } FcPatternDestroy(font); } FcPatternDestroy(pat);
Scott minster
source share