Skip to content

Find out if a font contains certain characters

2013-08-08

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
  FcInit();
  config = FcInitLoadConfigAndFonts();

  // load font
  pattern = FcNameParse((FcChar8*)font_name);
  FcDefaultSubstitute(pattern);
  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.

Advertisements

From → fontconfig

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: