首页  编辑  

平滑字体算法

Tags: /超级猛料/Font.字体/   Date Created:

平滑字体算法

先将文字放大一倍字号画到临时内存中, 再用AntiAlias算法缩小, 绝对光滑.

而且速度很快.我这有个AntiAlias算法的例子, 很简单但很说明问题:

其中核心代码如下:

procedure TAntiAliasForm.SeparateColor(color : TColor;

   var r, g, b : Integer);

begin

   r := color Mod 256;

   g := (color Div 256) Mod 256;

   b := color Div 65536;

end;

procedure TAntiAliasForm.CmdGoClick(Sender: TObject);

var

   txt                       : String;

   wid, hgt, x, y, i, j      : Integer;

   r, g, b, totr, totg, totb : Integer;

begin

   txt := InputText.Text;

   // Use the correct colors.

   big_bm.Canvas.Font.Color := InputText.Font.Color;

   big_bm.Canvas.Brush.Color := InputText.Color;

   // Set the new font.

   big_bm.Canvas.Font := InputText.Font;

   big_bm.Canvas.Font.Size := 2 * InputText.Font.Size;

   big_bm.Canvas.Refresh; // Make the font take effect.

   // Make big_bm and BigBox big enough.

   wid := big_bm.Canvas.TextWidth(txt);

   hgt := Round(big_bm.Canvas.TextHeight(txt) * 1.1);

   big_bm.Width := wid;

   big_bm.Height := hgt;

   big_bm.Canvas.Refresh;

   BigBox.Width := wid;

   BigBox.Height := hgt;

   BigBox.Refresh;

   // Draw the text in big_bm.

   big_bm.Canvas.TextOut(0, 0, txt);

   // Display it in BigBox.

   BigBox.Invalidate;

   // ********************************

   // Anti-alias the text into out_bm.

   // ********************************

   // Create a new out_bm.

   //  out_bm.Free;                    - Removed by RLV on 12/29/97

   //  out_bm := TBitmap.Create;       - Removed by RLV on 12/29/97

   out_bm.Width := wid Div 2;

   out_bm.Height := hgt Div 2;

   out_bm.Canvas.Refresh;

   // The "- 3" keeps us from falling off the edge

   // of BigBox. Over the edge Point would

   // return -1 and mess up the colors.

   for y := 0 to (big_bm.Height - 3) Div 2 do

   begin

       for x := 0 to (big_bm.Width - 3) Div 2 do

       begin

           // Compute the value of output pixel (x, y).

           totr := 0;

           totg := 0;

           totb := 0;

           for j := 0 to 1 do

           begin

               for i := 0 to 1 do

               begin

                   SeparateColor(big_bm.Canvas.Pixels

                       [2 * x + i, 2 * y + j], r, g, b);

                   totr := totr + r;

                   totg := totg + g;

                   totb := totb + b;

               end;

           end;

           out_bm.Canvas.Pixels[x, y] :=

               RGB(totr Div 4, totg Div 4, totb Div 4);

       end;

   end;

   OutBox.Invalidate;

   // Remove the hourglass cursor.

   Screen.Cursor := crDefault;

end;

当然, 它的代码还可以进一步优化, 比如用scanline代替pixels, 速度可以进一步

提高.