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