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 › algorithm to adapt text into a rectangle, too slow
Page Index Toggle Pages: 1
algorithm to adapt text into a rectangle, too slow (Read 502 times)
algorithm to adapt text into a rectangle, too slow
Oct 12th, 2006, 4:41pm
 
I have this algorithm to adapt text into a rectangle, but it is too slow. Someone sugest me how improve this code.

Code:

//*********************************************************
// change size to see the behavior
int rectWidth = 250;
int rectHeight = 270;
//*********************************************************
String texto ="Padres don't come up with big hit";
String[] tlines = new String[0];
PFont fuente;
int sizeFontOk;
float _textWidth;
float _textHeight;

void setup(){
size(350, 350);
background(0);

fill(255);
PFont font = loadFont("AgencyFB-Reg-48.vlw");
int x = 10;
int y = 10;
int rectWidth = 100;
int rectHeight = 150;
rect(x, y, rectWidth, rectHeight);
String texto = "El pedro del roque no tiene rabo";
drawTypo(x, y, rectWidth, rectHeight, texto, font );

}
void drawTypo(int x,int y,int rectWidth,int rectHeight, String texto, PFont font ){

String[] tlines = new String[0];
int sizeFont = 0;
boolean w = true;
float margingLeft,margingTop;
int temp;
float _textWidth, _textHeight;

rectWidth = (int)((float)rectWidth*0.95);
rectHeight = (int)((float)rectHeight*0.95);

String [] t = texto.split(" ");
float lineMax=0;
fill(255,0,0);

while(w){
sizeFont++;

textFont(font, sizeFont );


if(textWidth(texto)>=rectWidth){
w = false;
}

if(!w){
textFont(font, sizeFont-1);
}
}
boolean cw = true;
while(cw){
sizeFont++;
textFont(font, sizeFont);
tlines = new String[1];
tlines[0] = "";
int i=0;
_textWidth=0;
for(int e=0;e<t.length;e++){
if((textWidth(tlines[i])+textWidth(t[e]) )<=rectWidth ){
tlines[i] += t[e]+" ";
}else{
if(t[e]!="" ||t[e]!=" " ){
tlines = expand(tlines,tlines.length+1);
i++;
tlines[i] = t[e]+" ";
}
}
if(_textWidth < textWidth(tlines[i]) ){_textWidth = textWidth(tlines[i]);}
}

_textHeight = sizeFont * tlines.length;

if(_textHeight >= rectHeight ){cw = false;println("no massa llarg->" +_textHeight); }
//if(_textWidth >= rectWidth ){cw = false;}
}

// reendering the last one
sizeFont = sizeFont-1;
textFont(font, sizeFont);
tlines = new String[1];
tlines[0] = "";
int i=0;
_textWidth=0;
for(int e=0;e<t.length;e++){
if((textWidth(tlines[i])+textWidth(t[e]) )<=rectWidth ){
tlines[i] += t[e]+" ";
}else{
tlines = expand(tlines,tlines.length+1);
i++;
tlines[i] = t[e]+" ";
}
if(_textWidth < textWidth(tlines[i]) ){_textWidth = textWidth(tlines[i]);}
}
_textHeight = sizeFont * tlines.length;
if(_textHeight >= rectHeight ){println("no massa llarg 2 ->" +_textHeight); } //
if(_textWidth >= rectWidth ){println("no massa ample 2 ->" +_textHeight);}
// height
temp = (int)((float)rectWidth /0.95);
margingLeft = ((float)(temp - rectWidth)/2.0);
// width
//temp = (int)((float)rectWidth /0.95);

for(int j=0; j<tlines.length; j++){
text(tlines[j], margingLeft+x, sizeFont*(j+1)+y);

}

}


thanks,

Mar
Re: algorithm to adapt text into a rectangle, too
Reply #1 - Oct 12th, 2006, 7:23pm
 
why not just use text("blah", x, y, width, height)?
http://processing.org/reference/text_.html

or is that what you're complaining is too slow?
Re: algorithm to adapt text into a rectangle, too
Reply #2 - Oct 12th, 2006, 9:10pm
 
Yeah, I just discover this behavior of text method thanks.

I'm will improve it.

Thanks a lot Fry.
Re: algorithm to adapt text into a rectangle, too
Reply #3 - Oct 13th, 2006, 4:25pm
 
This is my new code, too more quick. Would be more quik with using direct a textHeigh method, but I try to look in java graphics api and I don't see it.

Code:

//*********************************************************
// change size to see the behavior
int rectWidth = 250;
int rectHeight = 200;
//*********************************************************

String[] tlines = new String[0];
PFont fuente;
int sizeFontOk;
float _textWidth;
float _textHeight;

void setup(){
size(350, 350);
background(0);

fill(255);
PFont font = loadFont("AgencyFB-Reg-48.vlw");
int x = 0;
int y = 0;

rect(x, y, rectWidth, rectHeight);
String texto = "El pedro del roque no tiene rabo";

drawTypo(x, y, rectWidth, rectHeight, texto, font );

}

void drawTypo(int x,int y,int rectWidth,int rectHeight, String texto, PFont font ){

String[] tlines = new String[0];
int sizeFont = 0;
boolean w = true;
float margingLeft;
int temp;
float _textHeight;
float descent;

rectWidth = (int)((float)rectWidth*0.95);
rectHeight = (int)((float)rectHeight*0.95);

String [] t = texto.split(" ");

fill(255,0,0);
while(w){

sizeFont++;
textFont(font, sizeFont);
tlines = new String[1];
tlines[0] = "";
int i=0;

for(int e=0;e<t.length;e++){
if((textWidth(tlines[i])+textWidth(t[e]) )<=rectWidth ){
tlines[i] += t[e]+" ";
}else{
tlines = expand(tlines,tlines.length+1);
i++;
tlines[i] = t[e]+" ";
}
}// end for

_textHeight = sizeFont * tlines.length;

if(_textHeight >= rectHeight ){ w = false; }

} // end while

temp = (int)((float)rectWidth /0.95);
margingLeft = ((float)(temp - rectWidth)/2.0);
sizeFont = sizeFont-1;
textFont(font, sizeFont);
textLeading(sizeFont);
text(texto, margingLeft+x, y, rectWidth, rectHeight);

}


cheers,

Mar
Page Index Toggle Pages: 1