1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
unit TAOpenGL;
{$mode objfpc}{$H+}
interface
uses
Classes, LazFileUtils, FPCanvas, FPImage, gl,
EasyLazFreeType, LazFreeTypeFPImageDrawer, LazFreeTypeFontCollection, TAFonts;
type
TTextureCacheItem = class
TextureID: Gluint;
TextWidth: Integer;
TextHeight: Integer;
end;
TGLFreeTypeHelper = class
private
FFont: TFreeTypeFont;
FImg: TFPMemoryImage;
FDrawer: TFPImageFreeTypeDrawer;
FTextureCache: TStringList;
protected
function BuildTextureName(AText: String): String;
procedure CreateTexture(AText: String; out ATextWidth, ATextHeight,
ATextureWidth, ATextureHeight: Integer; out ATextureID: GLuint);
function FindTexture(AText: String; out ATextWidth, ATextHeight,
ATextureWidth, ATextureHeight: Integer): GLuint;
public
constructor Create;
destructor Destroy; override;
procedure RenderText(AText: String; Alignments: TFreeTypeAlignments);
procedure SetFont(AFontName: String; AFontSize: Integer;
ABold: Boolean = false; AItalic: Boolean = false;
AUnderline: Boolean = false; AStrikethrough: Boolean = false);
procedure TextExtent(AText: String; out AWidth, AHeight: Integer);
end;
var
GLFreeTypeHelper: TGLFreeTypeHelper = nil;
implementation
uses
SysUtils;
function NextPowerOf2(n: Integer): Integer;
begin
Result := 1;
while Result < n do
Result := Result * 2;
end;
{ TGLFreeTypeHelper }
constructor TGLFreeTypeHelper.Create;
begin
FImg := TFPMemoryImage.Create(8, 8); // dummy size, will be updated when needed
FDrawer := TFPImageFreeTypeDrawer.Create(FImg);
FTextureCache := TStringList.Create;
FTextureCache.Sorted := true;
end;
destructor TGLFreeTypeHelper.Destroy;
var
i: Integer;
item: TTextureCacheItem;
begin
for i:=0 to FTextureCache.Count-1 do begin
item := TTextureCacheItem(FTextureCache.Objects[i]);
glDeleteTextures(1, @item.TextureID);
item.Free;
end;
FTextureCache.Free;
if FFont <> nil then FFont.Free;
FDrawer.Free;
FImg.Free;
inherited;
end;
{ The texture items are stored in the FTextureCache list and can be identified
by means of their name which is composed of the text and font parameters.
The name of the texture items is calculated here. }
function TGLFreeTypeHelper.BuildTextureName(AText: String): String;
begin
Result := Format('%s|%s|%d|%s', [
AText, FFont.Family, round(FFont.SizeInPoints*100), FFont.StyleAsString
]);
end;
procedure TGLFreeTypeHelper.CreateTexture(AText: String; out ATextWidth, ATextHeight,
ATextureWidth, ATextureHeight: Integer; out ATextureID: GLuint);
var
expanded_data: packed array of byte;
i, j: Integer;
c: TFPColor;
begin
if FFont = nil then
raise Exception.Create('No font selected.');
ATextWidth := round(FFont.TextWidth(AText));
ATextHeight := round(FFont.TextHeight(AText));
ATextureWidth := NextPowerOf2(ATextWidth);
ATextureHeight := NextPowerOf2(ATextHeight);
FImg.SetSize(ATextureWidth, ATextureHeight);
FDrawer.FillPixels(colTransparent);
FDrawer.DrawText(AText, FFont, 0,0, colRed, [ftaLeft, ftaTop]);
SetLength(expanded_data, 2*ATextureWidth * ATextureHeight);
for j:=0 to ATextureHeight-1 do
for i:=0 to ATextureWidth-1 do
begin
expanded_data[2*(i + j*ATextureWidth)] := 255; // Luminosity
if (i > ATextWidth) or (j > ATextHeight) then
expanded_data[2*(i + j*ATextureWidth) + 1] := 0 // Alpha
else begin
c := FImg.Colors[i,j];
expanded_data[2*(i + j*ATextureWidth) + 1] := c.Alpha shr 8;
end;
end;
// Set up texture parameters
glGenTextures(1, @ATextureID);
glBindTexture(GL_TEXTURE_2D, ATextureID);
// Create the texture
// Note that we are using GL_LUMINANCE_ALPHA to indicate that we are using
// two-channel data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ATextureWidth, ATextureHeight, 0,
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, @expanded_data[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
end;
{ Finds the texture id for the given text. Returns the texture id and the
size of text and texture. Note that the texture size must be a power of 2 and
thus can be different from the text size. }
function TGLFreeTypeHelper.FindTexture(AText: String;
out ATextWidth, ATextHeight, ATextureWidth, ATextureHeight: Integer): GLuint;
var
idx: Integer;
item: TTextureCacheItem;
txname: String;
begin
txname := BuildTextureName(AText);
idx := FTextureCache.IndexOf(txname);
if idx = -1 then begin
CreateTexture(AText, ATextWidth, ATextHeight, ATextureWidth, ATextureHeight, Result);
item := TTextureCacheItem.Create;
item.TextureID := Result;
item.TextWidth := ATextWidth;
item.TextHeight := ATextHeight;
FTextureCache.AddObject(txname, item);
end else begin
item := TTextureCacheItem(FTextureCache.Objects[idx]);
result := item.TextureID;
ATextWidth := item.TextWidth;
ATextHeight := item.TextHeight;
ATextureWidth := NextPowerOf2(ATextWidth);
ATextureHeight := NextPowerOf2(ATextHeight);
end;
end;
procedure TGLFreeTypeHelper.RenderText(AText: String; Alignments: TFreeTypeAlignments);
var
textureID: GLuint;
w, h: Integer;
w2, h2: Integer;
sx, sy: Double;
dx, dy: Integer;
begin
textureID := FindTexture(AText, w, h, w2, h2);
sx := w / w2;
sy := h / h2;
glMatrixMode(GL_MODELVIEW);
glPushMatrix;
// Note: We don't support ftaJustify)
if (ftaCenter in Alignments) then dx := -w div 2
else if (ftaRight in ALignments) then dx := -w
else dx := 0;
if (ftaVerticalCenter in Alignments) then dy := -h div 2
else if (ftaBottom in Alignments) then dy := -h
else if (ftaBaseline in Alignments) then dy := - h + round(FFont.Descent)
else dy := 0;
glTranslatef(dx, dy, 0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_QUADS);
glTexCoord2f(0.0, sy); glVertex2f(0, h);
glTexCoord2f(sx, sy); glVertex2f(w, h);
glTexCoord2f(sx, 0.0); glVertex2f(w, 0);
glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix;
end;
procedure TGLFreeTypeHelper.SetFont(AFontName: String; AFontSize: Integer;
ABold: Boolean = false; AItalic: Boolean = false;
AUnderline: Boolean = false; AStrikethrough: Boolean = false);
var
style: TFreeTypeStyles;
begin
if GLFreeTypeHelper = nil then
raise Exception.Create('InitFonts has not been called.');
style := [];
if ABold then Include(style, ftsBold);
if AItalic then Include(style, ftsItalic);
// Create a new font if not yet loaded
if (FFont = nil) or (FFont.Family <> AFontName) or (FFont.Style <> style) then
begin
FreeAndNil(FFont);
FFont := LoadFont(AFontName, style);
if FFont = nil then
raise Exception.CreateFmt('Font "%s" not found.', [AFontName]);
end;
// Set the requested font attributes.
FFont.SizeInPoints := AFontSize;
FFont.UnderlineDecoration := AUnderline;
FFont.StrikeoutDecoration := AStrikethrough;
FFont.Hinted := true;
FFont.Quality := grqHighQuality;
//FFont.ClearType := true;
end;
{ Returns the width and height of the specified text. If the text already has
been handled with the same font parameters it is stored in the FTextureCache
list. If not, the size is determined from the font. }
procedure TGLFreeTypeHelper.TextExtent(AText: String; out AWidth, AHeight: Integer);
var
txname: String;
idx: Integer;
item: TTextureCacheItem;
textureID: Gluint;
w2, h2: Integer;
begin
txname := BuildTextureName(AText);
idx := FTextureCache.IndexOf(txname);
if idx = -1 then begin
CreateTexture(AText, AWidth, AHeight, w2, h2, textureID);
item := TTextureCacheItem.Create;
item.TextureID := textureID;
item.TextWidth := AWidth;
item.TextHeight := AHeight;
idx := FTextureCache.AddObject(txname, item);
end;
item := TTextureCacheItem(FTextureCache.Objects[idx]);
AWidth := item.TextWidth;
AHeight := item.TextHeight;
end;
end.
|