2 ttf2png - True Type Font to PNG converter
3 Copyright (c) 2004-2006 Mikkosoft Productions
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
25 #include FT_FREETYPE_H
27 typedef struct sGlyphDef
36 unsigned round_to_pot(unsigned);
38 int save_defs(const char *, const GlyphDef *, int, int, int, int);
39 int save_png(const char *, const char *, int, int, char);
43 int main(int argc, char **argv)
66 char *out_fn="font.png";
77 while((i=getopt(argc, argv, "r:s:l:c:o:atvh?ed:"))!=-1)
84 if(!strcmp(optarg, "all"))
91 if(!isdigit(optarg[0]))
95 temp=strtol(optarg, &ptr, 0);
96 if(ptr[0]!=',' || !isdigit(ptr[1]))
101 printf("Not a valid range: %s\n", optarg);
107 end=strtol(ptr+1, NULL, 0);
112 size=strtol(optarg, NULL, 0);
115 cpl=strtol(optarg, NULL, 0);
118 cell=strtol(optarg, NULL, 0);
144 if(!strcmp(out_fn, "-"))
155 err=FT_Init_FreeType(&freetype);
158 fprintf(stderr, "Couldn't initialize FreeType library\n");
162 err=FT_New_Face(freetype, fn, 0, &face);
165 fprintf(stderr, "Couldn't load font file\n");
166 if(err==FT_Err_Unknown_File_Format)
167 fprintf(stderr, "Unknown file format\n");
173 const char *name=FT_Get_Postscript_Name(face);
174 printf("Font name: %s\n", name);
175 printf("Glyphs: %ld\n", face->num_glyphs);
178 err=FT_Set_Pixel_Sizes(face, 0, size);
181 fprintf(stderr, "Couldn't set size\n");
184 ascent=(face->bbox.yMax*face->size->metrics.x_scale)>>16;
185 ascent=(ascent+63)/64;
186 ch=((face->bbox.yMax-face->bbox.yMin)*face->size->metrics.y_scale)>>16;
188 cw=((face->bbox.xMax-face->bbox.xMin)*face->size->metrics.x_scale)>>16;
192 printf("Ascent %d\n",ascent);
193 printf("Descent %ld\n",(((face->bbox.yMin*face->size->metrics.y_scale)>>16)+63)/64);
194 printf("Max height %d\n",ch);
195 printf("Max width %d\n",cw);
197 if(verbose>=1 && (ch>cell || cw>cell)) fprintf(stderr,"Warning: character size exceeds cell size\n");
199 w=round_to_pot(cpl*cell);
200 if(seq && face->num_glyphs<end-begin)
201 h=(face->num_glyphs+cpl-1)/cpl*cell;
203 h=(end-begin+cpl-1)/cpl*cell;
206 data=(char *)malloc(w*h);
207 memset(data, 255, w*h);
211 if(face->num_glyphs<end-begin)
212 count=face->num_glyphs;
216 defs=(GlyphDef *)malloc(count*sizeof(GlyphDef));
220 for(i=begin; i<=end; ++i)
222 int glyph=FT_Get_Char_Index(face,i);
223 FT_Bitmap *bmp=&face->glyph->bitmap;
243 flags|=FT_LOAD_FORCE_AUTOHINT;
244 FT_Load_Glyph(face, glyph, flags);
245 FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
247 cx+=(cell-bmp->width)/2;
248 dy=ascent-face->glyph->bitmap_top;
253 printf(" Char %d: glyph %d, size %dx%d\n", i, glyph, bmp->width, bmp->rows);
254 if(bmp->width>cell || dy+bmp->rows>cell || dy<0) printf(" Warning: Character %d does not fit in cell\n", i);
259 fprintf(stderr, "Warning: Character %d not rendered (can't handle reversed bitmaps)\n", i);
263 for(y=0; y<bmp->rows; ++y) for(x=0; x<bmp->width; ++x)
265 if(cx+x<0 || cx+x>=w || cy+y<0 || cy+y>=h) continue;
266 data[cx+x+(cy+y)*w]=255-bmp->buffer[x+y*bmp->pitch];
274 defs[count].w=bmp->width;
275 defs[count].h=bmp->rows;
276 defs[count].ascent=face->glyph->bitmap_top-bmp->rows;
277 defs[count].advance=(int)(face->glyph->advance.x+32)/64;
283 while(seq && h/2>=(count+cpl-1)/cpl*cell)
287 save_defs(def_fn, defs, count, w, h, size);
288 save_png(out_fn, data, w, h, alpha);
290 if(verbose) printf("Converted %d glyphs\n", count);
295 unsigned round_to_pot(unsigned n)
309 printf("ttf2png - True Type Font to PNG converter\n"
310 "Copyright (c) 2004 Mikkosoft Productions\n"
311 "Distributed under the GNU General Public License\n\n"
312 "Usage: ttf2png [options] <TTF file>\n\n"
313 "Accepted options (default values in [brackets])\n"
314 " -r Range of characters to convert in the format low,high [0,255]\n"
315 " -s Font size to use, in pixels [10]\n"
316 " -l Number of characters to put in one line [16]\n"
317 " -c Character cell size, in pixels [16]\n"
318 " -o Output file name (or - for stdout) [font.png]\n"
319 " -a Force autohinter\n"
320 " -t Render font to alpha channel\n"
321 " -v Increase the level of verbosity\n"
322 " -e Use cells in sequence, rather than by code\n"
323 " -d Write a definition to the given file\n"
324 " -h Print this message\n");
327 int save_defs(const char *fn, const GlyphDef *defs, int count, int w, int h, int size)
335 fprintf(stderr, "Couldn't open %s\n",fn);
339 fprintf(out, "%d %d %d\n", w, h, size);
340 for(i=0; i<count; ++i)
342 const GlyphDef *d=defs+i;
343 fprintf(out, "%d %d %d %d %d %d %d\n", d->code, d->x, d->y, d->w, d->h, d->ascent, d->advance);
351 int save_png(const char *fn, const char *data, int w, int h, char alpha)
368 fprintf(stderr, "Couldn't open %s\n",fn);
373 pngs=png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
376 fprintf(stderr, "Error writing PNG file\n");
379 pngi=png_create_info_struct(pngs);
382 png_destroy_write_struct(&pngs, NULL);
383 fprintf(stderr, "Error writing PNG file\n");
387 png_init_io(pngs, out);
390 data2=(png_byte *)malloc(w*h*2);
394 data2[i*2+1]=255-data[i];
397 rows[i]=(png_byte *)(data2+i*w*2);
398 color=PNG_COLOR_TYPE_GRAY_ALPHA;
403 rows[i]=(png_byte *)(data+i*w);
404 color=PNG_COLOR_TYPE_GRAY;
406 png_set_IHDR(pngs, pngi, w, h, 8, color, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
407 png_set_rows(pngs, pngi, rows);
408 png_write_png(pngs, pngi, PNG_TRANSFORM_IDENTITY, NULL);
409 png_destroy_write_struct(&pngs, &pngi);
410 if(alpha) free(data2);
412 if(verbose) printf("Saved %dx%d PNG image to %s\n", w, h, fn);