]> git.tdb.fi Git - ttf2png.git/commitdiff
Version 0.1.1 0.1.1
authorMikko Rasa <tdb@tdb.fi>
Thu, 28 Oct 2004 19:23:40 +0000 (22:23 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 22 Nov 2012 07:47:29 +0000 (09:47 +0200)
Makefile
ttf2png.c

index 57bb3e9733805185758662ecebd06360eb3c9668..b1dbdd255efabc2c9edcc1b56c34e0fe71073b4b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,13 @@
 ttf2png: ttf2png.c
        gcc -Wall $^ -o $@ $(shell freetype-config --cflags --libs) $(shell pkg-config --cflags --libs libpng12)
 
 ttf2png: ttf2png.c
        gcc -Wall $^ -o $@ $(shell freetype-config --cflags --libs) $(shell pkg-config --cflags --libs libpng12)
 
-.PHONY: tarball
-tarball: ttf2png-0.1.tar.gz
-ttf2png-0.1.tar.gz: ttf2png.c Readme Makefile gpl.txt
-       rm -rf ttf2png-0.1
-       mkdir ttf2png-0.1
-       cp -a $^ ttf2png-0.1
-       tar -czf $@ ttf2png-0.1
-       rm -rf ttf2png-0.1
+VER=0.1.1
 
 
+.PHONY: tarball
+tarball: ttf2png-$(VER).tar.gz
+ttf2png-$(VER).tar.gz: ttf2png.c Readme Makefile gpl.txt
+       rm -rf ttf2png-$(VER)
+       mkdir ttf2png-$(VER)
+       cp -a $^ ttf2png-$(VER)
+       tar -czf $@ ttf2png-$(VER)
+       rm -rf ttf2png-$(VER)
index 97be0f1fc6ef4934f991bfc323c5b80a02eecd93..dfdde4b789c6428c14223c43bec4287a5a5646ea 100644 (file)
--- a/ttf2png.c
+++ b/ttf2png.c
@@ -27,6 +27,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 void usage();
 int save_png(char *, char *, int, int);
 
 void usage();
 int save_png(char *, char *, int, int);
 
+char   verbose=0;
+char   alpha=0;
+
 int main(int argc, char **argv)
 {
        FT_Library      freetype;
 int main(int argc, char **argv)
 {
        FT_Library      freetype;
@@ -45,7 +48,6 @@ int main(int argc, char **argv)
        int     cell=16;
        int     ascent;
        char    *out="font.png";
        int     cell=16;
        int     ascent;
        char    *out="font.png";
-       char    verbose=0;
        const char      *name;
        char    autohinter=0;
        int     count;
        const char      *name;
        char    autohinter=0;
        int     count;
@@ -56,7 +58,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
                return 1;
        }
 
-       while((o=getopt(argc,argv,"r:s:l:c:o:avh?"))!=-1)
+       while((o=getopt(argc,argv,"r:s:l:c:o:atvh?"))!=-1)
        {
                char    *ptr;
                int     temp;
        {
                char    *ptr;
                int     temp;
@@ -92,8 +94,11 @@ int main(int argc, char **argv)
                case 'a':
                        autohinter=1;
                        break;
                case 'a':
                        autohinter=1;
                        break;
+               case 't':
+                       alpha=1;
+                       break;
                case 'v':
                case 'v':
-                       verbose=1;
+                       ++verbose;
                        break;
                case 'h':
                case '?':
                        break;
                case 'h':
                case '?':
@@ -126,7 +131,11 @@ int main(int argc, char **argv)
        }
 
        name=FT_Get_Postscript_Name(face);
        }
 
        name=FT_Get_Postscript_Name(face);
-       if(verbose) printf("Font name: %s\n",name);
+       if(verbose)
+       {
+               printf("Font name: %s\n",name);
+               printf("Glyphs: %ld\n",face->num_glyphs);
+       }
 
        err=FT_Set_Pixel_Sizes(face,0,size);
        if(err)
 
        err=FT_Set_Pixel_Sizes(face,0,size);
        if(err)
@@ -134,17 +143,20 @@ int main(int argc, char **argv)
                fprintf(stderr,"Couldn't set size\n");
                return 1;
        }
                fprintf(stderr,"Couldn't set size\n");
                return 1;
        }
-       ascent=face->size->metrics.ascender/64;
-       ch=(face->size->metrics.ascender-face->size->metrics.descender)/64;
-       cw=face->size->metrics.max_advance/64;
+       ascent=(face->bbox.yMax*face->size->metrics.x_scale)>>16;
+       ascent=(ascent+63)/64;
+       ch=((face->bbox.yMax-face->bbox.yMin)*face->size->metrics.y_scale)>>16;
+       ch=(ch+63)/64;
+       cw=((face->bbox.xMax-face->bbox.xMin)*face->size->metrics.x_scale)>>16;
+       cw=(cw+63)/64;
        if(verbose)
        {
                printf("Ascent %d\n",ascent);
        if(verbose)
        {
                printf("Ascent %d\n",ascent);
-               printf("Descent %ld\n",face->size->metrics.descender/64);
-               printf("Height %d\n",ch);
-               printf("Width %d\n",cw);
+               printf("Descent %ld\n",(((face->bbox.yMin*face->size->metrics.y_scale)>>16)+63)/64);
+               printf("Max height %d\n",ch);
+               printf("Max width %d\n",cw);
        }
        }
-       if(ch>cell || cw>cell) fprintf(stderr,"ttf2png: Warning: character size exceeds cell size\n");
+       if(verbose>=1 && (ch>cell || cw>cell)) fprintf(stderr,"Warning: character size exceeds cell size\n");
 
        w=cpl*cell;
        h=(end-begin+cpl-1)/cpl*cell;
 
        w=cpl*cell;
        h=(end-begin+cpl-1)/cpl*cell;
