How to draw a bitmapFont scaled to fit in a rectangle.

Anything libgdx related goes here!

How to draw a bitmapFont scaled to fit in a rectangle.

Postby kazamera » Thu Jun 21, 2012 2:38 pm

Hello all, Im having an issue, im working in a map, that may have a lots of boxes "rectangles" and they all have a label, a text that indentifies them. But the size of them are different, so in some cases, i have to scale my bitmapFont do match the size of the box, my question here is, is there a way to draw a bitmapFont relative to some bound? like, draw the text at X,Y,WIDTH,HEIGHT, or something like that.

PS: im using the default font from ligdx

The boxes can change at each version of my app. Thats why i need a solution like that i think.

Im sorry for my poor english.

Thank you for ur time.
kazamera
 
Posts: 25
Joined: Tue May 29, 2012 6:54 pm

Re: How to draw a bitmapFont scaled to fit in a rectangle.

Postby Kerai » Fri Jun 22, 2012 3:53 am

BitmapFont.drawWrapped wraps text to lines if they are too long.

BitmapFont.getBounds() returns bounds for provided char sequence - you can calculate how much these bounds differ from your target and scale drawing text.
Kerai
 
Posts: 58
Joined: Thu Dec 08, 2011 12:51 pm

Re: How to draw a bitmapFont scaled to fit in a rectangle.

Postby kazamera » Tue Jun 26, 2012 5:55 pm

hmm, thanks for the reply, but how so? im trying to do the maths here but its not working hahaha.

here is what im doing to calculate the aproximated scale.

Code: Select all
TextBounds bounds = font12.getWrappedBounds(textWithLines,
               widhtOfTheBox);

float scale = ((bounds.width / bounds.height) / (widhtOfTheBox));


but is not working, im sorry im new on this thing.

thank u for ur time!
kazamera
 
Posts: 25
Joined: Tue May 29, 2012 6:54 pm

Re: How to draw a bitmapFont scaled to fit in a rectangle.

Postby kazamera » Wed Jun 27, 2012 7:19 pm

Hey i found the solution, it was prety simple...

Code: Select all

float scaleX = (myBox.width/bounds.width);
float scaleY = (myBox.height/bounds.height);



Thanks!
kazamera
 
Posts: 25
Joined: Tue May 29, 2012 6:54 pm

Re: How to draw a bitmapFont scaled to fit in a rectangle.

Postby Kerai » Thu Jun 28, 2012 1:58 am

I did this for scaling text that is too big and need to fit bounds including height
If it fits after wrapping, it is not scaled; if it is too high, it is scaled, checkec for bounds and then scaled once again to fit.
Not perfect, but it somehow does the thing
TextFit.java
Kerai
 
Posts: 58
Joined: Thu Dec 08, 2011 12:51 pm


Return to Libgdx

Who is online

Users browsing this forum: HD_92 and 6 guests