4

I'm trying to make my C++ program detect installed fonts on my Win32 machine. I tried fontconfig by taking the library from the GTK+ bundle.

I use the following test code:

#include<fontconfig.h>

FcBool success = FcInit ();
if ( !success ) {
    return false;
}

FcConfig *config = FcInitLoadConfigAndFonts ();
if(!config) {
    return false;
}

FcChar8 *s, *file;

FcPattern *p = FcPatternCreate();
FcObjectSet *os = FcObjectSetBuild (FC_FAMILY,NULL);
FcFontSet *fs = FcFontList(config, p, os);

LOG("Total fonts: %d\n", fs->nfont);
for (int i=0; fs && i < fs->nfont; i++) {
    FcPattern *font = fs->fonts[i];

    s = FcNameUnparse(font);
    LOG("Font: %s\n", s);
    free(s);

    if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
        LOG("Filename: %s\n", file);
    }
}

// destroy objects here ...

Unfortunately this test application only prints:

Total fonts: 0

I know there are fonts installed on my machine and I know that Gimp2.0 detects them, so there must be something wrong with my test code. Does anyone have any idea?

Other than linking the fontconfig-1.dll, I did nothing else. I haven't created any config files or anything, because I couldn't read anywhere about having to do that.

I am open to your suggestions, thanks!

cbaakman
  • 61
  • 3

1 Answers1

1

Instead of:

FcConfig *config = FcInitLoadConfigAndFonts ();

Try:

FcConfig *config = FcConfigCreate();
FcConfigAppFontAddDir(config, (const FcChar8 *)"C:\\Windows\\Fonts");

(this is the lazy version, you should adapt it to go off GetWindowsDirectory...)

Chris Burt-Brown
  • 2,717
  • 1
  • 16
  • 16