"Not enough space" error when printing a string

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.
User avatar
FloatingBanana
Prole
Posts: 11
Joined: Sat Mar 02, 2019 4:57 pm
Contact:

"Not enough space" error when printing a string

Post by FloatingBanana » Fri Jan 31, 2020 10:06 pm

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?

Code: Select all

if anyMistake(self.english) then
    print("Sorry, english is not my first language")
end

User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post by raidho36 » Fri Jan 31, 2020 10:54 pm

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.

User avatar
FloatingBanana
Prole
Posts: 11
Joined: Sat Mar 02, 2019 4:57 pm
Contact:

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

Post by FloatingBanana » Sat Feb 01, 2020 4:06 pm

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.

Code: Select all

if anyMistake(self.english) then
    print("Sorry, english is not my first language")
end

User avatar
pgimeno
Party member
Posts: 2181
Joined: Sun Oct 18, 2015 2:58 pm

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

Post by pgimeno » Sat Feb 01, 2020 5:42 pm

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)

User avatar
YounYokel
Prole
Posts: 33
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

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

Post by YounYokel » Sat Feb 01, 2020 6:12 pm

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
Let's make games with love.

Code: Select all

string.format('Made with LÖVE %s.%s (%s)', ({love.getVersion()})[1], ({love.getVersion()})[2], ({love.getVersion()})[4])

User avatar
zorg
Party member
Posts: 2921
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg » 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
YounYokel
Prole
Posts: 33
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

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

Post by YounYokel » Sat Feb 01, 2020 8:01 pm

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?
Let's make games with love.

Code: Select all

string.format('Made with LÖVE %s.%s (%s)', ({love.getVersion()})[1], ({love.getVersion()})[2], ({love.getVersion()})[4])

User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post by raidho36 » Sun Feb 02, 2020 12:26 am

I insist that you head over to Wikipedia and study carefully the article about UTF-8 encoding.

User avatar
YounYokel
Prole
Posts: 33
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

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

Post by YounYokel » 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.
Let's make games with love.

Code: Select all

string.format('Made with LÖVE %s.%s (%s)', ({love.getVersion()})[1], ({love.getVersion()})[2], ({love.getVersion()})[4])

User avatar
zorg
Party member
Posts: 2921
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg » Sun Feb 02, 2020 8:26 am

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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests