Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
FontBase:getAdvanceX question — Gideros Forum

FontBase:getAdvanceX question

antixantix Member
edited November 2015 in General questions
Hey all. Can anyone clarify what the parameters for this function actually do? I understand what the first parameter is (text) but the others are a mystery.

Parameters:
text: (string)
letterSpacing: (number, default = 0)
size: (number, optional)

Return value:

number: The width of the first size characters of text.

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    So I think the purpose of this function is to know how much space would the text use up, or more properly, where to place next character after this string
    and text is obviously text to use
    letterSpacing is amount of pixels between letters in the text
    and size I think is a font size to use, but now I'm not sure :)
  • antixantix Member
    edited November 2015
    Thanks ar2rsawseen, thats close enough ;)

    I'm trying to get some text to print across multiple lines. I have the following code and would appreciate if anyone can suggest any optimizations...
    local myFont = Font.new("ocr_s.fnt", "ocr_s.png")
     
    local myString = "The quick brown fox jumps over the lazy dog.|Once the quick brown fox jumped over the lazy dog, it fell down a hole, got skewered on some bunjee stakes, and bled to death.|It turns out that the lazy dog was actually quite the enterprising chap. He had dug a pit, filled it with stakes, and then concealed it with leaves and twigs.|Then he just had to wait for the quick (but quite stupid) brown fox to come around prancing and dancing and kill himself in the pit.|Yay, fox for dinner!!!"
     
    local function create_lines(text, font, maxWidth)
    	local function split(input, char) -- SPLIT FUNCTION FROM STACK EXCHANGE <a href="http://bit.ly/1iXGtNN" rel="nofollow">http://bit.ly/1iXGtNN</a>
    		local match = string.gmatch
    		local pattern = '([^'..char..']+)' -- OPTIMIZED SLIGHTLY BY ANTIX
    		local output = {}
    		for str in match(input, pattern) do -- MORE OPTIMIZATIONS AFTER FULLY READING THREAD
    			output[#output + 1] = str -- THIS IS FASTER THAN TABLE.INSERT
    		end
    		return output
    	end
     
    	local paragraphs = split(text, "\n") -- SPLIT TEXT INTO PARAGRAPHS
     
    	local lines = {}
     
    	for p = 1, #paragraphs do
    		local words = split(paragraphs[p], " ") -- SPLIT PARAGRAPH INTO WORDS
    		local word = 1
    		local line = ""
    		local lineWidth = 0
    		local done = false
     
    		repeat
    			local wordWidth = font:getAdvanceX(words[word] .. " ") -- GET PIXEL WIDTH OF NEXT WORD
    			if lineWidth + wordWidth >= maxWidth then -- IF ABOUT TO EXCEED MAXIMUM WIDTH, ADD LINE TO LINES AND START A NEW LINE
    				table.insert(lines, line)
    				line = ""
    				lineWidth = 0
    			end
    			line = line .. " " .. words[word] -- ADD WORD TO LINE, PLUS SPACE CHAR THAT WAS STRIPPED IN SPLIT FUNCTION
    			lineWidth = lineWidth + wordWidth 
    			word = word + 1
    			if word > #words then -- IF ALL WORDS PROCESSED, ADD LAST LINE TO LINES AND SET FLAG TO BEGIN NEXT PARAGRAPH
    				table.insert(lines, line)
    				done = true
    			end
    		until done
    	end
    	line = nil
    	words = nil
    	paragraphs = nil
     
    	return lines
    end
     
    local myLines = create_lines(myString, myFont, 352)
  • Btw, you can also search forum for TextWrap, which does a similar thing. But your solution might even be better.
    Does it work too slow for you?
  • @ar2rsawseen - I have only just got this working and its not tested for speed as of yet. I need to decide which method I will use to actually draw the text to screen now.

    I did grab TextWrap but I'm a bit stupid and found the code very hard to follow L-)
  • I modified the spilt function and it performs slightly faster now..
    	local function split(input, char) -- SPLIT FUNCTION FROM STACK EXCHANGE <a href="http://bit.ly/1iXGtNN" rel="nofollow">http://bit.ly/1iXGtNN</a>
    		local match = string.gmatch
    		local pattern = '([^'..char..']+)' -- OPTIMIZED SLIGHTLY BY ANTIX
    		local output = {}
    		local i = 1
    		for str in match(input, pattern) do
    			output[i] = str
    			i = i + 1
    		end
    		return output
    	end
    I edited my original post to reflect changes.
  • Final optimizations and I'm happy with it now :)
    	local function split(input, char) -- SPLIT FUNCTION FROM STACK EXCHANGE <a href="http://bit.ly/1iXGtNN" rel="nofollow">http://bit.ly/1iXGtNN</a>
    		local match = string.gmatch
    		local pattern = '([^'..char..']+)' -- OPTIMIZED SLIGHTLY BY ANTIX
    		local output = {}
    		for str in match(input, pattern) do -- MORE OPTIMIZATIONS AFTER FULLY READING THREAD
    			output[#output + 1] = str -- THIS IS FASTER THAN TABLE.INSERT
    		end
    		return output
    	end
Sign In or Register to comment.