How to compute text length - e.g. for printing?

Source code related resources

Moderator: Rathinagiri

Post Reply
User avatar
mol
Posts: 2872
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 133 times
Been thanked: 66 times
Contact:

How to compute text length - e.g. for printing?

Post by mol » Thu Sep 11, 2008 6:09 am

I need to compute text length for given font. Can somebody help me?

User avatar
Rathinagiri
Posts: 5181
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 141 times
Been thanked: 131 times
Contact:

Post by Rathinagiri » Thu Sep 11, 2008 8:13 am

Yes. Here it is. The returned length is somewhat accurate. :)

It is used in GridPrint. The returned value is in mm.

Code: Select all

function printLen( cString,nFontsize,cFontname)
return round(gettextwidth(Nil,cString,cFontname)*0.072/72*25.4*nFontsize,2)
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.


User avatar
Rathinagiri
Posts: 5181
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 141 times
Been thanked: 131 times
Contact:

Post by Rathinagiri » Thu Sep 11, 2008 12:20 pm

Most welcome.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
mol
Posts: 2872
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 133 times
Been thanked: 66 times
Contact:

Post by mol » Fri Jul 13, 2012 7:32 am

Hi everybody!
I need to return to this topic.
The method below computes text length with big margin of error when bold or italic is turned on.
I've added experimental factor = 1.2 for bold font, but, it isn't what I want
Do you know better method?

Regards, Marek

Code: Select all

function printLen( cString,nFontsize,cFontname)
return round(gettextwidth(Nil,cString,cFontname)*0.072/72*25.4*nFontsize,2)
Everything looks ALMOST fine for ARIAL font (too big error with too much capital letters) , but looks very bad with ARIAL NARROW FONT:
Image

I drew lines with length of computed text witdth...

Post Reply