@@ -32,12 +32,15 @@ freely, subject to the following restrictions:
32
32
local rich = {}
33
33
rich .__index = rich
34
34
35
- function rich :new (t ) -- syntax: rt = rich.new{text, width, resource1 = ..., ...}
35
+ function rich :new (t , stdcolor ) -- syntax: rt = rich.new{text, width, resource1 = ..., ...}
36
36
local obj = setmetatable ({parsedtext = {}, resources = {}}, rich )
37
37
obj .width = t [2 ]
38
38
obj :extract (t )
39
39
obj :parse (t )
40
- if love .graphics .isSupported (' canvas' ) then
40
+ -- set text standard color
41
+ if stdcolor and type (stdcolor ) == ' table' then love .graphics .setColor ( unpack (stdcolor ) ) end
42
+ if love .graphics .isSupported and love .graphics .isSupported (' canvas' ) then
43
+ obj :render ()
41
44
obj :render (true )
42
45
end
43
46
return obj
90
93
91
94
function rich :parse (t )
92
95
local text = t [1 ]
93
- -- look for {tags} or [tags]
94
- for textfragment , foundtag in text :gmatch ' ([^{]*){(.-)}' do
95
- parsefragment (self .parsedtext , textfragment )
96
- table.insert (self .parsedtext , self .resources [foundtag ] or foundtag )
96
+ if string.len (text ) > 0 then
97
+ -- look for {tags} or [tags]
98
+ for textfragment , foundtag in text :gmatch ' ([^{]*){(.-)}' do
99
+ parsefragment (self .parsedtext , textfragment )
100
+ table.insert (self .parsedtext , self .resources [foundtag ] or foundtag )
101
+ end
102
+ parsefragment (self .parsedtext , text :match (' [^}]+$' ))
97
103
end
98
- parsefragment (self .parsedtext , text :match (' [^}]+$' ))
99
104
end
100
105
101
106
-- [[ since 0.8.0, no autopadding needed any more
@@ -142,26 +147,25 @@ end
142
147
143
148
local function wrapText (parsedtext , fragment , lines , maxheight , x , width , i , fnt )
144
149
-- find first space, split again later if necessary
145
- if x > 0 then
146
- local n = fragment :find (' ' , 1 , true )
147
- local lastn = n
148
- while n do
149
- local newx = x + fnt :getWidth (fragment :sub (1 , n - 1 ))
150
- if newx > width then
151
- break
152
- end
153
- lastn = n
154
- n = fragment :find (' ' , n + 1 , true )
150
+ local n = fragment :find (' ' , 1 , true )
151
+ local lastn = n
152
+ while n do
153
+ local newx = x + fnt :getWidth (fragment :sub (1 , n - 1 ))
154
+ if newx > width then
155
+ break
155
156
end
156
- n = lastn or (# fragment + 1 )
157
- -- wrapping
158
- parsedtext [i ] = fragment :sub (1 , n - 1 )
159
- table.insert (parsedtext , i + 1 , fragment :sub ((fragment :find (' [^ ]' , n ) or (n + 1 )) - 1 ))
160
- lines [# lines ].height = maxheight
161
- maxheight = 0
162
- x = 0
163
- table.insert (lines , {})
157
+ lastn = n
158
+ n = fragment :find (' ' , n + 1 , true )
164
159
end
160
+ n = lastn or (# fragment + 1 )
161
+ -- wrapping
162
+ parsedtext [i ] = fragment :sub (1 , n - 1 )
163
+ table.insert (parsedtext , i + 1 , fragment :sub ((fragment :find (' [^ ]' , n ) or (n + 1 )) - 1 ))
164
+ lines [# lines ].height = maxheight
165
+ maxheight = 0
166
+ x = 0
167
+ table.insert (lines , {})
168
+
165
169
return maxheight , 0
166
170
end
167
171
@@ -223,6 +227,10 @@ local function doDraw(lines)
223
227
y = y + line .height
224
228
for j , fragment in ipairs (line ) do
225
229
if fragment .type == ' string' then
230
+ -- remove leading spaces for new lines
231
+ if string.sub (fragment [1 ], 1 , 1 ) == ' ' then
232
+ fragment [1 ] = string.sub (fragment [1 ], 2 )
233
+ end
226
234
love .graphics .print (fragment [1 ], fragment .x , y - fragment .height )
227
235
if rich .debug then
228
236
love .graphics .rectangle (' line' , fragment .x , y - fragment .height , fragment .width , fragment .height )
@@ -241,6 +249,7 @@ local function doDraw(lines)
241
249
colorr ,colorg ,colorb ,colora = love .graphics .getColor ()
242
250
end
243
251
end
252
+
244
253
end
245
254
end
246
255
0 commit comments