/
sgText.pas
1286 lines (1073 loc) · 53 KB
/
sgText.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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=============================================================================
// sgText.pas
//=============================================================================
//
// The Font unit relates to writing text to the screen,
// and to loading and styling the associated fonts.
//
// Change History:
//
// Version 3.0:
// - 2012-01-05: Aaron : changed the code to support using text drivers.
// - 2010-03-18: Andtew : Altered font loading to provide mappings for all fonts.
// - 2010-02-02: Andrew : Added string to FontAlignment
// - 2009-12-07: Andrew : Added font loading and freeing code..
// - 2009-06-05: Andrew : Using sgShared
//
// Version 2.0:
// - 2009-07-14: Andrew : Removed loading and freeing code.
// - 2009-01-05: Andrew : Added Unicode rendering
// - 2008-12-17: Andrew : Moved all integers to Longint
// - 2008-12-12: Andrew : Added simple string printing
// - 2008-12-10: Andrew : Fixed printing of string
//
// Version 1.1:
// - 2008-03-09: Andrew : Added extra exception handling
// - 2008-01-30: Andrew : Fixed Print strings for EOL as last char
// - 2008-01-25: Andrew : Fixed compiler hints
// - 2008-01-21: Andrew : Added Point/Rectangle overloads
// - 2008-01-17: Aki + Andrew: Refactor
//
// Version 1.0:
// - Various
//=============================================================================
/// Supports the presentation (drawing) of text to screen using loaded `Font`s
/// to style the text. Load a different ``Font`` for each unique text
/// presentation style (colour and size) you need in your game.
///
///@module Text
///@static
unit sgText;
//=============================================================================
interface
uses sgTypes;
//=============================================================================
//----------------------------------------------------------------------------
// Font loading routines
//----------------------------------------------------------------------------
/// Loads a font from file with the specified side. Fonts must be freed using
/// the FreeFont routine once finished with. Once the font is loaded you
/// can set its style using SetFontStyle. Fonts are then used to draw and
/// measure text in your programs.
///
/// @lib
/// @sn loadFontFile:%s size:%s
///
/// @class Font
/// @constructor
/// @csn initWithFontName:%s andSize:%s
function LoadFont(fontName: String; size: Longint): Font;
/// Frees the resources used by the loaded Font.
///
/// @lib
///
/// @class Font
/// @dispose
procedure FreeFont(var fontToFree: Font);
/// Loads and returns a font that can be used to draw text. The supplied
/// ``filename`` is used to locate the font to load. The supplied ``name`` indicates the
/// name to use to refer to this Font in SwinGame. The `Font` can then be
/// retrieved by passing this ``name`` to the `FontNamed` function.
///
/// @lib
/// @sn loadFontNamed:%s fromFile:%s size:%s
///
/// @class Font
/// @constructor
/// @csn initWithName:%s fromFile:%s size:%s
function LoadFontNamed(name, filename: String; size: Longint): Font;
/// Determines if SwinGame has a font loaded for the supplied name.
/// This checks against all fonts loaded, those loaded without a name
/// are assigned the filename as a default.
///
/// @lib
function HasFont(name: String): Boolean;
/// Determines the name that will be used for a font loaded with
/// the indicated fontName and size.
///
/// @lib
/// @sn fontName:%s size:%s
function FontNameFor(fontName: String; size: Longint): String;
/// Returns the `Font` that has been loaded with the specified name,
/// and font size using `LoadFont`.
///
/// @lib FontNamedWithSize
/// @sn fontNamed:%s withSize:%s
function FontNamed(name: String; size: Longint): Font; overload;
/// Returns the `Font` that has been loaded with the specified name,
/// see `LoadFontNamed`.
///
/// @lib
function FontNamed(name: String): Font; overload;
/// Releases the SwinGame resources associated with the font of the
/// specified ``name``.
///
/// @lib
procedure ReleaseFont(name: String);
/// Releases all of the fonts that have been loaded.
///
/// @lib
procedure ReleaseAllFonts();
//---------------------------------------------------------------------------
// Font properties
//---------------------------------------------------------------------------
/// Alters the style of the font. This is time consuming, so load
/// fonts multiple times and set the style for each if needed.
///
/// @lib
/// @sn font:%s setStyle:%s
///
/// @class Font
/// @setter FontStyle
procedure FontSetStyle(font: Font; value: FontStyle);
/// Returns the style settings for the font.
///
/// @lib
///
/// @class Font
/// @getter FontStyle
function FontFontStyle(font: Font): FontStyle;
/// Returns the width (in pixels) of the passed in text and the font it will be drawn with.
///
/// @lib
/// @sn font:%s widthOf:%s
///
/// @class Font
/// @method TextWidth
function TextWidth(theFont: Font; theText: String): Longint; overload;
/// Returns the height (in pixels) of the passed in text and the font it will be drawn with.
///
/// @lib
/// @sn font:%s heightOf:%s
///
/// @class Font
/// @method TextHeight
function TextHeight(theFont: Font; theText: String): Longint; overload;
{
function TextWidth(theText: WideString; theFont: Font): Longint; overload;
function TextHeight(theText: WideString; theFont: Font): Longint; overload;
}
/// Returns the font alignment for the passed in character (l = left. r = right, c = center).
///
/// @lib
function TextAlignmentFrom(str: String): FontAlignment;
//---------------------------------------------------------------------------
// Draw Text - using font
//---------------------------------------------------------------------------
/// Draws the text at the specified point using the color and font indicated.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib
/// @sn drawText:%s color:%s font:%s x:%s y:%s
procedure DrawText(theText: String; textColor: Color; theFont: Font; x, y: Single); overload;
/// Draws the text at the specified point using the color and font indicated.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextWithFontNamed
/// @sn drawText:%s color:%s fontNamed:%s x:%s y:%s
procedure DrawText(theText: String; textColor: Color; name: String; x, y: Single); overload;
/// Draws theText at the specified point using the color and font indicated.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextWithFontNamedSize
/// @sn drawText:%s color:%s fontNamed:%s size:%s x:%s y:%s
procedure DrawText(theText: String; textColor: Color; name: String; size: Longint; x, y: Single); overload;
/// Draws the text at the specified point using the color and font indicated.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextAtPoint
/// @sn drawText:%s color:%s font:%s pt:%s
procedure DrawText(theText: String; textColor: Color; theFont: Font; const pt: Point2D); overload;
/// Draws the text at the specified point using the color and font indicated.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextAtPointWithFontNamed
/// @sn drawText:%s color:%s fontNamed:%s pt:%s
procedure DrawText(theText: String; textColor: Color; name: String; const pt: Point2D); overload;
// Draws the text at the specified point using the color and font indicated.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextAtPointWithFontNamedAndSize
/// @sn drawText:%s color:%s fontNamed:%s size:%s pt:%s
procedure DrawText(theText: String; textColor: Color; name: String; size: Longint; const pt: Point2D); overload;
/// Draws the text onto the bitmap at the specified x,y location using the color and font indicated.
/// Drawing text is a slow operation, and drawing it to a bitmap, then drawing the bitmap to screen is a
/// good idea if the text does not change frequently.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextOnBitmap
/// @sn bitmap:%s drawText:%s color:%s font:%s atX:%s y:%s
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; theFont: Font; x, y: Longint); overload;
/// Draws the text onto the bitmap at the specified x,y location using the color and font indicated.
/// Drawing text is a slow operation, and drawing it to a bitmap, then drawing the bitmap to screen is a
/// good idea if the text does not change frequently.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextOnBitmapWithFontNamed
/// @sn bitmap:%s drawText:%s color:%s fontNamed:%s atX:%s y:%s
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; name: String; x, y: Longint); overload;
/// Draws the text onto the bitmap at the specified x,y location using the color and font indicated.
/// Drawing text is a slow operation, and drawing it to a bitmap, then drawing the bitmap to screen is a
/// good idea if the text does not change frequently.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextOnBitmapWithFontNamedAndSize
/// @sn bitmap:%s drawText:%s color:%s fontNamed:%s size:%s atX:%s y:%s
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; name: String; size: Longint; x, y: Longint); overload;
/// Draws the text onto the bitmap at the specified point using the color and font indicated.
/// Drawing text is a slow operation, and drawing it to a bitmap, then drawing the bitmap to screen is a
/// good idea if the text does not change frequently.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextOnBitmapAtPoint
/// @sn bitmap:%s drawText:%s color:%s font:%s atPt:%s
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; theFont: Font; const pt: Point2D); overload;
/// Draws the text onto the bitmap at the specified point using the color and font indicated.
/// Drawing text is a slow operation, and drawing it to a bitmap, then drawing the bitmap to screen is a
/// good idea if the text does not change frequently.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextOnBitmapAtPointWithFontNamed
/// @sn bitmap:%s drawText:%s color:%s fontNamed:%s atPt:%s
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; name: String; const pt: Point2D); overload;
/// Draws the text onto the bitmap at the specified point using the color and font indicated.
/// Drawing text is a slow operation, and drawing it to a bitmap, then drawing the bitmap to screen is a
/// good idea if the text does not change frequently.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextOnBitmapAtPointWithFontNamedAndSize
/// @sn bitmap:%s drawText:%s color:%s fontNamed:%s size:%s atPt:%s
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; name: String; size: Longint; const pt: Point2D); overload;
/// Draws the text onto the bitmap at the specified point using the color and font indicated.
/// Drawing text is a slow operation, and drawing it to a bitmap, then drawing the bitmap to screen is a
/// good idea if the text does not change frequently.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLines` procedure.
///
/// @lib DrawTextToBitmapAtPointWithFontNamedAndSize
/// @sn drawTextFont:%s string:%s textColor:%s backgroundColor:%s
function DrawTextTo(font: Font; str: String; clrFg, backgroundColor : Color) : Bitmap;
/// Draws the text onto the screen at the specified x,y location using the color and font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLinesOnScreen` procedure.
///
/// @lib
/// @sn drawText:%s color:%s font:%s onScreenAtX:%s y:%s
procedure DrawTextOnScreen(theText: String; textColor: Color; theFont: Font; x, y: Longint); overload;
/// Draws the text onto the screen at the specified x,y location using the color and font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLinesOnScreen` procedure.
///
/// @lib DrawTextOnScreenWithFontNamed
/// @sn drawText:%s color:%s fontNamed:%s onScreenAtX:%s y:%s
procedure DrawTextOnScreen(theText: String; textColor: Color; name: String; x, y: Longint);
/// Draws the text onto the screen at the specified x,y location using the color and font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLinesOnScreen` procedure.
///
/// @lib DrawTextOnScreenWithFontNamedAndSize
/// @sn drawText:%s color:%s fontNamed:%s size:%s onScreenAtX:%s y:%s
procedure DrawTextOnScreen(theText: String; textColor: Color; name: String; size: Longint; x, y: Longint);
/// Draws theText onto the screen at the specified point using the color and font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLinesOnScreen` procedure.
///
/// @lib DrawTextOnScreenAtPoint
/// @sn drawText:%s color:%s font:%s onScreenAtPt:%s
procedure DrawTextOnScreen(theText: String; textColor: Color; theFont: Font; const pt: Point2D); overload;
/// Draws theText onto the screen at the specified point using the color and font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLinesOnScreen` procedure.
///
/// @lib DrawTextOnScreenAtPointWithFontNamed
/// @sn drawText:%s color:%s fontNamed:%s onScreenAtPt:%s
procedure DrawTextOnScreen(theText: String; textColor: Color; name: String; const pt: Point2D); overload;
/// Draws theText onto the screen at the specified point using the color and font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version only draws a single line of text, to draw text that contains line breaks use the
/// `DrawTextLinesOnScreen` procedure.
///
/// @lib DrawTextOnScreenAtPointWithFontNamedAndSize
/// @sn drawText:%s color:%s fontNamed:%s size:%s onScreenAtPt:%s
procedure DrawTextOnScreen(theText: String; textColor: Color; name: String; size: Longint; const pt: Point2D); overload;
{ procedure DrawUnicodeOnScreen(theText: WideString; textColor: Color; theFont: Font; x, y: Longint); overload;
procedure DrawUnicodeOnScreen(theText: WideString; textColor: Color; theFont: Font; const pt: Point2D); overload;
procedure DrawUnicode(theText: WideString; textColor: Color; theFont: Font; x, y: Single); overload;
procedure DrawUnicode(theText: WideString; textColor: Color; theFont: Font; const pt: Point2D); overload; }
//---------------------------------------------------------------------------
// Draw Text Lines
//---------------------------------------------------------------------------
/// Draws the text at the specified x,y location using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib
/// @sn drawTextLines:%s textColor:%s backColor:%s font:%s align:%s atX:%s y:%s w:%s h:%s
procedure DrawTextLines(theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; x, y: Single; w, h: Longint); overload;
/// Draws the text at the specified x,y location using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib DrawTextLinesWithFontNamed
/// @sn drawTextLines:%s textColor:%s backColor:%s fontNamed:%s align:%s atX:%s y:%s w:%s h:%s
procedure DrawTextLines(theText: String; textColor, backColor: Color; name: String; align: FontAlignment; x, y:Single; w, h: Longint); overload;
/// Draws the text at the specified x,y location using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib DrawTextLinesWithFontNamedAndSize
/// @sn drawTextLines:%s textColor:%s backColor:%s fontNamed:%s size:%s align:%s atX:%s y:%s w:%s h:%s
procedure DrawTextLines(theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; x, y:Single; w, h: Longint); overload;
/// Draws the text in the specified rectangle using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib DrawTextLinesInRect
/// @sn drawTextLines:%s textColor:%s backColor:%s font:%s align:%s inRect:%s
procedure DrawTextLines(theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; const withinRect: Rectangle); overload;
/// Draws the text in the specified rectangle using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib DrawTextLinesInRectWithFontNamed
/// @sn drawTextLines:%s textColor:%s backColor:%s fontNamed:%s align:%s inRect:%s
procedure DrawTextLines(theText: String; textColor, backColor: Color; name: String; align: FontAlignment; const withinRect: Rectangle); overload;
/// Draws theText in the specified rectangle using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib DrawTextLinesInRectWithFontNamedAndSize
/// @sn drawTextLines:%s textColor:%s backColor:%s fontNamed:%s size:%s align:%s inRect:%s
procedure DrawTextLines(theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; const withinRect: Rectangle); overload;
/// Draws the text onto the bitmap at the specified x,y location using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib DrawTextLinesOnBitmap
/// @sn bitmap:%s drawTextLines:%s textColor:%s backColor:%s font:%s align:%s onScreenAtX:%s y:%s w:%s h:%s
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; x, y, w, h: Longint); overload;
/// Draws the text onto the bitmap at the specified x,y location using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib DrawTextLinesOnBitmapWithFontNamed
/// @sn bitmap:%s drawTextLines:%s textColor:%s backColor:%s fontNamed:%s align:%s onScreenAtX:%s y:%s w:%s h:%s
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; name: String; align: FontAlignment; x, y, w, h: Longint); overload;
/// Draws theText onto the bitmap at the specified x,y location using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib DrawTextLinesOnBitmapWithFontNamedAndSize
/// @sn bitmap:%s drawTextLines:%s textColor:%s backColor:%s fontNamed:%s size:%s align:%s onScreenAtX:%s y:%s w:%s h:%s
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; x, y, w, h: Longint); overload;
/// Draws the text onto the bitmap in the rectangle using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib DrawTextLinesInRectOnBitmap
/// @sn bitmap:%s drawTextLines:%s textColor:%s backColor:%s font:%s align:%s in:%s
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; const withinRect: Rectangle); overload;
/// Draws the text onto the bitmap in the rectangle using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib DrawTextLinesInRectOnBitmapWithFontNamed
/// @sn bitmap:%s drawTextLines:%s textColor:%s backColor:%s fontNamed:%s align:%s in:%s
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; name: String; align: FontAlignment; const withinRect: Rectangle); overload;
/// Draws the text onto the bitmap in the rectangle using the fore and back colors, and the font indicated.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawText` procedure.
///
/// @lib DrawTextLinesInRectOnBitmapWithFontNamedAndSize
/// @sn bitmap:%s drawTextLines:%s textColor:%s backColor:%s fontNamed:%s size:%s align:%s in:%s
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; const withinRect: Rectangle); overload;
/// Draws the text onto the screen at the specified x,y location using the fore and back colors, and the font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib
/// @sn drawTextLines:%s textColor:%s backColor:%s font:%s align:%s onScreenAtX:%s y:%s w:%s h:%s
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; x, y, w, h: Longint); overload;
/// Draws the text onto the screen at the specified x,y location using the fore and back colors, and the font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib DrawTextLinesOnScreenWithFontNamed
/// @sn drawTextLines:%s textColor:%s backColor:%s fontNamed:%s align:%s onScreenAtX:%s y:%s w:%s h:%s
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; name: String; align: FontAlignment; x, y, w, h: Longint); overload;
/// Draws the text onto the screen at the specified x,y location using the fore and back colors, and the font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib DrawTextLinesOnScreenWithFontNamedWithSize
/// @sn drawTextLines:%s textColor:%s backColor:%s fontNamed:%s size:%s align:%s onScreenAtX:%s y:%s w:%s h:%s
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; x, y, w, h: Longint); overload;
/// Draws the text onto the screen in the specified rectangle using the fore and back colors, and the font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib DrawTextLinesInRectOnScreen
/// @sn drawTextLines:%s textColor:%s backColor:%s font:%s align:%s onScreenInRect:%s
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; const withinRect: Rectangle); overload;
/// Draws the text onto the screen in the specified rectangle using the fore and back colors, and the font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib DrawTextLinesInRectOnScreenWithFontNamed
/// @sn drawTextLines:%s textColor:%s backColor:%s fontNamed:%s align:%s onScreenInRect:%s
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; name: String; align: FontAlignment; const withinRect: Rectangle); overload;
/// Draws the text onto the screen in the specified rectangle using the fore and back colors, and the font indicated.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// This version should be used to draw text that contains multiple lines, to draw a single line of text
/// use the `DrawTextOnScreen` procedure.
///
/// @lib DrawTextLinesInRectOnScreenWithFontNamedAndSize
/// @sn drawTextLines:%s textColor:%s backColor:%s fontNamed:%s size:%s align:%s onScreenInRect:%s
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; const withinRect: Rectangle); overload;
//---------------------------------------------------------------------------
// Draw Text - without font
//---------------------------------------------------------------------------
/// Draws text using a simple bitmap font that is built into SwinGame.
///
/// @lib DrawSimpleText
/// @sn drawText:%s color:%s x:%s y:%s
procedure DrawText(theText: String; textColor: Color; x, y: Single); overload;
/// Draws text using a simple bitmap font that is built into SwinGame.
///
/// @lib DrawSimpleTextPt
/// @sn drawText:%s color:%s pt:%s
procedure DrawText(theText: String; textColor: Color; const pt: Point2D); overload;
/// Draws text using a simple bitmap font that is built into SwinGame.
/// As the text is draw directly onto the screen the camera location does not effect its position.
/// This is useful for drawing text on a HUD or similar actions.
///
/// @lib DrawSimpleTextOnScreen
/// @sn drawText:%s color:%s onScreenX:%s y:%s
procedure DrawTextOnScreen(theText: String; textColor: Color; x, y: Single); overload;
/// Draws text using a simple bitmap font that is built into SwinGame.
///
/// @lib DrawSimpleTextOnBitmap
/// @sn bitmap:%s drawText:%s color:%s onScreenAtX:%s y:%s
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; x, y: Single); overload;
/// Draws the framerate to the screen using the supplied font.
///
/// @lib
/// @sn drawFramerateAtX:%s y:%s font:%s
procedure DrawFramerate(x, y: Longint; font: Font); overload;
/// Draws the framerate to the screen using the supplied font.
///
/// @lib DrawFramerateFontNamed
/// @sn drawFramerateAtX:%s y:%s fontNamed:%s
procedure DrawFramerate(x, y: Longint; name: String); overload;
/// Draws the framerate to the screen using the supplied font.
///
/// @lib DrawFramerateFontNamedSize
/// @sn drawFramerateAtX:%s y:%s fontNamed:%s size:%s
procedure DrawFramerate(x, y: Longint; name: String; size: Longint); overload;
/// Draws the framerate to the screen using a simple font built into SwinGame.
///
/// @lib DrawFramerateWithSimpleFont
/// @sn drawFramerateAtX:%s y:%s
procedure DrawFramerate(x, y: Longint); overload;
//=============================================================================
implementation
uses SysUtils, Classes,
stringhash, sgTrace, // libsrc
sgUtils, sgGeometry, sgGraphics, sgCamera, sgShared, sgResources, sgImages, sgDriverText;
//=============================================================================
const EOL = LineEnding; // from sgShared
var
_Fonts: TStringHash;
//----------------------------------------------------------------------------
function LoadFont(fontName: String; size: Longint): Font;
begin
result := LoadFontNamed(FontNameFor(fontName, size), fontName, size);
end;
procedure _DoFreeFont(var fontToFree: Font);
begin
if Assigned(fontToFree) then
begin
{$IFDEF TRACE}
Trace('Resources', 'IN', 'FreeFont', 'After calling free notifier');
{$ENDIF}
try
{$IFDEF TRACE}
Trace('Resources', 'IN', 'FreeFont', 'Before calling close font');
{$ENDIF}
CallFreeNotifier(fontToFree);
TextDriver.CloseFont(fontToFree);
Dispose(fontToFree);
fontToFree := nil;
{$IFDEF TRACE}
Trace('Resources', 'IN', 'FreeFont', 'At end of free font');
{$ENDIF}
except
RaiseException('Unable to free the specified font');
exit;
end;
end;
end;
procedure FreeFont(var fontToFree: Font);
begin
if Assigned(fontToFree) then ReleaseFont(fontToFree^.name);
fontToFree := nil;
end;
//----------------------------------------------------------------------------
function LoadFontNamed(name, filename: String; size: Longint): Font;
var
obj: tResourceContainer;
fnt: Font;
function _DoLoadFont(fontName: String; size: Longint): Font;
var
filename: String;
originalFilename: String;
begin
originalFilename := '';
{$IFDEF TRACE}
TraceEnter('sgText', '_DoLoadFont(', fontName + ' ' + IntToStr(size));
{$ENDIF}
filename := fontName;
if not FileExists(filename) then
begin
filename := PathToResource(filename, FontResource);
if not FileExists(filename) then
begin
originalFilename := filename;
filename := filename + '.ttf';
if not FileExists(filename) then
begin
RaiseWarning('Unable to locate font ' + fontName + ' at ' + originalFilename);
result := nil;
exit;
end;
end;
end;
{$IFDEF TRACE}
TraceIf(tlInfo, 'sgText', 'Info', '_DoLoadFont', 'About to load font from driver');
{$ENDIF}
result := TextDriver.LoadFont(name, filename, size);
{$IFDEF TRACE}
TraceExit('sgText', '_DoLoadFont = ' + HexStr(result) );
{$ENDIF}
end;
begin
{$IFDEF TRACE}
TraceEnter('sgText', 'LoadFontNamed(', name + ' ' + IntToStr(size));
{$ENDIF}
if _Fonts.containsKey(name) then
begin
result := FontNamed(name);
{$IFDEF TRACE}
TraceExit('sgText', 'Exit LoadFontNamed = ' + HexStr(result));
{$ENDIF}
exit;
end;
fnt := _DoLoadFont(filename, size);
if Assigned(fnt) then
begin
obj := tResourceContainer.Create(fnt);
if not _Fonts.setValue(name, obj) then raise Exception.create('Error loaded Font resource - ' + name);
end;
result := fnt;
{$IFDEF TRACE}
TraceExit('sgText', 'LoadFontNamed = ' + HexStr(result));
{$ENDIF}
end;
function HasFont(name: String): Boolean;
begin
result := _Fonts.containsKey(name);
end;
function FontNameFor(fontName: String; size: Longint): String;
begin
result := fontName + '|' + IntToStr(size);
end;
function FontNamed(name: String; size: Longint): Font;
var
filename: String;
begin
result := FontNamed(FontNameFor(name, size));
if (result = nil) then
begin
filename := PathToResource(name, FontResource);
if FileExists(name) or FileExists(filename) then
begin
result := LoadFontNamed(name, name, size);
end
end;
end;
function FontNamed(name: String): Font;
var
tmp : TObject;
begin
tmp := _Fonts.values[name];
if assigned(tmp) then result := Font(tResourceContainer(tmp).Resource)
else result := nil;
end;
procedure ReleaseFont(name: String);
var
fnt: Font;
begin
fnt := FontNamed(name);
if Assigned(fnt) then
begin
_Fonts.remove(name).free();
_DoFreeFont(fnt);
end;
end;
procedure ReleaseAllFonts();
begin
ReleaseAll(_Fonts, @ReleaseFont);
end;
//----------------------------------------------------------------------------
procedure FontSetStyle(font: Font; value: FontStyle);
begin
if not Assigned(font) then begin RaiseWarning('No font supplied to FontSetStyle'); exit; end;
//TTF_SetFontStyle(font^.fptr, Longint(value));
TextDriver.SetFontStyle(font,value);
end;
function FontFontStyle(font: Font): FontStyle;
begin
result := NormalFont;
if not Assigned(font) then begin RaiseWarning('No font supplied to FontFontStyle'); exit; end;
result := TextDriver.GetFontStyle(font);
end;
function IsSet(toCheck, checkFor: FontAlignment): Boolean; overload;
begin
result := (Longint(toCheck) and Longint(checkFor)) = Longint(checkFor);
end;
/// This function prints "str" with font "font" and color "clrFg"
/// * onto a rectangle of color "clrBg".
/// * It does not pad the text.
procedure PrintStrings(dest: Bitmap; font: Font; str: String; rc: Rectangle; clrFg, clrBg:Color; flags:FontAlignment) ;
begin
// If there's nothing to draw, return NULL
if (Length(str) = 0) or (font = nil) then exit;
if (rc.width <= 0) or (rc.height <= 0) then exit;
TextDriver.PrintStrings(dest,font,str,rc,clrFg, clrBg,flags)
end;
/// This function prints "str" with font "font" and color "clrFg"
/// * onto a rectangle of color "clrBg".
/// * It does not pad the text.
procedure PrintWideStrings(dest: Bitmap; font: Font; str: WideString; rc: Rectangle; clrFg, clrBg:Color; flags:FontAlignment);
begin
// If there's nothing to draw, return NULL
if (Length(str) = 0) or (font = nil) then exit;
if (rc.width <= 0) or (rc.height <= 0) then exit;
TextDriver.PrintWideStrings(dest,font,str,rc,clrFg,clrBg,flags);
end;
function DrawTextTo(font: Font; str: String; clrFg, backgroundColor : Color) : Bitmap;
var
resultBitmap : Bitmap;
bitmapSize : Rectangle;
begin
result := nil;
// If there's nothing to draw, return NULL
if (Length(str) = 0) or (font = nil) then exit;
bitmapSize.x := 0;
bitmapSize.y := 0;
bitmapSize.width := TextWidth(font, str) + 2;
bitmapSize.height := TextHeight(font,str) + 2;
resultBitmap := CreateBitmap(bitmapSize.width, bitmapSize.height);
PrintStrings(resultBitmap,font,str,bitmapSize,clrFg, ColorTransparent, AlignLeft);
result := resultBitmap;
end;
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; name: String; x, y: Longint); overload;
begin
DrawText(dest, theText, textColor, FontNamed(name), x, y);
end;
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; name: String; size: Longint; x, y: Longint); overload;
begin
DrawText(dest, theText, textColor, LoadFontNamed(FontNameFor(name, size), name, size), x, y);
end;
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; theFont: Font; x, y: Longint); overload;
var
rect: Rectangle;
begin
if not Assigned(theFont) then exit;
if dest = nil then begin RaiseWarning('Cannot draw text, as no destination was supplied'); exit; end;
if Length(theText) <= 0 then exit;
rect.x := x; // + 1;
rect.y := y; // + 1;
rect.width := TextWidth(theFont, theText); // + 2;
rect.height := TextHeight(theFont, theText); // + 2;
PrintStrings(dest, theFont, theText, rect, textColor, ColorTransparent, AlignLeft);
end;
procedure DrawUnicode(dest: Bitmap; theText: WideString; textColor: Color; theFont: Font; x, y: Longint); overload;
var
rect: Rectangle;
begin
if theFont = nil then begin RaiseWarning('The specified font is nil'); exit; end;
if dest = nil then begin RaiseWarning('Cannot draw text, as no destination was supplied'); exit; end;
if Length(theText) <= 0 then exit;
rect.x := x;
rect.y := y;
rect.width := TextWidth(theFont, theText);
rect.height := TextHeight(theFont, theText);
PrintWideStrings(dest, theFont, theText, rect, textColor, ColorTransparent, AlignLeft);
end;
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; name: String; const pt: Point2D); overload;
begin
DrawText(dest, theText, textColor, FontNamed(name), pt);
end;
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; name: String; size: Longint; const pt: Point2D); overload;
begin
DrawText(dest, theText, textColor, LoadFontNamed(FontNameFor(name, size), name, size), pt);
end;
procedure DrawText(dest: Bitmap; theText: String; textColor: Color; theFont: Font; const pt: Point2D); overload;
begin
DrawText(dest, theText, textColor, theFont, RoundInt(pt.x), RoundInt(pt.y));
end;
procedure DrawUnicode(dest: Bitmap; theText: WideString; textColor: Color; theFont: Font; const pt: Point2D); overload;
begin
DrawUnicode(dest, theText, textColor, theFont, RoundInt(pt.x), RoundInt(pt.y));
end;
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; name: String; align: FontAlignment; x, y, w, h: Longint); overload;
begin
DrawTextLines(dest, theText, textColor, backColor, FontNamed(name), align, x, y, w, h);
end;
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; x, y, w, h: Longint); overload;
begin
DrawTextLines(dest, theText, textColor, backColor, LoadFontNamed(FontNameFor(name, size), name, size), align, x, y, w, h);
end;
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; x, y, w, h: Longint); overload;
var
rect: Rectangle;
begin
if not Assigned(theFont) then exit;
if Length(theText) <= 0 then exit;
if (w <= 0) or (h <= 0) then exit;
rect.x := x; // + 1;
rect.y := y; // + 1;
rect.width := w; // - 2;
rect.height := h; // - 2;
PrintStrings(dest, theFont, theText, rect, textColor, backColor, align);
end;
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; name: String; align: FontAlignment; const withinRect: Rectangle); overload;
begin
DrawTextLines(dest, theText, textColor, backColor, FontNamed(name), align, withinRect);
end;
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; const withinRect: Rectangle); overload;
begin
DrawTextLines(dest, theText, textColor, backColor, LoadFontNamed(FontNameFor(name, size), name, size), align, withinRect);
end;
procedure DrawTextLines(dest: Bitmap; theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; const withinRect: Rectangle); overload;
begin
DrawTextLines(dest, theText, textColor, backColor, theFont, align, RoundInt(withinRect.x), RoundInt(withinRect.y), withinRect.width, withinRect.height);
end;
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; name: String; align: FontAlignment; x, y, w, h: Longint); overload;
begin
DrawTextLines(screen, theText, textColor, backColor, FontNamed(name), align, x, y, w, h);
end;
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; name: String; size: Longint; align: FontAlignment; x, y, w, h: Longint); overload;
begin
DrawTextLines(screen, theText, textColor, backColor, LoadFontNamed(FontNameFor(name, size), name, size), align, x, y, w, h);
end;
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; theFont: Font; align: FontAlignment; x, y, w, h: Longint); overload;
begin
DrawTextLines(screen, theText, textColor, backColor, theFont, align, x, y, w, h);
end;
procedure DrawTextLinesOnScreen(theText: String; textColor, backColor: Color; name: String; align: FontAlignment; const withinRect: Rectangle); overload;
begin
DrawTextLines(screen, theText, textColor, backColor, FontNamed(name), align, RoundInt(withinRect.x), RoundInt(withinRect.y), withinRect.width, withinRect.height);
end;