mcanet
YaBB Newbies
Offline
Posts: 36
uk
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