Page 1 of 2

"Not enough space" error when printing a string

Posted: Fri Jan 31, 2020 10:06 pm
by FloatingBanana
I'm making something like Undertale dialogs, and my code is basically this:

Code: Select all

local step = 0
local text = "this is a random text"

function love.load()
    love.graphics.setNewFont(30)
end

function love.update(dt)
    step = math.min(step + 20 * dt, #text)
end

function love.draw()
    local sub = string.sub(text, 1, step)

    love.graphics.print(sub, 100, 100)
end
When I run this code it works perfectly, but if the text has any special characters, for example:

Code: Select all

local step = 0
local text = "this is á random text"

function love.load()
	love.graphics.setNewFont(30)
end

function love.update(dt)
	step = math.min(step + 20 * dt, #text)
end

function love.draw()
	local sub = string.sub(text, 1, step)
	
	love.graphics.print(sub, 100, 100)
end
I get this error:

Code: Select all

Error

main.lua:15: UTF-8 decoding error: Not enough space


Traceback

[C]: in function 'print'
main.lua:15: in function 'draw'
[C]: in function 'xpcall'
Does anyone know how to solve this?

Re: "Not enough space" error when printing a string

Posted: Fri Jan 31, 2020 10:54 pm
by raidho36
What you experience is that a multibyte UTF-8 character gets cut into pieces and this corrupts the input stream. That's because Lua operates on binary strings, not UTF-8 multibyte character strings. There's a built-in module for handling UTF-8.

Re: "Not enough space" error when printing a string

Posted: Sat Feb 01, 2020 4:06 pm
by FloatingBanana
raidho36 wrote: Fri Jan 31, 2020 10:54 pm There's a built-in module for handling UTF-8.
I had already used the utf8 library, I tried this:

Code: Select all

local utf8 = require "utf8"

local step = 0
local text = "this is á random text"
local offset = utf8.offset(text, -1)

function love.load()
	love.graphics.setNewFont(30)
end

function love.update(dt)
	step = math.min(step + 20 * dt, offset)
end

function love.draw()
	local sub = string.sub(text, 1, utf8.offset(text, step))
	
	love.graphics.print(sub, 100, 100)
end
...and I got the same error.

Re: "Not enough space" error when printing a string

Posted: Sat Feb 01, 2020 5:42 pm
by pgimeno
Don't ask me why, but adding - 1 like below worked for me:

Code: Select all

local sub = string.sub(text, 1, utf8.offset(text, step) - 1)

Re: "Not enough space" error when printing a string

Posted: Sat Feb 01, 2020 6:12 pm
by YounYokel
Guys why doesn't this work?

Code: Select all

str = [[Popiš památku je název soutěže pro nové i zkušené wikipedisty, která se koná od 15. 
ledna do 15. února 2020, tedy jeden měsíc. Aktuální, již třetí ročník soutěže pořádá opět 
spolek Wikimedia Česká republika. Partnerem soutěže je portál PROPAMÁTKY.]]

local utf8 = require('utf8')

function love.draw()
   love.graphics.printf(utf8.sub(str, 1, 50), 16, 16, love.graphics.getWidth() - 16)
end

Re: "Not enough space" error when printing a string

Posted: Sat Feb 01, 2020 6:44 pm
by zorg
YounYokel wrote: Sat Feb 01, 2020 6:12 pm Guys why doesn't this work?
Checking the reference manual, i didn't see any utf8.sub function there.
( http://www.lua.org/manual/5.3/manual.html#6.5 - löve implements this itself fully as its own utf-8 lib)

Also, shouldn't step be math.floor-ed? at some point? not sure if it's a good idea to pass in math.min(step + 20 * dt, offset) into utf8.offset...

The minus one thing might be an off-by-one issue, depending on how string.sub actually indexes the string

Re: "Not enough space" error when printing a string

Posted: Sat Feb 01, 2020 8:01 pm
by YounYokel
zorg wrote: Sat Feb 01, 2020 6:44 pm
YounYokel wrote: Sat Feb 01, 2020 6:12 pm Guys why doesn't this work?
Checking the reference manual, i didn't see any utf8.sub function there.
( http://www.lua.org/manual/5.3/manual.html#6.5 - löve implements this itself fully as its own utf-8 lib)

Also, shouldn't step be math.floor-ed? at some point? not sure if it's a good idea to pass in math.min(step + 20 * dt, offset) into utf8.offset...

The minus one thing might be an off-by-one issue, depending on how string.sub actually indexes the string
Yeah, you're right. Using string.sub() works... sometimes.
I tried using this function which simply deletes the part of a string from i to j:

Code: Select all

function string_delete(s, i, j)
	return (string.sub(s, 1, i - 1) .. (j ~= nil and string.sub(s, i + j) or string.sub(s, i + 1)))
end

function love.draw()
   love.graphics.printf(string_delete(str, 5, 10), 16, 16, love.graphics.getWidth() - 16)
end
Weird that I didn't work with Russian or any other symbols but works with standard English symbols.

Does anyone know why doesn't it work?

Re: "Not enough space" error when printing a string

Posted: Sun Feb 02, 2020 12:26 am
by raidho36
I insist that you head over to Wikipedia and study carefully the article about UTF-8 encoding.

Re: "Not enough space" error when printing a string

Posted: Sun Feb 02, 2020 6:44 am
by YounYokel
raidho36 wrote: Sun Feb 02, 2020 12:26 am I insist that you head over to Wikipedia and study carefully the article about UTF-8 encoding.
Weird that other function does work which inserts a string in given position works which is almost the same.

Re: "Not enough space" error when printing a string

Posted: Sun Feb 02, 2020 8:26 am
by zorg
YounYokel wrote: Sun Feb 02, 2020 6:44 am
raidho36 wrote: Sun Feb 02, 2020 12:26 am I insist that you head over to Wikipedia and study carefully the article about UTF-8 encoding.
Weird that other function does work which inserts a string in given position works which is almost the same.
I'm pretty sure inserting to an index that would be between two bytes of a multibyte character would also break.