Calculation from Font-Size (pt) to Pixel ?

Post Reply
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Calculation from Font-Size (pt) to Pixel ?

Post by ukoenig »

Hello,

is there a clean Solution, to calculate Pixeldimension in Relation to Font - Pt ?
There was a lot of testing, to find the values / adjustment for SAY's from Source
in Relation to the Font-Style / Size.
( you can Download the Fonttool again ( Rel. 1.4b ), with some more fixes pointing out to this Problem )
I tested some Solutions from the Internet, but it was not 100 %

My calculation for Font : Bold, Italic, Underline
Image

A Solution from Internet :

*is the font-size given in pt, you can calculate the font-size in px by:
*fspx = 1.3333*fspt;
*e.g.
*a font-size specified with: font-size:10pt;
*will lead to a font-size of 13.3333px.
*A font-size, specified with: font-size:2.5em;
*is leading to a font-size, which is 2.5 times that of the bodies font-size.
*e.g.
*you specified the font-size of a div to 1.5 em, you can calculate its
*pixel-value by:
*fsdivpx = 1.3333*fsbodypt*fsdivem
*or
*fsdivpx = fsbodypx*fsdivem
*in both cases is the result 20px

Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
Patrizio
Posts: 90
Joined: Wed Nov 07, 2007 8:56 am
Location: Italy
Contact:

Re: Calculation from Font-Size (pt) to Pixel ?

Post by Patrizio »

It's more complicated.

You need to know DPI (x or y) of the device (screen in this case) for convert the two values.

Example
FontSize px = ?
FontSize pt = 14
DPI = 96 (standard screen dpi resolution)
Point in a inch = 72

FontSize (in px) = FontSize(in pt) * DpiX / 72 = 14 * 96 / 72 = 18.667 px
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Calculation from Font-Size (pt) to Pixel ?

Post by ukoenig »

Patrizio,

Thank You very much.
Is it possible for You, to test it with the Tools
and tell me Your results on Your Computer ?

My working Solution ( maybe some changes needed )

Code: Select all

// cTEXTFONT2 = Prompt

//--------------- TEXT-Width  ------------------// 
 
FUNCTION TEXT_W( oSay, oTFont ) 
hDC1 := oSay:GetDC()

IF aFTYPE[6] = .F. // Italic
   nTXTWIDTH :=  2 + INT( GettextWidth( hDC1, ALLTRIM(cTEXTFONT2), oTFont:hFont, )  / 2 )
ELSE
   nTXTWIDTH :=  5 + INT( GettextWidth( hDC1, ALLTRIM(cTEXTFONT2), oTFont:hFont, )  / 2 )
ENDIF
oSay:ReleaseDC()

SAYTEXT := '@ 10,10 Say oSay Prompt "' + ALLTRIM(cTEXTFONT2) + '" OF oDlg SIZE ' ;
   + ALLTRIM(STR(nTXTHEIGHT)) + "," + ALLTRIM(STR(nTXTWIDTH)) + " Pixel Font oFont" 

RETURN ( NIL )

 //--------------- TEXT-Height  ------------------// 
 
FUNCTION TEXT_H( oTFont ) 

IF aFTYPE[7] = .F. // Underline
   nTXTHEIGHT :=  2 + INT( Abs( oTFont:nHeight ) / 2 )
ELSE
   nTXTHEIGHT :=  10 + INT( Abs( oTFont:nHeight ) / 2 )
ENDIF
SAYTEXT := '@ 10,10 Say oSay Prompt "' + ALLTRIM(cTEXTFONT2) + '" OF oDlg SIZE ' ;
   + ALLTRIM(STR(nTXTHEIGHT)) + "," + ALLTRIM(STR(nTXTWIDTH)) + " Pixel Font oFont" 

RETURN ( NIL )
 
Best Regards
Uwe :cry:
Last edited by ukoenig on Wed Feb 17, 2010 11:48 am, edited 1 time in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
Patrizio
Posts: 90
Joined: Wed Nov 07, 2007 8:56 am
Location: Italy
Contact:

Re: Calculation from Font-Size (pt) to Pixel ?

Post by Patrizio »

With

Code: Select all

oFont := TFont():New( "Arial",21,-47,.F.,.T.,0,0,0,.T..T.)
oSay:SetColor( 16777215, 128 )
The preview say Size: 33 x 92
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Calculation from Font-Size (pt) to Pixel ?

Post by ukoenig »

The Result of Your settings on my Computer
I hope, it looks the same :

Image

Best regards
Uwe :roll:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
Post Reply