This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
flxtext.monkey
1018 lines (803 loc) · 26.9 KB
/
flxtext.monkey
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
Strict
Import flxextern
Import flxsprite
Import flxcamera
Import flxg
Import system.flxcolor
Import system.flxassetsmanager
Private
#If FLX_TEXT_DRIVER = "angelfont"
Import vendor.angelfont
#ElseIf FLX_TEXT_DRIVER = "fontmachine"
Import vendor.fontmachine
#End
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_8_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_8_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_9_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_9_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_10_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_10_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_11_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_11_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_12_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_12_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_13_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_13_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_14_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_14_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_15_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_15_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_16_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_16_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_17_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_17_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_18_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_18_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_19_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_19_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_20_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_20_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_21_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_21_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_22_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_22_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_23_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_23_flx.${FLX_FONT_EXTENSION}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_24_flx_${FLX_FONT_IMAGE_MASK}"
Import "data/fonts/${FLX_TEXT_DRIVER}/system_font_24_flx.${FLX_FONT_EXTENSION}"
Public
Class FlxText Extends FlxSprite Implements FlxSpriteRenderer
Global __CLASS__:Object
Const ALIGN_LEFT:Float = 0
Const ALIGN_CENTER:Float = 0.5
Const ALIGN_RIGHT:Float = 1
Const SYSTEM_FONT:String = "system"
Private
Global _FontsManager:FlxResourcesManager<FlxBitmapFont> = New FlxResourcesManager<FlxBitmapFont>()
Field _shadow:FlxColor
Field _value:String
Field _lines:FlxTextLine[]
Field _countLines:Int
Field _alignment:Float
Field _fontFamily:String
Field _fontSize:Int
Field _fontHeight:Int
Field _fontObject:FlxBitmapFont
Field _fontShadowEnabled:Bool
Field _fontBorderEnabled:Bool
Field _isEmpty:Bool
Public
Method New(x:Float, y:Float, width:Int = 0, text:String = "")
Super.New(x, y)
_fontShadowEnabled = False
_fontBorderEnabled = False
Self.width = width
SetRenderer(Self)
_shadow = New FlxColor(0)
moves = False
SetFormat(SYSTEM_FONT)
Text = text
End Method
Method Destroy:Void()
Local i:Int = 0, l:Int = _lines.Length()
While (i < l)
_lines[i] = Null
i += 1
Wend
_fontObject = Null
_shadow = Null
Super.Destroy()
End Method
Method SetFormat:FlxText(font:String = "", size:Int = 0, color:Int = FlxG.WHITE, alignment:Float = ALIGN_LEFT, shadowColor:Int = 0)
_alignment = alignment
_fontFamily = font
_fontSize = FlxAssetsManager.GetFont(font).GetValidSize(size)
Self.Color = color
Shadow = shadowColor
_ResetFont()
ResetHelpers()
Return Self
End Method
Method SetWidth:Void(width:Float)
Self.width = width
frameWidth = Self.width
ResetHelpers()
End Method
Method Text:String() Property
Return _value
End Method
Method Text:Void(text:String) Property
_value = text
ResetHelpers()
End Method
Method Size:Void(size:Int) Property
_fontSize = FlxAssetsManager.GetFont(_fontFamily).GetValidSize(size)
_ResetFont()
ResetHelpers()
End Method
Method Size:Int() Property
Return _fontSize
End Method
Method Font:String() Property
Return _fontFamily
End Method
Method Font:Void(font:String) Property
_fontFamily = font
_ResetFont()
ResetHelpers()
End Method
Method Alignment:Float() Property
Return _alignment
End Method
Method Alignment:Void(alignment:Float) Property
If (_alignment = alignment) Return
_alignment = alignment
_ResetAlignment()
End Method
Method Shadow:Int() Property
Return _shadow.argb
End Method
Method Shadow:Void(color:Int) Property
_shadow.SetARGB(color)
End Method
Method SetFontShadowEnabled:Void(enabled:Bool)
_fontShadowEnabled = enabled
End Method
Method SetFontBoderEnabled:Void(enabled:Bool)
_fontBorderEnabled = enabled
End Method
Method OnSpriteBind:Void(sprite:FlxSprite)
Pixels = Null
End Method
Method OnSpriteUnbind:Void()
End Method
Method OnSpriteRender:Void(x:Float, y:Float)
If (_isEmpty) Return
If (_shadow.argb <> 0) Then
Local oldColor:Int = FlxG._LastDrawingColor
Local oldAlpha:Int = FlxG._LastDrawingAlpha
If (_camera.Color <> FlxG.WHITE) Then
_mixedColor.MixRGB(_shadow, _camera._color)
If (FlxG._LastDrawingColor <> _mixedColor.argb) Then
SetColor(_mixedColor.r, _mixedColor.g, _mixedColor.b)
End If
Else
If (FlxG._LastDrawingColor <> _shadow.argb) Then
SetColor(_shadow.r, _shadow.g, _shadow.b)
End If
End If
If (_camera.Alpha < 1) Then
Local _mixedAlpha:Float = _camera.Alpha * _shadow.a
If (FlxG._LastDrawingAlpha <> _mixedAlpha) Then
SetAlpha(_mixedAlpha)
End If
Else
If (FlxG._LastDrawingAlpha <> _shadow.a) Then
SetAlpha(_shadow.a)
End If
End If
_fontObject.DrawText(Self, x + 1, y + 1)
_mixedColor.SetRGB(oldColor)
SetColor(_mixedColor.r, _mixedColor.g, _mixedColor.b)
SetAlpha(oldAlpha)
End If
_fontObject.DrawText(Self, x, y)
End Method
Method GetTextWidth:Float()
Return _fontObject.GetTextWidth(Self)
End Method
Method GetTextHeight:Float()
Return height
End Method
Method ResetHelpers:Void()
If (_value.Length() > 0) Then
If (width <> 0) Then
_ParseText()
Else
_countLines = 0
_AddLine(0, _value.Length(), _fontObject.GetTextWidth(Self, 0, _value.Length()))
_ResetAlignment()
End If
Self.height = _GetHeight()
frameHeight = Self.height
_isEmpty = False
Else
Self.height = 0
frameHeight = 0
_isEmpty = True
End If
Super.ResetHelpers()
End Method
Private
Method _ResetFont:Void()
_fontObject = _FontsManager.GetResource(_fontFamily + _fontSize, New FontResource(_fontFamily, _fontSize))
_fontHeight = _fontObject.GetFontHeight(Self)
End Method
Method _ResetAlignment:Void()
If (_value.Length() = 0) Return
For Local line:Int = 0 Until _countLines
_lines[line].xOffset = (width - _lines[line].width) * _alignment
Next
End Method
Method _GetHeight:Int()
If (width = 0) Then
Return _fontHeight
Else
Return _fontObject.GetTextHeight(Self)
End If
End Method
Method _ParseText:Void()
_countLines = 0
Local i:Int = 0, l:Int = _value.Length()
Local prevOffset:Int = 0
Local offset:Int = _FindNewLine(i, l)
If (offset >= 0) Then
While (offset >= 0)
_BuildLines(prevOffset, offset)
prevOffset = offset + 1
i = prevOffset
offset = _FindNewLine(i, l)
Wend
_BuildLines(prevOffset, _value.Length())
Else
_BuildLines(0, _value.Length())
End If
_ResetAlignment()
End Method
Method _BuildLines:Void(startPos:Int, endPos:Int)
Local textWidth:Int = _fontObject.GetTextWidth(Self, startPos, endPos)
If (width < textWidth) Then
Local textLength:Int = endPos - startPos
Local range:Int = Ceil(textLength / Float(Floor(textWidth / Float(width)) + 1))
Repeat
range += 1
If (range >= textLength) Then
range = textLength + 1
Exit
End If
textWidth = _fontObject.GetTextWidth(Self, startPos, startPos + range)
Until (textWidth >= width)
Local maxOffset:Int = startPos + range
Local minOffset:Int = startPos
Local offset:Int = maxOffset
Local tmpOffset:Int = 0
Local dirty:Bool = False
Local finalTextWidth:Int = 0
Local success:Bool
Repeat
Repeat
offset -= 1
If (offset - minOffset <= 1) Then
offset = minOffset + 1
Exit
End if
tmpOffset = -1
For Local i:Int = offset - 1 To minOffset Step - 1
success = False
If (_value[i] = KEY_SPACE Or _value[i] = KEY_TAB) Then
tmpOffset = i
success = True
Exit
End If
Next
If (tmpOffset < 0) Then
tmpOffset = _GetMinOffset(minOffset, offset)
Else
If (_value[minOffset] = KEY_SPACE Or _value[minOffset] = KEY_TAB) Then
For Local i:Int = minOffset To offset
If (_value[i] <> KEY_SPACE And _value[i] <> KEY_TAB) Then
minOffset = i
Exit
End If
Next
End If
End If
offset = tmpOffset
textWidth = _fontObject.GetTextWidth(Self, minOffset, offset)
Until (textWidth <= width)
dirty = False
finalTextWidth = _fontObject.GetTextWidth(Self, minOffset)
If (finalTextWidth > width And offset - minOffset > 1) Then
For Local i:Int = minOffset Until offset
If (_value[i] = KEY_SPACE Or _value[i] = KEY_TAB) Then
minOffset += 1
dirty = True
Else
Exit
End If
Next
For Local i:Int = offset - 1 To minOffset Step - 1
If (_value[i] = KEY_SPACE Or _value[i] = KEY_TAB) Then
offset -= 1
dirty = True
Else
Exit
End If
Next
If (offset - minOffset = 0) Continue
If ( Not dirty) Then
_AddLine(minOffset, offset, textWidth)
Else
_AddLine(minOffset, offset, _fontObject.GetTextWidth(Self, minOffset, offset))
End If
minOffset = offset
If (success) minOffset += 1
maxOffset = Min(minOffset + range, Min(maxOffset + range, endPos + 1))
offset = maxOffset
Else
Local l:Int = endPos
For Local i:Int = minOffset Until l
If (_value[i] = KEY_SPACE Or _value[i] = KEY_TAB) Then
minOffset += 1
dirty = True
Else
Exit
End If
Next
For Local i:Int = l - 1 To minOffset Step - 1
If (_value[i] = KEY_SPACE Or _value[i] = KEY_TAB) Then
l -= 1
dirty = True
Else
Exit
End If
Next
If (l - minOffset <= 0) Exit
If ( Not dirty) Then
_AddLine(minOffset, l, finalTextWidth)
Else
_AddLine(minOffset, l, _fontObject.GetTextWidth(Self, minOffset, l))
End If
Exit
End If
Forever
Else
_AddLine(startPos, endPos, textWidth)
End If
End Method
Method _FindNewLine:Int(i:Int, l:Int)
Local offset:Int = -1
While (i < l)
If (_value[i] = 10 Or _value[i] = KEY_ENTER) Then
offset = i
If (_value[i] = KEY_ENTER) offset += 1
Exit
End If
i += 1
Wend
Return offset
End Method
Method _GetMinOffset:Int(startPos:Int, endPos:Int)
Local offset:Int = endPos - startPos
While (_fontObject.GetTextWidth(Self, startPos, startPos + offset) > width)
offset -= 1
If (offset = 1) Return offset
Wend
Return startPos + offset
End Method
Method _AddLine:FlxTextLine(startPos:Int, endPos:Int, width:Int)
Local line:FlxTextLine
If (_countLines = _lines.Length()) Then
_lines = _lines.Resize(_countLines * 2 + 10)
line = New FlxTextLine(startPos, endPos, width)
_lines[_countLines] = line
Else
line = _lines[_countLines]
If (line = Null) Then
line = New FlxTextLine(startPos, endPos, width)
_lines[_countLines] = line
Else
line.Reset(startPos, endPos, width)
End If
End If
_countLines += 1
Return line
End Method
End Class
Private
Class FlxTextLine
Field xOffset:Float
Field startPos:Int
Field endPos:Int
Field width:Int
Method New(startPos:Int, endPos:Int, width:Int)
Reset(startPos, endPos, width)
End Method
Method Reset:Void(startPos:Int, endPos:Int, width:Int)
Self.startPos = startPos
Self.endPos = endPos
Self.width = width
End Method
End Class
Class FontResource Extends FlxResource<FlxBitmapFont>
Field size:Int
Field font:FlxBitmapFont
Method New(fontFamily:String, fontSize:Int)
Super.New(fontFamily)
Self.size = fontSize
End Method
Method Load:FlxBitmapFont()
font = New FlxBitmapFont(FlxAssetsManager.GetFont(name).GetPath(size))
Return font
End Method
Method Use:FlxBitmapFont()
Return font
End Method
Method Discard:Void()
font.Discard()
font = Null
End Method
End Class
#If FLX_TEXT_DRIVER = "angelfont"
Class FlxBitmapFont
Private
Field image:Image[] = New Image[1]
Field chars:Char[256]
Field kernPairs:IntMap<IntMap<KernPair>> = New IntMap<IntMap<KernPair>>
Global firstKp:IntMap<KernPair>
Global secondKp:KernPair
Field iniText:String
Field xOffset:Int
Field yOffset:Int
Public
Field useKerning:Bool = True
Field height:Int = 0
Field heightOffset:Int = 9999
Field lineGap:Int = 5
Method New(url:String)
iniText = LoadString(url+".fnt")
Local lines:String[] = iniText.Split(String.FromChar(10))
Local firstLine:String = lines[0]
If firstLine.Contains("<?xml")
Local lineList:List<String> = New List<String>(lines)
lineList.RemoveFirst()
lines = lineList.ToArray()
iniText = "~n".Join(lines)
End
Local pageCount:Int = 0
Local config:= LoadConfig(iniText)
Local nodes:= config.FindNodesByPath("font/chars/char")
For Local node:= EachIn nodes
Local id:Int = Int(node.GetAttribute("id"))
Local page:Int = Int(node.GetAttribute("page"))
If pageCount < page pageCount = page
chars[id] = New Char(Int(node.GetAttribute("x")), Int(node.GetAttribute("y")), Int(node.GetAttribute("width")), Int(node.GetAttribute("height")), Int(node.GetAttribute("xoffset")), Int(node.GetAttribute("yoffset")), Int(node.GetAttribute("xadvance")), page)
Local ch := chars[id]
If ch.height > Self.height Self.height = ch.height
If ch.yOffset < Self.heightOffset Self.heightOffset = ch.yOffset
Next
nodes = config.FindNodesByPath("font/kernings/kerning")
If (nodes <> Null) Then
For Local node:= EachIn nodes
Local first:Int = Int(node.GetAttribute("first"))
firstKp = kernPairs.Get(first)
If firstKp = Null
kernPairs.Add(first, New IntMap<KernPair>)
firstKp = kernPairs.Get(first)
End
Local second:Int = Int(node.GetAttribute("second"))
firstKp.Add(second, New KernPair(first, second, Int(node.GetAttribute("amount"))))
End
Else
useKerning = False
End If
If pageCount = 0
image[0] = LoadImage(url+".png")
If image[0] = Null image[0] = LoadImage(url + "_0.png")
Else
For Local page:= 0 To pageCount
If image.Length < page+1 image = image.Resize(page+1)
image[page] = LoadImage(url+"_"+page+".png")
End
End
End
Method Discard:Void()
For Local i:Image = EachIn image
i.Discard()
Next
End Method
Method GetTextWidth:Float(txt:FlxText, startPos:Int = 0, endPos:Int = -1)
If (startPos = endPos) Return 0
Local prevChar:Int = 0
Local width:Float = 0
If (endPos < 0) endPos = txt._value.Length()
Local asc:Int, ac:Char
For Local i:= startPos Until endPos
asc = txt._value[i]
ac = chars[asc]
If ac <> Null
If useKerning
Local firstKp:= kernPairs.Get(prevChar)
If firstKp <> Null
Local secondKp:= firstKp.Get(asc)
If secondKp <> Null
xOffset += secondKp.amount
End
EndIf
prevChar = asc
EndIf
width += ac.xAdvance
EndIf
Next
Return width
End Method
Method GetFontHeight:Int(txt:FlxText)
Local h:Int = 0
If (txt._value.Length() <> 0) Then
Local l:Int = txt._value.Length()
For Local i:= 0 Until l
Local asc:Int = txt._value[i]
Local ac:Char = chars[asc]
If ac <> Null And ac.height + ac.yOffset > h h = ac.height + ac.yOffset
Next
Else
h = chars[KEY_SPACE].height + chars[KEY_SPACE].yOffset
End If
Return h
End
Method GetTextHeight:Int(txt:FlxText)
Return ( (txt._fontHeight + lineGap) * txt._countLines) - lineGap
End Method
Method DrawText:Void(txt:FlxText, x:Int, y:Int)
Local lineIndex:Int = 0, countLines:Int = txt._countLines, line:FlxTextLine
Local prevChar:Int = 0, xOffset:Int = 0, yOffset:Int = y
Local i:Int = 0, l:Int
Local asc:Int, ac:Char
Local lineHeight:Int = txt._fontHeight + lineGap
While (lineIndex < countLines)
line = txt._lines[lineIndex]
i = line.startPos
l = line.endPos
xOffset = x + line.xOffset
While (i < l)
asc = txt._value[i]
ac = chars[asc]
If ac <> Null
If useKerning
firstKp = kernPairs.Get(prevChar)
If firstKp <> Null
secondKp = firstKp.Get(asc)
If secondKp <> Null
xOffset += secondKp.amount
End
EndIf
prevChar = asc
Endif
ac.Draw(image[ac.page], xOffset, yOffset)
xOffset += ac.xAdvance
Endif
i += 1
Wend
lineIndex += 1
yOffset += lineHeight
Wend
End Method
End Class
#ElseIf FLX_TEXT_DRIVER = "fontmachine"
Class FlxBitmapFont
Method New(fontDescriptionFilePath:String)
Local text:String = LoadString(fontDescriptionFilePath + ".txt")
If text = "" Then Print "FONT " + fontDescriptionFilePath + " WAS NOT FOUND!!!"
_kerning = New drawingpoint.DrawingPoint
LoadFontData(text, fontDescriptionFilePath, False)
End
Method Discard:Void()
For Local b:BitMapChar = EachIn borderChars
b.UnloadCharImage()
Next
For Local b:BitMapChar = EachIn faceChars
b.UnloadCharImage()
Next
For Local b:BitMapChar = EachIn shadowChars
b.UnloadCharImage()
Next
For Local i:Image = EachIn packedImages
i.Discard()
Next
End Method
Method GetTextWidth:Float(text:FlxText, fromChar:Int = 0, toChar:Int = -1)
If (fromChar = toChar) Return 0
Local twidth:Float
Local char:Int
Local lastchar:Int = 0
If (toChar < 0) toChar = text._value.Length()
For Local i:Int = fromChar Until toChar
char = text._value[i]
If faceChars[char] <> Null Then
lastchar = char
twidth = twidth + faceChars[char].drawingMetrics.drawingWidth + _kerning.x
End If
Next
Return twidth - faceChars[lastchar].drawingMetrics.drawingWidth + faceChars[lastchar].drawingMetrics.drawingSize.x
End Method
Method GetTextHeight:Float(text:FlxText)
Return ( (text._fontHeight + _kerning.y) * text._countLines) - _kerning.y
End
Method GetFontHeight:Int(txt:FlxText)
If faceChars[32] = Null Then Return 0
Return faceChars[32].drawingMetrics.drawingSize.y
End Method
Method DrawText:Void(text:FlxText, x:Float, y:Float)
If (text._fontShadowEnabled) DrawChars(text, x, y, shadowChars)
If (text._fontBorderEnabled) DrawChars(text, x, y, borderChars)
DrawChars(text, x, y, faceChars)
End
Private
Field borderChars:BitMapChar[]
Field faceChars:BitMapChar[]
Field shadowChars:BitMapChar[]
Method LoadFontData:Void(Info:String, fontName:String, dynamicLoad:bool)
if Info.StartsWith("P1") Then
LoadPacked(Info,fontName,dynamicLoad)
return
EndIf
Local tokenStream:String[] = Info.Split(",")
local index:Int = 0
borderChars = New BitMapChar[65536]
faceChars = New BitMapChar[65536]
shadowChars = New BitMapChar[65536]
Local prefixName:String = fontName
if prefixName.ToLower().EndsWith(".txt") Then prefixName = prefixName[..-4]
Local char:Int = 0
While index < tokenStream.Length
Local strChar:String = tokenStream[index]
If strChar.Trim() = "" Then
index += 1
Exit
endif
char = int(strChar)
index += 1
Local kind:String = tokenStream[index]
index += 1
Select kind
Case "{BR"
index += 3
Local bmpChar:BitMapChar = New BitMapChar
Local drawingMetrics:BitMapCharMetrics = bmpChar.drawingMetrics
drawingMetrics.drawingOffset.x = Int(tokenStream[index])
drawingMetrics.drawingOffset.y = Int(tokenStream[index+1])
drawingMetrics.drawingSize.x = Int(tokenStream[index+2])
drawingMetrics.drawingSize.y = Int(tokenStream[index+3])
drawingMetrics.drawingWidth = Int(tokenStream[index+4])
if dynamicLoad = False then
bmpChar.image = LoadImage(prefixName + "_BORDER_" + char + ".png")
bmpChar.image.SetHandle(-borderChars[char].drawingMetrics.drawingOffset.x,-borderChars[char].drawingMetrics.drawingOffset.y)
Else
bmpChar.SetImageResourceName prefixName + "_BORDER_" + char + ".png"
endif
borderChars[char] = bmpChar
index += 5
index += 1
Case "{SH"
index += 3
Local bmpChar:BitMapChar = New BitMapChar
Local drawingMetrics:BitMapCharMetrics = bmpChar.drawingMetrics
drawingMetrics.drawingOffset.x = Int(tokenStream[index])
drawingMetrics.drawingOffset.y = Int(tokenStream[index+1])
drawingMetrics.drawingSize.x = Int(tokenStream[index+2])
drawingMetrics.drawingSize.y = Int(tokenStream[index+3])
drawingMetrics.drawingWidth = Int(tokenStream[index+4])
Local filename:String = prefixName + "_SHADOW_" + char + ".png"
if dynamicLoad = False then
bmpChar.image = LoadImage(filename)
bmpChar.image.SetHandle(-shadowChars[char].drawingMetrics.drawingOffset.x,-shadowChars[char].drawingMetrics.drawingOffset.y)
Else
bmpChar.SetImageResourceName filename
endif
shadowChars[char] = bmpChar
index+=5
index += 1
Case "{FC"
index += 3
Local bmpChar:BitMapChar = New BitMapChar
Local drawingMetrics:BitMapCharMetrics = bmpChar.drawingMetrics
drawingMetrics.drawingOffset.x = Int(tokenStream[index])
drawingMetrics.drawingOffset.y = Int(tokenStream[index+1])
drawingMetrics.drawingSize.x = Int(tokenStream[index+2])
drawingMetrics.drawingSize.y = Int(tokenStream[index+3])
drawingMetrics.drawingWidth = Int(tokenStream[index+4])
if dynamicLoad = False then
bmpChar.image = LoadImage(prefixName + "_" + char + ".png")
bmpChar.image.SetHandle(-faceChars[char].drawingMetrics.drawingOffset.x,-faceChars[char].drawingMetrics.drawingOffset.y)
Else
bmpChar.SetImageResourceName prefixName + "_" + char + ".png"
endif
faceChars[char] = bmpChar
index += 5
index += 1
Default
Print "Error loading font! Char = " + char
End
Wend
borderChars = borderChars[..char+1]
faceChars = faceChars[..char+1]
shadowChars = shadowChars[..char+1]
End
Field packedImages:Image[]
Method LoadPacked:Void(info:String, fontName:String, dynamicLoad:bool)
Local header:String = info[.. info.Find(",")]
Local separator:String
Select header
Case "P1"
separator = "."
Case "P1.01"
separator = "_P_"
End Select
info = info[info.Find(",")+1..]
borderChars = New BitMapChar[65536]
faceChars = New BitMapChar[65536]
shadowChars = New BitMapChar[65536]
packedImages = New Image[256]
Local maxPacked:Int = 0
Local maxChar:Int = 0
Local prefixName:String = fontName
if prefixName.ToLower().EndsWith(".txt") Then prefixName = prefixName[..-4]
Local charList:string[] = info.Split(";")
For local chr:String = EachIn charList
Local chrdata:string[] = chr.Split(",")
if chrdata.Length() <2 Then Exit
Local char:bitmapchar.BitMapChar
Local charIndex:Int = int(chrdata[0])
if maxChar<charIndex Then maxChar = charIndex
select chrdata[1]
Case "B"
borderChars[charIndex] = New BitMapChar
char = borderChars[charIndex]
Case "F"
faceChars [charIndex] = New BitMapChar
char = faceChars[charIndex]
Case "S"
shadowChars [charIndex] = New BitMapChar
char = shadowChars[charIndex]
End Select
char.packedFontIndex = Int(chrdata[2])
if packedImages[char.packedFontIndex] = null Then
packedImages[char.packedFontIndex] = LoadImage(prefixName + separator + char.packedFontIndex + ".png")
if maxPacked<char.packedFontIndex Then maxPacked = char.packedFontIndex
endif
char.packedPosition.x = Int(chrdata[3])
char.packedPosition.y = Int(chrdata[4])
char.packedSize.x = Int(chrdata[5])
char.packedSize.y = Int(chrdata[6])
char.drawingMetrics.drawingOffset.x = Int(chrdata[8])
char.drawingMetrics.drawingOffset.y = Int(chrdata[9])
char.drawingMetrics.drawingSize.x = Int(chrdata[10])
char.drawingMetrics.drawingSize.y = Int(chrdata[11])
char.drawingMetrics.drawingWidth = Int(chrdata[12])
Next
borderChars = borderChars[..maxChar+1]
faceChars = faceChars[..maxChar+1]
shadowChars = shadowChars[..maxChar+1]
packedImages = packedImages[..maxPacked+1]
End
Method DrawChars:Void(text:FlxText, x:Float, y:Float, target:BitMapChar[])
Local lineIndex:Int = 0, countLines:Int = text._countLines, line:FlxTextLine
Local drx:Int = x, dry:Int = y
Local i:Int = 0, l:Int
Local char:Int
Local lineHeight:Int = text._fontHeight + _kerning.y
Local targetLength:Int = target.Length()
While (lineIndex < countLines)
line = text._lines[lineIndex]
i = line.startPos
l = line.endPos
drx = x + line.xOffset
While (i < l)
char = text._value[i]
If char >= 0 And char <= targetLength And target[char] <> Null Then
If target[char].packedFontIndex > 0 Then
DrawImageRect(packedImages[target[char].packedFontIndex], drx + target[char].drawingMetrics.drawingOffset.x, dry + target[char].drawingMetrics.drawingOffset.y, target[char].packedPosition.x, target[char].packedPosition.y, target[char].packedSize.x, target[char].packedSize.y)