Text justify align

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
User avatar
Fenrir
Party member
Posts: 222
Joined: Wed Nov 27, 2013 9:44 am
Contact:

Text justify align

Post by Fenrir »

Hey guys,

I would like to use the justify align for some texts, but here's what it produces:

Image

The last line is always justified too, making it quite weird. Is there any way to not justify this last line?
Thanks!
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Text justify align

Post by Positive07 »

Just an idea but you could probably use [wiki]Font:getWrap[/wiki] second argument (a table with the wrapped lines) and print the last line separately if it's width is not close to the desired width
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Text justify align

Post by davisdude »

It also looks like it's not justifying right. If you look closely, it seems like the space in the second line is being included in the text width...
Image
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Text justify align

Post by raidho36 »

Are there two whitespaces in that specific spot?
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Text justify align

Post by HugoBDesigner »

I think a good way of justifying text would be getting the amount of spaces, then getting the size of the text without them, then dividing the difference between the desired width and the space-less width, and dividing it by the amount of spaces in that line, rounding down. Well, we know that, and that's basically what already takes place, more or less. But, an extra step could be getting the difference between the desired width and the width gotten. Then dividing that by the number of words or letters in that line, and adding this extra space to each letter/word. I don't know if that'd look good, but I could try assembling a quick example if needed.

EDIT: Just went ahead and wrote an example. It's probably not optimized but still works. I made it justify per word. The only downside is if a line has no spaces, so it aligns to left (I could work around this later, maybe):

Code: Select all

function love.load()
	font = love.graphics.newFont(16)
	love.graphics.setFont(font)
	myText = {
		"This is just an",
		"example text on how",
		"justify could work.",
		"You can add as many",
		"lines as you want",
		"and it should",
		"still work, I think.",
		"123456789"
	}
	
	--Just for easier viewing, gets the longest line as the width
	local maxT = 0
	for i, v in ipairs(myText) do
		if font:getWidth(v) > maxT then
			maxT = font:getWidth(v)
		end
	end
	
	myRect = {
		x = 20, y = 20, width = maxT
	}
	
	love.window.setMode(myRect.width+2*myRect.x, #myText*font:getHeight()+2*myRect.y)
end

function love.draw()
	love.graphics.setColor(255, 0, 0, 105)
	love.graphics.rectangle("line", myRect.x, myRect.y, myRect.width, font:getHeight()*#myText)
	
	love.graphics.setColor(255, 255, 255, 255)
	for i, v in ipairs(myText) do
		local px = myRect.x
		local py = myRect.y + (i-1)*font:getHeight()
		
		local spaces = string.len(v)
		local s = v:gsub(" ", "")
		spaces = math.max(1, spaces - string.len(s)) --Amount of space characters in the line. Maxed to 1 to avoid dividing by 0
		local dif =  myRect.width - font:getWidth(s)
		
		local off = 0
		local t = v:split(" ")
		for j, w in ipairs(t) do
			love.graphics.print(w, math.floor(px + (j-1)*(dif/spaces) + off), py)
			off = off + font:getWidth(w)
		end
	end
end

function string:split(c) --I actually wrote this one, it accounts for splitters of multiple sizes
	local s = self
	local t = {""}
	local skip = 0
	for i = 1, string.len(s)+1-string.len(c) do
		local v = string.sub(s, i, i-1+string.len(c))
		if skip > 0 then
			skip = skip-1
		elseif v == c then
			table.insert(t, "")
			skip = string.len(c)-1
		else
			local lm = i
			if i == string.len(s)+1-string.len(c) then
				lm = -1
			end
			t[#t] = t[#t] .. string.sub(s, i, lm)
		end
	end
	
	return t
end
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Fenrir
Party member
Posts: 222
Joined: Wed Nov 27, 2013 9:44 am
Contact:

Re: Text justify align

Post by Fenrir »

davisdude wrote:It also looks like it's not justifying right. If you look closely, it seems like the space in the second line is being included in the text width...
Image
Yep I also spotted this issue, but it's a lot less problematic than the other. And thanks for you tips, but I'll probably stick to a left align for now, I don't want to go to something too tricky for now.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 218 guests