-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unit_Fonts.pas
489 lines (396 loc) · 13.4 KB
/
Unit_Fonts.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
unit Unit_Fonts;
interface
uses Classes, Controls, Graphics, Math, StrUtils, SysUtils, Windows,
dglOpenGL, OGLUtils,
MyGLBitmapFont, Unit_Color, Unit_Defaults;
const
SIZES_COUNT = 6;
type
TFontProps = record
Color: TColor4c;
Size: Integer;
Style: Integer;
Justify: TAlignment;
end;
TLFontLib = class
private
fFontScale: Single; //Tweak fonts scale to match VCL fonts/layout
fRenderScale: Single; //Viewport scale to design-size (DESK_SIZE_X * DESK_SIZE_Y)
fSizes: array [1..SIZES_COUNT] of Integer;
fLastIndex: Integer;
function ChooseFontSize(aFontSize: Single): Integer;
public
FontStyles: array [0..8, 1..SIZES_COUNT] of TMyBitmapFont;
constructor Create;
destructor Destroy; override;
function AddFont(aFile,aFontName: string; aFontStyle: TFontStyles): Integer;
function GetFont(aStyle: Integer): TFont;
procedure SetFontName(aStyle: Integer; aName: string);
property FontScale: Single read fFontScale write fFontScale;
property RenderScale: Single read fRenderScale write fRenderScale;
function GetCharWidth(aChar: Char; const aFont: TFontProps): Single;
function GetStringWidth(aString: string; const aFont: TFontProps): Single;
function GetStringHeight(const aFont: TFontProps; aString: string = ''): Single;
function RepositionEOLs(const aString: string; const aFont: TFontProps; aWidth,aPadFirst: Integer): string;
procedure SaveToFiles(aPath: String);
procedure Render(x,y,w,h: Integer; s: Single; const aString: string; const aFont: TFontProps; aCutLines: Boolean = True);
procedure RenderV(X,Y,W,H: Integer; aScale: Single; const aString: string; const aFont: TFontProps; aCutLines: Boolean = True);
end;
function MakeFont(aColor: TColor4c; aSize: Integer; aStyle: Integer; aJustify: TAlignment): TFontProps;
var
fFontLib: TLFontLib;
fsArialNormal,
fsArialBold,
fsArialItalic,
fsCourierNormal,
fsCourierBold:integer;
implementation
function MakeFont(aColor: TColor4c; aSize: Integer; aStyle: Integer; aJustify: TAlignment): TFontProps;
begin
Result.Color := aColor;
Result.Size := aSize;
Result.Style := aStyle;
Result.Justify := aJustify;
end;
{ TLFontLib }
constructor TLFontLib.Create;
begin
inherited;
fLastIndex := -1;
fFontScale := 1;
fRenderScale := 1;
fSizes[1] := 8;
fSizes[2] := 9;
fSizes[3] := 10;
fSizes[4] := 12;
fSizes[5] := 24;
fSizes[6] := 36;
fsArialNormal := AddFont('', 'Tahoma', []);
fsArialBold := AddFont('', 'Tahoma', [fsBold]);
end;
destructor TLFontLib.Destroy;
var
i:integer;
k:integer;
begin
for i:=low(FontStyles) to high(FontStyles) do
for k:=1 to SIZES_COUNT do
if FontStyles[i,k] <> nil then
FontStyles[i,k].Free;
//RemoveFontResourceEx(PChar(ExeDir+'Vegas Desert.ttf'), FR_PRIVATE, nil);
inherited;
end;
function TLFontLib.AddFont(aFile,aFontName: String; aFontStyle:TFontStyles):integer;
var k:integer;
begin
//Load font from file
if aFile<>'' then begin
if not FileExists(aFile) then begin
MessageBox(0, PChar('Font file could not be found:'+eol+aFile), 'Error', MB_ICONEXCLAMATION or MB_OK);
Result := -1;
exit;
end;
k := AddFontResourceEx(PChar(aFile), FR_PRIVATE, nil);
if k < 1 then begin
MessageBox(0, PChar('Font file could not be loaded:'+eol+aFile), 'Error', MB_ICONEXCLAMATION or MB_OK);
Result := -1;
exit;
end;
end;
Result := fLastIndex + 1;
//SetLength(FontStyles, Result+1);
for k:=1 to SIZES_COUNT do
begin
FontStyles[Result,k] := TMyBitmapFont.Create;
FontStyles[Result,k].Font.Name := aFontName;
FontStyles[Result,k].Font.Charset := DEFAULT_CHARSET;
FontStyles[Result,k].Font.Style := aFontStyle;
FontStyles[Result,k].Font.Size := fSizes[k];
FontStyles[Result,k].Ranges.Clear;
FontStyles[Result,k].Ranges.Add( #32, #125);
FontStyles[Result,k].Ranges.Add(#129, #129);
FontStyles[Result,k].Ranges.Add(#171);
FontStyles[Result,k].Ranges.Add(#184);
FontStyles[Result,k].Ranges.Add(#185);
FontStyles[Result,k].Ranges.Add(#187);
FontStyles[Result,k].Ranges.Add(#192, #255);
FontStyles[Result,k].PrepareImage;
end;
inc(fLastIndex);
end;
function TLFontLib.GetFont(aStyle:integer):TFont;
begin
Result := FontStyles[aStyle,1].Font;
end;
procedure TLFontLib.SetFontName(aStyle:integer; aName: String);
var k:integer;
begin
for k:=1 to SIZES_COUNT do begin
FontStyles[aStyle,k].Font.Name := aName;
FontStyles[aStyle,k].PrepareImage;
end;
end;
//Choose best font size depending on render scale
//(bigger scale requires smoother fonts) and font scale
function TLFontLib.ChooseFontSize(aFontSize: Single):integer;
var k:integer;
begin
Result := 1;
for k:=1 to SIZES_COUNT do
if aFontSize*fRenderScale*fFontScale >= fSizes[k] then
Result := k;
end;
function TLFontLib.GetCharWidth(aChar: Char; const aFont:TFontProps): Single;
var
F: TMyBitmapFont;
begin
Result := 0;
F := FontStyles[aFont.Style, ChooseFontSize(aFont.Size)];
if F = nil then exit;
//fRenderScale is not required, since it's already commited to render area size
Result := F.CalcCharWidth(AnsiChar(aChar)) * (aFont.Size/F.Font.Size) * fFontScale;
end;
function TLFontLib.GetStringWidth(aString: string; const aFont: TFontProps): Single;
var
Fnt: TMyBitmapFont;
SL: TStringList;
I: Integer;
begin
Result := 0;
Fnt := FontStyles[aFont.Style, ChooseFontSize(aFont.Size)];
if Fnt = nil then exit;
SL := TStringList.Create;
SL.Text := aString;
//fRenderScale is not required, since it's already commited to render area size
for I := 0 to SL.Count - 1 do
Result := max(Result, Fnt.CalcStringWidth(AnsiString(SL[I])) * (aFont.Size/Fnt.Font.Size) * fFontScale);
SL.Free;
end;
function TLFontLib.GetStringHeight(const aFont: TFontProps; aString: string = ''): Single;
var
F: TMyBitmapFont;
SL: TStringList;
begin
Result := 1;
F := FontStyles[aFont.Style, ChooseFontSize(aFont.Size)];
if F = nil then exit;
//fRenderScale is not required, since it's already commited to render area size
Result := F.CharHeight * (aFont.Size / F.Font.Size) * fFontScale;
if aString = '' then exit;
SL := TStringList.Create;
SL.Text := aString;
Result := Result * SL.Count; //todo: Tweak line spacing
SL.Free;
end;
function TLFontLib.RepositionEOLs(const aString: string; const aFont:TFontProps; aWidth,aPadFirst:Integer): string;
var
s: string;
LineLen: single;
LastSpace,LineCount,I: Integer;
begin
if aWidth = 0 then exit; //Avoid endless loops
//Clear old EOLs
s := StringReplace(aString, eol, ' ', [rfReplaceAll]);
s := TrimRight(s);
LineLen := 0;
LastSpace := 0;
LineCount := 1;
I := 1;
if Length(s) > 0 then
repeat
LineLen := LineLen + GetCharWidth(s[I], aFont);
if (((LineCount=1)and(LineLen > aWidth-aPadFirst))or(LineLen > aWidth)) then
begin
//If there are no spaces or line-breaks in the line - just cut it
if LastSpace=0 then LastSpace := I;
Insert(eol, s, LastSpace);
LineLen := 0;
I := LastSpace;
LastSpace := 0;
Inc(LineCount);
end;
if (s[I] = ' ') or (s[I] = '-') then LastSpace := I;
Inc(I);
until(I > Length(s));
Result := s;
end;
procedure TLFontLib.SaveToFiles(aPath: String);
var
i:integer;
k:integer;
Caption: String;
begin
for i:=low(FontStyles) to high(FontStyles) do
for k:=1 to SIZES_COUNT do
if FontStyles[i,k] <> nil then
begin
Caption := FontStyles[i,k].Font.Name;
if fsBold in FontStyles[i,k].Font.Style then Caption := Caption + '_Bold';
if fsItalic in FontStyles[i,k].Font.Style then Caption := Caption + '_Italic';
Caption := Caption + '_' + inttostr(FontStyles[i,k].Font.Size);
FontStyles[i,k].SaveToFile(aPath+Caption+'.bmp');
end;
end;
//X,Y defines top-left
//W,H defines bounding box
//aCutLines will trim the line and add '...' to the end
procedure TLFontLib.Render(x,y,w,h:integer; s: Single; const aString: string; const aFont:TFontProps; aCutLines:boolean=true);
var
F: TMyBitmapFont;
TxWidth,TxHeight: Single;
StringList: TStringList;
I: Integer;
begin
F := FontStyles[aFont.Style, ChooseFontSize(aFont.Size*s)];
if F = nil then exit;
if aString = '' then exit;
// aString := aString + '"_pI|,/\';
TxWidth := GetStringWidth(aString, aFont);
TxHeight := GetStringHeight(aFont, aString);
glPushMatrix;
glTranslatef(x,y,0);
if SHOW_TEXT_BOUNDS then
begin
glColor4f(1,0,0,0.5);
glPushMatrix;
glTranslatef(0.5,0.5,0);
glBegin(GL_LINE_LOOP);
glVertex2f(0,3);
glVertex2f(3,0);
glVertex2f(0,-3);
glVertex2f(-3,0);
glEnd;
glBegin(GL_LINE_LOOP);
glVertex2f(0,0);
glVertex2f(w,0);
glVertex2f(w,h);
glVertex2f(0,h);
glEnd;
glPopMatrix;
end;
glRotatef(180, 1, 0, 0);
glTranslatef(0,-Round((h-TxHeight)/2),0);
case aFont.Justify of
taLeftJustify: glTranslatef(0,0,0);
taCenter: glTranslatef(Round((w-TxWidth)/2),0,0);
taRightJustify: glTranslatef(w-TxWidth,0,0);
end;
if SHOW_TEXT_BOUNDS then
begin
glColor4f(1,0,0,0.5);
glBegin(GL_LINE_LOOP);
glVertex2f(0.5,-0.5);
glVertex2f(TxWidth-0.5,-0.5);
glVertex2f(TxWidth-0.5,-TxHeight+0.5);
glVertex2f(0.5,-TxHeight+0.5);
glEnd;
end;
StringList := TStringList.Create;
StringList.Text := aString;
for I := 0 to StringList.Count - 1 do
begin
glPushMatrix;
if (w <> 0) and aCutLines then
begin
StringList[I] := Trim(StringList[I]);
if (GetStringWidth(StringList[I], aFont) > w) then
if (GetStringWidth('...', aFont) < w) then
StringList[I] := LeftStr(StringList[I], Round(Length(StringList[I]) / GetStringWidth(StringList[I], aFont) * w) - 3) + '...'
else
StringList[I] := '';
end;
case aFont.Justify of
taLeftJustify: ;
taCenter: glTranslatef(Round(TxWidth-GetStringWidth(StringList[I],aFont)) div 2,0,0);
taRightJustify: glTranslatef(Round(TxWidth-GetStringWidth(StringList[I],aFont)),0,0);
end;
glPushMatrix;
glkScale(fFontScale * (aFont.Size/F.Font.Size));
F.RenderString(AnsiString(StringList[I]), aFont.Color);
glPopMatrix;
glPopMatrix;
glTranslatef(0, -Round(GetStringHeight(aFont)), 0);
end;
glPopMatrix;
StringList.Free;
end;
//X,Y defines top-left
//W,H defines bounding box
procedure TLFontLib.RenderV(X,Y,W,H: Integer; aScale: Single; const aString: string; const aFont: TFontProps; aCutLines: Boolean = True);
var
F:TMyBitmapFont;
TxWidth,TxHeight: Single;
StringList:TStringList;
i:integer;
begin
F := FontStyles[aFont.Style, ChooseFontSize(aFont.Size*aScale)];
if F = nil then exit;
if aString = '' then exit;
TxWidth := GetStringWidth(aString, aFont);
TxHeight := GetStringHeight(aFont, aString);
glPushMatrix;
glTranslatef(X,Y,0);
if SHOW_TEXT_BOUNDS then begin
glColor4f(1,0,0,0.5);
glPushMatrix;
glTranslatef(0.5,0.5,0);
glBegin(GL_LINE_LOOP);
glVertex2f(0,3);
glVertex2f(3,0);
glVertex2f(0,-3);
glVertex2f(-3,0);
glEnd;
glBegin(GL_LINE_LOOP);
glVertex2f(0,0);
glVertex2f(W,0);
glVertex2f(W,H);
glVertex2f(0,H);
glEnd;
glPopMatrix;
end;
glRotatef(180, 1, 0, 0);
glRotatef(90, 0, 0, 1);
glTranslatef(0,-(W-TxHeight)/2,0);
case aFont.Justify of
taLeftJustify: glTranslatef(-H,0,0);
taCenter: glTranslatef(-(H-TxWidth)/2,0,0);
taRightJustify: glTranslatef(0,0,0);
end;
if SHOW_TEXT_BOUNDS then begin
glColor4f(1,0,0,0.5);
glPushMatrix;
glTranslatef(0.5,0.5,0);
glBegin(GL_LINE_LOOP);
glVertex2f(0,0);
glVertex2f(TxWidth,0);
glVertex2f(TxWidth,-TxHeight);
glVertex2f(0,-TxHeight);
glEnd;
glPopMatrix;
end;
StringList := TStringList.Create;
StringList.Text := aString;
for i:=0 to StringList.Count-1 do begin
glPushMatrix;
if aCutLines then begin
StringList[i] := Trim(StringList[i]);
if (H<>0) and (GetStringWidth(StringList[i], aFont) >= H) then
StringList[i] := LeftStr(StringList[i], round(length(StringList[i])/GetStringWidth(StringList[i], aFont)*H)-3)+'...';
end;
case aFont.Justify of
taLeftJustify: ;
taCenter: glTranslatef((TxWidth-GetStringWidth(StringList[i],aFont))/2,0,0);
taRightJustify: glTranslatef((TxWidth-GetStringWidth(StringList[i],aFont)),0,0);
end;
glPushMatrix;
glkScale(fFontScale * (aFont.Size/F.Font.Size));
F.RenderString(AnsiString(StringList[i]), aFont.Color);
glPopMatrix;
glPopMatrix;
glTranslatef(0, -GetStringHeight(aFont), 0);
end;
glPopMatrix;
glEnable(GL_TEXTURE_2D); //GLScene text renderer disables it!
StringList.Free;
end;
end.