Find out if a font contains certain characters

In my previous post I presented a way to find out which font is being used to render a certain character, but now I had an almost opposite problem, figure out which characters are not being rendered by a certain font. This can be done with fontconfig by pulling the font’s character set with FcPatternGetCharSet() and then testing the character with FcCharSetHasChar(). Here’s a quick summary:

#include <stdio.h>
#include <fontconfig/fontconfig.h>

int main() {
  char font_name[] = "DejaVu Sans";
  char ch = 'a';
  FcConfig *config;
  FcPattern *pattern;
  FcCharSet *charset;
  FcResult result;

  // initialize fontconfig
  config = FcInitLoadConfigAndFonts();

  // load font
  pattern = FcNameParse((FcChar8*)font_name);
  FcConfigSubstitute(config, pattern, FcMatchFont);
  pattern = FcFontMatch(config, pattern, &result);

  // pull charset
  FcPatternGetCharSet(pattern, "charset", 0, &charset);

  // look for char
  if (FcCharSetHasChar(charset, (FcChar32)ch) == FcTrue) {
    printf("font contains char\n");
  } else {
    printf("font does not contain char\n");
  return 0;

For a full program to check any font for any characters, see here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s