c++ 您所在的位置:网站首页 如何查找字体名字 c++

c++

2024-07-10 06:56| 来源: 网络整理| 查看: 265

我听说 fontconfig 是在 linux 中获取字体的最佳选择。不幸的是,我一直在查看他们的开发人员文档,但我完全不知道自己在做什么。看起来没有简单的函数来获取系统字体列表。我必须改为执行模式搜索...对吧?

简而言之,使用 fontconfig 获取 True-Type 字体列表(它们的系列、外观和目录)的最佳方法是什么?当然,如果有比 fontconfig 更好的东西,我当然愿意接受其他解决方案。

最佳答案

我有一个类似的问题,并找到了这篇文章(fontconfig 文档有点难以理解)。 MindaugasJ 的响应很有用,但要注意调用诸如 FcPatternPrint() 或打印出 FcNameUnparse() 结果的额外行。此外,您需要将一个FC_FILE 参数添加到传递给FcObjectSetBuild 的参数列表中。像这样:

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() 函数的字体文件,给定一些字体“名称”。此代码能够使用 fontconfig 找到与名称匹配的最佳文件:

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 FcResult res; FcPattern* font = FcFontMatch(config, pat, &res); 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);

关于c++ - 如何使用 fontconfig 获取字体列表(C/C++)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10542832/



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有