Click here to Skip to main content
11,578,524 members (61,176 online)
Click here to Skip to main content

Interesting facts about fonts in .NET

, 8 Oct 2013 CPOL 7.1K 11
Rate this:
Please Sign up or sign in to vote.
In this post, we will talk about the wonderful Font class.

Introduction

Sometimes when I work with fonts, I get some questions which are not as obvious as I would like them to be. Let’s check some of them.

Using the Code

Q: How to identify a monospaced font?

A: It’s not such a simple question as it may seem. You can find the following advice in Google search results: describe the LOGFONT class in your project and use the ToLogFont method to convert the font to a corresponding object. After that (as legend has it), the first bit in the IfPitchAndFamily field should identify if the font is monospaced. So, this is sh*t! Nowadays the field is always equal to zero. Sometimes and somewhere this option worked, but it doesn’t work now. In real life, you have to use a not very pretty, but very efficient solution as:

// graphics — preliminary created instance of the Graphics class 
public static bool IsMonospace(Font font)
{    
    return Math.Abs(graphics.MeasureString("iii", font).Width -  
                    graphics.MeasureString("WWW", font).Width) < 1e-3; 
} 

Q: How to define the size of a string when using this font?

A: We will need Graphics which will be used to draw, and namely its MeasureString method. Pass the drawn text and used font to it and it gets the string size.

Q: What if I need to get the size of a definite part of a string?

A: You can do it with the Graphics.MeasureCharacterRanges method. But at first (and there is a good sample in MSDN), it is necessary to set target intervals of the characters with the StringFormat.SetMeasurableCharacterRanges method. This method has an amazing limitation, you can’t pass more than 32 intervals to it.

Q: This method gets too big boundaries. They include not only the characters but also some space near it. What to do?

A: Actually the regions that you get contain the symbols in a way they are listed in the initial font – together with a little space next to it. There is no neat way to get exact boundaries. You will have to
create a picture with the target characters and review it pixel-by-pixel (just don’t use Bitmap.GetPixel, it’s too time consuming, there are quicker methods) and find extreme characters of the string.

Q: I created a font by using its string name, got no exceptions. Is there such a font in the system?

A: Not always. The Font class constructor tries to get the most closely matching font (to its consideration) for this name. It’s better to check if the correct font was created:

var font = new Font(fontName, 12);  
vat isExist = font.Name == fontName; 

And it would also be good to check if the font family you use is able to support your FontStyle:

var fontFamily = new FontFamily(fontName);   
isExist &= fontFamily.IsStyleAvailable(fontStyle);

License

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

Share

About the Author

submit@perpetuumsoft.com
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questiongood tip Pin
Brian A Stephens8-Oct-13 7:25
professionalBrian A Stephens8-Oct-13 7:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 8 Oct 2013
Article Copyright 2013 by submit@perpetuumsoft.com
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid