2012-01-10 17:09:26 +00:00
|
|
|
unit GLUTBitmapFont;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2012-01-11 16:26:02 +00:00
|
|
|
Classes, SysUtils, GL, GLut, nvBaseFont;
|
2012-01-10 17:09:26 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TFontStyles = record
|
|
|
|
Name: string;
|
|
|
|
Bold: boolean;
|
|
|
|
Italic: boolean;
|
|
|
|
StrikeTrough: boolean;
|
|
|
|
Underline: boolean;
|
|
|
|
Font: pointer;
|
2012-01-11 16:26:02 +00:00
|
|
|
TextListBase: integer;
|
2012-01-10 17:09:26 +00:00
|
|
|
end;
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
{ TGLUTBitmapFont }
|
2012-01-10 17:09:26 +00:00
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
TGLUTBitmapFont = class(TNVBaseFont)
|
2012-01-10 17:09:26 +00:00
|
|
|
private
|
|
|
|
FFontList: array of TFontStyles;
|
|
|
|
FCount: integer;
|
|
|
|
FActiveFont: integer;
|
|
|
|
procedure FindStylizedFont;
|
2012-01-11 16:26:02 +00:00
|
|
|
function InitializeFont(AFont: pointer): integer;
|
2012-01-10 17:09:26 +00:00
|
|
|
protected
|
|
|
|
procedure SetFlags(AIndex: integer; AValue: boolean); override;
|
|
|
|
public
|
|
|
|
constructor Create(AName: string; ASize: integer); override;
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
|
|
|
//add stylized fonts
|
|
|
|
procedure Add(AName: string; ABold, AItalic, AStrikeTrough, AUnderline: boolean); override;
|
|
|
|
|
|
|
|
//text metrics
|
|
|
|
function TextHeight(Text: string): integer; override;
|
|
|
|
function TextWidth(Text: string): integer; override;
|
|
|
|
|
|
|
|
//printing function
|
|
|
|
procedure TextOut(x, y: double; Text: string); override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
{ TGLUTBitmapFont }
|
2012-01-10 17:09:26 +00:00
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
procedure TGLUTBitmapFont.FindStylizedFont;
|
2012-01-10 17:09:26 +00:00
|
|
|
var
|
|
|
|
item: TFontStyles;
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
//if more fonts defined then find stylized font
|
|
|
|
if FCount > 1 then
|
|
|
|
for i := 0 to FCount - 1 do
|
|
|
|
begin
|
|
|
|
item := FFontList[i];
|
|
|
|
|
|
|
|
if (item.Bold = Bold) and
|
|
|
|
(item.Italic = Italic) and
|
|
|
|
(item.StrikeTrough = StrikeTrough) and
|
|
|
|
(item.Underline = Underline) then
|
|
|
|
begin
|
|
|
|
FActiveFont := i;
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
//no font found, select default one
|
|
|
|
FActiveFont := 0;
|
|
|
|
end;
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
function TGLUTBitmapFont.InitializeFont(AFont: pointer): integer;
|
|
|
|
var
|
|
|
|
TextListBase: integer;
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
//just doing 7-bit ascii
|
|
|
|
TextListBase := glGenLists(128);
|
|
|
|
|
|
|
|
for i := 0 to 127 do
|
|
|
|
begin
|
|
|
|
glNewList(TextListBase + i, GL_COMPILE);
|
|
|
|
glutBitmapCharacter(AFont, i);
|
|
|
|
glEndList;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := TextListBase;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TGLUTBitmapFont.SetFlags(AIndex: integer; AValue: boolean);
|
2012-01-10 17:09:26 +00:00
|
|
|
begin
|
|
|
|
inherited SetFlags(AIndex, AValue);
|
|
|
|
|
|
|
|
FindStylizedFont;
|
|
|
|
end;
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
constructor TGLUTBitmapFont.Create(AName: string; ASize: integer);
|
2012-01-10 17:09:26 +00:00
|
|
|
begin
|
|
|
|
inherited Create(AName, ASize);
|
|
|
|
|
|
|
|
FSize := ASize;
|
|
|
|
Add(AName, False, False, False, False);
|
|
|
|
FActiveFont := 0;
|
|
|
|
end;
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
destructor TGLUTBitmapFont.Destroy;
|
2012-01-10 17:09:26 +00:00
|
|
|
var
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
//for i := 0 to FCount - 1 do
|
|
|
|
// FFontList[i].Font.Clean;
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
procedure TGLUTBitmapFont.Add(AName: string; ABold, AItalic, AStrikeTrough, AUnderline: boolean);
|
2012-01-10 17:09:26 +00:00
|
|
|
begin
|
|
|
|
Inc(FCount);
|
|
|
|
SetLength(FFontList, FCount);
|
|
|
|
|
|
|
|
with FFontList[FCount - 1] do
|
|
|
|
begin
|
2012-01-11 16:26:02 +00:00
|
|
|
case LowerCase(AName) of
|
|
|
|
'helvetica': case Size of
|
|
|
|
10: Font := GLUT_BITMAP_HELVETICA_10;
|
|
|
|
12: Font := GLUT_BITMAP_HELVETICA_12;
|
|
|
|
18: Font := GLUT_BITMAP_HELVETICA_18;
|
|
|
|
else
|
|
|
|
raise Exception.CreateFmt('GLUT font size %d does not exist for font %s', [Size, AName]);
|
|
|
|
end;
|
|
|
|
'times roman': case Size of
|
|
|
|
10: Font := GLUT_BITMAP_TIMES_ROMAN_10;
|
|
|
|
24: Font := GLUT_BITMAP_TIMES_ROMAN_24;
|
|
|
|
else
|
|
|
|
raise Exception.CreateFmt('GLUT font size %d does not exist for font %s', [Size, AName]);
|
|
|
|
end;
|
|
|
|
'fixed': case Size of
|
|
|
|
13: Font := GLUT_BITMAP_8_BY_13;
|
|
|
|
15: Font := GLUT_BITMAP_9_BY_15;
|
|
|
|
else
|
|
|
|
raise Exception.CreateFmt('GLUT font size %d does not exist for font %s', [Size, AName]);
|
|
|
|
end;
|
|
|
|
else
|
|
|
|
raise Exception.CreateFmt('GLUT font name not supported %s', [AName]);
|
|
|
|
end;
|
|
|
|
|
2012-01-10 17:09:26 +00:00
|
|
|
Name := AName;
|
|
|
|
Bold := ABold;
|
|
|
|
Italic := AItalic;
|
|
|
|
StrikeTrough := AStrikeTrough;
|
|
|
|
Underline := AUnderline;
|
2012-01-11 16:26:02 +00:00
|
|
|
TextListBase := InitializeFont(Font);
|
2012-01-10 17:09:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
FindStylizedFont;
|
|
|
|
end;
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
function TGLUTBitmapFont.TextHeight(Text: string): integer;
|
2012-01-10 17:09:26 +00:00
|
|
|
begin
|
|
|
|
Result := Size;
|
|
|
|
end;
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
function TGLUTBitmapFont.TextWidth(Text: string): integer;
|
2012-01-10 17:09:26 +00:00
|
|
|
begin
|
2012-01-11 16:26:02 +00:00
|
|
|
Result := glutBitmapLength(FFontList[FActiveFont].Font, PChar(Text));
|
2012-01-10 17:09:26 +00:00
|
|
|
end;
|
|
|
|
|
2012-01-11 16:26:02 +00:00
|
|
|
procedure TGLUTBitmapFont.TextOut(x, y: double; Text: string);
|
2012-01-10 17:09:26 +00:00
|
|
|
begin
|
2012-01-11 16:26:02 +00:00
|
|
|
glListBase(FFontList[FActiveFont].TextListBase);
|
|
|
|
glRasterPos2f(x, y);
|
|
|
|
glCallLists(Length(Text), GL_UNSIGNED_BYTE, @Text[1]);
|
2012-01-10 17:09:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|