-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrichtext.lua
198 lines (155 loc) · 4.55 KB
/
richtext.lua
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
local RichTextFuncs = {}
local ELEMENT_TAGS = {
['Font'] = 'font',
Stroke = 'stroke',
Bold = 'b',
Italic = 'i',
Underline = 'u',
Strikethrough = 's',
Uppercase = 'uc',
SmallCaps = 'sc'
}
local ATTRIBUTE_TAGS = {
Color = 'color',
Size = 'size',
Face = 'face',
Family = 'family',
Weight = 'weight',
Transparency = 'transparency',
Joins = 'joins',
Thickness = 'thickness'
}
function RichTextFuncs:ToString()
local text = self.BaseText
for el_name, val in self do
local tag = ELEMENT_TAGS[el_name]
if tag == nil then continue end
local opening_tag = tag
if type(val) == 'table' then
if next(val) == nil then continue end
for attr_name, attr_val in val do
local attr_tag = ATTRIBUTE_TAGS[attr_name]
if attr_tag == nil then continue end
local attr_val_str = ''
if typeof(attr_val) == 'Color3' then
attr_val_str = 'rgb('..tostring(math.round(attr_val.R * 255))..','..tostring(math.round(attr_val.G * 255))..','..tostring(math.round(attr_val.B * 255))..')'
else
attr_val_str = tostring(attr_val)
end
opening_tag ..= ' '..attr_tag..'="'..attr_val_str..'"'
end
elseif val == false then
continue
end
text = '<'..opening_tag..'>' .. text .. '</'..tag..'>'
end
return text
end
function RichTextFuncs:SetFont(fontSettings)
self.Font = fontSettings
return self
end
function RichTextFuncs:SetFontColor(color: Color3?)
self.Font.Color = color
return self
end
function RichTextFuncs:SetFontSize(size: number?)
self.Font.Size = size
return self
end
function RichTextFuncs:SetFontFace(face: string?)
self.Font.Face = face
return self
end
function RichTextFuncs:SetFontFamily(family: string?)
self.Font.Family = family
return self
end
function RichTextFuncs:SetFontWeight(weight: string? | number?)
self.Font.Weight = weight
return self
end
function RichTextFuncs:SetFontTransparency(transparency: number?)
self.Font.Transparency = transparency
return self
end
function RichTextFuncs:SetStroke(strokeSettings)
self.Stroke = strokeSettings
return self
end
function RichTextFuncs:SetStrokeColor(color: Color3?)
self.Stroke.Color = color
return self
end
function RichTextFuncs:SetStrokeJoins(joins: string?)
self.Stroke.Joins = joins
return self
end
function RichTextFuncs:SetStrokeThickness(thickness: number?)
self.Stroke.Thickness = thickness
return self
end
function RichTextFuncs:SetStrokeTransparency(transparency: number?)
self.Stroke.Transparency = transparency
return self
end
function RichTextFuncs:SetBold(val: boolean?)
self.Bold = if val ~= nil then val else true
return self
end
function RichTextFuncs:SetItalic(val: boolean?)
self.Italic = if val ~= nil then val else true
return self
end
function RichTextFuncs:SetUnderline(val: boolean?)
self.Underline = if val ~= nil then val else true
return self
end
function RichTextFuncs:SetStrikethrough(val: boolean?)
self.Strikethrough = if val ~= nil then val else true
return self
end
function RichTextFuncs:SetUppercase(val: boolean?)
self.Uppercase = if val ~= nil then val else true
return self
end
function RichTextFuncs:SetSmallCaps(val: boolean?)
self.SmallCaps = if val ~= nil then val else true
return self
end
local RichText = {}
RichText.LineBreak = '<br />'
RichText.LessThan = '<'
RichText.GreaterThan = '>'
RichText.Quote = '"'
RichText.Apostrophe = '''
RichText.Ampersand = '&'
function RichText.new(text: string?)
local this = {}
this.BaseText = text or ''
this.Font = {
Color = nil :: Color3?,
Size = nil :: number?,
Face = nil :: string?,
Family = nil :: string?,
Weight = nil :: string? | number?,
Transparency = nil :: number?
}
this.Stroke = {
Color = nil :: Color3?,
Joins = nil :: string?,
Thickness = nil :: number?,
Transparency = nil :: number?
}
this.Bold = false
this.Italic = false
this.Underline = false
this.Strikethrough = false
this.Uppercase = false
this.SmallCaps = false
return setmetatable(this, {
__index = RichTextFuncs,
__tostring = RichTextFuncs.ToString
})
end
return RichText