fontconfig

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
  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
Standard
fontconfig

Find out fallback font used by fontconfig for a certain character

If you want to know which font is being used for a certain character, try this:

FC_DEBUG=4 pango-view -q -t '{character}' 2>&1 | \
    grep -o 'family: "[^"]\+' | cut -c 10- | tail -n 1

FC_DEBUG=4 will cause fontconfig to print its attempts to match a font that contains that character. The last font listed is the one that matched. Here are some examples:

$ FC_DEBUG=4 pango-view -q -t '' 2>&1 | \
    grep -o 'family: "[^"]\+' | cut -c 10- | tail -n 1
FreeMono

$ FC_DEBUG=4 pango-view -q -t 'ളം' 2>&1 | \
    grep -o 'family: "[^"]\+' | cut -c 10- | tail -n 1
Lohit Malayalam

$ FC_DEBUG=4 pango-view -q -t 'दी' 2>&1 | \
    grep -o 'family: "[^"]\+' | cut -c 10- | tail -n 1
Lohit Hindi

You might want to take a look at pango-view’s window to make sure the font used is actually the one you were trying to figure out. To do that, drop the -q option and simply press q to quit once you’re done. You might have to tell pango-view to use sans, serif or mono fonts with the --font option. For example, in my system, I get:

$ FC_DEBUG=4 pango-view --font='sans' -t '' 2>&1 | \
    grep -o 'family: "[^"]\+' | cut -c 10- | tail -n 1
AR PL New Kai

$ FC_DEBUG=4 pango-view --font='serif' -t '' 2>&1 | \
    grep -o 'family: "[^"]\+' | cut -c 10- | tail -n 1
AR PL New Sung

$ FC_DEBUG=4 pango-view --font='mono' -t '' 2>&1 | \
    grep -o 'family: "[^"]\+' | cut -c 10- | tail -n 1
AR PL New Sung Mono

 

Standard