forked from AlexanderLindholt/TextPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextPlus.luau
More file actions
1783 lines (1553 loc) · 48.6 KB
/
TextPlus.luau
File metadata and controls
1783 lines (1553 loc) · 48.6 KB
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
--!optimize 2
--!native
--[[
TTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTT
TT TTTTT ttttt
TTTTTT xxx tttttt
TTTTTT eeeeeeeeee xxxxxxx xxxxxx tttttt +++++
TTTTTT eeeeeeeeeeeeee xxxxxx xxxxx ttttttttttttt +++++
TTTTTT eeeeeee eeeeee xxxxxx xxxxx ttttttttttttt +++++
TTTTTT eeeeee eeeee xxxxxxxxxxx tttttt +++++++++++
TTTTTT eeeeeeeeeeeeeeeeee xxxxxxxxxx tttttt +++++++++++++++++++
TTTTTT eeeeeeeeeeeeeeeeeee xxxxxxxxx tttttt +++++++++++++++++++
TTTTTT eeeee ee xxxxxxxxxxx tttttt +++ +++++
TTTTTT eeeeee xxxxx xxxxxx tttttt +++++
TTTTTT eeeeee eeeeeee xxxxx xxxxxxx tttttt +++++
TTTTTT eeeeeeeeeeeeeee xxxxxx xxxxxx ttttttttt +++++
eeeeeeeeee xxxxxx ttttttttt
ttttttt
v1.26.0
An efficient, robust, open-source text-rendering library for
Roblox, featuring custom fonts and advanced text control.
GitHub:
https://github.com/AlexanderLindholt/TextPlus
DevForum:
https://devforum.roblox.com/t/3521684
--------------------------------------------------------------------------------
MIT License
Copyright (c) 2025 Alexander Lindholt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--------------------------------------------------------------------------------
]]--
-- Services.
local CollectionService = game:GetService("CollectionService")
local TextService = game:GetService("TextService")
local Players = game:GetService("Players")
-- Signal library.
local Signal = CollectionService:GetTagged("Signal")[1]
if Signal then
Signal = require(Signal)
if type(Signal) == "table" and Signal.new then Signal = Signal.new end
end
-- User fonts.
local fonts = CollectionService:GetTagged("Fonts")[1]
if fonts then fonts = require(fonts) end
-- Player camera.
local camera = workspace.CurrentCamera
-- Character for when a character is missing in custom fonts.
local missingCharacter = "rbxassetid://75989824347198"
-- Lists for validity checks.
local optionsList = {
Font = true,
Size = true,
ScaleSize = true,
MinimumSize = true,
MaximumSize = true,
Color = true,
Transparency = true,
Pixelated = true,
Offset = true,
Rotation = true,
StrokeSize = true,
StrokeColor = true,
StrokeTransparency = true,
ShadowOffset = true,
ShadowColor = true,
ShadowTransparency = true,
LineHeight = true,
CharacterSpacing = true,
Truncate = true,
XAlignment = true,
YAlignment = true,
WordSorting = true,
LineSorting = true,
Dynamic = true
}
local scaleSizeTypes = {
RootX = true,
RootY = true,
RootXY = true,
FrameX = true,
FrameY = true,
FrameXY = true,
}
local xAlignments = {
Left = true,
Center = true,
Right = true,
Justified = true
}
local yAlignments = {
Top = true,
Center = true,
Bottom = true,
Justified = true
}
-- Option defaults.
local defaults = {
Font = Font.new("rbxasset://fonts/families/SourceSansPro.json"),
Size = 14,
ScaleSize = nil,
MinimumSize = nil,
MaximumSize = nil,
Color = Color3.fromRGB(0, 0, 0),
Transparency = 0,
Pixelated = false,
Offset = Vector2.zero,
Rotation = 0,
StrokeSize = 5,
StrokeColor = Color3.fromRGB(0, 0, 0),
ShadowOffset = Vector2.new(0, 20),
ShadowColor = Color3.fromRGB(50, 50, 50),
LineHeight = 1,
CharacterSpacing = 1,
Truncate = false,
XAlignment = "Left",
YAlignment = "Top",
WordSorting = false,
LineSorting = false,
Dynamic = false
}
local userDefaults = CollectionService:GetTagged("TextDefaults")[1]
if userDefaults then
userDefaults = require(userDefaults)
if type(userDefaults) == "table" then
for key in userDefaults do
if optionsList[key] then defaults[key] = userDefaults[key] end
end
end
end
for key, value in defaults do
if value == false then defaults[key] = nil end
end
-- Instance tables for recycling.
local textLabelsAmount, textLabels = 0, {}
local imageLabelsAmount, imageLabels = 0, {}
local uiStrokesAmount, uiStrokes = 0, {}
local foldersAmount, folders = 0, {}
-- Instance retrieve functions.
local function getTextLabel()
local instance = textLabels[textLabelsAmount]
if not instance then
textLabelsAmount += 1
return Instance.new("TextLabel")
end
textLabels[textLabelsAmount] = nil
textLabelsAmount -= 1
return instance
end
local function getImageLabel()
local instance = imageLabels[imageLabelsAmount]
if not instance then
imageLabelsAmount += 1
return Instance.new("ImageLabel")
end
imageLabels[imageLabelsAmount] = nil
imageLabelsAmount -= 1
return instance
end
local function getUIStroke()
local instance = uiStrokes[uiStrokesAmount]
if not instance then
uiStrokesAmount += 1
return Instance.new("UIStroke")
end
uiStrokes[uiStrokesAmount] = nil
uiStrokesAmount -= 1
return instance
end
local function getFolder()
local instance = folders[foldersAmount]
if not instance then
foldersAmount += 1
return Instance.new("Folder")
end
folders[foldersAmount] = nil
foldersAmount -= 1
return instance
end
-- Frame data tables.
local frameText = {}
local frameOptions = {}
local frameTextBounds = {}
local frameSizeConnections = {}
local frameUpdateSignals = if Signal then {} else nil
-- Roblox built-in text rendering stuff.
local textBoundsParams = Instance.new("GetTextBoundsParams")
textBoundsParams.Size = 100 -- Size limit for Roblox's built-in text-rendering.
local characterWidthCache = {}
-- Table for raw user fonts.
local rawFonts = {}
-- Verify and preload user fonts if any.
if fonts then
if type(fonts) ~= "table" then
warn("Font data is not a table.")
else
if not next(fonts) then
warn("Font data table is empty.")
else
local player = Players.LocalPlayer
local load = nil
if player then -- If running on client.
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = player.PlayerGui
local loading = 0
load = function(image) -- For preloading the font image assets.
-- Increment counter for currently loading images.
loading += 1
-- Setup image label for loading the current font.
local label = Instance.new("ImageLabel")
label.Size = UDim2.fromOffset(1, 1) -- As small as possible.
label.BackgroundTransparency = 1
label.ImageTransparency = 0.999 -- Trick to make the image invisible and still have it be loaded.
label.ResampleMode = Enum.ResamplerMode.Pixelated
label.Image = "rbxassetid://"..tostring(image)
label.Parent = screenGui -- It's crucial that we put it in a visible ScreenGui, otherwise it won't be loaded.
-- Detect load.
coroutine.resume(coroutine.create(function()
repeat
task.wait()
until label.IsLoaded
if loading == 1 then
screenGui:Destroy()
else
loading -= 1
end
end))
end
end
local function handleCharacters(characters, size)
local invertedFontSize = 1/size -- To avoid expensive division.
for key, value in characters do
-- Verify format.
if type(key) ~= "string" then return end
if type(value) ~= "table" then return end
if type(value[1]) ~= "number" then return end
if type(value[2]) ~= "number" then return end
if typeof(value[3]) ~= "Vector2" then return end
if type(value[4]) ~= "number" then return end
if type(value[5]) ~= "number" then return end
if type(value[6]) ~= "number" then return end
-- Precalculate normalized offset and x-advance.
value[4] *= invertedFontSize
value[5] *= invertedFontSize
value[6] *= invertedFontSize
end
return true
end
local function processFonts(parent, parentPath)
local remove = {} -- Because immediate removal will throw off the loop.
local freeze = {} -- Because freezing before removal will not allow for removal.
for key, value in parent do
if type(value) ~= "table" then
table.insert(remove, key)
else
local currentPath = parentPath.."."..key
if value.Image or value.Size or value.Characters then
-- Verify format.
if type(value.Image) ~= "number" then
warn("Missing an image id at '"..currentPath.."'.")
table.insert(remove, key)
continue
end
if type(value.Size) ~= "number" then
warn("Missing a size at '"..currentPath.."'.")
table.insert(remove, key)
continue
end
if type(value.Characters) ~= "table" then
warn("Missing characters at '"..currentPath.."'.")
table.insert(remove, key)
continue
end
if not handleCharacters(value.Characters, value.Size) then -- If not valid characters then.
warn("Invalid characters at '"..currentPath.."'.")
table.insert(remove, key)
continue
end
-- Insert for later freeze.
table.insert(freeze, key)
-- Insert the font into raw fonts table.
rawFonts[value] = true
-- Preload images.
if player then -- If running on client.
load(value.Image)
end
else
processFonts(value, currentPath)
table.freeze(value)
end
end
end
for _, key in remove do
parent[key] = nil
end
for _, key in freeze do
table.freeze(parent[key])
end
end
processFonts(fonts, "Fonts")
table.freeze(fonts)
end
end
end
-- Global types.
export type CustomFont = {
Image: number,
Size: number,
Characters: {
[string]: {}
}
}
export type Options = {
Font: Font | CustomFont?,
Size: number?,
ScaleSize:
"RootX" | "RootY" | "RootXY" |
"FrameX" | "FrameY" | "FrameXY"?,
MinimumSize: number?,
MaximumSize: number?,
Color: Color3?,
Transparency: number?,
Pixelated: boolean?,
Offset: Vector2?,
Rotation: number?,
StrokeSize: number?,
StrokeColor: Color3?,
StrokeTransparency: number?,
ShadowOffset: Vector2?,
ShadowColor: number?,
ShadowTransparency: number?,
LineHeight: number?,
CharacterSpacing: number?,
Truncate: boolean?,
XAlignment: "Left" | "Center" | "Right" | "Justified"?,
YAlignment: "Top" | "Center" | "Bottom" | "Justified"?,
WordSorting: boolean?,
LineSorting: boolean?,
Dynamic: boolean?
}
-- Local types.
type Connection = {
Connected: boolean,
Disconnect: typeof(
-- Erases the connection.
function(connection: Connection) end
)
}
type Signal<Parameters...> = {
Connect: typeof(
-- Connects a function.
function(signal: Signal<Parameters...>, callback: (Parameters...) -> ()): Connection end
),
Once: typeof(
-- Connects a function, then auto-disconnects after the first call.
function(signal: Signal<Parameters...>, callback: (Parameters...) -> ()): Connection end
),
Wait: typeof(
-- Yields the calling thread until the next fire.
function(signal: Signal<Parameters...>): Parameters... end
),
Fire: typeof(
-- Runs all connected functions, and resumes all waiting threads.
function(signal: Signal<Parameters...>, ...: Parameters...) end
),
DisconnectAll: typeof(
-- Erases all connections.<br>
-- <em>Much faster than calling <code>Disconnect</code> on each.</em>
function(signal: Signal<Parameters...>) end
),
Destroy: typeof(
-- Erases all connections and methods, making the signal unusable.<br>
-- Remove references to the signal to delete it completely.
function(signal: Signal<Parameters...>) end
)
}
-- Module.
local module = {}
--[[
Returns the last rendered text string for a frame.
]]--
module.GetText = function(frame: GuiObject): Options
-- Get, verify and return text.
local text = frameText[frame]
if not text then error("Invalid frame.", 2) end
return text
end
--[[
Returns the options for a frame.
]]--
module.GetOptions = function(frame: GuiObject): Options
-- Get, verify and return options.
local options = frameOptions[frame]
if not options then error("Invalid frame.", 2) end
return frameOptions[frame]
end
--[[
Returns the text bounds of a frame.
]]--
module.GetTextBounds = function(frame: GuiObject): Vector2
-- Get, verify and return text bounds.
local textBounds = frameTextBounds[frame]
if not textBounds then error("Invalid frame.", 2) end
return textBounds
end
--[[
Returns the update signal for a frame.
]]--
module.GetUpdateSignal = function(frame: GuiObject): Signal
-- Get, verify and return signal.
local signal = frameUpdateSignals[frame]
if not signal then error("Invalid frame.", 2) end
return signal
end
--[[
Returns a function for iterating through all characters in a frame.
<em>Ignores sorting folders.
Works with any sorting.</em>
]]--
module.GetCharacters = function(frame: GuiObject): {TextLabel | ImageLabel}
-- Get and verify options.
local options = frameOptions[frame]
if not options then error("Invalid frame.", 2) end
-- Create and return iterator.
return coroutine.wrap(function()
-- Identify sorting.
local lineSorting, wordSorting = options.LineSorting, options.WordSorting
if lineSorting and wordSorting then -- Full sorting.
-- Global character counter.
local index = 0
-- Loop through lines.
for _, line in frame:GetChildren() do
-- Verify instance.
if not line:IsA("Folder") then continue end
-- Loop through words.
for _, word in line:GetChildren() do
-- Loop through characters.
for _, character in word:GetChildren() do
-- Increment global character counter.
index += 1
-- Pass parameters to loop.
coroutine.yield(index, character)
end
end
end
elseif lineSorting or wordSorting then -- One sorting.
-- Global character counter.
local index = 0
-- Loop through words/lines.
for _, folder in frame:GetChildren() do
-- Verify instance.
if not folder:IsA("Folder") then continue end
-- Loop through characters.
for _, character in folder:GetChildren() do
-- Increment global character counter.
index += 1
-- Pass parameters to loop.
coroutine.yield(index, character)
end
end
else -- No sorting.
-- Identify character instance class for verification.
local characterClass = if typeof(options.Font) == "Font" then "TextLabel" else "ImageLabel"
-- Loop through characters.
for index, character in frame:GetChildren() do
-- Verify instance.
if not character:IsA(characterClass) then continue end
-- Pass parameters to loop.
coroutine.yield(index, character)
end
end
end)
end
local function clear(frame)
-- Get options.
local options = frameOptions[frame]
-- Identify character instance class and storage table.
local characterTable, characterClass = nil
if type(options.Font) == "table" then
characterTable = imageLabels
characterClass = "ImageLabel"
else
characterTable = textLabels
characterClass = "TextLabel"
end
-- Setup character stashing.
local function stashCharacter(character)
-- Remove and store character instance.
character.Parent = nil
table.insert(characterTable, character)
-- Remove and store character's stroke if existent.
local stroke = character:FindFirstChildOfClass("UIStroke")
if stroke then
stroke.Parent = nil
table.insert(uiStrokes, stroke)
end
-- Remove and store the main character if this is a shadow.
local main = character:FindFirstChildOfClass(characterClass)
if main then
-- Remove and store the main character instance.
main.Parent = nil
table.insert(characterTable, main)
-- Remove and store the main character's stroke if existent.
local mainStroke = main:FindFirstChildOfClass("UIStroke")
if mainStroke then
mainStroke.Parent = nil
table.insert(uiStrokes, mainStroke)
end
end
end
-- Identify sorting.
local lineSorting, wordSorting = options.LineSorting, options.WordSorting
if lineSorting and wordSorting then -- Full sorting.
-- Loop through lines.
for _, line in frame:GetChildren() do
-- Verify instance.
if not line:IsA("Folder") then continue end
-- Remove and store line folder.
line.Parent = nil
table.insert(folders, line)
-- Loop through words.
for _, word in line:GetChildren() do
-- Remove and store word folder.
word.Parent = nil
table.insert(folders, word)
-- Loop through characters.
for _, character in word:GetChildren() do
stashCharacter(character)
end
end
end
elseif lineSorting or wordSorting then -- One sorting.
-- Loop through words/lines.
for _, folder in frame:GetChildren() do
-- Verify instance.
if not folder:IsA("Folder") then continue end
-- Remove and store word/line folder.
folder.Parent = nil
table.insert(folders, folder)
-- Loop through characters.
for _, character in folder:GetChildren() do
stashCharacter(character)
end
end
else -- No sorting.
-- Loop through characters.
for _, character in frame:GetChildren() do
-- Verify instance.
if not character:IsA(characterClass) then continue end
stashCharacter(character)
end
end
end
local function render(frame, text, options)
-- Cache frame size.
local frameSize = frame.AbsoluteSize
local frameWidth = frameSize.X
local frameHeight = frameSize.Y
-- Options values.
local font = options.Font
local size = options.Size
local color = options.Color
local transparency = options.Transparency
local pixelated = options.Pixelated
local offset = options.Offset local offsetX, offsetY
local rotation = options.Rotation
local strokeSize = options.StrokeSize
local shadowOffset = options.ShadowOffset local shadowOffsetX, shadowOffsetY
local lineHeight = options.LineHeight
local characterSpacing = options.CharacterSpacing
local truncationEnabled = options.Truncate
local xAlignment = options.XAlignment
local yAlignment = options.YAlignment
local wordSorting = options.WordSorting
local lineSorting = options.LineSorting
local scaleSize = options.ScaleSize
if scaleSize then
-- Scale size.
if scaleSize:sub(1, 1) == "R" then -- Relative to root.
-- Find root size.
local root = frame:FindFirstAncestorOfClass("GuiBase")
local rootSize =
if root then
if root:IsA("ScreenGui") then
camera.ViewportSize
else
root.AbsoluteSize
else
Vector2.zero
-- Scale size.
if scaleSize == "RootX" then
size = size*0.01*rootSize.X
elseif scaleSize == "RootY" then
size = size*0.01*rootSize.Y
else
size = size*0.01*(rootSize.X + rootSize.Y)/2
end
else -- Relative to frame.
if scaleSize == "FrameX" then
size = size*0.01*frameWidth
elseif scaleSize == "FrameY" then
size = size*0.01*frameHeight
else
size = size*0.01*(frameWidth + frameHeight)/2
end
end
-- Limit scaled size.
if size < 1 then
size = 1
else
-- Custom limits.
local minimumSize = options.MinimumSize
if minimumSize and options.Size < minimumSize then
options.Size = minimumSize
end
local maximumSize = options.MaximumSize
if maximumSize and options.Size > maximumSize then
options.Size = maximumSize
end
-- Roblox font limit.
if type(font) ~= "table" and size > 100 then
size = 100
end
end
-- Ensure integer size.
size = math.round(size)
-- Scale the related options.
offsetX, offsetY = math.round(offset.X*0.01*size), math.round(offset.Y*0.01*size)
if strokeSize then strokeSize = math.round(strokeSize*0.01*size) end
if shadowOffset then shadowOffsetX, shadowOffsetY = math.round(shadowOffset.X*0.01*size), math.round(shadowOffset.Y*0.01*size) end
else
-- Ensure integer size.
size = math.round(size)
-- Save offsets in optimized format.
offsetX, offsetY = offset.X, offset.Y
if shadowOffset then shadowOffsetX, shadowOffsetY = shadowOffset.X, shadowOffset.Y end
end
lineHeight *= size
-- Setup character functions.
local getCharacterWidth, createCharacter
do
if type(font) == "table" then
-- Custom font.
local image = "rbxassetid://"..tostring(font.Image)
local scaleFactor = size/font.Size
local characters = font.Characters
local resampleMode = if pixelated then Enum.ResamplerMode.Pixelated else Enum.ResamplerMode.Default
--[[
Character data (table):
[1] = number - Size x
[2] = number - Size y
[3] = Vector2 - Image offset
[4] = number - Offset x
[5] = number - Offset y
[6] = number - X advance
]]--
getCharacterWidth = function(character)
local data = characters[character]
return if data then
data[6]*size*characterSpacing
else -- Missing character.
size*characterSpacing -- The 'missing' character is square, so height and width is the same.
end
if shadowOffset then
-- Shadow.
local shadowColor = options.ShadowColor
local shadowTransparency = options.ShadowTransparency
createCharacter = function(character, x, y)
-- Calculate information.
local data = characters[character]
if data then
-- Cache character data.
local width = data[1]
local height = data[2]
local imageSize = Vector2.new(width, height)
local imageOffset = data[3]
-- Calculate position and size.
local realX = x + data[4]*size
local realY = y + data[5]*size
local characterSize = UDim2.fromOffset(
math.round(realX + width*scaleFactor) - math.round(realX),
math.round(realY + height*scaleFactor) - math.round(realY)
)
-- Character shadow.
local shadow = getImageLabel()
do
-- Stylize.
shadow.BackgroundTransparency = 1
shadow.Image = image
shadow.ImageColor3 = shadowColor
shadow.ImageTransparency = shadowTransparency
shadow.ResampleMode = resampleMode
-- Image cutout.
shadow.ImageRectSize = imageSize
shadow.ImageRectOffset = imageOffset
-- Transformation.
shadow.Size = characterSize
shadow.Position = UDim2.fromOffset(
math.round(realX) + offsetX + shadowOffsetX,
math.round(realY) + offsetY + shadowOffsetY
)
shadow.Rotation = rotation
end
-- Main character.
do
-- Create and stylize.
local main = getImageLabel()
main.BackgroundTransparency = 1
main.Image = image
main.ImageColor3 = color
main.ImageTransparency = transparency
main.ResampleMode = resampleMode
-- Image cutout.
main.ImageRectSize = imageSize
main.ImageRectOffset = imageOffset
-- Transformation.
main.Size = characterSize
main.Position = UDim2.fromOffset(-shadowOffsetX, -shadowOffsetY) -- Counteract the shadow offset.
-- Name and parent.
main.Name = "Main"
main.Parent = shadow
end
-- Return character instance.
return shadow
else -- Missing character.
-- Create and stylize.
local imageLabel = getImageLabel()
imageLabel.BackgroundTransparency = 1
imageLabel.Image = missingCharacter
imageLabel.ImageColor3 = color
imageLabel.ImageTransparency = transparency
imageLabel.ResampleMode = resampleMode
-- Transformation.
imageLabel.Size = UDim2.fromOffset(size, size)
imageLabel.Position = UDim2.fromOffset(
x + offsetX,
y + offsetY
)
imageLabel.Rotation = rotation
-- Return character instance.
return imageLabel
end
end
else
-- No shadow.
createCharacter = function(character, x, y)
local data = characters[character]
if data then
-- Create and stylize.
local imageLabel = getImageLabel()
imageLabel.BackgroundTransparency = 1
imageLabel.Image = image
imageLabel.ImageColor3 = color
imageLabel.ImageTransparency = transparency
imageLabel.ResampleMode = resampleMode
-- Image cutout.
local width = data[1]
local height = data[2]
imageLabel.ImageRectSize = Vector2.new(width, height)
imageLabel.ImageRectOffset = data[3]
-- Transformation.
local realX = x + data[4]*size
local realY = y + data[5]*size
imageLabel.Size = UDim2.fromOffset(
math.round(realX + width*scaleFactor) - math.round(realX),
math.round(realY + height*scaleFactor) - math.round(realY)
)
imageLabel.Position = UDim2.fromOffset(
math.round(realX) + offsetX,
math.round(realY) + offsetY
)
imageLabel.Rotation = rotation
-- Return character instance.
return imageLabel
else -- Missing character.
-- Create and stylize.
local imageLabel = getImageLabel()
imageLabel.BackgroundTransparency = 1
imageLabel.Image = missingCharacter
imageLabel.ImageColor3 = color
imageLabel.ImageTransparency = transparency
-- Transformation.
imageLabel.Size = UDim2.fromOffset(size, size)
imageLabel.Position = UDim2.fromOffset(
x + offsetX,
y + offsetY
)
imageLabel.Rotation = rotation
-- Return character instance.
return imageLabel
end
end
end
else
-- Roblox font.
local strokeColor, strokeTransparency
if strokeSize then
if strokeSize < 1 then strokeSize = 1 end -- Limit again, in case it was scaled with size.
strokeColor = options.StrokeColor
strokeTransparency = options.StrokeTransparency
end
local invertedCharacterSpacing = 1/characterSpacing -- To avoid expensive division.
local fontKey = font.Family..tostring(font.Weight.Value)..tostring(font.Style.Value)
getCharacterWidth = function(character)
local characterKey = character..fontKey
local width = characterWidthCache[characterKey]
if not width then
textBoundsParams.Text = character
width = TextService:GetTextBoundsAsync(textBoundsParams).X*0.01
characterWidthCache[characterKey] = width
end
return width*size*characterSpacing
end
if shadowOffset then
-- Shadow.
local shadowColor = options.ShadowColor
local shadowTransparency = options.ShadowTransparency
createCharacter = function(character, x, y, width)
-- Calculate size.
local characterSize = UDim2.fromOffset(math.round(width*invertedCharacterSpacing), size)
-- Character shadow.
local shadow = getTextLabel()
do
-- Stylize.
shadow.BackgroundTransparency = 1
shadow.Text = character
shadow.TextSize = size
shadow.TextColor3 = shadowColor
shadow.TextTransparency = shadowTransparency
shadow.FontFace = font
shadow.TextXAlignment = Enum.TextXAlignment.Left
shadow.TextYAlignment = Enum.TextYAlignment.Top
-- Transformation.
shadow.Size = characterSize
shadow.Rotation = rotation
shadow.Position = UDim2.fromOffset(
x + offsetX + shadowOffsetX,
y + offsetY + shadowOffsetY
)
end
-- Main character.
local main = getTextLabel()
do