如何使用fontconfig的获取字体列表(C / C ++)? [英] How to use fontconfig to get font list (C/C++)?

查看:1787
本文介绍了如何使用fontconfig的获取字体列表(C / C ++)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到的fontconfig是在Linux中获得字体的最佳选择。不幸的是,我一直在寻找通过自己的开发者文档,我完全不知道我在做什么。这样看来没有简单的函数来获取系统的字体列表。我必须执行模式搜索,而不是...对吧?

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 I'm doing. It would appear there is no simple function to get a list of system fonts. I have to perform a pattern search instead... right?

在短,什么是得到True-Type字体(他们的家庭,脸部和目录)在FontConfig列表的最佳方式?当然,如果有什么东西比fontconfig的更好,我肯定是其他的解决方案打开。

In short, what is the best way to get a list of true-type fonts (their family, face, and directory) with fontconfig? Of course, if there's something better than fontconfig, I'm certainly open to other solutions.

推荐答案

我也有类似的问题,发现这个职位(fontconfig的文档是有点难以打通)。 MindaugasJ的反应是有用的,但要注意,额外的呼叫之类的东西 FcPatternPrint()或打印出 FcNameUnparse的结果()。此外,你需要一个 FC_FILE 参数添加到传递给 FcObjectSetBuild 参数列表。事情是这样的:

I had a similar question, and found this post (the fontconfig documentation is a little difficult to get through). MindaugasJ's response was useful, but watch out for the extra lines calling things like FcPatternPrint() or printing out the results of FcNameUnparse(). In addition, you need to add a FC_FILE argument to the list of arguments passed to FcObjectSetBuild. Something like this:

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);

我有一个稍微不同的问题解决在我需要找到字体文件传递给FreeType的的 FC_New_Face()函数给出一些字体名。此code是能够使用的fontconfig找到最好的文件相匹配的名称

I had a slightly different problem to solve in that I needed to find the font file to pass to freetype's FC_New_Face() function given some font "name". This code is able to use fontconfig to find the best file to match a 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);

这篇关于如何使用fontconfig的获取字体列表(C / C ++)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