Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Textwidth and font.quality issue


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Sep 8, 2014 10:00 AM Last Post By: Simon Horup Threads: [ Previous | Next ]
Simon Horup

Posts: 18
Registered: 10/5/01
Textwidth and font.quality issue  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 8, 2014 7:49 AM
Hi,

Usually when I need to find the pixel width of a string, I use Canvas.TextWidth.

However, it seems like the TextWidth method returns a wrong width under some circumstances.

For example, the following two calls to the method return the same width, even though the actual widths differ, depending on the set quality mode :

Canvas.Font.Name := 'Segoe UI';

Canvas.Font.Quality := fqClearTypeNatural;
ShowMessage( Canvas.TextWidth('123-testing-123').toString );

Canvas.Font.Quality := fqNonAntialiased;
ShowMessage( Canvas.TextWidth('123-testing-123').toString );

When the quality is set to "fqClearTypeNatural", the string gets a little wider, but TextWidth doesn't seem to recognize that.

So my question is, does anyone know how to get the correct string width, no matter what font quality setting is used on the used canvas ?

Thanks in advance :)
Simon H.

Simon Horup

Posts: 18
Registered: 10/5/01
Re: Textwidth and font.quality issue  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 8, 2014 10:00 AM   in response to: Simon Horup in response to: Simon Horup
It seems like the underlying Win API function "GetTextExtentPoint32" that is used TextWidth doesn't return the correct width if fqClearTypeNatural font quality is used on the input canvas.

I solved it by using the DrawText function instead :

aRect.create(0, 0, maxInt, maxInt);
drawText(aCanvas.handle, strValue, length(strValue), aRect, DT_CALCRECT);
width := aRect.right;
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02