- 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, j;
+ int x, y;
+ unsigned char origin_pixel = 0;
+ unsigned closest = range*range;
+
+ if(origin_x>=0 && (unsigned)origin_x<image->w && origin_y>=0 && (unsigned)origin_y<image->h)
+ origin_pixel = image->data[origin_x+origin_y*image->w];
+
+ x = origin_x-1;
+ y = origin_y-1;
+ for(i=1; (i<range && i*i<=closest); ++i, --x, --y) for(j=0; j<4; ++j)
+ {
+ unsigned k;
+ int dx = (j==0 ? 1 : j==2 ? -1 : 0);
+ int dy = (j==1 ? 1 : j==3 ? -1 : 0);
+
+ for(k=0; k<i*2; ++k, x+=dx, y+=dy)
+ {
+ unsigned char pixel = 0;
+ if(x>=0 && (unsigned)x<image->w && y>=0 && (unsigned)y<image->h)
+ pixel = image->data[x+y*image->w];
+
+ if((pixel^origin_pixel)&0x80)
+ {
+ unsigned d = 2*i*i + k*k - 2*k*i;
+ if(d<closest)
+ closest = d;
+ }
+ }
+ }
+
+ return sqrti(closest*0x3F01)/range;
+}
+
+int create_distance_field(const FT_Bitmap *bmp, Image *image, unsigned scale, unsigned margin)
+{
+ unsigned x, y;
+ Image base_image;
+
+ if(!bmp->width || !bmp->rows)
+ {
+ image->w = 0;
+ image->h = 0;
+ image->data = NULL;
+ return 0;
+ }
+
+ if(copy_bitmap(bmp, &base_image))
+ return -1;
+
+ image->w = (base_image.w-1)/scale+2*margin+1;
+ image->h = (base_image.h-1)/scale+2*margin+1;
+ image->data = (unsigned char *)malloc(image->w*image->h);
+ if(!image->data)
+ {
+ fprintf(stderr, "Cannot allocate %d bytes of memory for glyph\n", image->w*image->h);
+ free(base_image.data);
+ return -1;
+ }
+
+ for(y=0; y<image->h; ++y) for(x=0; x<image->w; ++x)
+ {
+ int bx = (x-margin)*scale+scale/2;
+ int by = (y-margin)*scale+scale/2;
+ unsigned char pixel = find_distance_to_edge(&base_image, bx, by, margin*scale);
+ if(bx>=0 && (unsigned)bx<base_image.w && by>=0 && (unsigned)by<base_image.h)
+ pixel |= base_image.data[bx+by*base_image.w]&0x80;
+ if(!(pixel&0x80))
+ pixel = 0x80-pixel;
+ image->data[x+y*image->w] = pixel;
+ }
+
+ free(base_image.data);
+
+ return 0;
+}
+
+int render_grid(Font *font, unsigned cellw, unsigned cellh, unsigned cpl, bool seq, bool npot)
+{
+ unsigned i;
+ int top = 0, bot = 0;
+ unsigned first, last;
+ unsigned maxw = 0, maxh = 0;
+
+ /* Find extremes of the glyph images. */
+ for(i=0; i<font->n_glyphs; ++i)
+ {
+ int y;
+
+ y = font->glyphs[i].offset_y+font->glyphs[i].image.h;
+ if(y>top)
+ top = y;
+ if(font->glyphs[i].offset_y<bot)
+ bot = font->glyphs[i].offset_y;
+ if(font->glyphs[i].image.w>maxw)
+ maxw = font->glyphs[i].image.w;
+ if(font->glyphs[i].image.h>maxh)
+ maxh = font->glyphs[i].image.h;
+ }
+
+ if(cellw==0)
+ {
+ /* Establish a large enough cell to hold all glyphs in the range. */
+ int square = (cellh==cellw);
+ cellw = maxw;
+ cellh = top-bot;
+ if(square)
+ {
+ if(cellh>cellw)
+ cellw = cellh;
+ else
+ cellh = cellw;
+ }
+ }
+
+ if(verbose>=1)
+ {
+ printf("Max size: %u x %u\n", maxw, maxh);
+ printf("Y range: [%d %d]\n", bot, top);
+ printf("Cell size: %u x %u\n", cellw, cellh);
+ if(maxw>cellw || (unsigned)(top-bot)>cellh)
+ fprintf(stderr, "Warning: character size exceeds cell size\n");
+ }
+
+ if(cpl==0)
+ {
+ /* Determine number of characters per line, trying to fit all the glyphs
+ in a square image. */
+ for(i=1;; i<<=1)
+ {
+ cpl = i/cellw;
+ if(cpl>0 && font->n_glyphs/cpl*cellh<=cpl*cellw)
+ break;
+ }
+ }
+
+ first = font->glyphs[0].code;
+ if(!seq)
+ first -= first%cpl;
+ last = font->glyphs[font->n_glyphs-1].code;
+
+ font->image.w = cpl*cellw;
+ if(!npot)
+ font->image.w = round_to_pot(font->image.w);
+ if(seq)
+ font->image.h = (font->n_glyphs+cpl-1)/cpl*cellh;
+ else
+ font->image.h = (last-first+cpl)/cpl*cellh;
+ if(!npot)
+ font->image.h = round_to_pot(font->image.h);
+
+ font->image.data = (unsigned char *)alloc_image_data(font->image.w, font->image.h);
+ if(!font->image.data)
+ return -1;
+ memset(font->image.data, 255, font->image.w*font->image.h);
+
+ for(i=0; i<font->n_glyphs; ++i)
+ {
+ Glyph *glyph;
+ unsigned ci, cx, cy;
+ unsigned x, y;
+
+ glyph = &font->glyphs[i];
+
+ if(seq)
+ ci = i;
+ else
+ ci = glyph->code-first;
+
+ cx = (ci%cpl)*cellw;
+ cy = (ci/cpl)*cellh;
+
+ if(cellw>glyph->image.w)
+ cx += (cellw-glyph->image.w)/2;
+ cy += top-glyph->offset_y-glyph->image.h;
+
+ 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>=font->image.w || 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];
+ }
+ }
+
+ return 0;