GetTextWidth

This topic contains 5 replies, has 3 voices, and was last updated by  misthema 5 years, 4 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2587

    297chrisc
    Participant

    Hi, Another problem :)

    When using this method on a string that has a space at the end
    – For Example “Hello “

    It returns the length of the string minus the space at the end

    Any way to get it to return the total length?

    Cheers
    Chris

    #3312

    Ziggy
    Keymaster

    I’ll take a look…

    #3315

    297chrisc
    Participant

    Thought of a simple workaround if anyone else needed it
    (Shouldn’t have taken me this long to figure out lol)
    String = “Hello “
    Width = Yourfont.GetTextWidth(String + “A”) – Yourfont.GetTextWidth(“A”)

    result is length of the string including the trailing space :)
    Simples :)

    #3376

    Ziggy
    Keymaster

    @297crisc: Your method is not 100% acurate as it takes into account the spacing on last leter, and not its drawing size, so on script fonts it could be wrong. I’ve updated the FontMachine module to another one that provides proper testsize calculation, including white spaces at the end of the string.

    #5717

    misthema
    Participant

    I have the newest FontMachine module, but it isn’t working.

    In file bitmapfont.monkey, starting from line 188:

    If lastchar >= 0 And lastchar < faceChars.Length() Then
    	if lastchar = 32 then
    		'Do nothing. We let the spacing at the end of the string.
    	ElseIf faceChars[lastchar] <> Null Then
    		twidth = twidth - faceChars[lastchar].drawingMetrics.drawingWidth 
    		twidth = twidth + faceChars[lastchar].drawingMetrics.drawingSize.x 
    	End If		
    End If

    So what you need, is metrics for a space. It does, however, include spaces once the space isn’t the one trailing.

    For example:
    “Hello World! ” is shorter than..
    “Hello World !”

    I’ve also noticed while making text inputs, that the cursor jumps back a little when I add a trailing space at the very end of the text.

    But seriously, excluding spaces from so called “StringWidth”? No. One should never do that.
    Spaces are valid part of a String.

    #5719

    misthema
    Participant

    Looks like I can only edit my post once… Well, anyway, double-posting then!

    If someone needs, here’s a working fix for trailing spaces:

    If lastchar >= 0 And lastchar < faceChars.Length() Then
    	If lastchar = 32 then
    		' Working space
    		twidth = twidth + faceChars[lastchar].drawingMetrics.drawingWidth + Kerning.x
    	ElseIf faceChars[lastchar] <> Null Then
    		twidth = twidth - faceChars[lastchar].drawingMetrics.drawingWidth 
    		twidth = twidth + faceChars[lastchar].drawingMetrics.drawingSize.x 
    	End If		
    End If

    Replace lines 188-196 in bitmapfont.monkey with this code and it works fine.

    And ziggy, please add this to the official FontMachine module.. My console-input will look stupid without this… ;P

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.