Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure the pixel width of a digit in a given font / size (C#)

I am trying to calculate the pixel width of Excel columns, as described in this post, using the official formula from the OpenXML specification. However, in order to apply this formula, I need to know the Maximum Digit Width of the Normal font, which is the pixel width of the widest numeric digit. The OpenXML specification gives this example as a clarification:

Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi).

I have checked that this is correct by visually examining a Calibri 11-point digit and it is indeed 7 pixels wide. So, I am trying to create a method that will return the maximum digit width for any font / size.

I have followed the recommendations made in this question, but it doesn't produce the results I am expecting.

Here's my test code:

var font = new Font("Calibri", 11.0f, FontStyle.Regular);

for (var i = 0; i < 10; i++)
{
    Debug.WriteLine(TextRenderer.MeasureText(i.ToString(), font));
}

This reports that all digits have a width of 15.

Any suggestions please?

Thanks, Tim

like image 765
Tim Coulter Avatar asked Dec 02 '09 13:12

Tim Coulter


1 Answers

First let me say that the code I'm about to show looks like a horrible hack and I'm not presenting it as a solution. Rather, I'm hoping it reveals a little more about the behavior of MeasureText that might lead you to your final solution.

I made a slight alteration to your code. I'm measuring the length of two underscore characters. Then, in the body of the for loop I'm measuring the desired character with underscores on either side and subtracting the length of the two underscore characters alone. For the test scenario I am getting a result of 7. The main purpose of this was to determine whether MeasureText is accounting for padding on a character-by-character basis or padding at the beginning/ending of the string. It seems that it is the latter. Perhaps this, combined with some of the other input you've received, will lead you to a more elegant solution.

var font = new Font("Calibri", 11.0f, FontStyle.Regular);
int underscoreWidth = TextRenderer.MeasureText("__", font).Width; 
for (var i = 0; i < 10; i++)
   {
      int charWidth = TextRenderer.MeasureText(String.Concat("_", i.ToString(), "_"), font).Width - underscoreWidth;
      Debug.WriteLine(charWidth);
   }
like image 82
lJohnson Avatar answered Sep 20 '22 12:09

lJohnson