-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuFont.pas
267 lines (233 loc) · 7.12 KB
/
uFont.pas
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
266
267
unit uFont;
interface
uses Windows, OpenGL;
type
// tbv. regel indeling
TLineVAlignment = (laTop, laBottom {, laLeft, laRight});
const
// de verzameling characters in het OpenGL font
FirstCharacter = 32;
LastCharacter = 126;
N_Characters = LastCharacter - FirstCharacter + 1;
// tbv. regel indeling
DefLineSpacing = 0; //2/12; //2 pixels per 12px font verhouding
type
TGLFont = class(TObject)
private
// de eerste displaylist van een compleet font
DisplayListBase: GLuint;
// het true-type Windows font in 256 display-lists (voor elk character 1)
GMF: array[FirstCharacter..N_Characters-1] of GLYPHMETRICSFLOAT;
// geladen font instellingen
Family: string;
SizePx: integer; // de grootte van het font in pixels
is3D: boolean;
procedure PrintText(const Value: string);
published
public
// object
constructor Create;
destructor Destroy; override;
//
function CreateFontDLs(DC: HDC; RC: HGLRC; Fontname: string; FontSize: integer; Font3D: boolean) : GLuint;
procedure DeleteFont;
function IsFontLoaded : boolean;
// op pixel afstand vanaf de linker-onder hoek van het scherm
procedure PrintXY(X,Y: integer; const Value: string; ScrW,ScrH: integer);
procedure PrintXYZ(X,Y,Z: GLfloat; const Value: string);
// per regel afbeelden..vanaf boven, of vanaf beneden (Alignment)
procedure PrintLine(Row: integer; const Value: string; VAlign: TLineVAlignment; ScrW,ScrH: integer);
end;
// een verzameling fonts
TGLFonts = class(TObject)
private
fFonts: array of TGLFont;
function GetFont(I:integer) : TGLFont;
function GetFontCount : integer;
{procedure SetFont(I:integer; Value: TGLFont);}
published
// property Font[I:integer]:TGLFont read GetFont {write SetFont};
public
property Font[I:integer]:TGLFont read GetFont {write SetFont};
property FontCount:integer read GetFontCount;
// object
constructor Create;
destructor Destroy; override;
//
function AddFont(DC: HDC; RC: HGLRC; Fontname: string; FontSize: integer; Font3D: boolean) : GLuint;
end;
implementation
{ TGLFont }
constructor TGLFont.Create;
begin
DisplayListBase := 0; //ongeldige DisplayList handle
Family := '';
SizePx := 0;
is3D := false;
end;
destructor TGLFont.Destroy;
begin
DeleteFont;
//
inherited;
end;
function TGLFont.CreateFontDLs(DC: HDC; RC: HGLRC; Fontname: string; FontSize: integer; Font3D: boolean) : GLuint;
var F,F1: HFONT;
b: boolean;
begin
Family := Fontname;
SizePx := FontSize;
is3D := Font3D;
{wglMakeCurrent(DC, RC);}
glPushMatrix;
// displaylist handles reserveren..
DisplayListBase := glGenLists(N_Characters);
// maak een verwijzing naar het gewenste font (italic, underline, strike, weight,..)
F := CreateFont(FontSize, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE + DEFAULT_PITCH, PChar(Fontname));
F1 := SelectObject(DC, F);
// 2D of 3D?
if Font3D then
// ==== outlined font ======================================================
// Maak een array van displaylists voor een true-type font.
// De glyphs 0..255 (256 in totaal) worden met een 0.05 extrusie een bepaalde
// 3D-"dikte" gegeven. De deviation staat standaard op 0.0, dwz. dat de
// characters net zo precies worden als het font.
//
// selecteer het font en glyphes aanmaken
b := wglUseFontOutlines(DC, FirstCharacter, N_Characters, DisplayListBase, 0.0, 0.05, WGL_FONT_POLYGONS, @GMF[FirstCharacter])
else
// ==== bitmapped font =====================================================
b := wglUseFontBitmaps(DC, FirstCharacter, N_Characters-1, DisplayListBase);
SelectObject(DC, F1);
DeleteObject(F);
if not b then
if DisplayListBase > 0 then begin
glDeleteLists(DisplayListBase, N_Characters);
DisplayListBase := 0;
Family := '';
{Msg("FontList is niet aangemaakt!");}
end;
glPopMatrix;
Result := DisplayListBase;
end;
procedure TGLFont.DeleteFont;
{var i: integer;}
begin
// DisplayLists wissen..
if DisplayListBase > 0 then glDeleteLists(DisplayListBase, N_Characters);
(*
if DisplayListBase > 0 then begin
for i:=0 to N_Characters-1 do
if glIsList(DisplayListBase+i) then glDeleteLists(DisplayListBase+i, 1);
end;
*)
DisplayListBase := 0;
Family := '';
SizePx := 0;
is3D := false;
end;
function TGLFont.IsFontLoaded: boolean;
begin
Result := (DisplayListBase<>0);
end;
procedure TGLFont.PrintText(const Value: string);
var L: integer;
begin
L := Length(Value);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glPushAttrib(GL_LIST_BIT);
glListBase(DisplayListBase - FirstCharacter);
glCallLists(L, GL_UNSIGNED_BYTE, @Value[1]);
glPopAttrib;
end;
procedure TGLFont.PrintXY(X, Y: integer; const Value: string; ScrW,ScrH: integer);
begin
// glPushMatrix;
{glPushAttrib(GL_TRANSFORM_BIT or GL_VIEWPORT_BIT);}
glDisable(GL_DEPTH_TEST);
// orthogonaal afbeelden (geen perspectief)
glViewPort(0,0,ScrW,ScrH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0,ScrW, 0,ScrH, -1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
// de tekenpositie instellen en de tekst afbeelden
glRasterPos2i(X,Y); //een kleine correctie..
PrintText(Value);
//
{glPopAttrib;}
// glPopMatrix;
end;
procedure TGLFont.PrintLine(Row: integer; const Value: string; VAlign: TLineVAlignment; ScrW, ScrH: integer);
const MarginLeft=2; MarginBottom=2;
var LineHeight, NLines,NLines1, DeltaPx,
X,Y: integer;
begin
// bereken het aantal regels met de huidige viewport dimensies
LineHeight := SizePx + Round(DefLineSpacing);
NLines := ScrH div LineHeight;
DeltaPx := ScrH - (NLines * LineHeight);
NLines1 := NLines - 1;
// regel nog in beeld?
if (Row<0) or (Row>NLines1) then Exit;
// positie vanaf links
X := MarginLeft;
// regel vanaf boven? of onder?..
case VAlign of
laTop : Y := (NLines1-Row)*LineHeight + DeltaPx;
laBottom : Y := Row*LineHeight;
end;
Y := Y + MarginBottom;
PrintXY(X,Y, Value, ScrW,ScrH);
end;
procedure TGLFont.PrintXYZ(X, Y, Z: GLfloat; const Value: string);
begin
glPushMatrix;
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
//
glRasterPos3f(X,Y,Z);
PrintText(Value);
glPopMatrix;
end;
{--- TGLFonts -----------------------------------}
constructor TGLFonts.Create;
begin
SetLength(fFonts, 0);
end;
destructor TGLFonts.Destroy;
var f: integer;
begin
for f:=Low(fFonts) to High(fFonts) do
with fFonts[f] do begin
if IsFontLoaded then DeleteFont;
Free;
end;
SetLength(fFonts, 0);
//
inherited;
end;
// property
function TGLFonts.GetFont(I: integer): TGLFont;
begin
Result := nil;
if (I<Low(fFonts)) or (I>High(fFonts)) then Exit;
Result := fFonts[I];
end;
function TGLFonts.GetFontCount: integer;
begin
Result := Length(fFonts);
end;
function TGLFonts.AddFont(DC: HDC; RC: HGLRC; Fontname: string; FontSize: integer; Font3D: boolean): GLuint;
var Len: integer;
begin
Len := Length(fFonts);
SetLength(fFonts, Len+1);
fFonts[Len] := TGLFont.Create;
Result := fFonts[Len].CreateFontDLs(DC,RC, Fontname,FontSize, Font3D);
end;
end.