Why aren't my fonts getting registered?

Category: 
Tags: fontsFontFactoryregister fontsfont directoryiText 5

I have a program using iTextSharp that includes the code

FontFactory.RegisterDirectories();
foreach (string fontname in FontFactory.RegisteredFonts) {
   Log.Info("**** Found registered font: " + fontname);
}
When I run it (using Mono on a CentOS box), the log shows only the core PostScript fonts:

  • zapfdingbats
  • times-roman
  • times-italic
  • helvetica-boldoblique
  • courier-boldoblique
  • helvetica-bold
  • helvetica
  • courier-oblique
  • helvetica-oblique
  • courier-bold
  • times-bolditalic
  • courier
  • times-bold
  • symbol

But I have 156 TTF files under my /usr/share/fonts directory tree (which is one of the directories mentioned in the code for the RegisterDirectories function). Why aren't these being registered?

Posted on StackOverflow on Nov 29, 2013 by dan04

There are subtle differences between iText and iTextSharp.

In iText, registerDirectories() looks like this:

public int registerDirectories() {
    int count = 0;
    String windir = System.getenv("windir");
    String fileseparator = System.getProperty("file.separator");
    if (windir != null && fileseparator != null) {
        count += registerDirectory(windir + fileseparator + "fonts");
    }
    count += registerDirectory("/usr/share/X11/fonts", true);
    count += registerDirectory("/usr/X/lib/X11/fonts", true);
    count += registerDirectory("/usr/openwin/lib/X11/fonts", true);
    count += registerDirectory("/usr/share/fonts", true);
    count += registerDirectory("/usr/X11R6/lib/X11/fonts", true);
    count += registerDirectory("/Library/Fonts");
    count += registerDirectory("/System/Library/Fonts");
    return count;
}

In iTextSharp however, the method looks like this:

public virtual int RegisterDirectories() {
    string dir = Path.Combine(
        Path.GetDirectoryName(Environment.GetFolderPath(
            Environment.SpecialFolder.System)), "Fonts");
    return RegisterDirectory(dir);
}

Java is platform independent, so we have to look for the 'usual suspects'. C# is Windows specific, so we can depend on the environment to tell us where to find fonts. Your question tells us that Mono doesn't support this, so you'll have to use FontFactory.RegisterDirectory("/usr/share/fonts");