From 0cf5a030243dc072cd53af71988e10e03e6462ae Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 28 Oct 2004 22:23:40 +0300 Subject: [PATCH] Version 0.1.1 --- Makefile | 17 +++++------ ttf2png.c | 85 ++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 74 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 57bb3e9..b1dbdd2 100644 --- 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) -.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) diff --git a/ttf2png.c b/ttf2png.c index 97be0f1..dfdde4b 100644 --- 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); +char verbose=0; +char alpha=0; + 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"; - char verbose=0; const char *name; char autohinter=0; int count; @@ -56,7 +58,7 @@ int main(int argc, char **argv) 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; @@ -92,8 +94,11 @@ int main(int argc, char **argv) case 'a': autohinter=1; break; + case 't': + alpha=1; + break; case 'v': - verbose=1; + ++verbose; break; case 'h': case '?': @@ -126,7 +131,11 @@ int main(int argc, char **argv) } 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) @@ -134,17 +143,20 @@ int main(int argc, char **argv) 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); - 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; @@ -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 cx=(i%cpl)*cell+(cell-cw)/2; + int cx=(i%cpl)*cell; 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); - 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;yrows;++y) for(x=0;xwidth;++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; } @@ -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" - " -v Enable verbose mode\n" + " -t Render font to alpha channel\n" + " -v Increase the level of verbosity\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_byte *data2; + int color; 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_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