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

Source code related resources

Moderator: Rathinagiri

Post Reply
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

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

Post by mol »

I need to compute text length for given font. Can somebody help me?
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

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

Post by Rathinagiri »

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: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

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

Post by Rathinagiri »

Most welcome.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

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

Post by mol »

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