Unsolved Inaccuracy with g.getStringWidth()?
-
I tried to write some code using g.getStringWidth() to scale text down to perfectly fit a panel, but I've run into a problem. I'm trying to fit text into an area of 500, but even when my text is below 500 pixels across, it is still cut off. It seems that the real text width is bigger than what getStringWidth is telling me.
Am I misunderstanding exactly what getStringWidth does, or is my code wrong maybe? It's the same with Engine.getStringWidth too. Snippet below image.
HiseSnippet 1125.3ocsVstaaTDEd1DuztlVIpTe.F4esQI3Z6lT.Et3KwFYQShENTPBgplr630CY2YL6NacLnJj3ogGCdI38f2.3b1wd20INzJC0+vxm4bY9Ne9bYFEq73IIpXhkyEKlwIVOvd7BodZuoLgjL7Dh08r07DcSR2EyXIIbehk0teIpzxoBI6ye8EcYgLoGu3HB4EJgG+4hHgt3zQs+JQX3.lO+BQTIqOr8POkrmJTkB.YW6FjYLuqXA7yXnY6XSrdu99BsJdrlAngXUoqxew3op4Ri8uPjHtLjiBMIig.YNdfJzGQLdJo2TQn+nUIbBAhxnhzeWS5+X6SE9h7yKngOHSAsvix7g0NqCucWCdMKCuFkf2FfjUIHUw.oGYO1KVLSWnAwy6aOTp4wSX.sWFJFaI67mUr6o.Kj55Qrq3ChAgbObeViFGPgu163pUApOQSeEKldYXJeDSxCoeFckyAbcOUzLkDDbqkaQMzSzGr3nSbLaA3iy2+CGWbXjJUpwSa0.rchJl5J.wFGSEzOsjIf796uW0eopSdrpOKMYpaMTFunWWsZ9EWOgqGA+on+ZUpVH4tSRkdZgR5FjEif5SfZrNggtl5oj5reJkEwhAagP4T0YUl1Ily.7nmJRvr74JOVXW.P9ItHuja22J70SKYXlrKZAvQIpPd8YPvAxoKXLcdl0hDZM59EtWbyS.dcr3m4P.QdwwPWWiDUQ5+iJgzsFkRqYbL.yZS9jmVWFB8Hn5LkCTHBN+5EAbYsCxukh6EuiUYR.lFi0.pCLICpDM8IO4ll1WF.D2lr+.5sutCnML23MXlKvDbMlI+NL1KlPcKt1OuDwQ2XrfnnUJLj7kTzbnUheWAopCTY3jS8eHTEVu0QfeuQxy4sh2.h6+Locy77L9bSgwl4M320ntXLnIXNgJySPP2d0yHlWaJe7iYy6DJBjbej+VhkUcA.p7f16XteV21dGupmhpjmoz7yktY8VPiH8lplLYi5vwGwpvPd7FUiy+i+2bzUlFcIO9.XtD.xbCgghqOo09tmzVdQfmYZVICUxgRg97Yb4csdfrbDH7quY3ILMCGOu7LvtY7Xs.gf0I7WA65LCqcrOgmbkVMCVakO2jXcecl1GtZTNNIiH7ww3QvvLi70k2etnrfPyiVsd7A1G15SZcXylOswQkTz5FZxpZxiwe7asmxEAS0kOgTBf12M.yG8VFfCaC.z152get9M09WaSt8NLX6oxOMjoWekJ9NhkJf+8WaOFtqRlHzKJ+Ni+21y91BwGYORn8ltYLtyFvHTi7t.iKecxCs6OYB2SW.vJ1C9t2MOEgXVvFbJClgA+yaeVZzXnPyiC2tDJGvtOqcv9DibCTFYfwboelveCeVprIJasTYyUJIQLuX0K8L8736ete1I.ljYO8yw9TTl1jjMGnLOGAOG6kddqGpa4Xqs0wmtsNd3153QaqiOaac7i1VG+32ri3qk6jpUQl1FB4zQ8yFhZY0WxfJvrpUx+P2+SYD
-
Yep, something is definitely wrong. Gave your snippet a whirl and the method just won't behave.
Report it.
On a design note, why aren't you using drawFittedText()?
-
@aaronventure Thanks for confirming, will do! It's just an aesthetic thing, I don't like the squashed look you get with drawFittedText()
-
@cynthasiser I think spaces might be misbehaving? Does it always fit correctly if you don't have the spaces in the string?
-
@cynthasiser Okay I'm very confused now, was just about to bug report when I tried the broken snippet again and it works now, it's displaying the correct amount of tests, with no ellipsis:
The code is identical as before, but it works now.
One important realisation though - the font being displayed in both the original broken version, and the current working (despite being identical) one, is Arial, not Oxygen as I specified.
Comparison using labels:
So it seems the behaviour is coming from a confusion between Arial and Oxygen, even though the code explicitly says to use Oxygen.
I'm using version 3.6.2 btw
-
@aaronventure No, I thought that too when I was getting the initial wrong behaviour, but I tried it and it still cut the text off
-
@cynthasiser I've also tried various other fonts in g.setFont, and they all display. I tried typing random words as the font, and they default to Arial when not recognised, so it seems my issue is that g.setFont cannot find Oxygen by name. The font exists on my system though, as labels can successfully display it.
-
Is the text something that is going to change at run time?
-
@d-healey it is yeah
-
Yeah that definitely makes it more problematic. Also be aware that fonts often render at different sizes on Windows and MacOS, so you might have more fun to come.
-
@d-healey Looking forward to it haha!
I've also realised I have two versions of HISE on my PC; 3.6.2 and 4.0.0 which is what caused my confusion. One's a desktop shortcut and the other was coming up when I searched HISE in the start menu!
The random difference in behaviour that I posted about earlier was a result of using different versions. In 4.0.0, I get the cut-off text with the ellipsis, whereas in 3.6.2 I get the "working" version, but BOTH versions fail to display the text in Oxygen when I say g.setFont("Oxygen", 20), and display Arial instead.
In 3.6.2, my code works with all fonts; even when I ask for Oxygen and get given Arial instead, g.getStringWidth knows we're in Arial now and scales correctly. Interestingly, if I put g.setFont("Default", 20) (the default being Oxygen), HISE crashes. Typing in font names that don't exist just makes the text display in Arial, so something about specifically "Default" really upsets g.setFont() in 3.6.2.
In 4.0.0, my code produces the cut-off text in every font I try. Using g.setFont("Oxygen", 20) still doesn't work and gives me text cut-off and in Arial. BUT when I try g.setFont("Default", 20) in 4.0.0, I no longer crash, but get Oxygen.
So what we've learned is:
- g.setFont("Default", 20) causes a crash in 3.6.2, which seems to have been fixed by 4.0.0
- g.getStringWidth() did work in 3.6.2, but doesn't anymore in 4.0.0
- Neither version can correctly display Oxygen using setFont(), at least in a paint routine on a panel.
I was able to replicate this in a fresh project; I draw a line 200 pixels long, draw text that clearly exceeds the line, and g.getStringWidth() tells us that the text is only 196.7 pixels. In 3.6.2, it says the text is 215.5 pixels which seems about right.
HiseSnippet 903.3ocsVstaaaCElxIJH18BZA5C.g+kCVfgc5kMfhh5Dm3Ai1jZLm0shggBFoijXCEoFEURbGJ16XeR1aP5gTxwxKAosFXFvF5b46vOctQOQqBf7bkl3073YY.w6t9SmIMICSXbIY79Du66eHK2.ZZop8lkwxygPhm2Z+rUgWy0ItO+6K2iIXx.XgJB4sJd.7ZdJ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