|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Calculate height in millimeters of a fontDear developers,
is there any way I can calculate the height in millimeters of a font at a certain size? Thank you in advance. GD1 wrote:
> Dear developers, This might get you headed in the right direction:> > is there any way I can calculate the height in millimeters of a font at > a certain size? > > Thank you in advance. http://en.wikipedia.org/wiki/Pica_%28unit_of_measure%29 Try this code.
----------------------- Dim canvas As Graphics Dim result As SizeF Dim stdHeight As Single Dim mmHeight As Single Dim inHeight As Single canvas = Me.CreateGraphics() ' ----- Get the height in default measurements. result = canvas.MeasureString("Ag", Me.Font) stdHeight = result.Height ' ----- Get the height in millimeters. canvas.PageUnit = GraphicsUnit.Millimeter result = canvas.MeasureString("Ag", Me.Font) mmHeight = result.Height canvas.ResetTransform() ' ----- Get the height in inches. canvas.PageUnit = GraphicsUnit.Inch result = canvas.MeasureString("Ag", Me.Font) inHeight = result.Height canvas.ResetTransform() canvas.Dispose() MsgBox("Standard: " & stdHeight & vbCrLf & _ "Millimeters: " & mmHeight & vbCrLf & _ "Inches: " & inHeight) ---------------------------------- This code is based on recipe #9.4 from the book Visual Basic 2005 Cookbook. ----- Tim Patrick Start-to-Finish Visual Basic 2005 Show quoteHide quote > Dear developers, > > is there any way I can calculate the height in millimeters of a font > at a certain size? > > Thank you in advance. >
Show quote
Hide quote
> Dim canvas As Graphics Thank you a lot.> Dim result As SizeF > Dim stdHeight As Single > Dim mmHeight As Single > Dim inHeight As Single > > canvas = Me.CreateGraphics() > > ' ----- Get the height in default measurements. > result = canvas.MeasureString("Ag", Me.Font) > stdHeight = result.Height > > ' ----- Get the height in millimeters. > canvas.PageUnit = GraphicsUnit.Millimeter > result = canvas.MeasureString("Ag", Me.Font) > mmHeight = result.Height > canvas.ResetTransform() > > ' ----- Get the height in inches. > canvas.PageUnit = GraphicsUnit.Inch > result = canvas.MeasureString("Ag", Me.Font) > inHeight = result.Height > canvas.ResetTransform() > > canvas.Dispose() > > MsgBox("Standard: " & stdHeight & vbCrLf & _ > "Millimeters: " & mmHeight & vbCrLf & _ > "Inches: " & inHeight) > But there's still a problem. I'm using this code to calculate what would be the height of a line on a MSWord document before actually writing it. The problem is that the height returned by that lines of code is different than the actual height of the line in Word. I'm not making confusion with zoom, pixels, etc... We're talking of millimeters. There's a fixed coefficient, which (I calculated it) is about 0.88 > ' ----- Get the height in millimeters. Quite strange, isn't it?> canvas.PageUnit = GraphicsUnit.Millimeter > result = canvas.MeasureString("Ag", Me.Font) > mmHeight = result.Height * 0.88 ' This is a consistent result! > canvas.ResetTransform() The FontFamily font member provides access to a lot of the original metrics
in the font. If you have an intance named myFont, check the myFont.FontFamily members, such as myFont.FontFamily.GetLineSpacing(). Remember that Word does a lot of extra typographical analysis and modification of the text that goes beyond the definition of the font. You would probably need to deal with things like kearning and leading on your own. ----- Tim Patrick Start-to-Finish Visual Basic 2005 Show quoteHide quote >> Dim canvas As Graphics >> Dim result As SizeF >> Dim stdHeight As Single >> Dim mmHeight As Single >> Dim inHeight As Single >> canvas = Me.CreateGraphics() >> >> ' ----- Get the height in default measurements. >> result = canvas.MeasureString("Ag", Me.Font) >> stdHeight = result.Height >> ' ----- Get the height in millimeters. >> canvas.PageUnit = GraphicsUnit.Millimeter >> result = canvas.MeasureString("Ag", Me.Font) >> mmHeight = result.Height >> canvas.ResetTransform() >> ' ----- Get the height in inches. >> canvas.PageUnit = GraphicsUnit.Inch >> result = canvas.MeasureString("Ag", Me.Font) >> inHeight = result.Height >> canvas.ResetTransform() >> canvas.Dispose() >> >> MsgBox("Standard: " & stdHeight & vbCrLf & _ >> "Millimeters: " & mmHeight & vbCrLf & _ >> "Inches: " & inHeight) > Thank you a lot. > > But there's still a problem. I'm using this code to calculate what > would be the height of a line on a MSWord document before actually > writing it. The problem is that the height returned by that lines of > code is different than the actual height of the line in Word. I'm not > making confusion with zoom, pixels, etc... We're talking of > millimeters. There's a fixed coefficient, which (I calculated it) is > about 0.88 > >> ' ----- Get the height in millimeters. >> canvas.PageUnit = GraphicsUnit.Millimeter >> result = canvas.MeasureString("Ag", Me.Font) >> mmHeight = result.Height * 0.88 ' This is a consistent result! >> canvas.ResetTransform() > Quite strange, isn't it? > > The FontFamily font member provides access to a lot of the original metrics Thank you for your reply.> in the font. If you have an intance named myFont, check the myFont.FontFamily > members, such as myFont.FontFamily.GetLineSpacing(). Remember that Word does > a lot of extra typographical analysis and modification of the text that goes > beyond the definition of the font. You would probably need to deal with things > like kearning and leading on your own. But I'm not able to interpret the result I get with GetLineSpacing() method. I get a 4-digits integer: what is that? Fonts are defined using "design units." You have to convert these to your
measurement system of choice to make them work. Here is the ratio you would use to target the display. > fontRatio = myFont.Height / myFont.FontFamily.GetLineSpacing(FontStyle.Regular) To be honest, the font metrics exposed through .NET seem a little light to me. There are pre-.NET APIs that let you delve deeply into the structure of fonts. Start with the Visual Studio documentation and the MSDN web site, as they have gobs of information on TrueType fonts and how to analyze them. I also wrote about this in Recipes #9.4 and #9.20 in O'Reilly's Visual Basic 2005 Cookbook. (Makes a great stocking stuffer!) In any case, if you want to process fonts at the level of Microsoft Word, you probably need to go beyond what is exposed in .NET. ----- Tim Patrick Start-to-Finish Visual Basic 2005 Show quoteHide quote >> The FontFamily font member provides access to a lot of the original >> metrics >> in the font. If you have an intance named myFont, check the >> myFont.FontFamily >> members, such as myFont.FontFamily.GetLineSpacing(). Remember that >> Word does >> a lot of extra typographical analysis and modification of the text >> that goes >> beyond the definition of the font. You would probably need to deal >> with things >> like kearning and leading on your own. > Thank you for your reply. > > But I'm not able to interpret the result I get with GetLineSpacing() > method. I get a 4-digits integer: what is that? > > What is that?> > fontRatio = myFont.Height / myFont.FontFamily.GetLineSpacing(FontStyle.Regular) > To be honest, the font metrics exposed through .NET seem a little light to I'm a beginner programmer, and I find this font-related stuff quite> me. There are pre-.NET APIs that let you delve deeply into the structure > of fonts. Start with the Visual Studio documentation and the MSDN web site, > as they have gobs of information on TrueType fonts and how to analyze them. > I also wrote about this in Recipes #9.4 and #9.20 in O'Reilly's Visual Basic > 2005 Cookbook. (Makes a great stocking stuffer!) In any case, if you want > to process fonts at the level of Microsoft Word, you probably need to go > beyond what is exposed in .NET. confusing. I can't understand why there isn't a simple way to get a consistent and coherent value which represents the height, in millimeters, of a line of a certain font at a certain size in a Microsoft Word document. I think I'll give up.
How to receive an array of doubles using VB from a C DLL
Question about rounding the decimal Need help re: vb.net windows forms Controlling how Excel starts when using VB.Net Automation DataView Question Having problems with SetWindowText api Parsing text files Ctype Datasource to DataTable .. always empty? Linking with .OBJ object files Event firing order |
|||||||||||||||||||||||