2 ttf2png - True Type Font to PNG converter
3 Copyright (c) 2004-2008 Mikko Rasa
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include FT_FREETYPE_H
54 unsigned round_to_pot(unsigned);
56 void init_font(Font *, FT_Face, unsigned, unsigned, int);
57 void render_grid(Font *, unsigned, unsigned, int);
58 void render_packed(Font *);
59 int save_defs(const char *, const Font *);
60 int save_png(const char *, const Image *, char);
64 int main(int argc, char **argv)
83 char *out_fn="font.png";
95 while((i=getopt(argc, argv, "r:s:l:c:o:atvh?ed:p"))!=-1)
102 if(!strcmp(optarg, "all"))
109 if(!isdigit(optarg[0]))
113 temp=strtol(optarg, &ptr, 0);
114 if(ptr[0]!=',' || !isdigit(ptr[1]))
119 printf("Not a valid range: %s\n", optarg);
125 end=strtol(ptr+1, NULL, 0);
130 size=strtol(optarg, NULL, 0);
133 cpl=strtol(optarg, NULL, 0);
136 cell=strtol(optarg, NULL, 0);
165 if(!strcmp(out_fn, "-"))
176 err=FT_Init_FreeType(&freetype);
179 fprintf(stderr, "Couldn't initialize FreeType library\n");
183 err=FT_New_Face(freetype, fn, 0, &face);
186 fprintf(stderr, "Couldn't load font file\n");
187 if(err==FT_Err_Unknown_File_Format)
188 fprintf(stderr, "Unknown file format\n");
194 const char *name=FT_Get_Postscript_Name(face);
195 printf("Font name: %s\n", name);
196 printf("Glyphs: %ld\n", face->num_glyphs);
199 err=FT_Set_Pixel_Sizes(face, 0, size);
202 fprintf(stderr, "Couldn't set size\n");
207 init_font(&font, face, begin, end, autohinter);
209 render_packed(&font);
211 render_grid(&font, cell, cpl, seq);
212 save_png(out_fn, &font.image, alpha);
214 save_defs(def_fn, &font);
216 for(i=0; i<font.n_glyphs; ++i)
217 free(font.glyphs[i].image.data);
219 free(font.image.data);
222 FT_Done_FreeType(freetype);
227 unsigned round_to_pot(unsigned n)
241 printf("ttf2png - True Type Font to PNG converter\n"
242 "Copyright (c) 2004-2008 Mikko Rasa, Mikkosoft Productions\n"
243 "Distributed under the GNU General Public License\n\n"
244 "Usage: ttf2png [options] <TTF file>\n\n"
245 "Accepted options (default values in [brackets])\n"
246 " -r Range of characters to convert in the format low,high [0,255]\n"
247 " -s Font size to use, in pixels [10]\n"
248 " -l Number of characters to put in one line [auto]\n"
249 " -c Character cell size, in pixels [auto]\n"
250 " -o Output file name (or - for stdout) [font.png]\n"
251 " -a Force autohinter\n"
252 " -t Render font to alpha channel\n"
253 " -v Increase the level of verbosity\n"
254 " -e Use cells in sequence, rather than by code\n"
255 " -p Pack the glyphs tightly instead of in a grid\n"
256 " -d Write a definition to the given file\n"
257 " -h Print this message\n");
260 void init_font(Font *font, FT_Face face, unsigned first, unsigned last, int autohinter)
265 font->ascent=(face->size->metrics.ascender+63)>>6;
266 font->descent=(face->size->metrics.descender+63)>>6;
270 printf("Ascent: %d\n", font->ascent);
271 printf("Descent: %d\n", font->descent);
276 for(i=first; i<=last; ++i)
279 FT_Bitmap *bmp=&face->glyph->bitmap;
284 n=FT_Get_Char_Index(face, i);
288 flags|=FT_LOAD_FORCE_AUTOHINT;
289 FT_Load_Glyph(face, n, flags);
290 FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
293 printf(" Char %u: glyph %u, size %dx%d\n", i, n, bmp->width, bmp->rows);
295 if(bmp->pixel_mode!=FT_PIXEL_MODE_GRAY)
297 fprintf(stderr, "Warning: Glyph %u skipped, not grayscale\n", n);
301 if(font->n_glyphs>=size)
304 font->glyphs=(Glyph *)realloc(font->glyphs, size*sizeof(Glyph));
307 glyph=&font->glyphs[font->n_glyphs++];
309 glyph->image.w=bmp->width;
310 glyph->image.h=bmp->rows;
311 glyph->image.data=(char *)malloc(bmp->width*bmp->rows);
312 glyph->offset_x=face->glyph->bitmap_left;
313 glyph->offset_y=face->glyph->bitmap_top-bmp->rows;
314 glyph->advance=(int)(face->glyph->advance.x+32)/64;
318 for(y=0; y<bmp->rows; ++y) for(x=0; x<bmp->width; ++x)
319 glyph->image.data[x+(glyph->image.h-1-y)*glyph->image.w]=bmp->buffer[x-y*bmp->pitch];
323 for(y=0; y<bmp->rows; ++y) for(x=0; x<bmp->width; ++x)
324 glyph->image.data[x+y*glyph->image.w]=bmp->buffer[x+y*bmp->pitch];
329 printf("Loaded %u glyphs\n", font->n_glyphs);
332 void render_grid(Font *font, unsigned cell, unsigned cpl, int seq)
336 unsigned first, last;
337 unsigned maxw=0, maxh=0;
341 first=font->glyphs[0].code&~(i-1);
343 if(last>=font->glyphs[font->n_glyphs-1].code)
347 for(i=0; i<font->n_glyphs; ++i)
351 y=font->glyphs[i].offset_y+font->glyphs[i].image.h;
354 if(font->glyphs[i].offset_y<bot)
355 bot=font->glyphs[i].offset_y;
356 if(font->glyphs[i].image.w>maxw)
357 maxw=font->glyphs[i].image.w;
358 if(font->glyphs[i].image.h>maxh)
359 maxh=font->glyphs[i].image.h;
371 printf("Max size: %u x %u\n", maxw, maxh);
372 printf("Y range: [%d %d]\n", bot, top);
373 if(maxw>cell || top-bot>cell)
374 fprintf(stderr,"Warning: character size exceeds cell size (%d)\n", cell);
379 unsigned count=(seq ? font->n_glyphs : last-first+1);
383 if(cpl>0 && (count+cpl-1)/cpl<=cpl)
388 font->image.w=round_to_pot(cpl*cell);
389 if(seq && font->n_glyphs<last-first+1)
390 font->image.h=(font->n_glyphs+cpl-1)/cpl*cell;
392 font->image.h=(last-first+cpl)/cpl*cell;
393 font->image.h=round_to_pot(font->image.h);
395 font->image.data=(char *)malloc(font->image.w*font->image.h);
396 memset(font->image.data, 255, font->image.w*font->image.h);
398 for(i=0; i<font->n_glyphs; ++i)
404 glyph=&font->glyphs[i];
413 cx=((glyph->code-first)%cpl)*cell;
414 cy=((glyph->code-first)/cpl)*cell;
417 if(cell>glyph->image.w)
418 cx+=(cell-glyph->image.w)/2;
419 cy+=top-glyph->offset_y-glyph->image.h;
424 for(y=0; y<glyph->image.h; ++y) for(x=0; x<glyph->image.w; ++x)
426 if(cx+x<0 || cx+x>=font->image.w || cy+y<0 || cy+y>=font->image.h) continue;
427 font->image.data[cx+x+(cy+y)*font->image.w]=255-glyph->image.data[x+y*glyph->image.w];
432 void render_packed(Font *font)
436 unsigned last_h=0xFFFF;
441 for(i=0; i<font->n_glyphs; ++i)
442 area+=(font->glyphs[i].image.w+1)*(font->glyphs[i].image.h+1);
444 for(font->image.w=1;; font->image.w<<=1)
446 font->image.h=(area*5/4)/font->image.w;
447 if(font->image.h<=font->image.w)
450 font->image.h=round_to_pot(font->image.h);
452 font->image.data=(char *)malloc(font->image.w*font->image.h);
453 memset(font->image.data, 255, font->image.w*font->image.h);
454 used_pixels=(char *)malloc(font->image.w*font->image.h);
455 memset(used_pixels, 0, font->image.w*font->image.h);
456 used_glyphs=(char *)malloc(font->n_glyphs);
457 memset(used_glyphs, 0, font->n_glyphs);
459 for(cy=0; cy<font->image.h;)
464 unsigned best_score=0;
466 for(; (cx<font->image.w && used_pixels[cx+cy*font->image.w]); ++cx);
467 if(cx>=font->image.w)
474 for(w=0; (cx+w<font->image.w && !used_pixels[cx+w+cy*font->image.w]); ++w);
476 for(i=0; i<font->n_glyphs; ++i)
481 if(!used_glyphs[i] && g->image.w<=w)
486 if(g->image.h==last_h)
505 used_glyphs[glyph-font->glyphs]=1;
509 for(y=0; y<glyph->image.h; ++y) for(x=0; x<glyph->image.w; ++x)
511 if(cx+x<0 || cx+x>=font->image.w || cy+y<0 || cy+y>=font->image.h) continue;
512 font->image.data[cx+x+(cy+y)*font->image.w]=255-glyph->image.data[x+y*glyph->image.w];
514 for(y=0; y<glyph->image.h+2; ++y) for(x=0; x<glyph->image.w+2; ++x)
516 if(cx+x<1 || cx+x>font->image.w || cy+y<1 || cy+y>font->image.h) continue;
517 used_pixels[cx+x-1+(cy+y-1)*font->image.w]=1;
520 last_h=glyph->image.h;
524 int save_defs(const char *fn, const Font *font)
532 fprintf(stderr, "Couldn't open %s\n",fn);
536 fprintf(out, "%d %d %d %d %d\n", font->image.w, font->image.h, font->size, font->ascent, font->descent);
537 for(i=0; i<font->n_glyphs; ++i)
539 const Glyph *g=&font->glyphs[i];
540 fprintf(out, "%u %u %u %u %u %d %d %d\n", g->code, g->x, g->y, g->image.w, g->image.h, g->offset_x, g->offset_y, g->advance);
548 int save_png(const char *fn, const Image *image, char alpha)
553 png_byte *rows[image->h];
565 fprintf(stderr, "Couldn't open %s\n",fn);
570 pngs=png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
573 fprintf(stderr, "Error writing PNG file\n");
576 pngi=png_create_info_struct(pngs);
579 png_destroy_write_struct(&pngs, NULL);
580 fprintf(stderr, "Error writing PNG file\n");
584 png_init_io(pngs, out);
587 data2=(png_byte *)malloc(image->w*image->h*2);
588 for(i=0; i<image->w*image->h; ++i)
591 data2[i*2+1]=255-image->data[i];
593 for(i=0; i<image->h; ++i)
594 rows[i]=(png_byte *)(data2+i*image->w*2);
595 color=PNG_COLOR_TYPE_GRAY_ALPHA;
599 for(i=0; i<image->h; ++i)
600 rows[i]=(png_byte *)(image->data+i*image->w);
601 color=PNG_COLOR_TYPE_GRAY;
603 png_set_IHDR(pngs, pngi, image->w, image->h, 8, color, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
604 png_set_rows(pngs, pngi, rows);
605 png_write_png(pngs, pngi, PNG_TRANSFORM_IDENTITY, NULL);
606 png_destroy_write_struct(&pngs, &pngi);
610 if(verbose) printf("Saved %dx%d PNG image to %s\n", image->w, image->h, fn);