#1 2011-09-03 13:17:26

fkaynakli
Member
Registered: 2011-06-14
Posts: 12

Is unicode string width calculation wrong?

Hi;
I am working on text alignment (left, right, center etc). You can download sample application from here.
I can not succeed on getting exact text width. Depending on font size, real text width differs from VCLCanvas.TextExtent().cx result.
If you try different font sizes (12, 14, 16, 18, 20, 22 etc.) by changing xFontSize constant, you will see strange results.
Thanks for your help. Best Regards...

Offline

#2 2011-09-03 13:52:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Is unicode string width calculation wrong?

TextExtent calls in fact the Windows API, so I don't know what you state what "real text width differs from TextExtent result".
Individual character positioning is not handled as in Windows, so it may differ if you enlarge the character spacing and kerning.

What is wrong? The text in the PDF? How does it be "wrong"? With which fonts and character sets?

If you can write some sample code to reproduce the problem, it would be easier to guess what's up.

Thanks!

Offline

#3 2011-09-03 14:29:26

fkaynakli
Member
Registered: 2011-06-14
Posts: 12

Re: Is unicode string width calculation wrong?

You can download the test application from here (I updated the demo just now). What I do is:

  • Get the text width by TextExtent (for ex. to variable TxtWidth)

  • Calculate the text's left value by removing TxtWidth from rectangle's right side

  • Draw bounding rectangle for testing. I expect that text will fit in the bounding rectangle but the text always overflows bounding rectangle

  • Overflow amount depends on text content (not character count) and font (in demo app I used Times New Roman and Arial)

Last edited by fkaynakli (2011-09-03 14:31:30)

Offline

#4 2011-09-04 11:19:16

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Is unicode string width calculation wrong?

AFAIK your sample code mix both TPdfCanvas and TCanvas methods, i.e. the Canvas and VCLCanvas.

The VCLCanvas will use Kerning for its font rendering, whereas Canvas won't.

If you use only one class of methods, the width will be consistent.
So IMHO there is no bug.

I recommend only use VCLCanvas, which will allow you more easier TCanvas access, with metafile rendering, and such.

Offline

#5 2011-09-04 13:56:25

fkaynakli
Member
Registered: 2011-06-14
Posts: 12

Re: Is unicode string width calculation wrong?

I mixed of them for two reasons:

  • I have to use CMYK colors and transparent PNG. Is it possible to use these with VCLCanvas?

  • I don't have to use kerning. If I can get unicode text width by PdfCanvas, it is enaugh for me.

Offline

Board footer

Powered by FluxBB