forked from GWRon/Dig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.gfx.bitmapfont.bmx
1280 lines (1030 loc) · 38.9 KB
/
base.gfx.bitmapfont.bmx
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
Rem
====================================================================
Bitmapfont + Manager classes
====================================================================
Bitmapfont classes using sprite atlases for faster drawing.
====================================================================
If not otherwise stated, the following code is available under the
following licence:
LICENCE: zlib/libpng
Copyright (C) 2002-now Ronny Otto, digidea.de
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
====================================================================
End Rem
SuperStrict
Import BRL.Max2D
Import BRL.Map
'to load from truetype
Import brl.FreeTypeFont
Import "base.util.rectangle.bmx"
Import "base.gfx.sprite.bmx"
Import "base.gfx.spriteatlas.bmx"
CONST SHADOWFONT:INT = 256
CONST GRADIENTFONT:INT = 512
Type TBitmapFontManager
Field baseFont:TBitmapFont
Field baseFontBold:TBitmapFont
Field baseFontItalic:TBitmapFont
Field baseFontSmall:TBitmapFont
Field _defaultFont:TBitmapFont
Field fonts:TStringMap = new TStringMap
Global systemFont:TBitmapFont
Global _instance:TBitmapFontManager
Global _defaultFlags:int = 0 'SMOOTHFONT
Function GetInstance:TBitmapFontManager()
if not _instance then _instance = new TBitmapFontManager
return _instance
End Function
Method GetDefaultFont:TBitmapFont()
'instead of doing it in "new" (no guarantee that graphicsmode
'is set up already)
if not systemFont then systemFont = TBitmapFont.Create("SystemFont", "", 12, SMOOTHFONT)
'if no default font was set, return the system font
if not _defaultFont then return systemFont
return _defaultFont
End Method
'get ignores the "SMOOTHFONT" flag to allow adding "crisp" fonts
Method Get:TBitmapFont(name:String="", size:Int=-1, style:Int=-1)
name = lower(name)
'fall back to default font if none was given
if name = "" then name = "default"
'no details given: return default font
If name = "default" And size = -1 And style = -1 then return GetDefaultFont()
'try to find default font settings for this font face
Local hasDefaultStyledFont:int = False
Local hasDefaultFont:int = False
Local defaultStyledFont:TBitmapFont = TBitmapFont(fonts.ValueForKey(name+"_-1_"+style))
Local defaultFont:TBitmapFont = defaultStyledFont
if not defaultStyledFont
defaultFont = TBitmapFont(fonts.ValueForKey(name))
defaultStyledFont = defaultFont
if defaultFont
hasDefaultFont = True
endif
else
hasDefaultStyledFont = True
endif
if not defaultStyledFont then defaultStyledFont = GetDefaultFont()
'no size given: use default font size
If size = -1 Then size = defaultStyledFont.FSize
'no style given: use default font style
If style = -1 Then style = defaultStyledFont.FStyle
local key:string = name + "_" + size + "_" + style
local font:TBitmapFont = TBitmapFont(fonts.ValueForKey(key))
if font then return font
'if the font wasn't found, use the defaultFont-fontfile to load this style
font = Add(name, defaultStyledFont.FFile, size, style)
rem
'insert as default too
if not hasDefaultStyledFont then fonts.Insert(name+"_-1_"+style, font)
if not hasDefaultFont then fonts.Insert(name, font)
'if SMOOTHFONT was used - add the unsmoothed too (for easier retrieval)
if (style & SMOOTHFONT) <> 0
local keyWithoutSmooth:string = name + "_" + size + "_" + (style - SMOOTHFONT)
if not TBitmapFont(fonts.ValueForKey(keyWithoutSmooth))
fonts.insert(keyWithoutSmooth, font)
endif
endif
endrem
Return font
End Method
Method Copy:TBitmapFont(sourceName:string, copyName:string, size:int=-1, style:int=-1)
local sourceFont:TBitmapFont = Get(sourceName, size, style)
Local newFont:TBitmapFont = TBitmapFont.Create(copyName, sourceFont.fFile, sourceFont.fSize, sourceFont.fStyle, sourceFont.fixedCharWidth, sourceFont.charWidthModifier)
fonts.Insert(copyName+"_"+sourceFont.fSize+"_"+sourceFont.fStyle, newFont)
return newFont
End Method
Method Add:TBitmapFont(name:String, file:String, size:Int, style:Int=0, ignoreDefaultStyle:int = False, fixedCharWidth:int=-1, charWidthModifier:Float=1.0)
name = lower(name)
if not ignoreDefaultStyle
style :| _defaultFlags
endif
local defaultFont:TBitmapFont = GetDefaultFont()
If size = -1 Then size = defaultFont.FSize
If style = -1 Then style = defaultFont.FStyle
If file = "" Then file = defaultFont.FFile
Local font:TBitmapFont = TBitmapFont.Create(name, file, size, style, fixedCharWidth, charWidthModifier)
Local key:string = name+"_"+size+"_"+style
fonts.Insert(key, font)
'insert as default font too (name + style, ignore size)
if not TBitmapFont(fonts.ValueForKey(name+"_-1_"+style)) then fonts.Insert(name+"_-1_"+style, font)
'insert as default font too (only name)
if not TBitmapFont(fonts.ValueForKey(name)) then fonts.Insert(name, font)
'if SMOOTHFONT was used - add the unsmoothed too (for easier retrieval)
if (style & SMOOTHFONT) <> 0
local styleNonSmooth:int = style - SMOOTHFONT
if not TBitmapFont(fonts.ValueForKey(name + "_-1_" + (style - SMOOTHFONT)))
fonts.insert(name + "_-1_" + (style - SMOOTHFONT), font)
endif
if not TBitmapFont(fonts.ValueForKey(name + "_" + size + "_" + (style - SMOOTHFONT)))
fonts.insert(name + "_" + size + "_" + (style - SMOOTHFONT), font)
endif
endif
'set default fonts if not done yet
if _defaultFont = null then _defaultFont = Font
if baseFont = null then baseFont = Font
if baseFontBold = null and style & BOLDFONT > 0 then baseFontBold = Font
if baseFontItalic = null and style & ITALICFONT > 0 then baseFontItalic = Font
Return Font
End Method
Method AddFont:TBitmapFont(font:TBitmapFont)
local key:string = font.FName + "_" + font.FSize + "_" + font.FStyle
fonts.insert(key, font)
End Method
End Type
'===== CONVENIENCE ACCESSORS =====
'convenience instance getter
Function GetBitmapFontManager:TBitmapFontManager()
return TBitmapFontManager.GetInstance()
End Function
'===== CONVENIENCE ACCESSORS =====
'not really needed - but for convenience to avoid direct call to the
'instance getter GetBitmapFontManager()
Function GetBitmapFont:TBitmapfont(name:string="", size:Int=-1, style:Int=-1)
Return TBitmapFontManager.GetInstance().Get(name, size, style)
End Function
Type TBitmapFontChar
Field area:TRectangle
Field charWidth:float
Field img:TImage
Method Init:TBitmapFontChar(img:TImage, x:int,y:int,w:Int, h:int, charWidth:float)
self.img = img
self.area = new TRectangle.Init(x, y, w, h)
self.charWidth = charWidth
Return self
End Method
End Type
Type TBitmapFont
'identifier
Field FName:string = ""
'source path
Field FFile:string = ""
'size of this font
Field FSize:int = 0
'style used in this font
Field FStyle:int = 0
'the original imagefont
Field FImageFont:TImageFont
Field chars:TBitmapFontChar[] = new TBitmapFontChar[256]
Field charsSprites:TSprite[] = new TSprite[0]
Field spriteSet:TSpritePack
'by default only the first 256 chars get loaded
'as soon as an "utf8"-code is requested, the font will re-init with
'more sprites
Field MaxSigns:Int = 256
Field glyphCount:int = 0
Field ExtraChars:String = ""
Field gfx:TMax2dGraphics
Field uniqueID:string =""
Field displaceY:float=100.0
'modifier * lineheight gets added at the end
Field lineHeightModifier:float = 1.05
'value the width of " " (space) is multiplied with
Field spaceWidthModifier:float = 1.0
Field charWidthModifier:float = 1.0
Field fixedCharWidth:int = -1
Field tabWidth:int = 15
'whether to use ints or floats for coords
Field drawAtFixedPoints:int = true
Field _charsEffectFunc:TBitmapFontChar(font:TBitmapFont, charKey:int, char:TBitmapFontChar, config:TData)[]
Field _charsEffectFuncConfig:TData[]
'by default this is 8bit alpha only
Field _pixmapFormat:int = PF_A8
Field _maxCharHeight:int = 0
Field _maxCharHeightAboveBaseline:int = 0
Field _hasEllipsis:int = -1
global drawToPixmap:TPixmap = null
global pixmapOrigin:TVec2D = new TVec2D.Init(0,0)
'DISABLECACHE global ImageCaches:TMap = CreateMap()
global eventRegistered:int = 0
global shadowColor:TColor = new TColor.clBlack
global embossColor:TColor = new TColor.clWhite
Const STYLE_NONE:int = 0
Const STYLE_EMBOSS:int = 1
Const STYLE_SHADOW:int = 2
Const STYLE_GLOW:int = 3
Function Create:TBitmapFont(name:String, url:String, size:Int, style:Int, fixedCharWidth:int = -1, charWidthModifier:Float = 1.0)
Local obj:TBitmapFont = New TBitmapFont
obj.FName = name
obj.FFile = url
obj.FSize = size
obj.FStyle = style
obj.uniqueID = name+"_"+url+"_"+size+"_"+style
obj.gfx = tmax2dgraphics.Current()
obj.fixedCharWidth = fixedCharWidth
obj.charWidthModifier = charWidthModifier
obj.FImageFont = LoadTrueTypeFont(url, size, style)
If not obj.FImageFont
'get system/current font
obj.FImageFont = GetImageFont()
endif
If not obj.FImageFont
Throw ("TBitmapFont.Create: font ~q"+url+"~q not found.")
Return Null 'font not found
endif
'create spriteset
obj.spriteSet = new TSpritePack.Init(null, obj.uniqueID+"_charmap")
'generate a charmap containing packed rectangles where to store images
obj.InitFont()
'listen to App-timer
'DISABLECACHE EventManager.registerListener( "App.onUpdate", TEventListenerRunFunction.Create(TBitmapFont.onUpdateCaches) )
Return obj
End Function
Method SetCharsEffectFunction(position:int, _func:TBitmapFontChar(font:TBitmapFont, charKey:int, char:TBitmapFontChar, config:TData), config:TData=null)
position :-1 '0 based
if _charsEffectFunc.length <= position
_charsEffectFunc = _charsEffectFunc[..position+1]
_charsEffectFuncConfig = _charsEffectFuncConfig[..position+1]
endif
_charsEffectFunc[position] = _func
_charsEffectFuncConfig[position] = config
End Method
'overrideable method
Method ApplyCharsEffect(config:TData=null)
'if instead of overriding a function was provided - use this
if _charsEffectFunc.length > 0
'for local _charKey:TIntKey = eachin chars.keys()
for local charKey:int = 0 until chars.length
'local charKey:Int = _charKey.Value
'local char:TBitmapFontChar = TBitmapFontChar(chars.ValueForKey(charKey))
local char:TBitmapFontChar = chars[charKey]
If Not char then
Continue
End If
'manipulate char
local _func:TBitmapFontChar(font:TBitmapFont, charKey:int, char:TBitmapFontChar, config:TData)
local _config:TData
for local i:int = 0 to _charsEffectFunc.length-1
_func = _charsEffectFunc[i]
_config = _charsEffectFuncConfig[i]
if not _config then _config = config
char = _func(self, charKey, char, _config)
Next
'overwrite char
'chars.Insert(charKey, char)
chars[charKey] = char
Next
endif
'else do nothing by default
End Method
'returns the same font in the given size/style combination
'it is more or less a wrapper to make acces more convenient
Method GetVariant:TBitmapFont(size:int=-1, style:int = -1)
if size = -1 then size = self.FSize
if style = -1 then style = self.FStyle
return TBitmapFontManager.GetInstance().Get(self.FName, size, style)
End Method
'generate a charmap containing packed rectangles where to store images
Method InitFont(config:TData=null )
'1. load chars
LoadCharsFromSource()
'2. Process the characters (add shadow, gradients, ...)
ApplyCharsEffect(config)
'3. store them into a packed (optimized) charmap
' -> creates a 8bit alpha'd image (grayscale with alpha ...)
CreateCharmapImage( CreateCharmap(1) )
End Method
'reinits the font and loads the characters above charcode 256
Method LoadExtendedCharacters()
MaxSigns = -1
InitFont()
End Method
'load glyphs of an imagefont as TBitmapFontChar into a char-TMap
Method LoadCharsFromSource(source:object=null)
local imgFont:TImageFont = TImageFont(source)
if imgFont = null then imgFont = FImageFont
Local glyph:TImageGlyph
Local glyphCount:Int = imgFont.CountGlyphs()
Local n:int
Local loadMaxGlyphs:int = glyphCount
if MaxSigns <> -1 then loadMaxGlyphs = MaxSigns
if extraChars = ""
extraChars :+ chr(8364) '€
extraChars :+ chr(8230) '…
extraChars :+ chr(8220) '“
extraChars :+ chr(8221) '”
extraChars :+ chr(8222) '„
extraChars :+ chr(171) '«
extraChars :+ chr(187) '»
'extraChars :+ chr(8227) '‣
'extraChars :+ chr(9662) '▾
extraChars :+ chr(9650) '▲
extraChars :+ chr(9660) '▼
extraChars :+ chr(9664) '◀
extraChars :+ chr(9654) '▶
extraChars :+ chr(9632) '■
endif
self.glyphCount = glyphCount
For Local i:Int = 0 Until loadMaxGlyphs
' For Local i:Int = 0 Until MaxSigns
n = imgFont.CharToGlyph(i)
If n < 0 or n > glyphCount then Continue
glyph = imgFont.LoadGlyph(n)
If not glyph then continue
'base displacement calculated with A-Z (space between
'TOPLEFT of 'ABCDE' and TOPLEFT of 'acen'...)
if i >= 65 AND i < 95 then displaceY = Min(displaceY, glyph._y)
resizeChars(i)
'chars.insert(i, new TBitmapFontChar.Init(glyph._image, glyph._x, glyph._y,glyph._w,glyph._h, glyph._advance))
if fixedCharWidth > 0
chars[i] = new TBitmapFontChar.Init(glyph._image, glyph._x, glyph._y,glyph._w ,glyph._h, fixedCharWidth)
else
chars[i] = new TBitmapFontChar.Init(glyph._image, glyph._x, glyph._y,glyph._w,glyph._h, glyph._advance * charWidthModifier)
endif
Next
For Local charNum:Int = 0 Until ExtraChars.length
n = imgFont.CharToGlyph( ExtraChars[charNum] )
If n < 0 or n > glyphCount then Continue
glyph = imgFont.LoadGlyph(n)
If not glyph then continue
resizeChars(ExtraChars[charNum])
'chars.insert(ExtraChars[charNum] , new TBitmapFontChar.Init(glyph._image, glyph._x, glyph._y,glyph._w,glyph._h, glyph._advance) )
if fixedCharWidth > 0
chars[ExtraChars[charNum]] = new TBitmapFontChar.Init(glyph._image, glyph._x, glyph._y,glyph._w ,glyph._h, fixedCharWidth)
else
chars[ExtraChars[charNum]] = new TBitmapFontChar.Init(glyph._image, glyph._x, glyph._y,glyph._w,glyph._h, glyph._advance * charWidthModifier)
endif
Next
End Method
Method resizeChars(index:int)
if index >= chars.length then
chars = chars[.. index + 1 + chars.length/3]
end if
end method
Method resizeCharsSprites(index:int)
if index >= charsSprites.length then
charsSprites = charsSprites[.. index + 1 + charsSprites.length/3]
end if
end method
'create a charmap-atlas with information where to optimally store
'each char
Method CreateCharmap:TSpriteAtlas(spaceBetweenChars:int=0)
local charmap:TSpriteAtlas = TSpriteAtlas.Create(64,64)
local bitmapFontChar:TBitmapFontChar
'for local _charKey:TIntKey = eachin chars.keys()
for Local charKey:int = 0 until chars.length
'local charKey:Int = _charKey.Value
'bitmapFontChar = TBitmapFontChar(chars.ValueForKey(charKey))
bitmapFontChar = chars[charKey]
if not bitmapFontChar then continue
charmap.AddElement(charKey, int(bitmapFontChar.area.GetW()+spaceBetweenChars), int(bitmapFontChar.area.GetH()+spaceBetweenChars) ) 'add box of char and package atlas
Next
return charmap
End Method
'create an image containing all chars
'the charmap-atlas contains information where to store each character
Method CreateCharmapImage(charmap:TSpriteAtlas)
local pix:TPixmap = CreatePixmap(charmap.w,charmap.h, _pixmapFormat) ; pix.ClearPixels(0)
'create spriteset
if not spriteSet then spriteSet = new TSpritePack.Init(null, uniqueID+"_charmap")
'loop through atlas boxes and add chars
For local _charKey:String = eachin charmap.elements.Keys()
local rect:TRectangle = TRectangle(charmap.elements.ValueForKey(_charKey))
Local charKey:Int = _charKey.ToInt()
'skip missing data
if (charKey > chars.length) or (not chars[charKey]) then continue
local bm:TBitmapFontChar = chars[charKey]
if not bm.img then continue
'draw char image on charmap
'local charPix:TPixmap = LockImage(TBitmapFontChar(chars.ValueForKey(charKey)).img)
local charPix:TPixmap = LockImage(bm.img)
'make sure the pixmaps are 8bit alpha-format
' If charPix.format <> 2 Then charPix.convert(PF_A8)
DrawImageOnImage(charPix, pix, int(rect.GetX()), int(rect.GetY()))
'UnlockImage(TBitmapFontChar(chars.ValueForKey(charKey)).img)
UnlockImage(bm.img)
' es fehlt noch charWidth - extraTyp?
resizeCharsSprites(charKey)
charsSprites[charKey] = new TSprite.Init(spriteSet, charKey, rect, null, 0)
Next
'set image to sprite pack
if IsSmooth()
spriteSet.image = LoadImage(pix)
else
'non smooth fonts should disable any filtering (eg. in virtual resolution scaling)
spriteSet.image = LoadImage(pix, 0)
endif
End Method
Method IsBold:Int()
return (FStyle & BOLDFONT)
End Method
Method IsSmooth:Int()
return (FStyle & SMOOTHFONT)
End Method
'Returns whether this font has a visible ellipsis char ("…")
Method HasEllipsis:int()
if _hasEllipsis = -1 then _hasEllipsis = GetWidth(chr(8230))
return _hasEllipsis
End Method
Method GetEllipsis:string()
if hasEllipsis() then return chr(8230)
return "..."
End Method
Method GetMaxCharHeight:int(includeBelowBaseLine:int=True)
if includeBelowBaseLine
if _maxCharHeight = 0 then _maxCharHeight = getHeight("gQ'_") 'including "()" adds too much to the font height
return _maxCharHeight
else
if _maxCharHeightAboveBaseline = 0 then _maxCharHeightAboveBaseline = getHeight("abCDE")
return _maxCharHeightAboveBaseline
endif
End Method
Method GetWidth:Float(text:String)
return draw(text,0,0,null,0).getX()
End Method
Method GetHeight:Float(text:String)
return draw(text,0,0,null,0).getY()
End Method
Method GetBlockHeight:Float(text:String, w:Float, h:Float, fixedLineHeight:int = -1)
return drawBlock(text, 0,0,w,h, null, null, 0, 0).GetY()
End Method
Method GetBlockWidth:Float(text:String, w:Float, h:Float, fixedLineHeight:int = -1)
return drawBlock(text, 0,0,w,h, null, null, 0, 0).GetX()
End Method
Method GetBlockDimension:TVec2D(text:String, w:Float, h:Float, fixedLineHeight:int = -1)
return drawBlock(text, 0,0,w,h, null, null, 0, 0, 1.0, True, False, fixedLineHeight)
End Method
'render to target pixmap/image/screen
Function SetRenderTarget:int(target:object=null)
'render to screen
if not target
drawToPixmap = null
return TRUE
endif
if TImage(target)
drawToPixmap = LockImage(TImage(target))
elseif TPixmap(target)
drawToPixmap = TPixmap(target)
endif
End Function
'splits a given text into an array of lines
'splitting is done on "spaces", "-"
'or in the middle of a word if "nicelyTruncateLastLine" is "FALSE"
Method TextToMultiLine:string[](text:string, w:float, h:float, lineHeight:float, nicelyTruncateLastLine:int=TRUE)
Local fittingChars:int = 0
Local processedChars:Int= 0
Local paragraphs:string[] = text.replace(chr(13), "~n").split("~n")
'the lines to output at the end
Local lines:string[]= null
'how many space is left to draw?
local heightLeft:float = h
'are we limited in height?
local limitHeight:int = (heightLeft > 0)
'for each line/paragraph
For Local i:Int= 0 Until paragraphs.length
'skip paragraphs if no space was left
if limitHeight and heightLeft < lineHeight then continue
local line:string = paragraphs[i]
'process each line - and if needed add a line break
repeat
'the part of the line which has to get processed at that moment
local linePartial:string = line
local breakPosition:int = line.length
'whether to skip the next char of a new line
local skipNextChar:int = FALSE
'as long as the part of the line does not fit into
'the given width, we have to search for linebreakers
while (w>0 and self.getWidth(linePartial) > w) and linePartial.length >0
'whether we found a break position by a rule
local FoundBreakPosition:int = FALSE
local spaces:int = 0
'search for "nice" linebreak:
'- if not on last line
'- if enforced to do so ("nicelyTruncateLastLine")
if i < (paragraphs.length-1) or nicelyTruncateLastLine
'search for the "most right" position of a
'linebreak
'no need to check for the last char (no break then ;-)
For local charPos:int = 0 until linePartial.length -1
'special line break rules (spaces, -, ...)
If linePartial[charPos] = Asc(" ")
'use first space in a row (" ")
if spaces = 0
breakPosition = charPos+1
FoundBreakPosition=TRUE
'if it is a " "-space, we have to skip it
skipNextChar = TRUE 'aka delete the " "
endif
spaces :+ 1
elseif linePartial[charPos] = Asc("-")
breakPosition = charPos+1
FoundBreakPosition=TRUE
skipNextChar = FALSE
spaces = 0
else
spaces = 0
endif
Next
'remove spaces at end
if spaces > 0
linePartial = linePartial[.. linePartial.length - spaces]
endif
endif
'if no line break rule hit, use a "cut" in the
'middle of a word
if not FoundBreakPosition then breakPosition = Max(0, linePartial.length-1 -1)
'cut off the part AFTER the breakposition
linePartial = linePartial[..breakPosition]
wend
'add that line to the lines to draw
lines :+ [linePartial]
heightLeft :- lineHeight
'strip the processed part from the original line
line = line[linePartial.length..]
' if skipNextChar then line = line[Min(1, line.length)..]
'until no text left, or no space left for another line
until line.length = 0 or (limitHeight and heightLeft < lineHeight)
'if the height was not enough - add a "..."
if line.length > 0
'get the line BEFORE
local currentLine:string = lines[lines.length-1]
'check whether we have to subtract some chars for the "..."
local ellipsisChar:string = GetEllipsis()
if (w > 0 and getWidth(currentLine + ellipsisChar) > w)
repeat
currentLine = currentLine[.. currentLine.length-1]
until getWidth(currentLine + ellipsisChar) <= w
currentLine = currentLine + ellipsisChar
else
currentLine = currentLine[.. currentLine.length] + ellipsisChar
endif
lines[lines.length-1] = currentLine
endif
Next
return lines
End Method
'draws the text lines in a given block according to given alignment.
'@nicelyTruncateLastLine: try to shorten a word with "..."
' or just truncate?
'@centerSingleLineOnBaseline: if only 1 line is given, is center
' calculated using baseline (no "y,g,p,...")
Method drawLinesBlock:TVec2D(lines:String[], x:Float, y:Float, w:Float, h:Float, alignment:TVec2D=null, color:TColor=null, style:int=0, doDraw:int = 1, special:float=1.0, nicelyTruncateLastLine:int=TRUE, centerSingleLineOnBaseline:int=False, fixedLineHeight:int = -1)
'use special chars (instead of text) for same height on all lines
Local alignedX:float = 0.0
Local lineMaxWidth:Float = 0
local lineWidth:Float = 0
Local lineHeight:float = getMaxCharHeight()
if fixedLineHeight > 0 then lineHeight = fixedLineHeight
'first height was calculated using all characters, but we now
'know if we could center using baseline only (only available
'when there is only 1 line to draw)
if fixedLineHeight <= 0
if lines.length = 1 and centerSingleLineOnBaseline
lineHeight = getMaxCharHeight(False)
'lineHeight = 0.25 * lineHeight + 0.75 * getMaxCharHeight(False)
'lineHeight :+ 1 'a bit of influence of "below baseline" chars
endif
endif
local blockHeight:Float = lineHeight * lines.length
if fixedLineHeight <= 0
if lines.length > 1
'add the lineHeightModifier for all lines but the first or
'single one
blockHeight :+ lineHeight * (lineHeightModifier-1.0)
endif
endif
'move along y according alignment
'-> aligned top: no change
'-> aligned bottom: move down by unused space so last line ends at Y + h
'-> aligned inbetween: move accordingly
if alignment
'empty space = height - (..)
'-> alignTop = add 0 of that space
'-> alignBottom = add 100% of that space
if alignment.GetY() <> ALIGN_TOP and h > 0
y :+ alignment.GetY() * (h - blockHeight)
endif
endif
'backup current setting
local fontStyle:TBitmapFontStyle = new TBitmapFontStyle.Push(FName, FSize, FStyle, color)
local startY:Float = y
For local i:int = 0 until lines.length
lineWidth = getWidth(lines[i])
lineMaxWidth = Max(lineMaxwidth, lineWidth)
'only align when drawing
If doDraw
if alignment and alignment.GetX() <> ALIGN_LEFT and w > 0
alignedX = x + alignment.GetX() * (w - lineWidth)
else
alignedX = x
endif
EndIf
local p:TVec2D = __drawStyled( lines[i], alignedX, y, color, style, doDraw, special, fontStyle)
if fixedLineHeight <= 0
y :+ Min(_maxCharHeight, Max(lineHeight, p.y))
'add extra spacing _between_ lines
If lines.length > 1 and i < lines.length-1
y :+ lineHeight * (lineHeightModifier-1.0)
Endif
else
y :+ fixedLineHeight
endif
Next
return new TVec2D.Init(lineMaxWidth, y - startY)
End Method
'draws the text in a given block according to given alignment.
'@nicelyTruncateLastLine: try to shorten a word with "..."
' or just truncate?
'@centerSingleLineOnBaseline: if only 1 line is given, is center
' calculated using baseline (no "y,g,p,...")
Method drawBlock:TVec2D(text:String, x:Float, y:Float, w:Float, h:Float, alignment:TVec2D=null, color:TColor=null, style:int=0, doDraw:int = 1, special:float=1.0, nicelyTruncateLastLine:int=TRUE, centerSingleLineOnBaseline:int=False, fixedLineHeight:Int = -1)
Local lineHeight:float = getMaxCharHeight()
Local lines:string[] = TextToMultiLine(text, w, h, lineHeight, nicelyTruncateLastLine)
return drawLinesBlock(lines, x, y, w, h, alignment, color, style, doDraw, special, nicelyTruncateLastLine, centerSingleLineOnBaseline, fixedLineHeight)
End Method
Method drawWithBG:TVec2D(value:String, x:Int, y:Int, bgAlpha:Float = 0.3, bgCol:Int = 0, style:int=0)
Local OldAlpha:Float = GetAlpha()
Local color:TColor = new TColor.Get()
local dimension:TVec2D = drawStyled(value,0,0, null, style,0)
SetAlpha bgAlpha
SetColor bgCol, bgCol, bgCol
DrawRect(x, y, dimension.GetX(), dimension.GetY())
color.setRGBA()
'backup current setting
local fontStyle:TBitmapFontStyle = new TBitMapFontStyle.Push( FName, FSize, FStyle, color )
local vec:TVec2D = __drawStyled(value, x, y, color, style, true, , fontStyle)
'restore backup
'style.Reset()
return vec
End Method
'can adjust used font or color
Method ProcessCommand:int(command:string, payload:string, fontStyle:TBitMapFontStyle)
if command = "color" and not fontStyle.ignoreColorTag
local colors:string[] = payload.split(",")
local color:TColor
if colors.length >= 3
color = new TColor
color.r = int(colors[0])
color.g = int(colors[1])
color.b = int(colors[2])
if colors.length >= 4
color.a = int(colors[3]) / 255.0
else
color.a = 1.0
endif
else
if not fontStyle.GetColor()
color = TColor.clWhite.Copy()
else
color = fontStyle.GetColor().Copy()
endif
endif
'backup current setting
fontStyle.PushColor( color )
endif
if command = "/color" and not fontStyle.ignoreColorTag
'local color:TColor =
fontStyle.PopColor()
endif
if command = "b" then fontStyle.PushFontStyle( BOLDFONT )
if command = "/b" then fontStyle.PopFontStyle( BOLDFONT )
if command = "i" then fontStyle.PushFontStyle( ITALICFONT )
if command = "/i" then fontStyle.PopFontStyle( ITALICFONT )
'adjust line height if another font is selected
if fontStyle.GetFont() <> self and fontStyle.GetFont()
fontStyle.styleDisplaceY = 0.5*(getMaxCharHeight() - fontStyle.GetFont().getMaxCharHeight())
else
'reset displace
fontStyle.styleDisplaceY = 0
endif
End Method
Method draw:TVec2D(text:String,x:Float,y:Float, color:TColor=null, doDraw:int=TRUE)
'backup current setting
local fontStyle:TBitmapFontStyle = new TBitmapFontStyle.Push(FName, FSize, FStyle, color)
local vec:TVec2D = __draw(text, x, y, color, doDraw, fontStyle)
'restore backup
'TBitmapFontStyle.Reset()
return vec
End Method
Method drawStyled:TVec2D(text:String,x:Float,y:Float, color:TColor=null, style:int=0, doDraw:int=1, special:float=-1.0)
'backup current setting
local fontStyle:TBitmapFontStyle = new TBitmapFontStyle.Push(FName, FSize, FStyle, color)
'backup current setting
'TBitmapFontStyle.Push(FName, FSize, FStyle, color)
local vec:TVec2D = __drawStyled(text, x, y, color, style, doDraw, special, fontStyle)
'restore backup
'fontStyle.Reset()
return vec
End Method
Method __drawStyled:TVec2D(text:String,x:Float,y:Float, color:TColor=null, style:int=0, doDraw:int=1, special:float=-1.0, fontStyle:TBitmapFontStyle)
if special = -1 then special = 1 '100%
if drawAtFixedPoints
x = int(x)
y = int(y)
endif
local height:float = 0.0
local width:float = 0.0
'backup old color
local oldColor:TColor
if doDraw then oldColor = new TColor.Get()
'emboss
if style = STYLE_EMBOSS
height:+ 1
if doDraw
SetAlpha float(special * 0.5 * oldColor.a)
fontStyle.ignoreColorTag :+ 1
__draw(text, x, y+1, embossColor, doDraw, fontStyle)
fontStyle.ignoreColorTag :- 1
endif
'shadow
else if style = STYLE_SHADOW
height:+ 1
width:+1
if doDraw
SetAlpha special*0.5*oldColor.a
fontStyle.ignoreColorTag :+ 1
__draw(text, x+1,y+1, shadowColor, doDraw, fontStyle)
fontStyle.ignoreColorTag :- 1
endif
'glow
else if style = STYLE_GLOW
if doDraw
fontStyle.ignoreColorTag :+ 1
shadowColor.SetRGB()
SetAlpha special*0.25*oldColor.a
__draw(text, x-2,y, ,doDraw, fontStyle)
__draw(text, x+2,y, ,doDraw, fontStyle)
__draw(text, x,y-2, ,doDraw, fontStyle)
__draw(text, x,y+2, ,doDraw, fontStyle)
SetAlpha special*0.5*oldColor.a
__draw(text, x+1,y+1, ,doDraw, fontStyle)
__draw(text, x-1,y-1, ,doDraw, fontStyle)
fontStyle.ignoreColorTag :- 1
endif
endif
if oldColor then SetAlpha oldColor.a
local result:TVec2D = __draw(text,x,y, color, doDraw, fontStyle)
if oldColor then oldColor.SetRGBA()
return result
End Method
Method __draw:TVec2D(text:String,x:Float,y:Float, color:TColor=null, doDraw:int=TRUE, fontStyle:TBitmapFontStyle)
local width:float = 0.0
local height:float = 0.0
local textLines:string[] = text.replace(chr(13), "~n").split("~n")
local currentLine:int = 0
local oldColor:TColor
if doDraw
oldColor = new TColor.Get()
if not color
color = oldColor.copy()
else
'take screen alpha into consideration
'create a copy to not modify the original
color = color.copy()
color.a :* oldColor.a
endif
'black text is default
' if not color then color = TColor.Create(0,0,0)
if color then color.SetRGBA()
endif
'set the lineHeight before the "for-loop" so it has a set
'value if a line "in the middle" just consists of spaces/nothing
'-> allows double-linebreaks
'control vars
local controlChar:int = asc("|")
local controlCharEscape:int = asc("\")
local controlCharStarted:int = FALSE
local currentControlCommandPayloadSeparator:string = "="
local currentControlCommand:string = ""
local currentControlCommandPayload:string = ""
local lineHeight:int = 0
local charCode:int
local displayCharCode:int 'if char is not found
local charBefore:int
local rotation:int = GetRotation()
local sprite:TSprite