Important alert: (current site time 5/20/2013 3:35:26 PM EDT)
 

article

Faster MeasureText (GDI)

Email
Submitted on: 6/14/2012 5:18:24 PM
By: Jason James Newland  
Level: Intermediate
User Rating: Unrated
Compatibility: C#
Views: 1636
author picture
 
     The TextRenderer.MeasureText method of System.Windows.Forms is OK for quick measurements but totally inefficient for mass calls within a loop. After banging my head against the wall, with a little guidence from a friend, I came up with this solution. This will store an array of integers (based on the number of a character index) from 0 - 255 initially in a Dictionary of Fonts (if the font passed to the method doesn't exist). Testing proved this to take around 60ms. Any other characters above 255 (unicode) are measured on the fly then stored in the array when needed, and all measurements are called via the public static MeasureStringWidth method. This, in a loop (such as a word-wrapping routine) will greatly improve speed by at least 30-40% if not more. It supports normal characters, bolded and italic measuring of any font, including mono-spaced.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace dIRC7.Helpers
{
/* Faster (cached) MeasureStringWidth class
By: Jason James Newland & Ryan Alexander
©2012 KangaSoft Software - All Rights Reserved
 */
#region Fields
internal class FontData
{
public int[] NormalCharacterWidth { get; set; }
public int[] BoldCharacterWidth { get; set; }
public int[] ItalicCharacterWidth { get; set; }
}
#endregion
public sealed class IrcMeasureStringWidth
{
#region Fields
private const TextFormatFlags FormatFlags = TextFormatFlags.Left | TextFormatFlags.NoPrefix | TextFormatFlags.NoPadding | TextFormatFlags.NoClipping;
private static readonly Dictionary Fonts = new Dictionary();
#endregion
#region Public measure string width
public static int MeasureStringWidth(Graphics g, Font font, string text)
{
FontData data;
if (!Fonts.ContainsKey(font))
{
data = new FontData
{
NormalCharacterWidth = BuildLookupList(g, font),
BoldCharacterWidth = BuildLookupList(g, new Font(font, FontStyle.Bold)),
ItalicCharacterWidth = BuildLookupList(g, new Font(font, FontStyle.Italic))
};
Fonts.Add(font, data);
}
else { data = Fonts[font]; }
return MeasureStringWidth(g, text.ToCharArray(), font, data);
}
#endregion
#region Private methods
#region Measure string width overloads
private static int MeasureStringWidth(Graphics g, IList text, Font font, FontData data)
{
if (text == null || text.Count == 0) { return 0; }
return text.Count == 1 ? MeasureStringWidth(g, text[0], font, data) : text.Sum(chr => MeasureStringWidth(g, chr, font, data));
}
private static int MeasureStringWidth(IDeviceContext g, char chr, Font font, FontData data)
{
var chrValue = (int)chr;
if ((font.Bold && font.Italic) || font.Bold)
{
if (chrValue > 255 && data.BoldCharacterWidth[chrValue] == 0)
{
data.BoldCharacterWidth[chrValue] = MeasureString(g, font, chr.ToString());
}
return data.BoldCharacterWidth[chrValue];
}
if (font.Italic)
{
if (chrValue > 255 && data.ItalicCharacterWidth[chrValue] == 0)
{
data.ItalicCharacterWidth[chrValue] = MeasureString(g, font, chr.ToString());
}
return data.ItalicCharacterWidth[chrValue];
}
if (chrValue > 255 && data.NormalCharacterWidth[chrValue] == 0)
{
data.NormalCharacterWidth[chrValue] = MeasureString(g, font, chr.ToString());
}
return data.NormalCharacterWidth[chrValue];
}
#endregion
#region Build main character look-up list
private static int[] BuildLookupList(IDeviceContext g, Font font)
{
var lookUp = new int[char.MaxValue];
for (var i = (char)0; i < (char)256; i++)
{
lookUp[i] = MeasureString(g, font, i.ToString());
}
return lookUp;
}
#endregion
#region GDI measure text string
private static int MeasureString(IDeviceContext g, Font font, string text)
{
return string.IsNullOrEmpty(text) ? 0 : TextRenderer.MeasureText(g, text, font, Size.Empty, FormatFlags).Width;
}
#endregion
#endregion
}
}


Other 21 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/14/2012 8:31:44 PMJason Newland

See the updated version/zip download
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.