@@ -157,21 +169,32 @@ int main(int argc, char **argv)
                int                     glyph=FT_Get_Char_Index(face,i);
                FT_Bitmap       *bmp=&face->glyph->bitmap;
                int                     x,y;
                int                     glyph=FT_Get_Char_Index(face,i);
                FT_Bitmap       *bmp=&face->glyph->bitmap;
                int                     x,y;
-               int                     cx=(i%cpl)*cell+(cell-cw)/2;
+               int                     cx=(i%cpl)*cell;
                int                     cy=(i/cpl)*cell;
                int                     flags=0;
                int                     cy=(i/cpl)*cell;
                int                     flags=0;
+               int                     dy;
                if(!glyph) continue;
                fflush(stdout);
                if(autohinter) flags|=FT_LOAD_FORCE_AUTOHINT;
                FT_Load_Glyph(face,glyph,flags);
                FT_Render_Glyph(face->glyph,FT_RENDER_MODE_NORMAL);
                if(!glyph) continue;
                fflush(stdout);
                if(autohinter) flags|=FT_LOAD_FORCE_AUTOHINT;
                FT_Load_Glyph(face,glyph,flags);
                FT_Render_Glyph(face->glyph,FT_RENDER_MODE_NORMAL);
-               cx+=face->glyph->bitmap_left;
-               cy+=ascent-face->glyph->bitmap_top;
+               if(cell>bmp->width) cx+=(cell-bmp->width)/2;
+               dy=ascent-face->glyph->bitmap_top;
+               cy+=dy;
+               if(verbose>=2)
+               {
+                       printf("  Char %d: glyph %d, size %dx%d\n",i,glyph,bmp->width,bmp->rows);
+                       if(bmp->width>cell || dy+bmp->rows>cell || dy<0) printf("  Warning: Character %d does not fit in cell\n",i);
+               }
+               if(bmp->pitch<0)
+               {
+                       fprintf(stderr,"Warning: Character %d not rendered (can't handle reversed bitmaps)\n",i);
+                       continue;
+               }
                for(y=0;y<bmp->rows;++y) for(x=0;x<bmp->width;++x)
                {
                        if(cx+x<0 || cx+x>=w || cy+y<0 || cy+y>=h) continue;
                for(y=0;y<bmp->rows;++y) for(x=0;x<bmp->width;++x)
                {
                        if(cx+x<0 || cx+x>=w || cy+y<0 || cy+y>=h) continue;
-                       if(bmp->pitch>0)
-                               data[cx+x+(cy+y)*w]=255-bmp->buffer[x+y*bmp->pitch];
+                       data[cx+x+(cy+y)*w]=255-bmp->buffer[x+y*bmp->pitch];
                }
                ++count;
        }
                }
                ++count;
        }
@@ -196,7 +219,8 @@ void usage()
                "  -c  Character cell size, in pixels [16]\n"
                "  -o  Output file name [font.png]\n"
                "  -a  Force autohinter\n"
                "  -c  Character cell size, in pixels [16]\n"
                "  -o  Output file name [font.png]\n"
                "  -a  Force autohinter\n"
-               "  -v  Enable verbose mode\n"
+               "  -t  Render font to alpha channel\n"
+               "  -v  Increase the level of verbosity\n"
                "  -h  Print this message\n");
 }
 
                "  -h  Print this message\n");
 }
 
@@ -207,6 +231,8 @@ int save_png(char *fn, char *data, int w, int h)
        png_info                *pngi;
        png_byte                *rows[h];
        int                     i;
        png_info                *pngi;
        png_byte                *rows[h];
        int                     i;
+       png_byte                *data2;
+       int                     color;
 
        out=fopen(fn,"wb");
        if(!out)
 
        out=fopen(fn,"wb");
        if(!out)
@@ -230,12 +256,31 @@ int save_png(char *fn, char *data, int w, int h)
        }
 
        png_init_io(pngs,out);
        }
 
        png_init_io(pngs,out);
-       png_set_IHDR(pngs,pngi,w,h,8,PNG_COLOR_TYPE_GRAY,PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
-       for(i=0;i<h;++i)
-               rows[i]=(png_byte *)(data+i*w);
+       if(alpha)
+       {
+               data2=(png_byte *)malloc(w*h*2);
+               for(i=0;i<w*h;++i)
+               {
+                       data2[i*2]=255;
+                       data2[i*2+1]=255-data[i];
+               }
+               for(i=0;i<h;++i)
+                       rows[i]=(png_byte *)(data2+i*w*2);
+               color=PNG_COLOR_TYPE_GRAY_ALPHA;
+       }
+       else
+       {
+               for(i=0;i<h;++i)
+                       rows[i]=(png_byte *)(data+i*w);
+               color=PNG_COLOR_TYPE_GRAY;
+       }
+       png_set_IHDR(pngs,pngi,w,h,8,color,PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
        png_set_rows(pngs,pngi,rows);
        png_write_png(pngs,pngi,PNG_TRANSFORM_IDENTITY,NULL);
        png_destroy_write_struct(&pngs,&pngi);
        png_set_rows(pngs,pngi,rows);
        png_write_png(pngs,pngi,PNG_TRANSFORM_IDENTITY,NULL);
        png_destroy_write_struct(&pngs,&pngi);
+       if(alpha) free(data2);
+
+       if(verbose) printf("Saved %dx%d PNG image to %s\n",w,h,fn);
 
        return 0;
 }
 
        return 0;
 }