Calculate height in millimeters of a font

G

GD1

Dear developers,

is there any way I can calculate the height in millimeters of a font at
a certain size?

Thank you in advance.
 
T

Tim Patrick

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)
 
G

GD1

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?
 
T

Tim Patrick

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.
 
G

GD1

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?
 
T

Tim Patrick

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.
 
G

GD1

What is that?

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.

I'm a beginner programmer, and I find this font-related stuff quite
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top