We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › variable size text in a fixed size rectangle
Page Index Toggle Pages: 1
variable size text in a fixed size rectangle (Read 612 times)
variable size text in a fixed size rectangle
Jun 17th, 2008, 5:41pm
 
Hey, can anyone please help me with this:
I have a given text. I have to display it using text() and rect() in a rectangle.

I am using text(string, position x, position y, size in x, size in y) and I determine (size in x) and (size in y)  using textWidth and textAscent.

Then in  I need to calculate how many alphabets will fit in the given rectangle of the given text . But there is no way it seems  to count the number of characters in  a string?
Even worse, I can't figure out how to relate number of characters and font size with the  size parameters of text() .

What is the role of Font Size in it?
Can anyone help with any of these?
Re: variable size text in a fixed size rectangle
Reply #1 - Jun 17th, 2008, 5:50pm
 
Quote:
My first problem is that in the statment
text(string, position x, position y, size in x, size in y)  
how do I determine (size in x) and (size in y) ?

you don't have to determine those values, you have to set them! if you specify those two last parameters, your text should wrap in a rectangle :

Code:
text("this text should fit in a square of dim: 50x50)", 10, 10, 50, 50); 



Quote:
I need to calculate how many alphabets will fit in the given rectangle.

You can calculate how much pixels a text will need by using the textWidth() method (see reference).
http://processing.org/reference/textWidth_.html
But it only works with inline text, and since yours is going to be wrapped, it's not so handy (unless you can tell/calculate where the text lines are going to break).
Re: variable size text in a fixed size rectangle
Reply #2 - Jun 18th, 2008, 10:22am
 
thanks a tonne...
Ur idea has helped me a lot..
In my case , the size of x and the size of y are doing the clipping of the text on their own.
No need for some text breaking mumbo jumbo.
by keeping constant font size I am able to get what I desire..

http://turing.lecolededesign.com:16080/~rshukla/applet/
Page Index Toggle Pages: 1