New bug : width for a text with blank char

Hi all,

On one of the last corona daily build (coming between 3100 and 3113 , there is a new bug (who doesn’t append on the last officiel 3068 release if i remember well) :

When you ask for the width of a display text :

  • on the emulator, it returns the width of the text WITHOUT the last blank characters, acting like a trim()

  • but on the real device (at least on both iphone and android i tested), it returns the real width.

Exemple :

 local myText = display.newText("Test ", 0, 0, native.systemFont, 12) print (myText.width) -- result : -- 23.92 : on emulator -- 26.07 : on my iphone !!

Hi,

Are you sure about this?

Using native.systemFont means using a different font for each device and once a text object is generated, it is a display object (image).

That aside, i’ve seen many cases where the simulator is (seem) off on text objects, especialy height and width.

I usually run an android device at the same time and when I see there is a mismatch, I create a win32 build, which often corrects the issue.

So, i trust the simulator the least :slight_smile:

sure.

Because for this exemple, i used “native.systemFont”, but in my real game, i used a custom font.

And the same code was working (result are the same on emulator and real devices) on the official release, and then, suddently, everything was " trim () " during a new daily build…

sounds like a bug then.

you should register it.

i don’t know how to do. I said it to Rob

if you have reported this to Rob outside this post, i don’t get the point of the post <unfollow>

to prevent other people, no ?

You’re welcome to file a bug report on this (main.lua, config.lua, build.settings and any needed assets in a .zip file --include a custom font to eliminate ) and use the Report a bug link at the top of the page, however, I’m not convinced this is a bug.

Each operating system renders text a little differently. If you’re in the simulator on a Mac and build and test on Android, you’re likely going to get different results.  We have also made changes to font handling since 3068 to fix a bug where it would cut off the last character on Windows which could cause these discrepancies. 

Rob

@triogical

Comparing image width is quite unreliable. You can print string.len(myText.text) to see it is still 5 in both simulator and device.

this is a big problem.

try this :

 local image = display.newImageRect( "Icon.png", 100, 100 ) local myText = display.newText("Test", 0, 0, native.systemFont, 18) local myText2 = display.newText("Test ", 0, 0, native.systemFont, 18) print (image.width, myText.width, myText2.width) -- result on emulator : 100 35.892055511475 35.892055511475 -- result on device : 100 33.19xxxxxxx 46.23xxxxxx

The problem is not that myText.width is not the same value on simulator and on device.

The problem is that if you add a space at the end of your text, myText.width = myText2.width on the emulator, and myText.width <> myText2.width on devices !

so everything is shifted to the screen in my game.

screenshot in the next post

Rob please can you fill a bug report for me ?

@triogical

  1. It’s a simulator, not an emulator. :rolleyes:

  2. You can file the bug your self here: https://developer.coronalabs.com/content/bug-submission

  3. While I have no real investment in this, I don’t believe this is a bug.  Text rendering is a strange beast and is in my experience never 100% consistent between the simulator and actual devices.  This is no surprise since they are two separate OSes and there are many other differences.  The fonts themselves can be part of the problem.

( Rob already said this, but I think you missed it.)

Hi.  You said this was a big problem, but I think it may be solvable.  

I think you’re looking at this and saying, “I have problem X and I can’t solve it unless the text renders at exactly the same size between simulator and devices.”

I am saying, “Let’s examine problem X and see if there is a solution that does not rely on exact sizes.”

Please tell us why you need the text sizes to be exactly the same between simulator and device and we may have suggestions to help resolve your design issues.

Hopefully your promised screenshots will help us to help you.

submited…

how can i download a screenshot ?

Uploading Screenshots

  1. The forums has a mechanism, but I never use it so I can’t say.

  2. You can host the images on any site, your gitHub account, etc. then link to them with the little button that looks like a photo.

Guessing At Problem X

I’m going to go out on a limb here, but it sounds like you’re trying to achieve consistent spacing between two different text objects.

The easiest way to do this is to place the second text object relative to the first like this:

local label1 = display.newText( "Score:", 100, 100, native.systemFont, 16 ) local label2 = display.newText( tostring(100), 0, label1.y, native.systemFont, 16 ) label2.anchorX = 0 label2.x = label1.x + label1.contentWidth/2 + 20 -- 20 is a gap I have randomly selected

If you click on “More Reply Options” by the “Post” button, you will be taken to a screen where you can attach screen shots.

As stated above, this is most likely not a bug but the fact that Android will render text differently than a Mac will. To make text work with Corona we have to use the operating system’s text rendering features to turn the text into an image. We are really at the mercy of what the operating system does. Also you are using native.systemFont which is 100% guaranteed to be different on every device. Even Apple and Google have changed the default fonts based on different versions of the OS.  Some older Androids use Droid Sans, modern ones use Roboto. Amazon uses a different font on it’s Kindle Fire devices.

I know you said you’re using a custom font, this is why we asked for a demo project with the font. 

Rob

Remark: it reproduces only on Windows simulator. On Mac width is different on the simulator.

Difference at screen for me between “simulator on windows” (with shift), and all other platforms (see attached image) :

  • simulator on mac = ok
  • device iphone = ok
  • device android = ok
     
    ok this is not a big big bug and i can bypass it and found a solution to avoid to add blank spaces at the end of my text, but in all case this is a real bug when a = b on a platform, and then a <> b on another one !
     
    so the problem seems to be only on the simulator running on windows who “trim” the text.

nice description of the bug :

 local native\_1 = display.newText("a test" , 0, 0, native.systemFont, 18) local native\_2 = display.newText("a test ", 0, 0, native.systemFont, 18) local perso\_1 = display.newText("a test" , 0, 0, MY\_FONT, 18) local perso\_2 = display.newText("a test ", 0, 0, MY\_FONT, 18) if native\_1.width == native\_2.width then print("Bug!", native\_1.width, native\_2.width) else print("OK", native\_1.width, native\_2.width) end if perso\_1.width == perso\_2.width then print ("Bug!", perso\_1.width, perso\_2.width) else print ("OK", perso\_1.width, perso\_2.width) end --[[simulator on windows : Bug 44.437782287598 44.437782287598 Bug 47.856071472168 47.856071472168 simulator on mac : OK 44 50 OK 48 54 device iphone : OK 43.86xxxxxxx 48.60xxxxxx OK 48.60xxxxxx 53.34xxxxxxx]]--

So Rob, not a bug ? :slight_smile:

The solution I gave you can be used to resolve this.

Simply do this:

  1. place the coin image relative to the edge of the button.

  2. create the text label

  3. Set the text label’s anchorX to 1

  4. Place it relative to the coin’s position, minus its width and an extra buffer.

You can also do more complicated (still easy) calculations and make the text and coin nicely center on the button.

This might help: https://coronalabs.com/blog/2014/02/11/tutorial-methods-for-positioning-text/