2 ttf2png - True Type Font to PNG converter
3 Copyright (c) 2004 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
28 int save_png(char *, char *, int, int);
30 int main(int argc, char **argv)
59 while((o=getopt(argc,argv,"r:s:l:c:o:avh?"))!=-1)
66 if(!isdigit(optarg[0])) temp=-1;
69 temp=strtol(optarg,&ptr,0);
70 if(ptr[0]!=',' || !isdigit(ptr[1])) temp=-1;
73 printf("Not a valid range: %s\n",optarg);
77 end=strtol(ptr+1,NULL,0);
81 size=strtol(optarg,NULL,0);
84 cpl=strtol(optarg,NULL,0);
87 cell=strtol(optarg,NULL,0);
112 err=FT_Init_FreeType(&freetype);
115 fprintf(stderr,"Couldn't initialize FreeType library\n");
119 err=FT_New_Face(freetype,fn,0,&face);
122 fprintf(stderr,"Couldn't load font file\n");
123 if(err==FT_Err_Unknown_File_Format)
124 fprintf(stderr,"Unknown file format\n");
128 name=FT_Get_Postscript_Name(face);
129 if(verbose) printf("Font name: %s\n",name);
131 err=FT_Set_Pixel_Sizes(face,0,size);
134 fprintf(stderr,"Couldn't set size\n");
137 ascent=face->size->metrics.ascender/64;
138 ch=(face->size->metrics.ascender-face->size->metrics.descender)/64;
139 cw=face->size->metrics.max_advance/64;
142 printf("Ascent %d\n",ascent);
143 printf("Descent %ld\n",face->size->metrics.descender/64);
144 printf("Height %d\n",ch);
145 printf("Width %d\n",cw);
147 if(ch>cell || cw>cell) fprintf(stderr,"ttf2png: Warning: character size exceeds cell size\n");
150 h=(end-begin+cpl-1)/cpl*cell;
151 data=(char *)malloc(w*h);
152 memset(data,255,w*h);
155 for(i=begin;i<=end;++i)
157 int glyph=FT_Get_Char_Index(face,i);
158 FT_Bitmap *bmp=&face->glyph->bitmap;
160 int cx=(i%cpl)*cell+(cell-cw)/2;
165 if(autohinter) flags|=FT_LOAD_FORCE_AUTOHINT;
166 FT_Load_Glyph(face,glyph,flags);
167 FT_Render_Glyph(face->glyph,FT_RENDER_MODE_NORMAL);
168 cx+=face->glyph->bitmap_left;
169 cy+=ascent-face->glyph->bitmap_top;
170 for(y=0;y<bmp->rows;++y) for(x=0;x<bmp->width;++x)
172 if(cx+x<0 || cx+x>=w || cy+y<0 || cy+y>=h) continue;
174 data[cx+x+(cy+y)*w]=255-bmp->buffer[x+y*bmp->pitch];
179 save_png(out,data,w,h);
181 if(verbose) printf("Converted %d glyphs\n",count);
188 printf("ttf2png - True Type Font to PNG converter\n"
189 "Copyright (c) 2004 Mikkosoft Productions\n"
190 "Distributed under the GNU General Public License\n\n"
191 "Usage: ttf2png [options] <TTF file>\n\n"
192 "Accepted options (default values in [brackets])\n"
193 " -r Range of characters to convert in the format low,high [0,255]\n"
194 " -s Font size to use, in pixels [10]\n"
195 " -l Number of characters to put in one line [16]\n"
196 " -c Character cell size, in pixels [16]\n"
197 " -o Output file name [font.png]\n"
198 " -a Force autohinter\n"
199 " -v Enable verbose mode\n"
200 " -h Print this message\n");
203 int save_png(char *fn, char *data, int w, int h)
214 fprintf(stderr,"Couldn't open output file\n");
218 pngs=png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
221 fprintf(stderr,"Error writing PNG file\n");
224 pngi=png_create_info_struct(pngs);
227 png_destroy_write_struct(&pngs,NULL);
228 fprintf(stderr,"Error writing PNG file\n");
232 png_init_io(pngs,out);
233 png_set_IHDR(pngs,pngi,w,h,8,PNG_COLOR_TYPE_GRAY,PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
235 rows[i]=(png_byte *)(data+i*w);
236 png_set_rows(pngs,pngi,rows);
237 png_write_png(pngs,pngi,PNG_TRANSFORM_IDENTITY,NULL);
238 png_destroy_write_struct(&pngs,&pngi);