Click here to Skip to main content
13,149,756 members (61,143 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

15.3K views
1 bookmarked
Posted 20 Jan 2012

Detecting If A Given Font Style Exists in C#

, 20 Jan 2012
Rate this:
Please Sign up or sign in to vote.
Detecting if a given font style exists in C#

In a previous article, Creating a WYSIWYG font ComboBox using C#, there is a hacky bit of code which uses a try catch block to handle processing when a given font style doesn't exist. This article describes a better way of handling this requirement without relying on the exception handler.

Originally we used the following code to determine if a font style exists (some of the additional code has been removed for clarity):

protected virtual Font GetFont(string fontFamilyName)
 {
   Font font;

   font = this.GetFont(fontFamilyName, FontStyle.Regular);
   if (font == null)
     font = this.GetFont(fontFamilyName, FontStyle.Bold);
   if (font == null)
     font = this.GetFont(fontFamilyName, FontStyle.Italic);
   if (font == null)
     font = this.GetFont(fontFamilyName, FontStyle.Bold | FontStyle.Italic);

   return font;
 }

 protected virtual Font GetFont(string fontFamilyName, FontStyle fontStyle)
 {
   Font font;

   try
   {
     font = new Font(fontFamilyName, this.PreviewFontSize, fontStyle);
   }
   catch
   {
     font = null;
   }

   return font;
 }

This code essentially "tests" each style by attempting to create a font instance of a given style. If the style doesn't exist, an exception is thrown and the code moves onto the next style.

A better way is to use the IsStyleAvailable function of the FontFamily object. You simply create an instance of this object with the name of the font you wish to query, then call the method with the style to test. Note that the constructor for FontFamily will throw an exception if the font you try to create doesn't exist.

Switching the GetFont method above to use IsStyleAvailable ends up looking like this:

protected virtual Font GetFont(string fontFamilyName)
{
  Font font;

  using (FontFamily family = new FontFamily(fontFamilyName))
  {
    if (family.IsStyleAvailable(FontStyle.Regular))
      font = this.GetFont(fontFamilyName, FontStyle.Regular);
    else if (family.IsStyleAvailable(FontStyle.Bold))
      font = this.GetFont(fontFamilyName, FontStyle.Bold);
    else if (family.IsStyleAvailable(FontStyle.Italic))
      font = this.GetFont(fontFamilyName, FontStyle.Italic);
    else if (family.IsStyleAvailable(FontStyle.Bold | FontStyle.Italic))
      font = this.GetFont(fontFamilyName, FontStyle.Bold | FontStyle.Italic);
    else
      font = null;
  }

  return font;
}

protected virtual Font GetFont(string fontFamilyName, FontStyle fontStyle)
{
  return new Font(fontFamilyName, this.PreviewFontSize, fontStyle);
}

Based on this, a simple method to check if a given font name and style exists is presented below. As the constructor for FontFamily throws an ArgumentException if the given font doesn't exist, we can trap that and return false. Any other error will be thrown, rather than being silently ignored as in our earlier solution.

public bool DoesFontExist(string fontFamilyName, FontStyle fontStyle)
{
  bool result;

  try
  {
    using (FontFamily family = new FontFamily(fontFamilyName))
      result = family.IsStyleAvailable(fontStyle);
  }
  catch (ArgumentException)
  {
    result = false;
  }

  return result;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Richard James Moss
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Franc Morales23-Dec-15 21:50
memberFranc Morales23-Dec-15 21:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.2 | Last Updated 20 Jan 2012
Article Copyright 2012 by Richard James Moss
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid