Mastodon
Programmierung

Fonts: Anti-Aliasing Text

Of course it is slower than non-antialiased text. So it is important to
optimize the routine. Here is some part of the routine I use, it’s
probably the part that’s most important to optimize (might be faster
when precalculating OfsPtr to an array?):


[code lang=”delphi”]For OuterY := 0 To TextDIB.Height-1 Do Begin
TextPtr := ResultBitmap.ScanLine[OuterY];
InnerOffset := 0;
For OuterX := 0 To TempBitmap.Width-1 Do Begin
// get average color
VSum := 0;
For InnerY := 0 To Zoom2 Do Begin
OfsPtr := TempBitmap.ScanLine[(OuterY SHL FQuality)+InnerY];
For InnerX := 0 To Zoom2 Do Begin
Inc (VSum,OfsPtr^[InnerOffset+InnerX]);
End;
End;
// set pixel
TextPtr^ := VSum SHR ShiftValue;
// next pixel
Inc (TextPtr);
Inc (InnerOffset,FZoom);
End;
End;
[/code]

I guess I have to explain some variables. The quality is set by
FQuality, some variables that are used often are precalculated:
[code lang=”delphi”] FZoom := 1 SHL FQuality;
Zoom2 := FZoom-1;
ShiftValue := FQuality SHL 1;
TempBitmap is FZoom times larger than ResultBitmap
[/code]

As I always draw the text to 8 bit bitmaps, OfsPtr = ^TByteArray where
TByteArray = Array[0..65535] of Byte. The result is 8 bit, too (so
TextPtr = ^Byte) because I use this result to combine it with the
original image by some other procedure. This is more flexible, because
you have to calculate the result only once and can use it for colored
text, its shadow and any other effect you can think of. Of course you
can do this in one step, then you have to interpret black (or white) as
transparent and grays as semi-transparent. The code would look like:

[code lang=”delphi”] VVal := VSum SHR ShiftValue;
TextPtr^.Red := TextPtr^.Red+((SomeColor.Red-TextPtr^.Red)*VVal DIV 256);
TextPtr^.Green := TextPtr^.Green+((SomeColor.Green-TextPtr^.Green)*VVal DIV 256);
TextPtr^.Blue := TextPtr^.Blue+((SomeColor.Blue-TextPtr^.Blue)*VVal DIV 256);
[/code]
But I guess it’s not much slower when doing this in a second step where
VVal := TextPtr^ (the difference is that you save all VVals in
ResultBitmap before you use them) and really much more powerful when
adding routines for effects like shadow, glow etc. to your routine.

[tags]Delphi, Graphic, Fonts[/tags]

0 Kommentare zu “Fonts: Anti-Aliasing Text

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.