Ich habe meinen Code zur Demonstration stark vereinfacht, damit sollte gut sichtbar sein, wieso ich 8Bit Zeichen brauche.
Im fertigen Code ist die BGRABitmap als Textur im VRAM.
Die for-Schleife mit x und y entfällt, die TexturPos[?] wird als Uniform dem Shader übergeben.
Code: Alles auswählen
TForm1 = class(TForm)
Edit1: TEdit;
procedure Edit1Change(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
private
BGRABitmap: TBGRABitmap;
TexturPos: array[0..256] of record
Width, Left: integer;
end;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormShow(Sender: TObject);
var
BGRAGradientScanner: TBGRAGradientScanner;
TextHeight, w, i: integer;
c: UnicodeString;
begin
BGRABitmap := TBGRABitmap.Create;
with Edit1 do begin
BGRABitmap.FontName := Font.Name;
BGRABitmap.FontStyle := Font.Style;
BGRABitmap.FontHeight := Font.Height;
BGRABitmap.FontQuality := fqSystemClearType;
BGRABitmap.FontAntialias := False;
end;
TexturPos[0].Left := 0;
for i := 0 to 255 do begin
c := UnicodeToUTF8(i);
w := BGRABitmap.TextSize(c).cx;
TexturPos[i].Width := w;
TexturPos[i + 1].Left := w + TexturPos[i].Left;
end;
TextHeight := BGRABitmap.FontFullHeight;
BGRABitmap.SetSize(TexturPos[256].Left, TextHeight);
BGRABitmap.Fill(BGRA($00, $FF, $00, $80));
for i := 0 to 255 do begin
BGRAGradientScanner := TBGRAGradientScanner.Create(BGRA(255, 255, 0), BGRA(255, 0, 0), gtLinear, PointF(0, 0), PointF(0, TextHeight), True, True);
BGRABitmap.TextOut(TexturPos[i].Left, 0, UnicodeToUTF8(i), BGRAGradientScanner);
BGRAGradientScanner.Free;
end;
// BGRABitmap.SaveToFile('test.bmp');
end;
procedure TForm1.Edit1Change(Sender: TObject);
var
UniText: UnicodeString;
CharIndex, i, y, x, cur: integer;
begin
UniText := UTF8Decode(Edit1.Text);
cur := 0;
for i := 0 to Length(UniText) - 1 do begin
CharIndex := byte(UniText[i + 1]);
for y := 0 to BGRABitmap.FontFullHeight do begin
for x := 0 to TexturPos[CharIndex].Width do begin
Canvas.Pixels[cur + x, y] := BGRABitmap.Canvas.Pixels[x + TexturPos[CharIndex].Left, y];
end;
end;
cur := cur + TexturPos[CharIndex].Width + 10;
end;
end;