Color of colored text not kept by getWrap()

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
deslua
Prole
Posts: 3
Joined: Tue Mar 08, 2016 4:31 pm

Color of colored text not kept by getWrap()

Post by deslua »

Hey! :)

In 0.10.0 colored text got introduced to be easily drawn using love.graphics.printf.
I want to print colored text line by line and do this using font:getWrap.
But apparently this function does not keep the color of the colored text!

Here is an example. The left and right block of text should look the same, but they don't: :o

Code: Select all

coloredtext = {
    {255,0,0}, "Red.........................",
    {0,255,0}, "Green.......................",
    {0,0,255}, "Blue........................",
}

function love.draw()
    font = love.graphics.newFont(24)
    love.graphics.setFont(font)
    love.graphics.printf(coloredtext, 0, 0, 100)

    width, wrappedtext = font:getWrap(coloredtext, 100)

    for i, t in ipairs(wrappedtext) do
        love.graphics.printf(t, 200, 30*(i-1), 100)
    end
end
Is there a workaround for this or am I overlooking something? :huh:
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Color of colored text not kept by getWrap()

Post by Nixola »

Printf wraps text on its own, no need to use getWrap.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
deslua
Prole
Posts: 3
Joined: Tue Mar 08, 2016 4:31 pm

Re: Color of colored text not kept by getWrap()

Post by deslua »

Nixola wrote:Printf wraps text on its own, no need to use getWrap.
Sure, but I only want to print certain lines of the colored text. Like:

Code: Select all

printFromTo(coloredtext, width, onlythisline, x, y)
deslua
Prole
Posts: 3
Joined: Tue Mar 08, 2016 4:31 pm

Re: Color of colored text not kept by getWrap()

Post by deslua »

I ended up making a function that behaves like getWrap but for colored text.
I really think a function like this should be added to a future version of LOVE!

Code: Select all

function getColoredWrap(font, ct, width)
    local wt      = {} -- wrapped text
    local ctTable = {} -- to be filled out and returned
    local ctRest  = {} -- to do list (sort of)

    _, wt = font:getWrap(ct, width)

    if type(ct) == "string" then -- string!
        return wt
    else -- colored text!
        for i, el in ipairs(ct) do -- copy ct to ctRest
            ctRest[i] = el
        end

        local i = 1 -- index for ctTable
        local j = 1 -- index for ctRest
        local k = 1 -- index for wt

        ctTable[1] = {}
        while ctRest[j+1] and wt[k] do
            from, to = string.find(wt[k], ctRest[j+1])
            if from and to then -- wt contains full ct line
                -- copy full ct line with color
                ctTable[k][i  ] = ctRest[j  ]
                ctTable[k][i+1] = ctRest[j+1]
                wt[k] = string.sub(wt[k], to + 1, -1)
                i = i + 2
                j = j + 2
            else -- wt is not containing a full ct line
                -- copy wt line in ct color and modify ct
                ctTable[k][i  ] = ctRest[j  ]
                ctTable[k][i+1] = wt[k]
                ctRest[j+1] = string.sub(ctRest[j+1], #wt[k] + 1, -1)
                i = 1
                k = k + 1
                ctTable[k] = {}
            end
        end
        return ctTable
    end
end
Löve,
Deslua
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 7 guests