Fontconfig:无法找到应用程序字体

huangapple go评论59阅读模式
英文:

Fontconfig: cannot find application font

问题

我模仿了这个链接中的代码(https://stackoverflow.com/questions/10542832/how-to-use-fontconfig-to-get-font-list-c-c),只是我想要添加对本地字体的支持,所以代码被修改如下。

FcConfig* config = FcInitLoadConfigAndFonts();
FcConfigAppFontAddDir(config, (FcChar8*) "../fonts");
FcConfigSubstitute(config, pat, FcMatchPattern);
FcDefaultSubstitute(pat);

FcStrList* dirs = FcConfigGetFontDirs(config);
FcChar8* current;
while ((current = FcStrListNext(dirs)) != NULL)
    printf("%s\n", (char*) current);

FcResult res;
FcPattern* font = FcFontMatch(config, pat, &res);
if (font)
{
   FcChar8* file = NULL;
   if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch)
   {
      printf("%s\n", (char*) file);
   }
   FcPatternDestroy(font);
}

FcPatternDestroy(pat);
FcStrListDone(dirs);

然而,我发现了以下情况:

  • fonts目录中出现了一个.uuid文件,表示该目录(以及其中的字体文件)被正确找到。
  • 打印的目录不包含我的自定义目录。
  • 即使我引用了我放入自定义目录中的字体,并且在系统字体目录中不存在,系统仍然找不到它,而是匹配了一个备用字体(Noto Sans CJK)。

更新:
只能通过字体名称而不是文件名引用字体。案件已结。

英文:

I imitated the code in this link, except that I wanted to add support for local fonts, so the code was adapted like this.

FcConfig* config = FcInitLoadConfigAndFonts();
FcConfigAppFontAddDir(config, (FcChar8*) "../fonts");
FcConfigSubstitute(config, pat, FcMatchPattern);
FcDefaultSubstitute(pat);

FcStrList* dirs = FcConfigGetFontDirs(config);
FcChar8* current;
while ((current = FcStrListNext(dirs)) != NULL)
    printf("%s\n", (char*) current);

FcResult res;
FcPattern* font = FcFontMatch(config, pat, &res);
if (font)
{
   FcChar8* file = NULL;
   if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch)
   {
      printf("%s\n", (char*) file);
   }
   FcPatternDestroy(font);
}

FcPatternDestroy(pat);
FcStrListDone(dirs);

However, I found the following situation:

  • A .uuid file appears in fonts directory, meaning that the directory (along with font files in it) is correctly found.
  • The printed directories does not contain my custom directory.
  • Even when I am referencing a font that I put into the custom directory and does not exist in system font directories, the system cannot find it, and instead a fallback font (Noto Sans CJK) is matched instead.

Update:
The fonts can only be referenced through font name as opposed to file name. Case closed.

答案1

得分: 2

更新: 字体只能通过字体名称引用,而不是文件名称。案件已结案。

英文:

Update: The fonts can only be referenced through font name as opposed to file name. Case closed.

huangapple
  • 本文由 发表于 2023年7月10日 12:11:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定