Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi 6 : How can I display large size high quality Text via the TextOut() method?

I have a TImage component that I print a Text string to using TCanvas.TextOut(). I set the height and width of the TImage to a large size like 50 pixels X (TextWidth) pixels, and set the Canvas font Height to something a little smaller like 48 pixels. I then BitBlt() the TImage's bitmap on to the main Canvas. What I see on the screen is big skinny letters that are terribly jagged. What I want instead are thick jumbo letters that appear smooth. The reason for using the TImage/BitBlt combo is because I need to do resizing and alpha blending of the text on the fly.

What is the easiest way for me to get big smooth letters to be printed to my TImage bitmap?

like image 704
Robert Oschler Avatar asked Jan 22 '23 07:01

Robert Oschler


1 Answers

Are you never displaying the TImage? Then you should really use an off-screen bitmap instead. This is a very common technique to achieve double buffering (flicker-free rendering).

For example,

var
  bm: TBitmap;

procedure InitOffscreenBitmap;
begin
  bm := TBitmap.Create;
  bm.SetSize(bmWidth, bmHeight);
end;

procedure DrawBitmap;
begin
  // Draw on bm
end;

procedure Swap;
begin
  BitBlt(Canvas.Handle, X, Y, bmWidth, bmHeight, bm.Canvas.Handle, 0, 0, SRCCOPY)
end;

If you are using a modern version of Windows (e.g. Vista+), or Windows XP with ClearType enabled (for some very odd reason, it is disabled by default), text should be smooth. Just make sure that you use a modern font. Most of them will do, but very old fonts such as MS Sans Serif cannot be smoothed using ClearType.

Also, naturally, it is imperative that bm has the same background colour as the form, because alpha-blending will occur when the text is drawn on bm. So if the form is clRed (for some perverse reason), you need to do

bm.Canvas.Brush.Color := clRed;
bm.Canvas.Brush.Style := bsSolid;
bm.FillRect(Rect(0, 0, bmWidth, bmHeight));

prior to

bm.TextOut(...)

Just so we are talking about the same thing: Isn't this smooth enough?

procedure TForm3.FormPaint(Sender: TObject);
begin
  Canvas.Font.Name := 'Segoe UI';
  Canvas.Font.Height := 64;
  Canvas.TextOut(10, 10, 'This is an example.');
end;

Sample Text Output
(High-Res)

like image 152
Andreas Rejbrand Avatar answered Feb 07 '23 17:02

Andreas Rejbrand