Message-ID: <3B1DEF45.1D46CF36@borland.newsgroups> Date: Wed, 06 Jun 2001 18:52:21 +1000 From: Charles Hacker X-Mailer: Mozilla 4.76 [en] (Win95; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.delphi.graphics Subject: Re: Antialias ? References: <9fkl19$308$02$1@news.t-online.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 132.234.46.157 X-Trace: dnews 991817545 132.234.46.157 (6 Jun 2001 01:52:25 -0700) Lines: 130 Path: dnews Xref: dnews borland.public.delphi.graphics:39469 Alexander Adam wrote: > > I've searched the web for a while but found nothing simple to use for > antialiasing a font which is on a tcanvas, any ideas ? I snagged this bit of code that may be of use... By Gustavo Daud } procedure DrawAAText(Dest: TBitmap; DX, DY: Integer; Text: String); type pRGBLine = ^TRGBLine; TRGBLine = Array[Word] of TRGBTriple; {Separate R, G and B values from the color} procedure SeparateColor(Color: TColor; var r: Byte; var g: byte; var b: byte); begin R := Color and $FF0000 shr 16; G := Color and $00FF00 shr 8; B := Color and $0000FF; end; var TempBitmap: TBitmap; x, y : Integer; totr, totg, totb : Integer; j, i : Integer; Line : pRGBLine; TempLine : Array[0..1] of pRGBLine; begin {Creates a temporary bitmap do work with supersampling} TempBitmap := TBitmap.Create; with TempBitmap do begin PixelFormat := pf24bit; {Copy attributes from previous bitmap} Canvas.Font.Assign(Dest.Canvas.Font); Canvas.Brush.Assign(Dest.Canvas.Brush); Canvas.Pen.Assign(Dest.Canvas.Pen); Canvas.Font.Size := Canvas.Font.Size * 2; {Make it twice larger to apply supersampling later} Width := Canvas.TextWidth(Text); Height := Canvas.TextHeight(Text); {To prevent unexpected junk} if (Width div 2) + DX > Dest.Width then Width := (Dest.Width - DX) * 2; if (Height div 2) + DY > Dest.Height then Height := (Dest.Height - DY) * 2; {If the brush style is clear, then copy the image from the} {previous image to create the propher effect} if Canvas.Brush.Style = bsClear then begin Canvas.Draw(-DX, -DY, Dest); Canvas.Stretchdraw(Rect(0, 0, Width * 2, Height * 2), TempBitmap); end; {Draws the text using double size} Canvas.TextOut(0, 0, Text); end; {Draws the antialiased image} for y := 0 to ((TempBitmap.Height) div 2) - 1 do begin {If the y pixel is outside the clipping region, do the proper action} if dy + y < 0 then Continue else if Dy + y > Dest.Height - 1 then Break; {Scanline for faster access} Line := Dest.ScanLine[DY + y]; TempLine[0] := TempBitmap.Scanline[2*y]; TempLine[1] := TempBitmap.Scanline[(2*y) + 1]; for x := 0 to ((TempBitmap.Width) div 2) - 1 do begin {If the x pixel is outside the clipping region, do the proper action} if dx + x < 0 then Continue else if Dx + x > Dest.Width - 1 then Break; {Compute the value of the output pixel (x, y) } TotR := 0; TotG := 0; TotB := 0; for j := 0 to 1 do begin for i := 0 to 1 do with TempLine[j][2*x+i] do begin inc(TotR, rgbtRed); inc(TotG, rgbtGreen); inc(TotB, rgbtBlue); end; end; {Set the pixel values thru scanline} with Line[DX + x] do begin rgbtRed := TotR div 4; rgbtGreen := TotG div 4; rgbtBlue := TotB div 4; end; end; end; {Free the temporary bitmap} TempBitmap.Free; end; -- Charles Hacker Lecturer in Electronics and Computing School of Engineering Griffith University - Gold Coast Australia