I was trying to use BitmapText’s getHeight and getLineWidth to make a box around some text, but the values returned are a lot bigger than they need to be; at least in my case. Like 30% extra width and 100% extra height. What should I do to get accurate values?
@pspeed This is my code (kind of, I’m using Clojure), where e is an Element from tonegodgui:
[java]BitmapText bt = new BitmapText(e.getTextElement().getFont());
bt.setText(text);
e.setDimensions(bt.getLineWidth(), bt.getHeight());[/java]
And this is the result:
I measured in paint, the grey box is 189 x 35, and that’s exactly what getHeight and getLineWidth return (I checked), so there is nothing wrong with setDimensions. The string used is “ITEM TOOLTIP LOL”, with no extra whitespace.
As for scaling the text: I dunno, maybe tonegodgui does something.
@tuffe
I do… BitmapText sucks =) Kidding (sort of).
The problem is that BitmapText sets the font size using scaling, so what @pspeed mentioned avoiding is impossible to avoid =(
BitmapCharacterSet uses setRenderSize… and the quad scale is multiplied by this.
Because it uses scaling to set character size, alignment is always going to be a problem. It’ll be close… most of the time… but the further away you get from the default render size, the worse the alignment issues get.
It may be able to be fixed by altering the way getLineWidth and getHeight are currently handled, but I haven’t looked at it close enough to know for sure.
@pspeed would probably know this better than I as I believe he has been cursed with updating this when a problem arises.
@t0neg0d said:
The problem is that BitmapText sets the font size using scaling, so what @pspeed mentioned avoiding is impossible to avoid =(
If you mean “scaling” in the setLocalScale() sense then this is incorrect. It sets the quad size based on the setSize() parameter on the BitmapText. It’s not really a “scale” in JME terms.
I don’t really do anything magic that I’m aware of and Lemur seems to have no problems with BitmapText bounding boxes or alignment. The only fudge I’ve had to make is to bump out the x size by a tiny amount in my “preferred size” calculation because I think there is a “less than” somewhere in BitmapText where there should be a “less than or equal to”.
This is the entirety of how I calculated a text component’s preferred size in Lemur:
[java]
public void calculatePreferredSize( Vector3f size ) {
// Make sure that the bitmapText reports a reliable
// preferred size
bitmapText.setBox(null);
size.x = bitmapText.getLineWidth();
size.y = bitmapText.getHeight();
if( offset != null ) {
size.x += Math.abs(offset.x);
size.y += Math.abs(offset.y);
size.z += Math.abs(offset.z);
}
size.x += 0.01f;
// Reset any text box we already had
bitmapText.setBox(textBox);
}
[/java]
You can safely ignore the “offset” bits that has to do with how I handle shadow layers.
@pspeed said:
If you mean "scaling" in the setLocalScale() sense then this is incorrect. It sets the quad size based on the setSize() parameter on the BitmapText. It's not really a "scale" in JME terms.
I don’t really do anything magic that I’m aware of and Lemur seems to have no problems with BitmapText bounding boxes or alignment. The only fudge I’ve had to make is to bump out the x size by a tiny amount in my “preferred size” calculation because I think there is a “less than” somewhere in BitmapText where there should be a “less than or equal to”.
This is the entirety of how I calculated a text component’s preferred size in Lemur:
[java]
public void calculatePreferredSize( Vector3f size ) {
// Make sure that the bitmapText reports a reliable
// preferred size
bitmapText.setBox(null);
size.x = bitmapText.getLineWidth();
size.y = bitmapText.getHeight();
if( offset != null ) {
size.x += Math.abs(offset.x);
size.y += Math.abs(offset.y);
size.z += Math.abs(offset.z);
}
size.x += 0.01f;
// Reset any text box we already had
bitmapText.setBox(textBox);
}
[/java]
You can safely ignore the “offset” bits that has to do with how I handle shadow layers.
This is how I am doing this as well… if you try and set the font size larger and then use VAlign.Center, you’ll start seeing issues.
@t0neg0d said:
This isn't really vertically aligning anything if the rectangle is the size of the line height. =(
Which is what the OP was doing…
So I made the label full screen… still seems pretty centered:
I drew the black boxes in photoshop… they are the same size.
Ah, but you know… I’m not using BitmapText to do the centering, anyway. I do my own alignment so… move along, nothing to see here. I already do that same alignment for many other components so there was no reason really to let BitmapText do it.
@pspeed said:
Ah, but you know... I'm not using BitmapText to do the centering, anyway. I do my own alignment so... move along, nothing to see here. I already do that same alignment for many other components so there was no reason really to let BitmapText do it.