Interesting Facts About Fonts in .NET
Today we will talk about a wonderful Font class. Sometimes when I work with the fonts I get some questions which are not that obvious as I would like them to be.
Florida (I-Newswire) August 13, 2013 - Let's check some of them.
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 the Google search results: describe LOGFONT class in your project and use 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 shit! Nowadays the field is always equal to zero. Sometimes and somewhere this option worked, but in doesn't now. In real life you have to use 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 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 to it. What to do?
A: Actually the regions that you get contain the symbols in the 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 font in the system?
A: Not always. 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);
About Perpetuum Software LLC
As a Gold Certified Partner in the Microsoft Partner program, Perpetuum Software has been providing More..a wide range of Microsoft Visual Studio components for over 8 years. Perpetuum Software product lines include WinForms, ASP.NET, Silverlight, and Mobile components, and tools for SQL Server Reporting Services. Among them are .NET reporting tool, OLAP component, and such data visualization libraries as gauge and charting products.Less..
Perpetuum Software LLC
Oak fair Blvd
Phone : 514-0531
Published On:August 13, 2013
Print Release:Print Release
If you have questions regarding information in this press release contact the company listed above. I-Newswire.com is a press release service and not the author of this press release.The information that is on or available through this site is for informational purposes only and speaks only as of the particular date or dates of that information. As some companies and PR Agencies submit their press releases once per week,month or quarter, make sure to check the official company website for accurate release dates as our site displays the I-Newswire.com press release distribution date only.We do not guarantee the accuracy or completeness of information on or available through this site, and we are not responsible for or omissions in that information or for actions taken in reliance on that information.
New AJ Classifieds Software V4 Is Launched!
Windows 7 Registry Optimizer
B2B Soft's New Mobile POS Set to Bust Long Holiday Lines and Improve Sales for Wireless Retailers
ProofHub, the 2nd Best Project Management Tool Now Allows Users to View All Projects Calendar
ESET Named 'Best Endpoint Security Vendor' at Reseller Hot 50 Awards