From 8665ed42e9f40edaa902d963c933f8627a3178e6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 20 Nov 2006 03:12:30 +0200 Subject: [PATCH] Version 0.2.1 --- Makefile | 2 +- Readme | 5 ++- ttf2png.c | 120 +++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 92 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index c201847..7ec1398 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ ttf2png: ttf2png.c gcc -Wall $^ -o $@ $(shell freetype-config --cflags --libs) $(shell pkg-config --cflags --libs libpng12) -VER=0.2 +VER=0.2.1 .PHONY: tarball tarball: ttf2png-$(VER).tar.gz diff --git a/Readme b/Readme index c387b10..b9628ef 100644 --- a/Readme +++ b/Readme @@ -1,5 +1,5 @@ ttf2png - True Type Font to PNG converter -Copyright (c) 2004 Mikkosoft Productions +Copyright (c) 2004-2006 Mikko Rasa, Mikkosoft Productions Software requirements @@ -11,6 +11,9 @@ C compiler (preferably GCC) Changes +0.2.1 +- Don't create too large image with sequential mode if the range is sparse + 0.2 - Added output to stdout - Clean up code a bit diff --git a/ttf2png.c b/ttf2png.c index 4fbe990..bd84106 100644 --- a/ttf2png.c +++ b/ttf2png.c @@ -1,6 +1,6 @@ /* ttf2png - True Type Font to PNG converter -Copyright (c) 2004 Mikkosoft Productions +Copyright (c) 2004-2006 Mikkosoft Productions This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,36 +24,49 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include #include FT_FREETYPE_H +typedef struct sGlyphDef +{ + int code; + int x,y; + int w,h; + int ascent; + int advance; +} GlyphDef; + unsigned round_to_pot(unsigned); void usage(); -int save_png(char *, char *, int, int); +int save_defs(const char *, const GlyphDef *, int, int, int, int); +int save_png(const char *, const char *, int, int, char); char verbose=0; -char alpha=0; int main(int argc, char **argv) { - FT_Library freetype; - FT_Face face; - int err; - int o; + char *fn; int begin=0; int end=255; - char *fn; int size=10; int cpl=16; - int w,h; - int i; - char *data; - int ch,cw; int cell=16; - int ascent; - char *out="font.png"; char autohinter=0; + char seq=0; + char alpha=0; + + FT_Library freetype; + FT_Face face; + int ch,cw; + int ascent; + + int err; + int i; int count; + + int w,h; + char *data; + char *out_fn="font.png"; + char *def_fn=NULL; - char seq=0; - FILE *def=NULL; + GlyphDef *defs=NULL; if(argc<2) { @@ -61,11 +74,11 @@ int main(int argc, char **argv) return 1; } - while((o=getopt(argc, argv, "r:s:l:c:o:atvh?ed:"))!=-1) + while((i=getopt(argc, argv, "r:s:l:c:o:atvh?ed:"))!=-1) { char *ptr; int temp; - switch(o) + switch(i) { case 'r': if(!strcmp(optarg, "all")) @@ -84,7 +97,10 @@ int main(int argc, char **argv) temp=-1; } if(temp<0) + { printf("Not a valid range: %s\n", optarg); + exit(1); + } else { begin=temp; @@ -102,7 +118,7 @@ int main(int argc, char **argv) cell=strtol(optarg, NULL, 0); break; case 'o': - out=optarg; + out_fn=optarg; break; case 'a': autohinter=1; @@ -125,7 +141,7 @@ int main(int argc, char **argv) break; } } - if(!strcmp(out,"-")) + if(!strcmp(out_fn, "-")) verbose=0; if(optind!=argc-1) @@ -181,7 +197,7 @@ int main(int argc, char **argv) if(verbose>=1 && (ch>cell || cw>cell)) fprintf(stderr,"Warning: character size exceeds cell size\n"); w=round_to_pot(cpl*cell); - if(seq) + if(seq && face->num_glyphsnum_glyphs+cpl-1)/cpl*cell; else h=(end-begin+cpl-1)/cpl*cell; @@ -191,10 +207,14 @@ int main(int argc, char **argv) memset(data, 255, w*h); if(def_fn) - def=fopen(def_fn, "w"); - - if(def) - fprintf(def, "%d %d %d\n", w, h, size); + { + if(face->num_glyphsnum_glyphs; + else + count=end-begin; + + defs=(GlyphDef *)malloc(count*sizeof(GlyphDef)); + } count=0; for(i=begin; i<=end; ++i) @@ -246,18 +266,28 @@ int main(int argc, char **argv) data[cx+x+(cy+y)*w]=255-bmp->buffer[x+y*bmp->pitch]; } - if(def) - fprintf(def, "%d %d %d %d %d %d %d\n",i, cx, cy, bmp->width, bmp->rows, face->glyph->bitmap_top-bmp->rows, (int)(face->glyph->advance.x+32)/64); + if(def_fn) + { + defs[count].code=i; + defs[count].x=cx; + defs[count].y=cy; + defs[count].w=bmp->width; + defs[count].h=bmp->rows; + defs[count].ascent=face->glyph->bitmap_top-bmp->rows; + defs[count].advance=(int)(face->glyph->advance.x+32)/64; + } ++count; } - if(def) - fclose(def); + while(seq && h/2>=(count+cpl-1)/cpl*cell) + h/=2; - save_png(out, data, w, h); + if(def_fn) + save_defs(def_fn, defs, count, w, h, size); + save_png(out_fn, data, w, h, alpha); - if(verbose) printf("Converted %d glyphs\n",count); + if(verbose) printf("Converted %d glyphs\n", count); return 0; } @@ -294,7 +324,31 @@ void usage() " -h Print this message\n"); } -int save_png(char *fn, char *data, int w, int h) +int save_defs(const char *fn, const GlyphDef *defs, int count, int w, int h, int size) +{ + FILE *out; + int i; + + out=fopen(fn, "w"); + if(!out) + { + fprintf(stderr, "Couldn't open %s\n",fn); + return -1; + } + + fprintf(out, "%d %d %d\n", w, h, size); + for(i=0; icode, d->x, d->y, d->w, d->h, d->ascent, d->advance); + } + + fclose(out); + + return 0; +} + +int save_png(const char *fn, const char *data, int w, int h, char alpha) { FILE *out; png_struct *pngs; @@ -311,7 +365,7 @@ int save_png(char *fn, char *data, int w, int h) out=fopen(fn, "wb"); if(!out) { - fprintf(stderr, "Couldn't open output file\n"); + fprintf(stderr, "Couldn't open %s\n",fn); return -1; } } -- 2.43.0