]> git.tdb.fi Git - ttf2png.git/blob - ttf2png.c
Version 0.2.2
[ttf2png.git] / ttf2png.c
1 /*
2 ttf2png - True Type Font to PNG converter
3 Copyright (c) 2004-2007 Mikko Rasa
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 offset_x;
33         int offset_y;
34         int advance;
35 } GlyphDef;
36
37 unsigned round_to_pot(unsigned);
38 void usage();
39 int save_defs(const char *, const GlyphDef *, int, int, int, int, int, int);
40 int save_png(const char *, const char *, int, int, char);
41
42 char verbose=0;
43
44 int main(int argc, char **argv)
45 {
46         char *fn;
47         int  begin=0;
48         int  end=255;
49         int  size=10;
50         int  cpl=16;
51         int  cell=16;
52         char autohinter=0;
53         char seq=0;
54         char alpha=0;
55
56         FT_Library freetype;
57         FT_Face    face;
58         int  ch,cw;
59         int  ascent;
60         int  descent;
61
62         int  err;
63         int  i;
64         int  count;
65
66         int  w,h;
67         char *data;
68         char *out_fn="font.png";
69
70         char *def_fn=NULL;
71         GlyphDef *defs=NULL;
72
73         if(argc<2)
74         {
75                 usage();
76                 return 1;
77         }
78
79         while((i=getopt(argc, argv, "r:s:l:c:o:atvh?ed:"))!=-1)
80         {
81                 char *ptr;
82                 int  temp;
83                 switch(i)
84                 {
85                 case 'r':
86                         if(!strcmp(optarg, "all"))
87                         {
88                                 begin=0;
89                                 end=0x110000;
90                         }
91                         else
92                         {
93                                 if(!isdigit(optarg[0]))
94                                         temp=-1;
95                                 else
96                                 {
97                                         temp=strtol(optarg, &ptr, 0);
98                                         if(ptr[0]!=',' || !isdigit(ptr[1]))
99                                                 temp=-1;
100                                 }
101                                 if(temp<0)
102                                 {
103                                         printf("Not a valid range: %s\n", optarg);
104                                         exit(1);
105                                 }
106                                 else
107                                 {
108                                         begin=temp;
109                                         end=strtol(ptr+1, NULL, 0);
110                                 }
111                         }
112                         break;
113                 case 's':
114                         size=strtol(optarg, NULL, 0);
115                         break;
116                 case 'l':
117                         cpl=strtol(optarg, NULL, 0);
118                         break;
119                 case 'c':
120                         cell=strtol(optarg, NULL, 0);
121                         break;
122                 case 'o':
123                         out_fn=optarg;
124                         break;
125                 case 'a':
126                         autohinter=1;
127                         break;
128                 case 't':
129                         alpha=1;
130                         break;
131                 case 'v':
132                         ++verbose;
133                         break;
134                 case 'h':
135                 case '?':
136                         usage();
137                         return 0;
138                 case 'e':
139                         seq=1;
140                         break;
141                 case 'd':
142                         def_fn=optarg;
143                         break;
144                 }
145         }
146         if(!strcmp(out_fn, "-"))
147                 verbose=0;
148
149         if(optind!=argc-1)
150         {
151                 usage();
152                 return 1;
153         }
154         
155         fn=argv[optind];
156
157         err=FT_Init_FreeType(&freetype);
158         if(err)
159         {
160                 fprintf(stderr, "Couldn't initialize FreeType library\n");
161                 return 1;
162         }
163
164         err=FT_New_Face(freetype, fn, 0, &face);
165         if(err)
166         {
167                 fprintf(stderr, "Couldn't load font file\n");
168                 if(err==FT_Err_Unknown_File_Format)
169                         fprintf(stderr, "Unknown file format\n");
170                 return 1;
171         }
172
173         if(verbose)
174         {
175                 const char *name=FT_Get_Postscript_Name(face);
176                 printf("Font name: %s\n", name);
177                 printf("Glyphs: %ld\n", face->num_glyphs);
178         }
179
180         err=FT_Set_Pixel_Sizes(face, 0, size);
181         if(err)
182         {
183                 fprintf(stderr, "Couldn't set size\n");
184                 return 1;
185         }
186         ascent=(face->bbox.yMax*face->size->metrics.y_scale)>>16;
187         ascent=(ascent+63)/64;
188         descent=(face->bbox.yMin*face->size->metrics.y_scale)>>16;
189         descent=(ascent+63)/64;
190         ch=((face->bbox.yMax-face->bbox.yMin)*face->size->metrics.y_scale)>>16;
191         ch=(ch+63)/64;
192         cw=((face->bbox.xMax-face->bbox.xMin)*face->size->metrics.x_scale)>>16;
193         cw=(cw+63)/64;
194         if(verbose)
195         {
196                 printf("Ascent %d\n", ascent);
197                 printf("Descent %d\n", descent);
198                 printf("Max height %d\n", ch);
199                 printf("Max width %d\n", cw);
200         }
201         if(verbose>=1 && (ch>cell || cw>cell)) fprintf(stderr,"Warning: character size exceeds cell size\n");
202
203         w=round_to_pot(cpl*cell);
204         if(seq && face->num_glyphs<end-begin)
205                 h=(face->num_glyphs+cpl-1)/cpl*cell;
206         else
207                 h=(end-begin+cpl-1)/cpl*cell;
208         h=round_to_pot(h);
209         
210         data=(char *)malloc(w*h);
211         memset(data, 255, w*h);
212
213         if(def_fn)
214         {
215                 if(face->num_glyphs<end-begin)
216                         count=face->num_glyphs;
217                 else
218                         count=end-begin;
219                 
220                 defs=(GlyphDef *)malloc(count*sizeof(GlyphDef));
221         }
222
223         count=0;
224         for(i=begin; i<=end; ++i)
225         {
226                 int       glyph=FT_Get_Char_Index(face,i);
227                 FT_Bitmap *bmp=&face->glyph->bitmap;
228                 int       x,y;
229                 int       cx,cy;
230                 int       flags=0;
231                 int       dy;
232
233                 if(!glyph) continue;
234                 
235                 if(seq)
236                 {
237                         cx=(count%cpl)*cell;
238                         cy=(count/cpl)*cell;
239                 }
240                 else
241                 {
242                         cx=(i%cpl)*cell;
243                         cy=(i/cpl)*cell;
244                 }
245
246                 if(autohinter)
247                         flags|=FT_LOAD_FORCE_AUTOHINT;
248                 FT_Load_Glyph(face, glyph, flags);
249                 FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
250                 if(cell>bmp->width)
251                         cx+=(cell-bmp->width)/2;
252                 dy=ascent-face->glyph->bitmap_top;
253                 cy+=dy;
254                 
255                 if(verbose>=2)
256                 {
257                         printf("  Char %d: glyph %d, size %dx%d\n", i, glyph, bmp->width, bmp->rows);
258                         if(bmp->width>cell || dy+bmp->rows>cell || dy<0) printf("  Warning: Character %d does not fit in cell\n", i);
259                 }
260                 
261                 if(bmp->pitch<0)
262                 {
263                         fprintf(stderr, "Warning: Character %d not rendered (can't handle reversed bitmaps)\n", i);
264                         continue;
265                 }
266                 
267                 for(y=0; y<bmp->rows; ++y) for(x=0; x<bmp->width; ++x)
268                 {
269                         if(cx+x<0 || cx+x>=w || cy+y<0 || cy+y>=h) continue;
270                         data[cx+x+(cy+y)*w]=255-bmp->buffer[x+y*bmp->pitch];
271                 }
272
273                 if(def_fn)
274                 {
275                         defs[count].code=i;
276                         defs[count].x=cx;
277                         defs[count].y=cy;
278                         defs[count].w=bmp->width;
279                         defs[count].h=bmp->rows;
280                         defs[count].offset_x=face->glyph->bitmap_left;
281                         defs[count].offset_y=face->glyph->bitmap_top-bmp->rows;
282                         defs[count].advance=(int)(face->glyph->advance.x+32)/64;
283                 }
284
285                 ++count;
286         }
287
288         while(seq && h/2>=(count+cpl-1)/cpl*cell)
289                 h/=2;
290
291         if(def_fn)
292                 save_defs(def_fn, defs, count, w, h, size, ascent, descent);
293         save_png(out_fn, data, w, h, alpha);
294
295         if(verbose) printf("Converted %d glyphs\n", count);
296
297         return 0;
298 }
299
300 unsigned round_to_pot(unsigned n)
301 {
302         n-=1;
303         n|=n>>1;
304         n|=n>>2;
305         n|=n>>4;
306         n|=n>>8;
307         n|=n>>16;
308
309         return n+1;
310 }
311         
312 void usage()
313 {
314         printf("ttf2png - True Type Font to PNG converter\n"
315                 "Copyright (c) 2004 Mikkosoft Productions\n"
316                 "Distributed under the GNU General Public License\n\n"
317                 "Usage: ttf2png [options] <TTF file>\n\n"
318                 "Accepted options (default values in [brackets])\n"
319                 "  -r  Range of characters to convert in the format low,high [0,255]\n"
320                 "  -s  Font size to use, in pixels [10]\n"
321                 "  -l  Number of characters to put in one line [16]\n"
322                 "  -c  Character cell size, in pixels [16]\n"
323                 "  -o  Output file name (or - for stdout) [font.png]\n"
324                 "  -a  Force autohinter\n"
325                 "  -t  Render font to alpha channel\n"
326                 "  -v  Increase the level of verbosity\n"
327                 "  -e  Use cells in sequence, rather than by code\n"
328                 "  -d  Write a definition to the given file\n"
329                 "  -h  Print this message\n");
330 }
331
332 int save_defs(const char *fn, const GlyphDef *defs, int count, int w, int h, int size, int ascent, int descent)
333 {
334         FILE *out;
335         int  i;
336
337         out=fopen(fn, "w");
338         if(!out)
339         {
340                 fprintf(stderr, "Couldn't open %s\n",fn);
341                 return -1;
342         }
343
344         fprintf(out, "%d %d %d %d %d\n", w, h, size, ascent, descent);
345         for(i=0; i<count; ++i)
346         {
347                 const GlyphDef *d=defs+i;
348                 fprintf(out, "%d %d %d %d %d %d %d %d\n", d->code, d->x, d->y, d->w, d->h, d->offset_x, d->offset_y, d->advance);
349         }
350
351         fclose(out);
352
353         return 0;
354 }
355
356 int save_png(const char *fn, const char *data, int w, int h, char alpha)
357 {
358         FILE       *out;
359         png_struct *pngs;
360         png_info   *pngi;
361         png_byte   *rows[h];
362         int        i;
363         png_byte   *data2;
364         int        color;
365
366         if(!strcmp(fn, "-"))
367                 out=stdout;
368         else
369         {
370                 out=fopen(fn, "wb");
371                 if(!out)
372                 {
373                         fprintf(stderr, "Couldn't open %s\n",fn);
374                         return -1;
375                 }
376         }
377
378         pngs=png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
379         if(!pngs)
380         {
381                 fprintf(stderr, "Error writing PNG file\n");
382                 return -1;
383         }
384         pngi=png_create_info_struct(pngs);
385         if(!pngi)
386         {
387                 png_destroy_write_struct(&pngs, NULL);
388                 fprintf(stderr, "Error writing PNG file\n");
389                 return -1;
390         }
391
392         png_init_io(pngs, out);
393         if(alpha)
394         {
395                 data2=(png_byte *)malloc(w*h*2);
396                 for(i=0; i<w*h; ++i)
397                 {
398                         data2[i*2]=255;
399                         data2[i*2+1]=255-data[i];
400                 }
401                 for(i=0; i<h; ++i)
402                         rows[i]=(png_byte *)(data2+i*w*2);
403                 color=PNG_COLOR_TYPE_GRAY_ALPHA;
404         }
405         else
406         {
407                 for(i=0; i<h; ++i)
408                         rows[i]=(png_byte *)(data+i*w);
409                 color=PNG_COLOR_TYPE_GRAY;
410         }
411         png_set_IHDR(pngs, pngi, w, h, 8, color, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
412         png_set_rows(pngs, pngi, rows);
413         png_write_png(pngs, pngi, PNG_TRANSFORM_IDENTITY, NULL);
414         png_destroy_write_struct(&pngs, &pngi);
415         if(alpha) free(data2);
416
417         if(verbose) printf("Saved %dx%d PNG image to %s\n", w, h, fn);
418
419         return 0;
420 }