473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calculate height in millimeters of a font

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.

Oct 23 '06 #1
7 5097

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

Oct 23 '06 #2
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
Dear developers,

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

Thank you in advance.

Oct 23 '06 #3
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?

Oct 24 '06 #4
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
>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?

Oct 24 '06 #5
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?

Oct 24 '06 #6
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
>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?

Oct 24 '06 #7
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.

Oct 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zsolt Koppany | last post by:
Hi, I use dynamic menus and would like to know how I can figure out the height of a text (for exalple "Product"). I need it to calculate text positions. Zsolt
10
by: John | last post by:
I have a table with two rows. On the first row is a text box and in the second row is an image. I have set the table cellpadding to 0 and cellspacing to 0. The table is leaving extra spaces in the...
11
by: Rithish | last post by:
If I have something like, <TABLE><TR><TD STYLE="'Font-Family:Arial'">Arial Text</TD></TR></TABLE> What would be the height of the cell? I believe it would be corresponding to the font used......
12
by: Stanimir Stamenkov | last post by:
Here are two cases regarding inline-level elements' line-height, padding and background, which I doesn't understand: <div style="background: black; color: white; line-height: 1.5">...
12
by: paii, Ron | last post by:
Sorry about that last one. Does anyone know how to calculate the width a string of text for given Font name and size? I want to buildup a block of text strings to display in a unbound control,...
1
by: Mike Collins | last post by:
I am trying to create some pages and have them take 100% of the window, but I cannot get 100% to work. If I set the height of my div, the page displays a scroll bar and it looks like I set the...
10
by: dtmfcc | last post by:
My website is at www.simi-therapy.com My CSS is at http://www.simi-therapy.com/simitherapy-screen.css Question -- on the dark blue bar under the beach image, one change caused another...
1
by: pravinnweb | last post by:
can anyone tell me how to set auto height to outer div that is in green box id "gray-background" it should increase relatively to inner div "smbox" here is the css and html code it should work in...
2
by: ssmeshack | last post by:
Hai all, Im having problem to set minimum height to <td> tag. It only takes height only not min-height. Im ie7 browser and developing web based system in Visual Web Deveploper 2008 (C# .Net). ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.