]> git.tdb.fi Git - ttf2png.git/blob - ttf2png.c
67228e533095c119070af16beaa950da87cb8378
[ttf2png.git] / ttf2png.c
1 /*
2 ttf2png - True Type Font to PNG converter
3 Copyright (c) 2004-2008 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 <ctype.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <png.h>
25 #include <ft2build.h>
26 #include FT_FREETYPE_H
27
28 typedef struct sImage
29 {
30         unsigned w, h;
31         char     *data;
32 } Image;
33
34 typedef struct sGlyph
35 {
36         unsigned code;
37         Image    image;
38         unsigned x, y;
39         int      offset_x;
40         int      offset_y;
41         int      advance;
42 } Glyph;
43
44 typedef struct sFont
45 {
46         unsigned size;
47         int      ascent;
48         int      descent;
49         unsigned n_glyphs;
50         Glyph    *glyphs;
51         Image    image;
52 } Font;
53
54 unsigned round_to_pot(unsigned);
55 void usage();
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);
61
62 char verbose = 0;
63
64 int main(int argc, char **argv)
65 {
66         char *fn;
67         int  begin = 0;
68         int  end = 255;
69         int  size = 10;
70         int  cpl = 0;
71         int  cell = 0;
72         char autohinter = 0;
73         char seq = 0;
74         char alpha = 0;
75         char pack = 0;
76
77         FT_Library freetype;
78         FT_Face    face;
79
80         int  err;
81         int  i;
82
83         char *out_fn = "font.png";
84         char *def_fn = NULL;
85
86         Font font;
87
88         if(argc<2)
89         {
90                 usage();
91                 return 1;
92         }
93
94         while((i = getopt(argc, argv, "r:s:l:c:o:atvh?ed:p")) != -1)
95         {
96                 char *ptr;
97                 int  temp;
98                 switch(i)
99                 {
100                 case 'r':
101                         if(!strcmp(optarg, "all"))
102                         {
103                                 begin = 0;
104                                 end = 0x110000;
105                         }
106                         else
107                         {
108                                 if(!isdigit(optarg[0]))
109                                         temp = -1;
110                                 else
111                                 {
112                                         temp = strtol(optarg, &ptr, 0);
113                                         if(ptr[0]!=',' || !isdigit(ptr[1]))
114                                                 temp = -1;
115                                 }
116                                 if(temp<0)
117                                 {
118                                         printf("Not a valid range: %s\n", optarg);
119                                         exit(1);
120                                 }
121                                 else
122                                 {
123                                         begin = temp;
124                                         end = strtol(ptr+1, NULL, 0);
125                                 }
126                         }
127                         break;
128                 case 's':
129                         size = strtol(optarg, NULL, 0);
130                         break;
131                 case 'l':
132                         cpl = strtol(optarg, NULL, 0);
133                         break;
134                 case 'c':
135                         cell = strtol(optarg, NULL, 0);
136                         break;
137                 case 'o':
138                         out_fn = optarg;
139                         break;
140                 case 'a':
141                         autohinter = 1;
142                         break;
143                 case 't':
144                         alpha = 1;
145                         break;
146                 case 'v':
147                         ++verbose;
148                         break;
149                 case 'h':
150                 case '?':
151                         usage();
152                         return 0;
153                 case 'e':
154                         seq = 1;
155                         break;
156                 case 'd':
157                         def_fn = optarg;
158                         break;
159                 case 'p':
160                         pack = 1;
161                         break;
162                 }
163         }
164         if(!strcmp(out_fn, "-"))
165                 verbose = 0;
166
167         if(optind!=argc-1)
168         {
169                 usage();
170                 return 1;
171         }
172
173         fn = argv[optind];
174
175         err = FT_Init_FreeType(&freetype);
176         if(err)
177         {
178                 fprintf(stderr, "Couldn't initialize FreeType library\n");
179                 return 1;
180         }
181
182         err = FT_New_Face(freetype, fn, 0, &face);
183         if(err)
184         {
185                 fprintf(stderr, "Couldn't load font file\n");
186                 if(err==FT_Err_Unknown_File_Format)
187                         fprintf(stderr, "Unknown file format\n");
188                 return 1;
189         }
190
191         if(verbose)
192         {
193                 const char *name = FT_Get_Postscript_Name(face);
194                 printf("Font name: %s\n", name);
195                 printf("Glyphs:    %ld\n", face->num_glyphs);
196         }
197
198         err = FT_Set_Pixel_Sizes(face, 0, size);
199         if(err)
200         {
201                 fprintf(stderr, "Couldn't set size\n");
202                 return 1;
203         }
204
205         font.size = size;
206         init_font(&font, face, begin, end, autohinter);
207         if(pack)
208                 render_packed(&font);
209         else
210                 render_grid(&font, cell, cpl, seq);
211         save_png(out_fn, &font.image, alpha);
212         if(def_fn)
213                 save_defs(def_fn, &font);
214
215         for(i=0; i<font.n_glyphs; ++i)
216                 free(font.glyphs[i].image.data);
217         free(font.glyphs);
218         free(font.image.data);
219
220         FT_Done_Face(face);
221         FT_Done_FreeType(freetype);
222
223         return 0;
224 }
225
226 unsigned round_to_pot(unsigned n)
227 {
228         n -= 1;
229         n |= n>>1;
230         n |= n>>2;
231         n |= n>>4;
232         n |= n>>8;
233         n |= n>>16;
234
235         return n+1;
236 }
237
238 void usage()
239 {
240         printf("ttf2png - True Type Font to PNG converter\n"
241                 "Copyright (c) 2004-2008  Mikko Rasa, Mikkosoft Productions\n"
242                 "Distributed under the GNU General Public License\n\n"
243                 "Usage: ttf2png [options] <TTF file>\n\n"
244                 "Accepted options (default values in [brackets])\n"
245                 "  -r  Range of characters to convert in the format low,high [0,255]\n"
246                 "  -s  Font size to use, in pixels [10]\n"
247                 "  -l  Number of characters to put in one line [auto]\n"
248                 "  -c  Character cell size, in pixels [auto]\n"
249                 "  -o  Output file name (or - for stdout) [font.png]\n"
250                 "  -a  Force autohinter\n"
251                 "  -t  Render font to alpha channel\n"
252                 "  -v  Increase the level of verbosity\n"
253                 "  -e  Use cells in sequence, rather than by code\n"
254                 "  -p  Pack the glyphs tightly instead of in a grid\n"
255                 "  -d  Write a definition to the given file\n"
256                 "  -h  Print this message\n");
257 }
258
259 void init_font(Font *font, FT_Face face, unsigned first, unsigned last, int autohinter)
260 {
261         unsigned i;
262         unsigned size = 0;
263
264         font->ascent = (face->size->metrics.ascender+63)>>6;
265         font->descent = (face->size->metrics.descender+63)>>6;
266
267         if(verbose>=1)
268         {
269                 printf("Ascent:    %d\n", font->ascent);
270                 printf("Descent:   %d\n", font->descent);
271         }
272
273         font->n_glyphs = 0;
274         font->glyphs = NULL;
275         for(i=first; i<=last; ++i)
276         {
277                 unsigned  n;
278                 FT_Bitmap *bmp = &face->glyph->bitmap;
279                 int       x, y;
280                 int       flags = 0;
281                 Glyph     *glyph;
282
283                 n = FT_Get_Char_Index(face, i);
284                 if(!n)
285                         continue;
286
287                 if(autohinter)
288                         flags |= FT_LOAD_FORCE_AUTOHINT;
289                 FT_Load_Glyph(face, n, flags);
290                 FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
291
292                 if(verbose>=2)
293                         printf("  Char %u: glyph %u, size %dx%d\n", i, n, bmp->width, bmp->rows);
294
295                 if(bmp->pixel_mode!=FT_PIXEL_MODE_GRAY)
296                 {
297                         fprintf(stderr, "Warning: Glyph %u skipped, not grayscale\n", n);
298                         continue;
299                 }
300
301                 if(font->n_glyphs>=size)
302                 {
303                         size += 16;
304                         font->glyphs = (Glyph *)realloc(font->glyphs, size*sizeof(Glyph));
305                 }
306
307                 glyph = &font->glyphs[font->n_glyphs++];
308                 glyph->code = i;
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;
315
316                 if(bmp->pitch<0)
317                 {
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];
320                 }
321                 else
322                 {
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];
325                 }
326         }
327
328         if(verbose>=1)
329                 printf("Loaded %u glyphs\n", font->n_glyphs);
330 }
331
332 void render_grid(Font *font, unsigned cell, unsigned cpl, int seq)
333 {
334         unsigned i;
335         int      top = 0, bot = 0;
336         unsigned first, last;
337         unsigned maxw = 0, maxh = 0;
338
339         for(i=1;; i<<=1)
340         {
341                 first = font->glyphs[0].code&~(i-1);
342                 last = first+i-1;
343                 if(last>=font->glyphs[font->n_glyphs-1].code)
344                         break;
345         }
346
347         for(i=0; i<font->n_glyphs; ++i)
348         {
349                 int y;
350
351                 y = font->glyphs[i].offset_y+font->glyphs[i].image.h;
352                 if(y>top)
353                         top = y;
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;
360         }
361
362         if(cell==0)
363         {
364                 cell = top-bot;
365                 if(maxw>cell)
366                         cell = maxw;
367         }
368
369         if(verbose>=1)
370         {
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);
375         }
376
377         if(cpl==0)
378         {
379                 unsigned count = (seq ? font->n_glyphs : last-first+1);
380                 for(i=1;; i<<=1)
381                 {
382                         cpl = i/cell;
383                         if(cpl>0 && (count+cpl-1)/cpl<=cpl)
384                                 break;
385                 }
386         }
387
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;
391         else
392                 font->image.h = (last-first+cpl)/cpl*cell;
393         font->image.h = round_to_pot(font->image.h);
394
395         font->image.data = (char *)malloc(font->image.w*font->image.h);
396         memset(font->image.data, 255, font->image.w*font->image.h);
397
398         for(i=0; i<font->n_glyphs; ++i)
399         {
400                 Glyph    *glyph;
401                 int      cx, cy;
402                 unsigned x, y;
403
404                 glyph = &font->glyphs[i];
405
406                 if(seq)
407                 {
408                         cx = (i%cpl)*cell;
409                         cy = (i/cpl)*cell;
410                 }
411                 else
412                 {
413                         cx = ((glyph->code-first)%cpl)*cell;
414                         cy = ((glyph->code-first)/cpl)*cell;
415                 }
416
417                 if(cell>glyph->image.w)
418                         cx += (cell-glyph->image.w)/2;
419                 cy += top-glyph->offset_y-glyph->image.h;
420
421                 glyph->x = cx;
422                 glyph->y = cy;
423
424                 for(y=0; y<glyph->image.h; ++y) for(x=0; x<glyph->image.w; ++x)
425                 {
426                         if(cx+x<0 || cx+x>=font->image.w || cy+y<0 || cy+y>=font->image.h)
427                                 continue;
428                         font->image.data[cx+x+(cy+y)*font->image.w] = 255-glyph->image.data[x+y*glyph->image.w];
429                 }
430         }
431 }
432
433 void render_packed(Font *font)
434 {
435         unsigned i;
436         unsigned area = 0;
437         unsigned last_h = 0xFFFF;
438         char     *used_glyphs;
439         char     *used_pixels;
440         unsigned cx = 0, cy;
441
442         for(i=0; i<font->n_glyphs; ++i)
443                 area+=(font->glyphs[i].image.w+1)*(font->glyphs[i].image.h+1);
444
445         for(font->image.w=1;; font->image.w<<=1)
446         {
447                 font->image.h = (area*5/4)/font->image.w;
448                 if(font->image.h<=font->image.w)
449                         break;
450         }
451         font->image.h = round_to_pot(font->image.h);
452
453         font->image.data = (char *)malloc(font->image.w*font->image.h);
454         memset(font->image.data, 255, font->image.w*font->image.h);
455         used_pixels = (char *)malloc(font->image.w*font->image.h);
456         memset(used_pixels, 0, font->image.w*font->image.h);
457         used_glyphs = (char *)malloc(font->n_glyphs);
458         memset(used_glyphs, 0, font->n_glyphs);
459
460         for(cy=0; cy<font->image.h;)
461         {
462                 unsigned w;
463                 unsigned x, y;
464                 Glyph    *glyph = NULL;
465                 unsigned best_score = 0;
466
467                 for(; (cx<font->image.w && used_pixels[cx+cy*font->image.w]); ++cx) ;
468                 if(cx>=font->image.w)
469                 {
470                         cx = 0;
471                         ++cy;
472                         last_h = 0xFFFF;
473                         continue;
474                 }
475                 for(w=0; (cx+w<font->image.w && !used_pixels[cx+w+cy*font->image.w]); ++w) ;
476
477                 for(i=0; i<font->n_glyphs; ++i)
478                 {
479                         Glyph *g;
480
481                         g = &font->glyphs[i];
482                         if(!used_glyphs[i] && g->image.w<=w)
483                         {
484                                 unsigned score;
485
486                                 score = g->image.h+1;
487                                 if(g->image.h==last_h)
488                                         score *= g->image.w;
489                                 else
490                                         score += g->image.w;
491
492                                 if(score>best_score)
493                                 {
494                                         glyph = g;
495                                         best_score = score;
496                                 }
497                         }
498                 }
499
500                 if(!glyph)
501                 {
502                         cx += w;
503                         continue;
504                 }
505
506                 used_glyphs[glyph-font->glyphs] = 1;
507                 glyph->x = cx;
508                 glyph->y = cy;
509
510                 for(y=0; y<glyph->image.h; ++y) for(x=0; x<glyph->image.w; ++x)
511                 {
512                         if(cx+x<0 || cx+x>=font->image.w || cy+y<0 || cy+y>=font->image.h)
513                                 continue;
514                         font->image.data[cx+x+(cy+y)*font->image.w] = 255-glyph->image.data[x+y*glyph->image.w];
515                 }
516                 for(y=0; y<glyph->image.h+2; ++y) for(x=0; x<glyph->image.w+2; ++x)
517                 {
518                         if(cx+x<1 || cx+x>font->image.w || cy+y<1 || cy+y>font->image.h)
519                                 continue;
520                         used_pixels[cx+x-1+(cy+y-1)*font->image.w] = 1;
521                 }
522
523                 last_h = glyph->image.h;
524         }
525 }
526
527 int save_defs(const char *fn, const Font *font)
528 {
529         FILE     *out;
530         unsigned i;
531
532         out = fopen(fn, "w");
533         if(!out)
534         {
535                 fprintf(stderr, "Couldn't open %s\n",fn);
536                 return -1;
537         }
538
539         fprintf(out, "%d %d %d %d %d\n", font->image.w, font->image.h, font->size, font->ascent, font->descent);
540         for(i=0; i<font->n_glyphs; ++i)
541         {
542                 const Glyph *g = &font->glyphs[i];
543                 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);
544         }
545
546         fclose(out);
547
548         return 0;
549 }
550
551 int save_png(const char *fn, const Image *image, char alpha)
552 {
553         FILE       *out;
554         png_struct *pngs;
555         png_info   *pngi;
556         png_byte   *rows[image->h];
557         int        i;
558         png_byte   *data2;
559         int        color;
560
561         if(!strcmp(fn, "-"))
562                 out = stdout;
563         else
564         {
565                 out = fopen(fn, "wb");
566                 if(!out)
567                 {
568                         fprintf(stderr, "Couldn't open %s\n",fn);
569                         return -1;
570                 }
571         }
572
573         pngs = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
574         if(!pngs)
575         {
576                 fprintf(stderr, "Error writing PNG file\n");
577                 return -1;
578         }
579         pngi = png_create_info_struct(pngs);
580         if(!pngi)
581         {
582                 png_destroy_write_struct(&pngs, NULL);
583                 fprintf(stderr, "Error writing PNG file\n");
584                 return -1;
585         }
586
587         png_init_io(pngs, out);
588         if(alpha)
589         {
590                 data2 = (png_byte *)malloc(image->w*image->h*2);
591                 for(i=0; i<image->w*image->h; ++i)
592                 {
593                         data2[i*2] = 255;
594                         data2[i*2+1] = 255-image->data[i];
595                 }
596                 for(i=0; i<image->h; ++i)
597                         rows[i] = (png_byte *)(data2+i*image->w*2);
598                 color = PNG_COLOR_TYPE_GRAY_ALPHA;
599         }
600         else
601         {
602                 for(i=0; i<image->h; ++i)
603                         rows[i] = (png_byte *)(image->data+i*image->w);
604                 color = PNG_COLOR_TYPE_GRAY;
605         }
606         png_set_IHDR(pngs, pngi, image->w, image->h, 8, color, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
607         png_set_rows(pngs, pngi, rows);
608         png_write_png(pngs, pngi, PNG_TRANSFORM_IDENTITY, NULL);
609         png_destroy_write_struct(&pngs, &pngi);
610         if(alpha)
611                 free(data2);
612
613         if(verbose)
614                 printf("Saved %dx%d PNG image to %s\n", image->w, image->h, fn);
615
616         fclose(out);
617
618         return 0;
619 }