- printf("ttf2png - True Type Font to PNG converter\n"
- "Copyright (c) 2004 Mikkosoft Productions\n"
- "Distributed under the GNU General Public License\n\n"
- "Usage: ttf2png [options] <TTF file>\n\n"
- "Accepted options (default values in [brackets])\n"
- " -r Range of characters to convert in the format low,high [0,255]\n"
- " -s Font size to use, in pixels [10]\n"
- " -l Number of characters to put in one line [16]\n"
- " -c Character cell size, in pixels [16]\n"
- " -o Output file name (or - for stdout) [font.png]\n"
- " -a Force autohinter\n"
- " -t Render font to alpha channel\n"
- " -v Increase the level of verbosity\n"
- " -e Use cells in sequence, rather than by code\n"
- " -d Write a definition to the given file\n"
- " -h Print this message\n");
+ unsigned i;
+ unsigned area = 0;
+ unsigned last_h = 0xFFFF;
+ char *used_glyphs;
+ char *used_pixels;
+ unsigned cx = 0, cy;
+
+ for(i=0; i<font->n_glyphs; ++i)
+ area+=(font->glyphs[i].image.w+1)*(font->glyphs[i].image.h+1);
+
+ for(font->image.w=1;; font->image.w<<=1)
+ {
+ font->image.h = (area*5/4)/font->image.w;
+ if(font->image.h<=font->image.w)
+ break;
+ }
+ font->image.h = round_to_pot(font->image.h);
+
+ font->image.data = (char *)malloc(font->image.w*font->image.h);
+ memset(font->image.data, 255, font->image.w*font->image.h);
+ used_pixels = (char *)malloc(font->image.w*font->image.h);
+ memset(used_pixels, 0, font->image.w*font->image.h);
+ used_glyphs = (char *)malloc(font->n_glyphs);
+ memset(used_glyphs, 0, font->n_glyphs);
+
+ for(cy=0; cy<font->image.h;)
+ {
+ unsigned w;
+ unsigned x, y;
+ Glyph *glyph = NULL;
+ unsigned best_score = 0;
+
+ for(; (cx<font->image.w && used_pixels[cx+cy*font->image.w]); ++cx) ;
+ if(cx>=font->image.w)
+ {
+ cx = 0;
+ ++cy;
+ last_h = 0xFFFF;
+ continue;
+ }
+ for(w=0; (cx+w<font->image.w && !used_pixels[cx+w+cy*font->image.w]); ++w) ;
+
+ for(i=0; i<font->n_glyphs; ++i)
+ {
+ Glyph *g;
+
+ g = &font->glyphs[i];
+ if(!used_glyphs[i] && g->image.w<=w)
+ {
+ unsigned score;
+
+ score = g->image.h+1;
+ if(g->image.h==last_h)
+ score *= g->image.w;
+ else
+ score += g->image.w;
+
+ if(score>best_score)
+ {
+ glyph = g;
+ best_score = score;
+ }
+ }
+ }
+
+ if(!glyph)
+ {
+ cx += w;
+ continue;
+ }
+
+ used_glyphs[glyph-font->glyphs] = 1;
+ glyph->x = cx;
+ glyph->y = cy;
+
+ for(y=0; y<glyph->image.h; ++y) for(x=0; x<glyph->image.w; ++x)
+ {
+ if(cx+x<0 || cx+x>=font->image.w || cy+y<0 || cy+y>=font->image.h)
+ continue;
+ font->image.data[cx+x+(cy+y)*font->image.w] = 255-glyph->image.data[x+y*glyph->image.w];
+ }
+ for(y=0; y<glyph->image.h+2; ++y) for(x=0; x<glyph->image.w+2; ++x)
+ {
+ if(cx+x<1 || cx+x>font->image.w || cy+y<1 || cy+y>font->image.h)
+ continue;
+ used_pixels[cx+x-1+(cy+y-1)*font->image.w] = 1;
+ }
+
+ last_h = glyph->image.h;
+ }