How to use fontconfig to get font list (C/C++)?

后端 未结 2 403
执念已碎
执念已碎 2021-02-06 03:40

I hear that fontconfig is the best option for getting fonts in linux. Unfortunately, I\'ve been looking through their developer documentation and I have absolutely no clue what

2条回答
  •  萌比男神i
    2021-02-06 04:15

    This is not exactly what you are asking for, but it will give you the list of fonts available.

    #include 
    
    FcPattern *pat;
    FcFontSet *fs;
    FcObjectSet *os;
    FcChar8 *s, *file;
    FcConfig *config;
    FcBool result;
    int i;
    
    result = FcInit();
    config = FcConfigGetCurrent();
    FcConfigSetRescanInterval(config, 0);
    
    // show the fonts (debugging)
    pat = FcPatternCreate();
    os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, (char *) 0);
    fs = FcFontList(config, pat, os);
    printf("Total fonts: %d", fs->nfont);
    for (i=0; fs && i < fs->nfont; i++) {
        FcPattern *font = fs->fonts[i];//FcFontSetFont(fs, i);
        FcPatternPrint(font);
        s = FcNameUnparse(font);
        if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
            printf("Filename: %s", file);
        }
        printf("Font: %s", s);
        free(s);
    }
    if (fs) FcFontSetDestroy(fs);
    

提交回复
热议问题