Odd Question About Fonts

Just starting out? Need help? Post your questions and find answers here.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Odd Question About Fonts

Post by chris319 »

I have an odd question about fonts.

When I draw a character in PB there is an enclosing background box which extends above and below the character forms themselves. Is this a function of PB, Windows, or the font itself? Does anyone know how the size of this enclosing box is calculated? I'm trying to align some text based on the tops and bottoms of the actual characters, rather than the enclosing box.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Odd Question About Fonts

Post by luis »

That's the cell around the char. It's there to make room for the ascending and descending part of the chars and their accents.
The height of the cell should be constant, the width is variable.
The chars are all aligned on a base line with part of some chars ascending and other descending from that.

You may find some of the info you are looking for in the TEXTMETRIC structure
http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Some background on the nomenclature used
http://support.microsoft.com/kb/74299
"Have you tried turning it off and on again ?"
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Odd Question About Fonts

Post by chris319 »

How would I set up and access a textMetric structure in PB for a given font?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Odd Question About Fonts

Post by luis »

chris319 wrote:How would I set up and access a textMetric structure in PB for a given font?

Code: Select all

 Define TM.TEXTMETRIC
 
 pointsize = 48
 
 ; http://msdn.microsoft.com/en-us/library/dd183499%28v=vs.85%29.aspx
 fnt = LoadFont(#PB_Any, "Arial", -pointsize)
 font_obj = FontID(fnt)
 
 HWnd = 0 ; handle of your win here, 0 = desktop
 hDC = GetDC_(HWnd)
 
 Debug "logical height requested = " + StrF((pointsize * GetDeviceCaps_(hDC, #LOGPIXELSY)) / 72.0)
 
 old_obj = SelectObject_(hDC, font_obj)
 GetTextMetrics_(hDC, @TM)
 SelectObject_(hDC, old_obj)
 
 Debug "Height = " + TM\tmHeight
 Debug "InternalLeading = " + TM\tmInternalLeading
 Debug "Ascent = " + TM\tmAscent
 Debug "Descent = " + TM\tmDescent 
 Debug "cell height = " + Str(TM\tmHeight + TM\tmInternalLeading)
"Have you tried turning it off and on again ?"
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Odd Question About Fonts

Post by chris319 »

Thank you for posting that sample code. Nice!
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Odd Question About Fonts

Post by chris319 »

Here is a good description of font metrics:

http://www.freetype.org/freetype2/docs/ ... phs-3.html
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Odd Question About Fonts

Post by IdeasVacuum »

It is indeed, thanks for that link! You can by the way use PB's 2D drawing lib to determine text size. That would be cross-platform so might be worth considering.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply