Skip to content

Instantly share code, notes, and snippets.

@drott
Created December 5, 2012 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drott/4215546 to your computer and use it in GitHub Desktop.
Save drott/4215546 to your computer and use it in GitHub Desktop.
quick & dirty font metrics on c#
Code (adapted from http://msdn.microsoft.com/en-us/library/xwf9s90b.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 - this links also explains about the em/fontDesignUnits to Pixel conversion)
static void Main(string[] args)
{
int ascent;
float ascentPixel;
int descent;
float descentPixel;
int lineSpacing;
float lineSpacingPixel;
FontFamily fontFamily = new FontFamily("Liberation Sans");
Font font = new Font(fontFamily, 16, FontStyle.Regular, GraphicsUnit.Pixel);
ascent = fontFamily.GetCellAscent(FontStyle.Regular);
ascentPixel = font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular);
descent = fontFamily.GetCellDescent(FontStyle.Regular);
descentPixel = font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular);
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);
lineSpacingPixel = font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular);
Console.WriteLine("Liberation Sans, 16 (fontSize: " + font.Size + ") - lineSpacingPixel: " + lineSpacingPixel + ", ascentPixel " + ascentPixel + ", descent " + descentPixel + ", ascent+descent: " + (ascentPixel + descentPixel));
Console.ReadLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment