]> git.tdb.fi Git - ttf2png.git/blob - ttf2png.c
Version 0.2.1
[ttf2png.git] / ttf2png.c
1 /*
2 ttf2png - True Type Font to PNG converter
3 Copyright (c) 2004-2006 Mikkosoft Productions
4
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.
9
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.
14
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
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <png.h>
24 #include <ft2build.h>
25 #include FT_FREETYPE_H
26
27 typedef struct sGlyphDef
28 {
29         int code;
30         int x,y;
31         int w,h;
32         int ascent;
33         int advance;
34 } GlyphDef;
35
36 unsigned round_to_pot(unsigned);
37 void usage();
38 int save_defs(const char *, const GlyphDef *, int, int, int, int);
39 int save_png(const char *, const char *, int, int, char);
40
41 char verbose=0;
42
43 int main(int argc, char **argv)
44 {
45         char *fn;
46         int  begin=0;
47         int  end=255;
48         int  size=10;
49         int  cpl=16;
50         int  cell=16;
51         char autohinter=0;
52         char seq=0;
53         char alpha=0;
54
55         FT_Library freetype;
56         FT_Face    face;
57         int  ch,cw;
58         int  ascent;
59
60         int  err;
61         int  i;
62         int  count;
63
64         int  w,h;
65         char *data;
66         char *out_fn="font.png";
67
68         char *def_fn=NULL;
69         GlyphDef *defs=NULL;
70
71         if(argc<2)
72         {
73                 usage();
74                 return 1;
75         }
76
77         while((i=getopt(argc, argv, "r:s:l:c:o:atvh?ed:"))!=-1)
78         {
79                 char *ptr;
80                 int  temp;
81                 switch(i)
82                 {
83                 case 'r':
84                         if(!strcmp(optarg, "all"))
85                         {
86                                 begin=0;
87                                 end=0x110000;
88                         }
89                         else
90                         {
91                                 if(!isdigit(optarg[0]))
92                                         temp=-1;
93                                 else
94                                 {
95                                         temp=strtol(optarg, &ptr, 0);
96                                         if(ptr[0]!=',' || !isdigit(ptr[1]))
97                                                 temp=-1;
98                                 }
99                                 if(temp<0)
100                                 {
101                                         printf("Not a valid range: %s\n", optarg);
102                                         exit(1);
103                                 }
104                                 else
105                                 {
106                                         begin=temp;
107                                         end=strtol(ptr+1, NULL, 0);
108                                 }
109                         }
110                         break;
111                 case 's':
112                         size=strtol(optarg, NULL, 0);
113                         break;
114                 case 'l':
115                         cpl=strtol(optarg, NULL, 0);
116                         break;
117                 case 'c':
118                         cell=strtol(optarg, NULL, 0);
119                         break;
120                 case 'o':
121                         out_fn=optarg;
122                         break;
123                 case 'a':
124                         autohinter=1;
125                         break;
126                 case 't':
127                         alpha=1;
128                         break;
129                 case 'v':
130                         ++verbose;
131                         break;
132                 case 'h':
133                 case '?':
134                         usage();
135                         return 0;
136                 case 'e':
137                         seq=1;
138                         break;
139                 case 'd':
140                         def_fn=optarg;
141                         break;
142                 }
143         }
144         if(!strcmp(out_fn, "-"))
145                 verbose=0;
146
147         if(optind!=argc-1)
148         {
149                 usage();
150                 return 1;
151         }
152         
153         fn=argv[optind];
154
155         err=FT_Init_FreeType(&freetype);
156         if(err)
157         {
158                 fprintf(stderr, "Couldn't initialize FreeType library\n");
159                 return 1;
160         }
161
162         err=FT_New_Face(freetype, fn, 0, &face);
163         if(err)
164         {
165                 fprintf(stderr, "Couldn't load font file\n");
166                 if(err==FT_Err_Unknown_File_Format)
167                         fprintf(stderr, "Unknown file format\n");
168                 return 1;
169         }
170
171         if(verbose)
172         {
173                 const char *name=FT_Get_Postscript_Name(face);
174                 printf("Font name: %s\n", name);
175                 printf("Glyphs: %ld\n", face->num_glyphs);
176         }
177
178         err=FT_Set_Pixel_Sizes(face, 0, size);
179         if(err)
180         {
181                 fprintf(stderr, "Couldn't set size\n");
182                 return 1;
183         }
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;
187         ch=(ch+63)/64;
188         cw=((face->bbox.xMax-face->bbox.xMin)*face->size->metrics.x_scale)>>16;
189         cw=(cw+63)/64;
190         if(verbose)
191         {
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);
196         }
197         if(verbose>=1 && (ch>cell || cw>cell)) fprintf(stderr,"Warning: character size exceeds cell size\n");
198
199         w=round_to_pot(cpl*cell);
200         if(seq && face->num_glyphs<end-begin)
201                 h=(face->num_glyphs+cpl-1)/cpl*cell;
202         else
203                 h=(end-begin+cpl-1)/cpl*cell;
204         h=round_to_pot(h);
205         
206         data=(char *)malloc(w*h);
207         memset(data, 255, w*h);
208
209         if(def_fn)
210         {
211                 if(face->num_glyphs<end-begin)
212                         count=face->num_glyphs;
213                 else
214                         count=end-begin;
215                 
216                 defs=(GlyphDef *)malloc(count*sizeof(GlyphDef));
217         }
218
219         count=0;
220         for(i=begin; i<=end; ++i)
221         {
222                 int       glyph=FT_Get_Char_Index(face,i);
223                 FT_Bitmap *bmp=&face->glyph->bitmap;
224                 int       x,y;
225                 int       cx,cy;
226                 int       flags=0;
227                 int       dy;
228
229                 if(!glyph) continue;
230                 
231                 if(seq)
232                 {
233                         cx=(count%cpl)*cell;
234                         cy=(count/cpl)*cell;
235                 }
236                 else
237                 {
238                         cx=(i%cpl)*cell;
239                         cy=(i/cpl)*cell;
240                 }
241
242                 if(autohinter)
243                         flags|=FT_LOAD_FORCE_AUTOHINT;
244                 FT_Load_Glyph(face, glyph, flags);
245                 FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
246                 if(cell>bmp->width)
247                         cx+=(cell-bmp->width)/2;
248                 dy=ascent-face->glyph->bitmap_top;
249                 cy+=dy;
250                 
251                 if(verbose>=2)
252                 {
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);
255                 }
256                 
257                 if(bmp->pitch<0)
258                 {
259                         fprintf(stderr, "Warning: Character %d not rendered (can't handle reversed bitmaps)\n", i);
260                         continue;
261                 }
262                 
263                 for(y=0; y<bmp->rows; ++y) for(x=0; x<bmp->width; ++x)
264                 {
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];
267                 }
268
269                 if(def_fn)
270                 {
271                         defs[count].code=i;
272                         defs[count].x=cx;
273                         defs[count].y=cy;
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;
278                 }
279
280                 ++count;
281         }
282
283         while(seq && h/2>=(count+cpl-1)/cpl*cell)
284                 h/=2;
285
286         if(def_fn)
287                 save_defs(def_fn, defs, count, w, h, size);
288         save_png(out_fn, data, w, h, alpha);
289
290         if(verbose) printf("Converted %d glyphs\n", count);
291
292         return 0;
293 }
294
295 unsigned round_to_pot(unsigned n)
296 {
297         n-=1;
298         n|=n>>1;
299         n|=n>>2;
300         n|=n>>4;
301         n|=n>>8;
302         n|=n>>16;
303
304         return n+1;
305 }
306         
307 void usage()
308 {
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");
325 }
326
327 int save_defs(const char *fn, const GlyphDef *defs, int count, int w, int h, int size)
328 {
329         FILE *out;
330         int  i;
331
332         out=fopen(fn, "w");
333         if(!out)
334         {
335                 fprintf(stderr, "Couldn't open %s\n",fn);
336                 return -1;
337         }
338
339         fprintf(out, "%d %d %d\n", w, h, size);
340         for(i=0; i<count; ++i)
341         {
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);
344         }
345
346         fclose(out);
347
348         return 0;
349 }
350
351 int save_png(const char *fn, const char *data, int w, int h, char alpha)
352 {
353         FILE       *out;
354         png_struct *pngs;
355         png_info   *pngi;
356         png_byte   *rows[h];
357         int        i;
358         png_byte   *data2;
359         int        color;
360
361         if(!strcmp(fn, "-"))
362                 out=stdout;
363         else
364         {
365                 out=fopen(fn, "wb");
366                 if(!out)
367                 {
368                         fprintf(stderr, "Couldn't open %s\n",fn);
369                         return -1;
370                 }
371         }
372
373         pngs=png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
374         if(!pngs)
375         {
376                 fprintf(stderr, "Error writing PNG file\n");
377                 return -1;
378         }
379         pngi=png_create_info_struct(pngs);
380         if(!pngi)
381         {
382                 png_destroy_write_struct(&pngs, NULL);
383                 fprintf(stderr, "Error writing PNG file\n");
384                 return -1;
385         }
386
387         png_init_io(pngs, out);
388         if(alpha)
389         {
390                 data2=(png_byte *)malloc(w*h*2);
391                 for(i=0; i<w*h; ++i)
392                 {
393                         data2[i*2]=255;
394                         data2[i*2+1]=255-data[i];
395                 }
396                 for(i=0; i<h; ++i)
397                         rows[i]=(png_byte *)(data2+i*w*2);
398                 color=PNG_COLOR_TYPE_GRAY_ALPHA;
399         }
400         else
401         {
402                 for(i=0; i<h; ++i)
403                         rows[i]=(png_byte *)(data+i*w);
404                 color=PNG_COLOR_TYPE_GRAY;
405         }
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);
411
412         if(verbose) printf("Saved %dx%d PNG image to %s\n", w, h, fn);
413
414         return 0;
415 }