anachronik
Junior Member
Offline
Posts: 80
Re: create font with rect()
Reply #11 - Oct 10th , 2007, 3:44am
Nice to know that ewjordan.
I've make some work over this sketch.
Actually i just want the space between each letter to be only one square length(=rectSize).
I guess as my letters are 5x5 I've to find each letter width in order to have the same space between each letter.That where i'm stuck actually, i've try to figure it out without sucess but i'm not far i guess.
Here's the code:
Quote: // thx to fjen and nokoony void setup () { size ( 300, 50 ); background (255); noStroke (); fill (0); initBitFonts(); //guidelines for ( int i=0; i < width ; i++){ stroke (0, 80); //line( i*4, 0, height, i*4); //nice background line (i*5, 0, i*5, height ); } line (0, height /2, width , height /2); noLoop (); }void draw () { drawText( "mai" , 20, height /4 ); }void drawText ( String txt, int x, int y ) { float rectSize = 5.0; float letterW = 0.0; float offset = 0.0; int [] rowSize = {0,0,0,0,0}; //have to tweak it, according to the letter //float letterSize = letterW * rectSize + letterW; //loop for handling each string character. for ( int i = 0; i<txt.length (); i++ ) { println (i); String cKey = txt.charAt (i)+"" ; int [] letter = (int [])(bitfont1.get ( cKey )); //get the current character float lx, ly; //define the width of a letter according to the max numbers of 1 in a row. for ( int j = 0; j < 5; j++ ) { for (int l =0; l < 5; l++){ if (letter[5*j + l] ==1){ rowSize[j]++; } } } //assign the width of the bitfont letter // for exemple m = 5 (1,1,1,1,1) n = 3 (0,1,1,1,0) i = 1 (0,0,1,0,0) //see the letters tab. //pb seems to be there or during the previous loop letterW = max (rowSize); println (letterW);// display correct value for each cycles but sums them // for exemple, the word "mai" should display 5,3,1 //and it display 5,8,9 like letterW never go back to 0 //tried to initialize it at the start of the loop but do not work. //letterW*rectSize = width of the letter //just add a rectSize a space like a blank square between each letter. offset = i*((letterW*rectSize)+rectSize); //loop for handling each matrix value //trace the shape of the font for ( int k = 0 ; k < letter.length ; k++ ) { lx = k % 5; ly = k / 5; if ( letter[k] == 0 ) continue ; rect ( x + offset + lx*rectSize, y + ly*rectSize, rectSize, rectSize ); } } }
and the letters tab:
HashMap bitfont1;
void initBitFonts() {
bitfont1 = new HashMap();
bitfont1.put("a", new int[]{
1, 1, 1, 0, 0,
1, 0, 1, 0, 0,
1, 1, 1, 0, 0,
1, 0, 1, 0, 0,
1, 0, 1, 0, 0
}
);
bitfont1.put("i", new int[]{
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0
}
);
bitfont1.put("m", new int[]{
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 1, 0, 1,
1, 0, 1, 0, 1,
1, 0, 1, 0, 1
}
);
}
Am i on the good way or totally wrong ?