Skip to content

Commit cb6f229

Browse files
committed
-update to suppor a standard color
-sample screen added
1 parent 2395f17 commit cb6f229

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

richtext.lua

+34-25
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ freely, subject to the following restrictions:
3232
local rich = {}
3333
rich.__index = rich
3434

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 = ..., ...}
3636
local obj = setmetatable({parsedtext = {}, resources = {}}, rich)
3737
obj.width = t[2]
3838
obj:extract(t)
3939
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()
4144
obj:render(true)
4245
end
4346
return obj
@@ -90,12 +93,14 @@ end
9093

9194
function rich:parse(t)
9295
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('[^}]+$'))
97103
end
98-
parsefragment(self.parsedtext, text:match('[^}]+$'))
99104
end
100105

101106
-- [[ since 0.8.0, no autopadding needed any more
@@ -142,26 +147,25 @@ end
142147

143148
local function wrapText(parsedtext, fragment, lines, maxheight, x, width, i, fnt)
144149
-- 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
155156
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)
164159
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+
165169
return maxheight, 0
166170
end
167171

@@ -223,6 +227,10 @@ local function doDraw(lines)
223227
y = y + line.height
224228
for j, fragment in ipairs(line) do
225229
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
226234
love.graphics.print(fragment[1], fragment.x, y - fragment.height)
227235
if rich.debug then
228236
love.graphics.rectangle('line', fragment.x, y - fragment.height, fragment.width, fragment.height)
@@ -241,6 +249,7 @@ local function doDraw(lines)
241249
colorr,colorg,colorb,colora = love.graphics.getColor()
242250
end
243251
end
252+
244253
end
245254
end
246255

sample.png

22.9 KB
Loading

0 commit comments

Comments
 (0)