Page 1 of 1

Color of colored text not kept by getWrap()

Posted: Tue Mar 08, 2016 4:50 pm
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:

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

Posted: Tue Mar 08, 2016 7:37 pm
by Nixola
Printf wraps text on its own, no need to use getWrap.

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

Posted: Tue Mar 08, 2016 7:43 pm
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)

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

Posted: Wed Mar 09, 2016 4:48 pm
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