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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
unit sdltruetypefont;
{
$Id: sdltruetypefont.pas,v 1.5 2005/05/26 21:22:28 savage Exp $
}
{******************************************************************************}
{ }
{ JEDI-SDL : Pascal units for SDL - Simple DirectMedia Layer }
{ Wrapper class for SDL_ttf }
{ }
{ The initial developer of this Pascal code was : }
{ Dominqiue Louis <Dominique@SavageSoftware.com.au> }
{ }
{ Portions created by Dominqiue Louis are }
{ Copyright (C) 2000 - 2001 Dominqiue Louis. }
{ }
{ }
{ Contributor(s) }
{ -------------- }
{ }
{ }
{ Obtained through: }
{ Joint Endeavour of Delphi Innovators ( Project JEDI ) }
{ }
{ You may retrieve the latest version of this file at the Project }
{ JEDI home page, located at http://delphi-jedi.org }
{ }
{ The contents of this file are used with permission, subject to }
{ the Mozilla Public License Version 1.1 (the "License"); you may }
{ not use this file except in compliance with the License. You may }
{ obtain a copy of the License at }
{ http://www.mozilla.org/MPL/MPL-1.1.html }
{ }
{ Software distributed under the License is distributed on an }
{ "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
{ implied. See the License for the specific language governing }
{ rights and limitations under the License. }
{ }
{ Description }
{ ----------- }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ Requires }
{ -------- }
{ The SDL Runtime libraris on Win32 : SDL.dll on Linux : libSDL.so }
{ They are available from... }
{ http://www.libsdl.org . }
{ }
{ Programming Notes }
{ ----------------- }
{ }
{ }
{ }
{ }
{ Revision History }
{ ---------------- }
{ September 23 2004 - DL : Initial Creation }
{
$Log: sdltruetypefont.pas,v $
Revision 1.5 2005/05/26 21:22:28 savage
Update to Input code.
Revision 1.1 2005/05/25 23:15:42 savage
Latest Changes
Revision 1.4 2005/05/25 22:55:01 savage
Added InputRect support.
Revision 1.3 2005/05/13 14:02:49 savage
Made it use UniCode rendering by default.
Revision 1.2 2005/05/13 11:37:52 savage
Improved wordwrapping algorithm
Revision 1.1 2004/09/30 22:39:50 savage
Added a true type font class which contains a wrap text function.
Changed the sdl_ttf.pas header to reflect the future of jedi-sdl.
}
{******************************************************************************}
interface
uses
sdl,
sdl_ttf;
type
TRenderType = ( rtLatin1, rtUTF8, rtUnicode );
TSDLFontStyle = ( fsBold, fsItalic, fsUnderline, fsStrikeOut );
TSDLFontStyles = set of TSDLFontStyle;
TTrueTypeFont = class( TObject )
private
FFont : PTTF_Font;
FSolid : Boolean;
FBackGroundColour : TSDL_Color;
FForeGroundColour : TSDL_Color;
FRenderType : TRenderType;
FStyle : TSDLFontStyles;
FFontFile : string;
FFontSize : integer;
procedure PrepareFont;
protected
public
constructor Create( aFontFile : string; aRenderStyle : TSDLFontStyles = [ ]; aFontSize : integer = 14 );
destructor Destroy; override;
function DrawText( aText : WideString ) : PSDL_Surface; overload;
function DrawText( aText : WideString; aWidth, aHeight : Integer ) : PSDL_Surface; overload;
function Input(aDestination: PSDL_Surface; aX, aY, aWidth, aHeight: integer; var aText: string; aMaxChars: integer = 10 ): PSDL_Surface;
property BackGroundColour : TSDL_Color read FBackGroundColour write FBackGroundColour;
property ForeGroundColour : TSDL_Color read FForeGroundColour write FForeGroundColour;
property FontFile : string read FFontFile write FFontFile;
property RenderType : TRenderType read FRenderType write FRenderType;
property Solid : Boolean read FSolid write FSolid;
property Style : TSDLFontStyles read FStyle write FStyle;
property FontSize : integer read FFontSize write FFontSize;
end;
implementation
uses
SysUtils;
{ TTrueTypeFont }
constructor TTrueTypeFont.Create( aFontFile : string; aRenderStyle : TSDLFontStyles; aFontSize : integer );
begin
inherited Create;
if FileExists( aFontFile ) then
begin
FStyle := aRenderStyle;
FFontSize := aFontSize;
FSolid := false;
FBackGroundColour.r := 255;
FBackGroundColour.g := 255;
FBackGroundColour.b := 255;
FForeGroundColour.r := 0;
FForeGroundColour.g := 0;
FForeGroundColour.b := 0;
FRenderType := rtUnicode;
if ( TTF_Init >= 0 ) then
begin
FFontFile := aFontFile;
end
else
raise Exception.Create( 'Failed to Initialiase SDL_TTF' );
end
else
raise Exception.Create( 'Font File does not exist' );
end;
destructor TTrueTypeFont.Destroy;
begin
if FFont <> nil then
TTF_CloseFont( FFont );
TTF_Quit;
inherited;
end;
function TTrueTypeFont.DrawText( aText : WideString ) : PSDL_Surface;
begin
PrepareFont;
result := nil;
case FRenderType of
rtLatin1 :
begin
if ( FSolid ) then
begin
result := TTF_RenderText_Solid( FFont, PChar( string( aText ) ), FForeGroundColour );
end
else
begin
result := TTF_RenderText_Shaded( FFont, PChar( string( aText ) ), FForeGroundColour, FBackGroundColour );
end;
end;
rtUTF8 :
begin
if ( FSolid ) then
begin
result := TTF_RenderUTF8_Solid( FFont, PChar( string( aText ) ), FForeGroundColour );
end
else
begin
result := TTF_RenderUTF8_Shaded( FFont, PChar( string( aText ) ), FForeGroundColour, FBackGroundColour );
end;
end;
rtUnicode :
begin
if ( FSolid ) then
begin
result := TTF_RenderUNICODE_Solid( FFont, PUInt16( aText ), FForeGroundColour );
end
else
begin
result := TTF_RenderUNICODE_Shaded( FFont, PUInt16( aText ), FForeGroundColour, FBackGroundColour );
end;
end;
end;
end;
function TTrueTypeFont.DrawText( aText : WideString; aWidth, aHeight : Integer ) : PSDL_Surface;
var
textw, texth, i, yPos : integer;
strChopped : WideString;
SurfaceList : array of PSDL_Surface;
strlist : array of WideString;
ReturnedSurface : PSDL_Surface;
BltRect : TSDL_Rect;
begin
PrepareFont;
// Do an initial check to see if it already fits
case FRenderType of
rtLatin1 :
begin
if TTF_SizeText( FFont, PChar( string( aText ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
result := DrawText( aText );
exit;
end
end;
end;
rtUTF8 :
begin
if TTF_SizeUTF8( FFont, PChar( string( aText ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
result := DrawText( aText );
exit;
end
end;
end;
rtUnicode :
begin
if TTF_SizeUNICODE( FFont, PUInt16( aText ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
result := DrawText( aText );
exit;
end
end;
end;
end;
// Create the Surface we will be returning
ReturnedSurface := SDL_DisplayFormat( SDL_CreateRGBSurface( SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, aWidth, aHeight, 16, 0, 0, 0, 0 ) );
// If we are still here there is some serious parsing to do
case FRenderType of
rtLatin1 :
begin
strChopped := aText;
i := Length( strChopped );
while ( i <> 0 ) do
begin
if ( string( strChopped[ i ] ) <> ' ' ) and ( Integer( string( strChopped[ i ] ) ) <> 13 ) then
dec( i )
else
begin
dec( i );
if TTF_SizeText( FFont, PChar( string( Copy( strChopped, 0, i ) ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
SetLength( strlist, Length( strlist ) + 1 );
strlist[ Length( strlist ) - 1 ] := Copy( strChopped, 0, i );
strChopped := Copy( strChopped, i + 2, Length( strChopped ) - ( i - 1 ) );
i := Length( strChopped );
if TTF_SizeText( FFont, PChar( string( strChopped ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
SetLength( strlist, Length( strlist ) + 1 );
strlist[ Length( strlist ) - 1 ] := Copy( strChopped, 0, i );
strChopped := Copy( strChopped, i + 2, Length( strChopped ) - ( i - 1 ) );
i := Length( strChopped );
end;
end;
end;
end;
end;
end;
SetLength( SurfaceList, Length( strlist ) );
for i := Low( strlist ) to High( strlist ) do
begin
if ( FSolid ) then
begin
SurfaceList[ i ] := TTF_RenderText_Solid( FFont, PChar( string( strlist[ i ] ) ), FForeGroundColour );
end
else
begin
SurfaceList[ i ] := TTF_RenderText_Shaded( FFont, PChar( string( strlist[ i ] ) ), FForeGroundColour, FBackGroundColour );
end;
end;
end;
rtUTF8 :
begin
strChopped := aText;
i := Length( strChopped );
while ( i <> 0 ) do
begin
if ( string( strChopped[ i ] ) <> ' ' ) and ( Integer( string( strChopped[ i ] ) ) <> 13 ) then
dec( i )
else
begin
dec( i );
if TTF_SizeUTF8( FFont, PChar( string( Copy( strChopped, 0, i ) ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
SetLength( strlist, Length( strlist ) + 1 );
strlist[ Length( strlist ) - 1 ] := Copy( strChopped, 0, i );
strChopped := Copy( strChopped, i + 2, Length( strChopped ) - ( i - 1 ) );
i := Length( strChopped );
if TTF_SizeUTF8( FFont, PChar( string( strChopped ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
SetLength( strlist, Length( strlist ) + 1 );
strlist[ Length( strlist ) - 1 ] := Copy( strChopped, 0, i );
strChopped := Copy( strChopped, i + 2, Length( strChopped ) - ( i - 1 ) );
i := Length( strChopped );
end;
end;
end;
end;
end;
end;
SetLength( SurfaceList, Length( strlist ) );
for i := Low( strlist ) to High( strlist ) do
begin
if ( FSolid ) then
begin
SurfaceList[ i ] := TTF_RenderUTF8_Solid( FFont, PChar( string( strlist[ i ] ) ), FForeGroundColour );
end
else
begin
SurfaceList[ i ] := TTF_RenderUTF8_Shaded( FFont, PChar( string( strlist[ i ] ) ), FForeGroundColour, FBackGroundColour );
end;
end;
end;
rtUnicode :
begin
strChopped := aText;
i := Length( strChopped );
while ( i <> 0 ) do
begin
if ( string( strChopped[ i ] ) <> ' ' ) and ( Integer( string( strChopped[ i ] ) ) <> 13 ) then
dec( i )
else
begin
dec( i );
if TTF_SizeUNICODE( FFont, PUInt16( Copy( strChopped, 0, i ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
SetLength( strlist, Length( strlist ) + 1 );
strlist[ Length( strlist ) - 1 ] := Copy( strChopped, 0, i );
strChopped := Copy( strChopped, i + 2, Length( strChopped ) - ( i - 1 ) );
i := Length( strChopped );
if TTF_SizeUNICODE( FFont, PUInt16( strChopped ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
SetLength( strlist, Length( strlist ) + 1 );
strlist[ Length( strlist ) - 1 ] := Copy( strChopped, 0, i );
strChopped := Copy( strChopped, i + 2, Length( strChopped ) - ( i - 1 ) );
i := Length( strChopped );
end;
end;
end;
end;
end;
end;
SetLength( SurfaceList, Length( strlist ) );
for i := Low( strlist ) to High( strlist ) do
begin
if ( FSolid ) then
begin
SurfaceList[ i ] := TTF_RenderUNICODE_Solid( FFont, PUInt16( strlist[ i ] ), FForeGroundColour );
end
else
begin
SurfaceList[ i ] := TTF_RenderUNICODE_Shaded( FFont, PUInt16( strlist[ i ] ), FForeGroundColour, FBackGroundColour );
end;
end;
end;
end;
// Now Draw the SurfaceList onto the resulting Surface
yPos := 6;
for i := Low( SurfaceList ) to High( SurfaceList ) do
begin
BltRect.x := 6;
BltRect.y := yPos;
BltRect.w := SurfaceList[ i ].w;
BltRect.h := SurfaceList[ i ].h;
SDL_BlitSurface( SurfaceList[ i ], nil, ReturnedSurface, @BltRect );
yPos := yPos + TTF_FontHeight( FFont );
end;
result := ReturnedSurface;
for i := Low( SurfaceList ) to High( SurfaceList ) do
begin
SDL_FreeSurface( SurfaceList[ i ] );
end;
SetLength( SurfaceList, 0 );
SetLength( strlist, 0 );
end;
function TTrueTypeFont.Input(aDestination: PSDL_Surface; aX, aY, aWidth: integer; aHeight : integer; var aText : string; aMaxChars: integer): PSDL_Surface;
var
event : TSDL_Event;
ch : integer;
BackSurface, TextSurface : PSDL_Surface;
rect : SDL_Rect;
textw, texth : integer;
Done : boolean;
PassedInText : string;
begin
PassedInText := aText;
BackSurface := SDL_AllocSurface( aDestination.flags,
aDestination.w,
aDestination.h,
aDestination.format.BitsPerPixel,
aDestination.format.Rmask,
aDestination.format.Gmask,
aDestination.format.Bmask, 0 );
rect.x := aX;
rect.y := aY;
rect.w := aWidth;
rect.h := aHeight;
SDL_BlitSurface( aDestination, nil, BackSurface, nil );
SDL_FillRect( BackSurface, @rect, SDL_MapRGB( aDestination.format, 0, 0, 0 ) );
TextSurface := DrawText( aText + '|' );
// start input
SDL_EnableUNICODE( 1 );
Done := false;
while ( not Done ) and ( SDL_WaitEvent( @event ) > 0 ) do
begin
if event.type_ = SDL_KEYDOWN then
begin
ch := event.key.keysym.unicode;
case ch of
SDLK_RETURN :
begin
Done := true;
end;
SDLK_ESCAPE :
begin
aText := PassedInText;
Done := true;
end;
SDLK_BACKSPACE :
begin
if ( Length( aText ) > 0 ) then
begin
aText := Copy( aText, 0, Length( aText ) - 1 );
if TextSurface <> nil then
SDL_FreeSurface( TextSurface );
TextSurface := DrawText( aText + '|' );
end;
end;
else
begin
if Length( aText ) < aMaxChars then
begin
if ( chr( ch ) <> '' ) then
begin
aText := aText + chr( ch );
if ( aText <> '' )
and ( TTF_SizeUNICODE( FFont, PUInt16( aText ), textw, texth ) = 0 ) then
begin
if ( textw > aWidth ) then
aText := Copy( aText, 0, Length( aText ) - 1 );
end;
if TextSurface <> nil then
SDL_FreeSurface( TextSurface );
TextSurface := DrawText( aText + '|' );
end;
end;
end;
end;
end;
SDL_BlitSurface( BackSurface, nil, aDestination, nil );
SDL_BlitSurface( TextSurface, nil, aDestination, @rect );
SDL_Flip( aDestination );
end;
if TextSurface <> nil then
SDL_FreeSurface( TextSurface );
if aText <> '' then
TextSurface := DrawText( aText );
SDL_FreeSurface( BackSurface );
result := TextSurface;
end;
procedure TTrueTypeFont.PrepareFont;
var
renderstyle : integer;
begin
if FFont <> nil then
TTF_CloseFont( FFont );
FFont := TTF_OpenFont( PChar( FFontFile ), FFontSize );
renderstyle := TTF_STYLE_NORMAL;
if ( fsBold in FStyle ) then
renderstyle := renderstyle or TTF_STYLE_BOLD;
if ( fsItalic in FStyle ) then
renderstyle := renderstyle or TTF_STYLE_ITALIC;
if ( fsUnderline in FStyle ) then
renderstyle := renderstyle or TTF_STYLE_UNDERLINE;
TTF_SetFontStyle( FFont, renderstyle );
end;
end.
|