Page 1 of 3

[Library] Popo

Posted: Mon Jan 19, 2015 6:17 am
by adnzzzzZ
Hi. I've created a library for getting programmable text happening with LÖVE. The idea is that you can program the text in a character based fashion so that you can hopefully achieve a huge number of effects just like you can when you're programming anything else.

Popo

Re: [Library] Popo

Posted: Mon Jan 19, 2015 7:20 am
by Muris
Looks really nice. I was just thinking the other day how I would do a textbox with varying colors for "important names" inside a textbox and this looks like it could solve that problem.

Would it be possible to use different fonts/fontsizes in one string? Like using bolded text on some parts of the text. Not that I am in need for such thing, but just out of curiosity.

I didn't try using it, but did a quick glimpse on the code, I guess you cannot use text alignment with it by default, but that would be just a matter of changing the print into printf.

Edit: Actually I guess that doesn't work. I guess the code is printing one letter per time with the print.

Re: [Library] Popo

Posted: Mon Jan 19, 2015 7:28 am
by adnzzzzZ
Muris wrote: Would it be possible to use different fonts/fontsizes in one string? Like using bolded text on some parts of the text. Not that I am in need for such thing, but just out of curiosity.
I don't think this is possible right now. I could implement it in the future though. One way you can get different sizes on certain characters though is by playing with the sx, sy parameters which control the character's scale. As for bolding I'm not sure if there's a good way that will look as good as if you were doing it with a bold font, but drawing the character multiple times with small offsets could work. To do that just check how I draw each character internally and copy that.

Re: [Library] Popo

Posted: Mon Jan 19, 2015 4:28 pm
by davisdude
Nice library! Looks useful.
For what Muris mentioned you could try implementing something found in Robin's richtext.

Re: [Library] Popo

Posted: Mon Jan 19, 2015 4:45 pm
by SiENcE
@adnzzzzZ
I already saw it on github today and it's very nice!

It's somewhat similar to richtext as davisdude already mentioned. I would appreciate a more advanced version similar like "richtext". "richtext" had some flaws like you cannot dynamically change what you have defined. Everything is rendered (and only than) when creating an richtext object.

So applying functions to characters or words is still missing.

Re: [Library] Popo

Posted: Mon Jan 19, 2015 6:34 pm
by adnzzzzZ
davisdude wrote:Nice library! Looks useful.
For what Muris mentioned you could try implementing something found in Robin's richtext.
What I have in mind for what Muris mentioned is to just enable love.graphics.setFont() to be called whenever a special function is defined. Something like this:

Code: Select all

local bold_font = love.graphics.newFont('bold_font.ttf', 36)
text = Text('[BOLD](font) not bold', {
    font = function(dt, c)
        love.graphics.setFont(bold_font)
    end
})
So when this font function is defined it overrides the text's normal font.

Re: [Library] Popo

Posted: Mon Jan 19, 2015 11:30 pm
by Muris
Sorry that I am quite noob and I am quite unfamiliar with making issues and proposing fixes. Anyways I had a problem with the word wrapping, I noticed there is self.wrap_width in your Popo, and it does indeed wrap the text, but sadly sometimes it does it in a middle of a word. So I managed to do a following fix, it might not be all that good but here goes and especially if there is font changing going on, this will become quite problematic.

Code: Select all

-- define nextWhiteSpace before the loop, at here
    local nextWhiteSpace = 0
    self.characters = {}
    local str = ""
    local line = 0

.....
        -- Move to new line if over wrap_width
        if self.wrap_width and nextWhiteSpace and nextWhiteSpace <= i then
            nextWhiteSpace = stripped_text:find("%s", i )
            local w = self.font:getWidth( str .. stripped_text:sub( i, nextWhiteSpace ) )
            if w > self.wrap_width then 
                line = line + 1
                str = c
            end      
        end
        local w =	self.font:getWidth(c)
        text_w = self.font:getWidth(str)
        


Re: [Library] Popo

Posted: Tue Jan 20, 2015 12:11 am
by Positive07
Can this lib handle UTF8 characters? Because if it doesnt, splitting the text into characters is not a good idea

I proposed a method to get the string in the issue tracker, with that and the font you could get the width of the text, with that you could use this function to center align your text:

Code: Select all

local tw = font:getWidth(text)
local w = love.graphics.getWidth()

function n (dt,c)
    c.x = c.x + (w-tw)/2
end
Not sure how you could do that when wrapping, but I guess it is possible (internally at least)

Re: [Library] Popo

Posted: Tue Jan 20, 2015 12:17 am
by adnzzzzZ
Positive07 wrote:Can this lib handle UTF8 characters? Because if it doesnt, splitting the text into characters is not a good idea

I proposed a method to get the string in the issue tracker, with that and the font you could get the width of the text, with that you could use this function to center align your text:

Code: Select all

local tw = font:getWidth(text)
local w = love.graphics.getWidth()

function n (dt,c)
    c.x = c.x + (w-tw)/2
end
Not sure how you could do that when wrapping, but I guess it is possible (internally at least)
If you wanna get the text as it will be printed then you can access it via str_text from a character. So in your example you can do:

Code: Select all

function n(dt, c)
    c.x = c.x + (love.graphics.getWidth() - c.text.font:getWidth(c.str_text))/2
end

Re: [Library] Popo

Posted: Tue Jan 20, 2015 12:28 am
by Positive07
adnzzzzZ wrote:

Code: Select all

function n(dt, c)
    c.x = c.x + (love.graphics.getWidth() - c.text.font:getWidth(c.str_text))/2
end
Nice! :awesome:

So I'll ask again just in case you didnt read it: Can this lib handle UTF-8?