Created
December 5, 2012 13:35
-
-
Save drott/4215546 to your computer and use it in GitHub Desktop.
quick & dirty font metrics on c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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