1

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;
};
Marta
  • 21
  • 3

1 Answers1

1

I found the answer myself. The selector string must begin with a bl**dy ":". ":Sans:Mono:Regular" works, but "Sans:Mono:Regular" fails...

Now it's almost OK, but it includes *.pfb and some other unwanted files. Is there another selector keyword that only includes vector fonts (.otf, .ttf) in the list?

The code below works.

#include <stdio.h>
#include <stdlib.h>
#include <string.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*)":Sans:Mono:Regular");
    os = FcObjectSetBuild (FC_FILE, NULL);

    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);
        if (!strcmp(&file[strlen(file)-3], "ttf") ||
            !strcmp(&file[strlen(file)-3], "otf"))
            printf("font# %d  %s\n", i, (char*)file);
    };

    FcFontSetDestroy(fs);
    FcObjectSetDestroy(os);
    FcPatternDestroy(pat);
    FcConfigDestroy(config);
    FcFini();
    return 0;
};
Marta
  • 21
  • 3