Home All Groups Group Topic Archive Search About

Calculate height in millimeters of a font

Author
23 Oct 2006 6:41 PM
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.

Author
23 Oct 2006 7:49 PM
zacks
GD1 wrote:
> Dear developers,
>
> is there any way I can calculate the height in millimeters of a font at
> a certain size?
>
> Thank you in advance.

This might get you headed in the right direction:

http://en.wikipedia.org/wiki/Pica_%28unit_of_measure%29
Author
23 Oct 2006 8:01 PM
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)

----------------------------------
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.
>
Author
24 Oct 2006 9:26 AM
GD1
Show quote Hide 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?
Author
24 Oct 2006 4:31 PM
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.

-----
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?
>
Author
24 Oct 2006 6:33 PM
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?
Author
24 Oct 2006 7:44 PM
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.

-----
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?
>
Author
24 Oct 2006 8:40 PM
GD1
>
> > fontRatio = myFont.Height / myFont.FontFamily.GetLineSpacing(FontStyle.Regular)

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.